/**
 * Floating Div 2.0
 *
 * Neon 'Thi' Zero
 *
 * Compress: http://javascriptcompressor.com/
 *
 * Available Styles: images => 'floatingDiv_image'
 * 					 default Popup => 'floatingDiv_bar'
 * 								   => the style defaults to 'floatingDiv_bar' if incorectly defined.
 *
 */
var destination = "/uploads/js/FloatingDiv/";
        

function createImgPopup(src){
    floatingDiv.popup.simpleImage(src);
}

function createPopupOfLink(link, width, height, dragable, style, overlay, menu, x, y){
    floatingDiv.manager.createPopup("<iframe src=" + link + " class=floatingDiv_popupContent width=100% height=100% ></iframe>", x, y, width, height, menu, dragable, overlay, style);
}

function createPopupOfString(str, width, height, dragable, style, overlay, menu, x, y){
    floatingDiv.manager.createPopup(str, x, y, width, height, menu, dragable, overlay, style);
}
function hidePopup(id){
    if (id == null) 
        id = floatingDiv.defaultPopupID;
    floatingDiv.manager.closePopup(id);
}
var floatingDiv = {
    defaultPopupID: "floatingDiv_dragBox",
    defaultInnerPopupID: "floatingDiv_popUpContents",
    defaultOverlayID: "floatingDiv_over",
    relativeX: 0, //Relative X of top left of the popup. relative on the view area.
    relativeY: 0, //Relative Y of top left of the popup. relative on the view area.
    image: null,
    menu: true,
    width: 500,
    height: 500,
    live: false,
    init: function(){
		//document.write("<div id=\"" + this.defaultOverlayID + "\" style=\"display:none;\"></div>" + "<div class=\"floatingDiv_sh\" id=\"" + this.defaultPopupID + "\" style=\"left:200px;top:150px; display: none;\"><div id=\"" + this.defaultPopupID + "Inner\"></div></div>");
        document.body.innerHTML += "<div id=\"" + this.defaultOverlayID + "\" style=\"display:none;\"></div>" + "<div class=\"floatingDiv_sh\" id=\"" + this.defaultPopupID + "\" style=\"left:200px;top:150px; display: none;\"><div id=\"" + this.defaultPopupID + "Inner\"></div></div>";
        //        document.write("<div id=\"" + this.defaultOverlayID + "\" style=\"display:none;\"></div>");
        //        document.write("<div class=\"floatingDiv_sh\" id=\"" + this.defaultPopupID + "\" style=\"left:200px;top:150px; display: none;\"><div id=\"" + this.defaultPopupID + "Inner\"></div></div>");
        this.live = true;
    },
    popup: {
        simpleImage: function(src){
            //LOADING
            floatingDiv.manager.createPopup("<div class=floatingDiv_blackDiv ><img id=floatingDiv_imgTag src=\"" + destination + "img/loading.gif" + "\" alt=\"Click here to cancel loading\" onclick=\"javascript: floatingDiv.manager.cancelImgPopup('" + floatingDiv.defaultPopupID + "')\" width=" + 124 + " height=" + 124 + " /></div>", floatingDiv.calc.centerX(124, 124), floatingDiv.calc.centerY(124, 124), 124, 124, false, false, true, "floatingDiv_image");
            //LOADING the proper image
            floatingDiv.image = new Image();
            floatingDiv.image.onload = this.simpleImage_;
            floatingDiv.image.src = src;
        },
        simpleImage_: function(){
            if (floatingDiv.image != null) {
                var newImg = floatingDiv.image;
                var src = newImg.src;
                var height = newImg.height;
                var width = newImg.width;
                var pageHeight = floatingDiv.calc.getPageHeight();
                var pageWidth = floatingDiv.calc.getPageWidth();
                var newHeight = height;
                var newWidth = width;
                //alert(newWidth + " " + newHeight)
                /*
                 * resizing image if bigger than screen
                 */
                if (newHeight > pageHeight - 100) {
                    newHeight = floatingDiv.calc.removeDecimal(pageHeight - 100);
                    newWidth = floatingDiv.calc.removeDecimal((newHeight / height) * width);
                    height = newHeight;
                    width = newWidth;
                }
                if (newWidth > pageWidth - 100) {
                    newWidth = floatingDiv.calc.removeDecimal(pageWidth - 100);
                    newHeight = floatingDiv.calc.removeDecimal((newWidth / width) * height);
                    height = newHeight;
                    width = newWidth;
                }
                /*
                 * Position middle (close to it any way)
                 */
                //createPopupOfString("<img id=floatingDiv_imgTag alt=\"Click on the image to close\" onclick=\"javascript: hidePopup('" + floatingDiv_popupID + "')\" src=\"" + src + "\" width=" + newWidth + " height=" + newHeight + " />", newWidth, newHeight, false, 3, true, true);
                floatingDiv.manager.createPopup("<img id=floatingDiv_imgTag alt=\"Click on the image to close\" onclick=\"javascript: floatingDiv.manager.closePopup('" + floatingDiv.defaultPopupID + "')\" src=\"" + src + "\" width=" + newWidth + " height=" + newHeight + " />", floatingDiv.calc.centerX(newWidth, newHeight), floatingDiv.calc.centerY(newWidth, newHeight), newWidth, newHeight, true, false, true, "floatingDiv_image");
                
                
            }
        },
        simpleLink: function(src){
            var newWidth = 500;
            var newHeight = 500;
            floatingDiv.manager.createPopup("<iframe src=" + src + " class=floatingDiv_popupContent width=100% height=100% ></iframe>", floatingDiv.calc.centerX(newWidth, newHeight), floatingDiv.calc.centerY(newWidth, newHeight), newWidth, newHeight, true, true, true, "default");
            
        },
        linkWithSize: function(src, width, height){
            floatingDiv.manager.createPopup("<iframe src=" + src + " class=floatingDiv_popupContent width=100% height=100% ></iframe>", floatingDiv.calc.centerX(newWidth, newHeight), floatingDiv.calc.centerY(newWidth, newHeight), newWidth, newHeight, true, true, true, "default");
            
        }
        
    },
    manager: {
        popupOpen: false,
        createPopup: function(contents, x, y, width, height, menu, dragable, overlay, style){
            if (contents == null) 
                contents = "ERROR: could not find the contents...";
            if (width == null) 
                width = 500;
            if (height == null) 
                height = 500;
            if (dragable == null) 
                dragable = true;
            if (style == null) 
                style = "default";
            if (overlay == null) 
                overlay = true;
            if (menu == null) 
                menu = true;
            if (x == null) 
                x = floatingDiv.calc.centerX(width, height);
            if (y == null) 
                y = floatingDiv.calc.centerY(width, height);
		
		floatingDiv.relativeX = x - floatingDiv.calc.scrollX();
		floatingDiv.relativeY = y - floatingDiv.calc.scrollY();
		floatingDiv.width = width;
		floatingDiv.height = height;
		floatingDiv.menu = menu;
            
            
            if (!floatingDiv.live) {
                //alert("initing")
                floatingDiv.init();
                //alert("finish init")
            }
            
            var floatingDiv_popupID = floatingDiv.defaultPopupID;
            style = this.verifyStyle(style);
            if (this.popupOpen) {
                this.closePopup(floatingDiv_popupID);
            }
            var box = document.getElementById(floatingDiv_popupID);
            var boxInner = box;//document.getElementById(floatingDiv_popupID + "Inner");
            var str = "";
            if (menu != null && menu) {
            
                str += "<div class=\"";
                str += style; //Style
                if (dragable != null && dragable) {
                    str += " floatingDiv_handle\"";
                    str += "onmousedown=\"dragStart(event, '" + floatingDiv_popupID + "')\"";
                }
                else {
                    str += "\" ";
                }
                
                if (style == "floatingDiv_image") {
                    //str += "><a href=\"javascript: floatingDiv.manager.closePopup('" + floatingDiv_popupID + "')\" ><img src=\"" + destination + "img/close_bottom.png\" /></a></div>";
                    str += "><a href=\"javascript: floatingDiv.manager.closePopup('" + floatingDiv_popupID + "')\" ><img src=\"" + destination + "img/close_bottom.png\" /></a></div>";
                    
                }
                else {
                    str += "><table width=100% height=\"20px\" border=\"0\" cellspacing=\"0\"><tr id=popupMenuTR width=100% height=\"20px\"><td id=popupMenuLeft height=\"20px\"><img id=popupMenuLeftImg src=\"" + destination + "img/popup_drag.png\" /></td><td id=popupMenuMid margin=\"0\" height=\"20px\"></td><td id=popupMenuRight width=\"20px\" height=\"20px\"><a href=\"javascript: floatingDiv.manager.closePopup('" + floatingDiv_popupID + "')\" ><img id=popupMenuRightImg src=\"" + destination + "img/popup_close_top.png\" /></a></td></tr></table></div>";
                    //str += "><div id=lol1></div><div id=lol2></div><div id=lol3><a href=\"javascript: hidePopup('" + floatingDiv_popupID + "')\" ><img src=\"" + destination +"img/close_top.png\" /></a></div></div>";
                
                }
                
            }
            
            if (style == "floatingDiv_image") {
                str = "<div id=\"floatingDiv_popUpContents\" >" + contents + "</div>" + str;
                menu = true;
                
            }
            else {
                str += "<div id=\"floatingDiv_popUpContents\" >" + contents + "</div>";
                
            }
            
            boxInner.innerHTML = str;
            this.setStyles(floatingDiv.defaultPopupID, floatingDiv.defaultInnerPopupID, x, y, width, height, menu);
            if (overlay) 
                this.showOverlay();
            this.showPopup(floatingDiv_popupID);
            
            //window.onscroll = floatingDiv.manager.posFixedRedraw;
            floatingDiv.manager.addScrollEvent();
	    // return floatingDiv_popupID;
        
        },
        verifyStyle: function(style){
            if (style == "floatingDiv_image") 
                return style;
            else 
                if (style == "floatingDiv_bar") 
                    return style;
                else 
                    return "floatingDiv_bar";
            
        },
	addScrollEvent: function(){

	if (browser.isIE) {
        window.attachEvent("onscroll", floatingDiv.manager.posFixedRedraw);

    }
    if (browser.isNS) {
        window.addEventListener("scroll", floatingDiv.manager.posFixedRedraw, false);
    }
		
		
	},
	removeScrollEvent: function(){
	if (browser.isIE) {
        window.detachEvent("onscroll", floatingDiv.manager.posFixedRedraw);

    }
    if (browser.isNS) {
        window.removeEventListener("scroll", floatingDiv.manager.posFixedRedraw, true);

    }
	},
	posFixedRedraw: function(){
		floatingDiv.manager.setStyles(floatingDiv.defaultPopupID, floatingDiv.defaultInnerPopupID, floatingDiv.relativeX + floatingDiv.calc.scrollX(), floatingDiv.relativeY + floatingDiv.calc.scrollY(), floatingDiv.width, floatingDiv.height, floatingDiv.menu);
	
	},
	
        setStyles: function(id, innerID, x, y, width, height, menu){
            var id2 = innerID;
            var ele = document.getElementById(id);//OUTER div
            var ele2 = document.getElementById(id2);//INNER div
            ele.style.width = width + "px";
            if (menu) {
                ele.style.height = (height + 20) + "px";
            }
            else {
                ele.style.height = height + "px";
            }
            ele2.style.width = width + "px";
            ele2.style.height = height + "px";
            ele.style.top = y + "px";
            ele.style.left = x + "px";
        },
        showPopup: function(id){
            if (!this.popupOpen) {
                var ele = document.getElementById(id);
                ele.style.display = "block";
                this.popupOpen = true;
            }
        },
        showOverlay: function(){
            var overlay = document.getElementById(floatingDiv.defaultOverlayID);
            overlay.style.height = floatingDiv.calc.getDocHeight() + "px";
            overlay.style.width = document.body.clientWidth + "px";
            overlay.style.display = "block";
        },
        hidePopup: function(){
		    floatingDiv.manager.removeScrollEvent();
            var ele = document.getElementById(floatingDiv.defaultPopupID);
            ele.style.display = "none";

        },
        hideOverlay: function(){
            var ele = document.getElementById(floatingDiv.defaultOverlayID);
            ele.style.display = "none";
        },
        closePopup: function(id){
		    floatingDiv.manager.removeScrollEvent();
            var ele = document.getElementById(id);
            ele.style.display = "none";
            this.hideOverlay();
            this.popupOpen = false;
        },
        closeAll: function(){
            this.hidePopup();
            this.hideOverlay();
            this.popupOpen = false;
        },
        cancelImgPopup: function(id){
            floatingDiv.image.onload = "";
            floatingDiv.image.src = "";
            this.closePopup(id);
        }
    },
    calc: {
        getDocHeight: function(){
            var D = document;
            return Math.max(Math.max(D.body.scrollHeight, D.documentElement.scrollHeight), Math.max(D.body.offsetHeight, D.documentElement.offsetHeight), Math.max(D.body.clientHeight, D.documentElement.clientHeight));
        },
        centerX: function(width, height){
            var pageHeight = this.getPageHeight();
            var pageWidth = this.getPageWidth();
            return this.removeDecimal((pageWidth - width) / 2) + this.scrollX();
        },
        centerY: function(width, height){
            var pageHeight = this.getPageHeight();
            var pageWidth = this.getPageWidth();
            //alert(this.scrollY() + " " + document.documentElement.scrollTop + " " + document.body.scrollTop + " " + window.pageYOffset + " " + this.removeDecimal((pageHeight - height) / 2) + document.body.scrollTop)
            return this.removeDecimal((pageHeight - height) / 2) + this.scrollY();//+10 to give some extra space near top/menu
        },
        scrollX: function(){
            var myPageX;
            if (document.documentElement.scrollLeft > 0) {
                myPageX = document.documentElement.scrollLeft;
            }
            else 
                if (document.body.scrollLeft > 0) {
                    myPageX = document.body.scrollLeft;
                }
                else {
                    myPageX = 0;
                }
            return myPageX;
        },
        scrollY: function(){
            var myPageY;
            if (document.documentElement.scrollTop > 0) {
                myPageY = document.documentElement.scrollTop;
            }
            else 
                if (document.body.scrollTop > 0) {
                    myPageY = document.body.scrollTop;
                }
                else {
                    myPageY = 0;
                }
            return myPageY;
        },
        getPageWidth: function(){
            // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
            
            if (typeof window.innerWidth != 'undefined') {
                return window.innerWidth;
            }
            // IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
            else 
                if (typeof document.documentElement != 'undefined' &&
                typeof document.documentElement.clientWidth !=
                'undefined' &&
                document.documentElement.clientWidth != 0) {
                    return document.documentElement.clientWidth;
                }
                // older versions of IE
                else {
                    return document.getElementsByTagName('body')[0].clientWidth;
                }
        },
        getPageHeight: function(){
            // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
            if (typeof window.innerWidth != 'undefined') {
                return window.innerHeight;
            }
            // IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
            else 
                if (typeof document.documentElement != 'undefined' &&
                typeof document.documentElement.clientWidth !=
                'undefined' &&
                document.documentElement.clientWidth != 0) {
                    return document.documentElement.clientHeight;
                }
                // older versions of IE
                else {
                    return document.getElementsByTagName('body')[0].clientHeight;
                }
        },
        removeDecimal: function(X){
            return (X < 0 ? -1 : +1) * Math.floor(Math.abs(X));
        }
    }
};
try{
document.write("<div id=\"" +floatingDiv.defaultOverlayID + "\" style=\"display:none;\"></div>" + "<div class=\"floatingDiv_sh\" id=\"" + floatingDiv.defaultPopupID + "\" style=\"left:200px;top:150px; display: none;\"><div id=\"" + floatingDiv.defaultPopupID + "Inner\"></div></div>");
floatingDiv.live = true;	
}catch(e){
floatingDiv.live = false;	
}
//floatingDiv.init();//Throws some silly error for IE7!!! ERR!!!

//*****************************************************************************
// Do not remove this notice.
//
// Copyright 2001 by Mike Hall.
// See http://www.brainjar.com for terms of use.
//*****************************************************************************

// Determine browser and version.

function Browser(){

    var ua, s, i;
    
    this.isIE = false;
    this.isNS = false;
    this.version = null;
    
    ua = navigator.userAgent;
    
    s = "MSIE";
    if ((i = ua.indexOf(s)) >= 0) {
        this.isIE = true;
        this.version = parseFloat(ua.substr(i + s.length));
        return;
    }
    
    s = "Netscape6/";
    if ((i = ua.indexOf(s)) >= 0) {
        this.isNS = true;
        this.version = parseFloat(ua.substr(i + s.length));
        return;
    }
    
    // Treat any other "Gecko" browser as NS 6.1.
    
    s = "Gecko";
    if ((i = ua.indexOf(s)) >= 0) {
        this.isNS = true;
        this.version = 6.1;
        return;
    }
}

var browser = new Browser();

// Global object to hold drag information.

var dragObj = new Object();
dragObj.zIndex = 1000;

function dragStart(event, id){

    var el;
    var x, y;
    
    // If an element id was given, find it. Otherwise use the element being
    // clicked on.
    
    if (id) 
        dragObj.elNode = document.getElementById(id);
    else {
        if (browser.isIE) 
            dragObj.elNode = window.event.srcElement;
        if (browser.isNS) 
            dragObj.elNode = event.target;
        
        // If this is a text node, use its parent element.
        
        if (dragObj.elNode.nodeType == 3) 
            dragObj.elNode = dragObj.elNode.parentNode;
    }
    
    // Get cursor position with respect to the page.
    
    if (browser.isIE) {
        x = window.event.clientX + document.documentElement.scrollLeft +
        document.body.scrollLeft;
        y = window.event.clientY + document.documentElement.scrollTop +
        document.body.scrollTop;
    }
    if (browser.isNS) {
        x = event.clientX + window.scrollX;
        y = event.clientY + window.scrollY;
    }
    
    // Save starting positions of cursor and element.
    // alert("X "  + x + " Y " + y)
    dragObj.cursorStartX = x;
    dragObj.cursorStartY = y;
    dragObj.elStartLeft = parseInt(dragObj.elNode.style.left, 10);
    dragObj.elStartTop = parseInt(dragObj.elNode.style.top, 10);
    
    if (isNaN(dragObj.elStartLeft)) 
        dragObj.elStartLeft = 0;
    if (isNaN(dragObj.elStartTop)) 
        dragObj.elStartTop = 0;
    
    // Update element's z-index.
    
    dragObj.elNode.style.zIndex = ++dragObj.zIndex;
    
    // Capture mousemove and mouseup events on the page.
    
    if (browser.isIE) {
        document.attachEvent("onmousemove", dragGo);
        document.attachEvent("onmouseup", dragStop);
        window.event.cancelBubble = true;
        window.event.returnValue = false;
    }
    if (browser.isNS) {
        document.addEventListener("mousemove", dragGo, true);
        document.addEventListener("mouseup", dragStop, true);
        event.preventDefault();
    }
}

function dragGo(event){

    var x, y;
    
    // Get cursor position with respect to the page.
    
    if (browser.isIE) {
        x = window.event.clientX + document.documentElement.scrollLeft +
        document.body.scrollLeft;
        y = window.event.clientY + document.documentElement.scrollTop +
        document.body.scrollTop;
    }
    if (browser.isNS) {
        x = event.clientX + window.scrollX;
        y = event.clientY + window.scrollY;
    }
    
    // Move drag element by the same amount the cursor has moved.
    dragObj.elNode.style.left = (dragObj.elStartLeft + x - dragObj.cursorStartX) + "px";
    dragObj.elNode.style.top = (dragObj.elStartTop + y - dragObj.cursorStartY) + "px";
    
    floatingDiv.relativeX = (dragObj.elStartLeft + x - dragObj.cursorStartX)  - floatingDiv.calc.scrollX();
    floatingDiv.relativeY = (dragObj.elStartTop + y - dragObj.cursorStartY) - floatingDiv.calc.scrollY();
   
   if (browser.isIE) {
        window.event.cancelBubble = true;
        window.event.returnValue = false;
    }
    if (browser.isNS) 
        event.preventDefault();
}

function dragStop(event){

    // Stop capturing mousemove and mouseup events.
    if (browser.isIE) {
        document.detachEvent("onmousemove", dragGo);
        document.detachEvent("onmouseup", dragStop);
    }
    if (browser.isNS) {
        document.removeEventListener("mousemove", dragGo, true);
        document.removeEventListener("mouseup", dragStop, true);
    }
    
}