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

var CMSTools = {


    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 (!CMSTools.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) {
                CMSTools.changeOpacity(elem, 100-((100/t_steps) * c_step));
            } else {
                CMSTools.changeOpacity(elem, 0+((100/t_steps) * c_step));
            }

            c_step++;
            setTimeout("CMSTools.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 + ")";
    },
    
    //
    // Popup Window functions
    //
    popupWinCreate: function(contents, bg_colour, 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
        if (this._yk_box){
            this._yk_box.close();
        }
        var _yk_box = window.open("", "dynamic_win", "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars='auto',resizable='true'");

        // set the height of the overlay element
        //yk_bg.style.height = CMSTools.winXY().y + "px";
        
        _yk_box.resizeTo(w,h);
        
        _yk_box.moveTo( ((CMSTools.docXY().x/2) + CMSTools.scrollXY().x) - (w/2), ((CMSTools.docXY().y/2) + CMSTools.scrollXY().y) - (h/2));
        
        // add the contents
        _yk_box.document.write(contents);
        
        _yk_box.document.body.style.backgroundColor = bg_colour;
        
        _yk_box.focus();
        
        // save the references
        this._yk_box = _yk_box;


    },
    
    popupWinUpdateContents: function(contents)
    {
        if (this.isset(this._yk_box)) {
            this._yk_box.document.body.innerHTML = content;
            return 1;
        }
        return 0;
    },

    //
    // 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 = CMSTools.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("CMSTools.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 = ((CMSTools.docXY().x/2) + CMSTools.scrollXY().x) - (w/2) + "px";
        this._yk_box.style.top = ((CMSTools.docXY().y/2) + CMSTools.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";
        
        // move bg
        //this._yk_bg.height = "px";

        // add click close
        CMSTools.addEvent(document,"mouseup",function(e) { CMSTools.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;
        }
    },
    
    popupAddCloseBut: function()
    {
    	if (this.isset(this._yk_box)) {
            this._yk_box.innerHTML += "<a href='#' id='yk_close' onclick='CMSTools.popupHide(); return false;' title='Close'>Close</a>";
            return 1;
        }
        return 0;
    },

    trace: function(trace)
    {
        try {
            console.log(trace);
        } catch(e) {

            var console_div = document.createElement('div');
            document.body.appendChild(console_div);
            console_div.id = "console_div";

            var a = document.createElement('a');
            a.onclick = 'javascript:console_div.style.display="none"; return false;';
            a.innerHTML='close';
            console_div.appendChild(a);

            var a = document.createElement('a');
            a.onclick = 'javascript:console_div.trace_div.innerHTML=""; return false;';
            a.innerHTML='clear';
            console_div.appendChild(a);

            var trace_div = document.createElement('div');
            console_div.appendChild(trace_div);
            trace_div.id = "trace_div";

            trace_div.innerHTML += '<br/>' + trace;

            console_div.style.position = "absolute";
            console_div.style.top = "0";
            console_div.style.left = "0";
            console_div.style.background = "#fff";
            console_div.style.padding = "20px";


        }

    },


    // Lightbox Map Popup
    popupModal: function (site_path, width, height)
    {
        var popup_code = "";
        popup_code += "<div id='yk_popup' class='yk_popup'>\n";
        popup_code += "<a href='#' id='yk_close' onclick='CMSTools.popupHide(); return false;' title='Close'>Close</a>";
        popup_code += "<iframe name='inlineframe' id='yk_iframe' src='" + site_path + "' frameborder='0' scrolling='auto' width='" + width + "' height='" + height + "'></iframe>\n";
        popup_code += "</div>\n";
        CMSTools.popupCreate(popup_code, "000000", 80, {width: width, height: height});
    }

}