// add bind function
Function.prototype.bind = function(obj) {
    var method = this,
    temp = function() {
        return method.apply(obj, arguments);
    };
    return temp;
}

var Yak = {

    isset: function(v)
    {
        return ((typeof(v)=='undefined' || v.length==0) ? false : true);
    },

    docXY: function() {
    var w=0,h=0,d=document;
    if(typeof( window.innerWidth ) == 'number' ) {
        w = window.innerWidth;
        h = window.innerHeight;
    } else if(d.documentElement && (d.documentElement.clientWidth || d.documentElement.clientHeight ) ) {
        w = d.documentElement.clientWidth;
        h = d.documentElement.clientHeight;
    } else if(d.body && (d.body.clientWidth || d.body.clientHeight ) ) {
        w = d.body.clientWidth;
        h = d.body.clientHeight;
    }
    return {x:w,y:h};
    },

    winXY: function() {
        if (document.body.clientHeight) {
            return {x:document.body.clientWidth, y:document.body.clientHeight};
        } else {
            return {x:window.innerWidth, y:window.innerHeight};
        }
    },

    scrollXY: function() {
    var x=0,y=0,d=document,w=window;
    if (typeof(w.pageYOffset) == 'number' ) {
        y = w.pageYOffset;
        x = w.pageXOffset;
    } else if(d.body && (d.body.scrollLeft || d.body.scrollTop)) {
        y = d.body.scrollTop;
        x = d.body.scrollLeft;
    } else if(d.documentElement && (d.documentElement.scrollLeft || d.documentElement.scrollTop)) {
        y = d.documentElement.scrollTop;
        x = d.documentElement.scrollLeft;
    }
    return {x: x,y: y};
    },

    pointerXY: function(event)
    {
        return {
            x: event.pageX || (event.clientX +
            (document.documentElement.scrollLeft || document.body.scrollLeft)),
            y: event.pageY || (event.clientY +
            (document.documentElement.scrollTop || document.body.scrollTop))
        };
    },

    addEvent: function(obj,evt,fn) {
        if (obj.addEventListener) {
            obj.addEventListener(evt,fn,false);
        } else if (obj.attachEvent) {
            obj.attachEvent('on'+evt,fn);
        }
    },

    removeEvent: function (obj,evt,fn) {
        if (obj.removeEventListener) {
            obj.removeEventListener(evt,fn,false);
        } else if (obj.detachEvent) {
            obj.detachEvent('on'+evt,fn);
        }
    },


    fadeOut: function(elem, duration, callback)
    {
        var steps = duration / 50;
        this.fade(elem.id,0,steps,callback, 1);
    },

    fadeIn: function(elem, duration, callback)
    {
        var steps = duration / 50;
        this.fade(elem.id,0,steps,callback, -1);
    },

    fade: function(id, c_step, t_steps, callback, direction)
    {
        if (!Yak.isset(direction)) {
            direction = 1;
        }

        elem = document.getElementById(id);

        if (c_step > t_steps) {
            if (callback != null) {
                callback(elem);
                return;
            }
        } else {

            // fade it
            if (direction > 0) {
                Yak.changeOpacity(elem, 100-((100/t_steps) * c_step));
            } else {
                Yak.changeOpacity(elem, 0+((100/t_steps) * c_step));
            }

            c_step++;
            setTimeout("Yak.fade('" + id + "', " + c_step + "," + t_steps + "," + callback + "," + direction + ")",50);
        }
    },

    //
    // Changes the opacity of an element
    //
    changeOpacity: function(elem, new_opacity)
    {
        elem.style.opacity = (new_opacity / 100);
        elem.style.MozOpacity = (new_opacity / 100);
        elem.style.KhtmlOpacity = (new_opacity / 100);
        elem.style.filter = "alpha(opacity=" + new_opacity + ")";
    },

    //
    // Overlay functions
    //
    popupCreate: function(contents, bg_colour, opacity, opt)
    {
        var w = h = 0;

        if (this.isset(opt)) {
            if (this.isset(opt['width'])) w = opt['width'];
            if (this.isset(opt['height'])) h = opt['height'];
        }

        // create the new overlay element
        var yk_bg = document.createElement("DIV");
        yk_bg.id = 'yk_bg';

        // create the new box element
        var yk_box = document.createElement("DIV");
        yk_box.id = 'yk_box';

        // set the height of the overlay element
        yk_bg.style.height = Yak.winXY().y + "px";
        yk_bg.style.backgroundColor = bg_colour;

        // change the opacity
        this.changeOpacity(yk_bg, opacity);

        // add the contents
        yk_box.innerHTML = contents;

        // hide it
        yk_box.style.marginLeft = "-999em";

        // add the popup code
        document.body.insertBefore(yk_bg, document.body.firstChild);
        document.body.insertBefore(yk_box, document.body.firstChild);

        // save the references
        this._yk_box = yk_box;
        this._yk_bg = yk_bg;

        // break this into two steps so the window is drawn
        setTimeout("Yak.popupCreateStep2(" + w + "," + h + ");", 0);

    },

    popupCreateStep2: function(w,h)
    {

        // set width and height of the box
        if (!w) w = this._yk_box.clientWidth;
        if (!h) h = this._yk_box.clientHeight;

        // align to the middle
        this._yk_box.style.left = ((Yak.docXY().x/2) + Yak.scrollXY().x) - (w/2) + "px";
        this._yk_box.style.top = ((Yak.docXY().y/2) + Yak.scrollXY().y) - (h/2) + "px";

        this._yk_box.style.display = 'block';

        // show it
        //this._yk_box.style.zIndex = 100001;
        this._yk_box.style.marginLeft = "0";

        // add click close
        Yak.addEvent(document,"mouseup",function(e) { Yak.popupClick(e); });

    },

    popupCreateLoading: function(bg_colour, opacity, opt)
    {
        var html = "<p><img src='/images/cms/loading.gif' alt='Loading' /></p>";
        return this.popupCreate(html, bg_colour, opacity, opt);
    },

    //
    // Resets the position of the box based on it's content
    // BUGGY
    //
    popupResetPosition: function()
    {
        var yk_box = document.getElementById("yk_box");
        if (!yk_box) return 0;

        // hide it
        yk_box.style.zIndex = -1;

        var w = yk_box.clientWidth;
        var h = yk_box.clientHeight;

        // align to the middle
        yk_box.style.left = ((this.docXY().x/2) + this.scrollXY().x) - (w/2) + "px";
        yk_box.style.top = ((this.docXY().y/2) + this.scrollXY().y) - (h/2) + "px";

        // show it
        yk_box.style.zIndex = 100001;
    },

    popupUpdateContents: function(contents)
    {
        if (this.isset(this._yk_box)) {
            this._yk_box.innerHTML = contents;
            return 1;
        }
        return 0;
    },

    popupVisible: function()
    {
        return this.isset(this._yk_box);
    },

    popupClick: function(e)
    {
        // get event and target
        var e = e || window.event;
        var t = e.target || e.srcElement;

        // loop up to the box
        while (t && t.id != "yk_box" && t.tagName != "BODY") {
            t = t.parentNode;
        }

        // only hide the box if we're not clicking inside the box
        if (!t || t.tagName == "BODY") {
            this.popupHide();
        } else {
            return 1;
        }
    },

    popupHide: function()
    {
        // remove background layer
        if (this.isset(this._yk_bg)) {
            this._yk_bg.parentNode.removeChild(this._yk_bg);
            delete this._yk_bg;
        }

        // remove yk_box
        if (this.isset(this._yk_box)) {
            this._yk_box.parentNode.removeChild(this._yk_box);
            delete this._yk_box;
        }
    },

    fadeColour: function (id, property, start_color, finish_color, duration, step)
    {

        if (step > duration/50) {
            return;
        }

        if (step == null) {
            step = 0;
        }
        var elem;
        if (elem = document.getElementById(id)) {
            elem.style[property] = this.colourFade(start_color, finish_color, 100-((100/(duration/50)) * step));
            step++;
            setTimeout( function(){ this.fadeColour(id, property, start_color,  finish_color, duration, step); }.bind(this), 50);
        }
    },

    colourFade: function (start, end, percent)
    {

        function hex2dec(hex){return(parseInt(hex,16));}
        function dec2hex(dec){return (dec < 16 ? "0" : "") + dec.toString(16);}

        var r1=hex2dec(start.slice(0,2));
        var g1=hex2dec(start.slice(2,4));
        var b1=hex2dec(start.slice(4,6));

        var r2=hex2dec(end.slice(0,2));
        var g2=hex2dec(end.slice(2,4));
        var b2=hex2dec(end.slice(4,6));

        var pc = percent/100;

        var r= Math.floor(r2+(pc*(r1-r2)) + .5);
        var g= Math.floor(g2+(pc*(g1-g2)) + .5);
        var b= Math.floor(b2+(pc*(b1-b2)) + .5);

        return("#" + dec2hex(r) + dec2hex(g) + dec2hex(b));
    }
}