// 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;
        }
	}
}
