﻿var sidePad = 100;
var topBottomPad = 20;

//absolute positioning shortcuts
$.fn.pos = function(options) {
    var defaults = {
        top: null,
        bottom: null,
        right: null,
        left: null,
        width: null,
        height: null
    }
    var options = $.extend(defaults, options);

    return this.each(function() {
        var obj = $(this);
        $(obj).css('position', 'absolute');
        if (options.top != null) $(obj).css('top', options.top + 'px');
        if (options.left != null) $(obj).css('left', options.left + 'px');
        if (options.bottom != null) $(obj).css('bottom', options.bottom + 'px');
        if (options.right != null) $(obj).css('right', options.right + 'px');
        if (options.width != null) $(obj).css('width', options.width + 'px');
        if (options.height != null) $(obj).css('height', options.height + 'px');
    });
}

//center on screen shortcut
$.fn.centerOnScreen = function() {
    return this.each(function() {
        var obj = $(this);
        obj.pos({ top: $(window).height() / 2 - obj.height() / 2, left: $(window).width() / 2 - obj.width() / 2 });
    });
}
$.fn.resizeToScreen = function() {
    return this.each(function() {
        var obj = $(this);
        obj.pos({ left: sidePad, right: sidePad, top: topBottomPad, bottom: topBottomPad });
    });
}

//set margin shortcuts
$.fn.margin = function(options) {
    var defaults = {
        top: null,
        bottom: null,
        right: null,
        left: null,
        all: null
    }
    var options = $.extend(defaults, options);
    if (options.all != null) {
        options.top = options.all;
        options.bottom = options.all;
        options.right = options.all;
        options.left = options.all;
    }

    return this.each(function() {
        var obj = $(this);
        if (options.top != null) $(obj).css('margin-top', options.top + 'px');
        if (options.bottom != null) $(obj).css('margin-bottom', options.bottom + 'px');
        if (options.right != null) $(obj).css('margin-right', options.right + 'px');
        if (options.left != null) $(obj).css('margin-left', options.left + 'px');
    });
}

//set padding shortcuts
$.fn.padding = function(options) {
    var defaults = {
        top: null,
        bottom: null,
        right: null,
        left: null,
        all: null
    }
    var options = $.extend(defaults, options);
    if (options.all != null) {
        options.top = options.all;
        options.bottom = options.all;
        options.right = options.all;
        options.left = options.all;
    }

    return this.each(function() {
        var obj = $(this);
        if (options.top != null) $(obj).css('padding-top', options.top + 'px');
        if (options.bottom != null) $(obj).css('padding-bottom', options.bottom + 'px');
        if (options.right != null) $(obj).css('padding-right', options.right + 'px');
        if (options.left != null) $(obj).css('padding-left', options.left + 'px');
    });
}

//set element id shortcut
$.fn.id = function(id) {
    return this.each(function() {
        var obj = $(this);
        $(obj).attr('id', id);
    });
}
//set element name shortcut
$.fn.name = function(name) {
    return this.each(function() {
        var obj = $(this);
        $(obj).attr('name', name);
    });
}



//img preloading
function ImagePreloader(images, callBack, callBackVar) {
    this.callBackVar = callBackVar;
    this.callBack = callBack;
    this.nLoaded = 0;
    this.nProcessed = 0;
    this.aImages = new Array;
    this.nImages = images.length;
    for (var i = 0; i < images.length; i++) this.preload(images[i]);
}
ImagePreloader.prototype.preload = function(image) {
    var oImage = new Image;
    this.aImages.push(oImage);
    oImage.onload = ImagePreloader.prototype.onload;
    oImage.onerror = ImagePreloader.prototype.onerror;
    oImage.onabort = ImagePreloader.prototype.onabort;
    oImage.oImagePreloader = this;
    oImage.bLoaded = false;
    oImage.src = image;
}
ImagePreloader.prototype.onComplete = function() {
    this.nProcessed++;
    if (this.nProcessed == this.nImages) {
        this.callBack(this.callBackVar);
    }
}
ImagePreloader.prototype.onload = function() {
    this.bLoaded = true; this.oImagePreloader.nLoaded++;
    this.oImagePreloader.onComplete();
}
ImagePreloader.prototype.onerror = function() {
    this.bError = true; this.oImagePreloader.onComplete();
}
ImagePreloader.prototype.onabort = function() {
    this.bAbort = true;
    this.oImagePreloader.onComplete();
}

function setOp(id, opacity) { var object = document.getElementById(id).style; var ieobj = document.getElementById(id); var filter = "alpha(opacity=" + opacity + ")"; object.filter = filter; if (opacity == 100) object.filter = ''; object.opacity = (opacity / 100); object.MozOpacity = (opacity / 100); object.KhtmlOpacity = (opacity / 100); }
//object fading effects
var faders = new Array();
function OpFader(id, opStart, opEnd, steps, duration, loopMode) {
    this.id = id;
    this.curOp = opStart;
    this.tarOp = opEnd;
    this.tmr = null;
    this.obj = document.getElementById(id);
    this.rate = (duration / steps);
    this.step = Math.abs((this.curOp - this.tarOp) / steps);
    this.loopMode = false;
    if (loopMode != null) this.loopMode = loopMode;

    //if (this.loopMode) alert('loop!');
}
OpFader.prototype.stop = function() {
    if (this.tmr) { clearTimeout(this.tmr); this.tmr = null; }
}
OpFader.prototype.start = function() {
    this.stop();
    this.tmr = setTimeout('stepFade("' + this.id + '");', this.rate);
}

OpFader.prototype.setSpeed = function(steps, duration) {
    this.rate = (duration / steps);
    this.step = Math.abs((this.curOp - this.tarOp) / steps);
}
OpFader.prototype.doStep = function() {
    this.stop();
    if (this.tarOp > this.curOp) {
        this.curOp += this.step;
        if (this.curOp > 100 || this.curOp >= this.tarOp) {
            this.curOp = this.tarOp;
            if (this.loopMode) {
                this.curOp -= this.step;
                this.tarOp = 1;
            }
        }
    } else if (this.tarOp < this.curOp) {
        this.curOp -= this.step;
        if (this.loopMode && this.curOp < 50) {
            this.curOp = 50;
            this.tarOp = 100;
        } else if (this.curOp < 0 || this.curOp <= this.tarOp) {
            this.curOp = this.tarOp;
        }
    }
    if (this.curOp > 0)
        this.obj.style.display = 'block';
    else
        this.obj.style.display = 'none';

    setOp(this.id, this.curOp);
    if (this.curOp != this.tarOp)
        this.start();
}

function getFader(id) {
    for (var i = faders.length - 1; i >= 0; i -= 1) {
        if (faders[i].id == id) {
            return faders[i];
        }
    }
}
function stepFade(id) {
    var fdr = getFader(id);
    if (fdr)
        fdr.doStep();
}
function fadeTo(id, opStart, opEnd, duration, loopMode) {
    var stepCost = 25;
    var steps = duration / 35;
    var fdr = getFader(id);
    if (fdr != null) {
        fdr.stop();
        fdr.loopMode = loopMode;
        fdr.tarOp = opEnd;
        fdr.setSpeed(steps, duration);
        fdr.start();
    } else {
        fdr = new OpFader(id, opStart, opEnd, steps, duration, loopMode);
        faders[faders.length] = fdr;
        fdr.start();
    }
}
function fadeIn(id, duration, loopMode) {
    if (duration == null) duration = 250;
    if (loopMode == null) loopMode = false;
    fadeTo(id, 0, 100, duration, loopMode);
}
function fadeOut(id, duration, loopMode) {
    if (duration == null) duration = 250;
    if (loopMode == null) loopMode = false;
    fadeTo(id, 100, 0, duration, loopMode);
}

//drag and drop windows
var grabx = 0; var graby = 0; var orix = 0; var oriy = 0; var elex = 0; var eley = 0; var algor = 0; var dragobj = null;
var mousex = 0;var mousey = 0;
function falsefunc() { return false; }
function init() { document.onmousemove = update; update(); }
function getMouseXY(e) {
    if (!e) e = window.event; // works on IE, but not NS (we rely on NS passing us the event)

    if (e) {
        if (e.pageX || e.pageY) {
            mousex = e.pageX;
            mousey = e.pageY;
            algor = '[e.pageX]';
            if (e.clientX || e.clientY) algor += ' [e.clientX] '
        }
        else if (e.clientX || e.clientY) {
            mousex = e.clientX + document.body.scrollLeft;
            mousey = e.clientY + document.body.scrollTop;
            algor = '[e.clientX]';
            if (e.pageX || e.pageY) algor += ' [e.pageX] '
        }
    }
}
function update(e) { getMouseXY(e); }
function grab(context) {

    document.onmousedown = falsefunc;
    dragobj = context;
    //dragobj.style.zIndex = 10;
    document.onmousemove = drag;
    document.onmouseup = drop;
    grabx = mousex;
    graby = mousey;
    elex = orix = dragobj.offsetLeft;
    eley = oriy = dragobj.offsetTop;
    update();
}

function drag(e) {
    if (dragobj) {
        elex = orix + (mousex - grabx);
        eley = oriy + (mousey - graby);
        dragobj.style.position = "absolute";
        dragobj.style.left = (elex).toString(10) + 'px';
        dragobj.style.top = (eley).toString(10) + 'px';
    }
    update(e);
    return false;
}

function drop() {
    if (dragobj) {
        dragobj = null;
    }
    update();
    document.onmousemove = update;
    document.onmouseup = null;
    document.onmousedown = null;
}

document.onmousemove = update; // update(event) implied on NS, update(null) implied on IE
update();