/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 * Layout script to configure UI elements.
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 */
/* Copyright (c) 2010 FoXSE Ltd */

/* Constants */
var DEFAULT_MARGIN_TOP = 4;

var bodyTopOffset = 125;
var bodyRightOffset = 415;
var noSP_right_offset = 30;

var sideBarTop = DEFAULT_MARGIN_TOP;
var sideBarSpacer = 22;
var sideBarMovePad = -110;

/* IE adjustments */
var ieWidthAdjust = 20;
var ieHeightAdjust = 5;

/* Dynamic variables */
var setSPLeft = false;
var spFloating = false;

var sizeInt = -1;
var spInt = -1;

var side_panel_enabled = true;
var side_panel_novfloat = false;

/* Start-up region */
function startUpNoSP() {
    side_panel_enabled = false;
    doStartUp();
}

function noVFloat() {
    side_panel_novfloat = true;
}

function doStartUp() {   
    browserAdjust();

	printDate(null, false);
	sizeElements();
	
	//Set interval to call resize checker
	if (sizeInt != -1) {
	    clearInterval(sizeInt);
	}
	
	sizeInt = setInterval("sizeElements()", 333);
}

function browserAdjust() {
    //IE-specific adjustments
	if (browserIsIE())	{
	    bodyTopOffset += ieHeightAdjust;
	    bodyRightOffset -= ieWidthAdjust;
	}
}


/* Procedural region */

function sizeElements() {
    //alignBar();
	alignPanels();
}

function alignBar() {
    var rightIndent = 60;
    
    //gradient
	var grad = document.getElementById("rightGrad");
	if (grad) {
	    grad.style.left = (document.body.clientWidth + (browserIsIE() ? ieWidthAdjust : 15) - 100) + "px";
	}
	
	//Stock ticker
    var ticker = document.getElementById("tickerTapeContainer");
    if (ticker) {
        ticker.style.width = (document.body.clientWidth - parseInt(ticker.style.left) + 10) + "px";
    }
}

/* 
 * Check if the side panel is where it
 * needs to be (X & Y).
*/

function spNeedsAlign() {
    var needs_align = false;
    var sb = document.getElementById("sideBar");
	
	//vertical
	if (side_panel_auto_scroll && (!side_panel_novfloat)) {
	    var scrollY = getScrollY();

	    var sbTopValue = sb.style.marginTop;
	    if (!sbTopValue || !parseInt(sbTopValue)) {
	        //set if unset
	        sb.style.marginTop = DEFAULT_MARGIN_TOP + "px";
	    }
    	
	    if (scrollY < 100) {
	        if (isPosDiff(parseInt(sb.style.marginTop), sideBarTop)) {
	            needs_align = true;
	        }
	        
	    } else {
	        if (isPosDiff(parseInt(sb.style.marginTop), (scrollY + sideBarMovePad))) {
	            needs_align = true;
	        }
	    }
	}
	    
//	//horizontal
//	var bodyWidth = (document.body.clientWidth - bodyRightOffset);

//	if (isPosDiff(parseInt(sb.style.left), (bodyWidth + sideBarSpacer))) {
//	    needs_align = true;
//	}
	
	//return combined x and y
	return needs_align;
}

function isPosDiff(n1, n2) {
    var pos_threshold = 3;
    
    var ndiff = (n1 - n2);
	ndiff = Math.sqrt(ndiff * ndiff);
	
	return (ndiff > pos_threshold);
}

/* 
 * Sets the position of the side panel (X & Y).
 */

function alignSidePanel() {
    //check if aligned
	if (spNeedsAlign()) {
	    var sb = document.getElementById("sideBar");

        //vertical
        if (side_panel_auto_scroll && (!side_panel_novfloat)) {
            var scrollY = getScrollY();

            if (scrollY < 100) {
                floatLayerY(sb, sideBarTop);
            } else {
                floatLayerY(sb, scrollY + sideBarMovePad);
            }
        }
	    
	    //horizontal
	    //var bodyWidth = (document.body.clientWidth - bodyRightOffset);

	    //floatLayerX(sb, bodyWidth + sideBarSpacer);
	    
	} else {
	    //stop the quick interval
	    if (spInt != -1) {
	        clearInterval(spInt);
	    }
	    
	    spFloating = false;
	}
}

/* 
 * Float the layer towards its target.
 */

function floatLayerY(l, ypos) {
    var curTop = parseInt(l.style.marginTop);
    
    var diff = (ypos - curTop);
    diff = Math.sqrt(diff * diff);
    
    //set this step
    var step;

    if (diff < step) {
        step = diff;
    } else {
        step = (diff / 4);
    }
    
    //move element closer to target position
    if (curTop > ypos) {
        l.style.marginTop = (curTop - step) + "px";
    } else if (curTop < ypos) {
        l.style.marginTop = (curTop + step) + "px";
    }
}

function floatLayerX(l, xpos) {
    var curLeft = parseInt(l.style.left);
    
    var diff = (xpos - curLeft);
    diff = Math.sqrt(diff * diff);
    
    //set this step
    var step;

    if (diff < step) {
        step = diff;
    } else {
        step = (diff / 4);
    }
    
    //move element closer to target position
    if (curLeft > xpos) {
        l.style.left = (curLeft - step) + "px";
    } else if (curLeft < xpos) {
        l.style.left = (curLeft + step) + "px";
    }
}

/* 
 * Check all panels are where they should be.
 */

function alignPanels() {	
	//Main body
	//var mb = document.getElementById("mainBody");
	//var bodyWidth;
	//
//	//adjust for SP
//	if (side_panel_enabled) {
//	    bodyWidth = (document.body.clientWidth - bodyRightOffset);
//	} else {
//	    bodyWidth = (document.body.clientWidth - noSP_right_offset);
//	}
	
    //mb.style.width = (bodyWidth + "px");
    //mb.style.top = (bodyTopOffset + "px");
	
	//Side Panel
	if (side_panel_enabled) {
//	    //Set side panel initial left
//	    if (!setSPLeft) {
//	        var sb = document.getElementById("sideBar");
//	        sb.style.left = (bodyWidth + sideBarSpacer) + "px";
//    	    
//	        setSPLeft = true;
//	    }
    	
	    //Side panel float check
	    if (spNeedsAlign() && !spFloating) {
	        spFloating = true;
    	    
	        //Set interval to call side panel checker
	        if (spInt != -1) {
	            clearInterval(spInt);
	        }

	        spInt = setInterval("alignSidePanel()", 30);
            
            //call now
            alignSidePanel();
	    }
	}
	
//	//IE-specific adjustments
//	if (browserIsIE()) {
//	    var tl = document.getElementById("topLayer");
//		tl.style.width = (document.body.clientWidth + ieWidthAdjust) + "px";
//		
//		var nb = document.getElementById("navBar");
//		nb.style.width = (document.body.clientWidth + ieWidthAdjust) + "px";
//	}
}

function setPanelHeight(panel_id, top_offset) {
    setPanelHeight(panel_id, top_offset, null);
}

function setPanelHeight(panel_id, top_offset, min_height) {
    var newHeight = (getWindowHeight() - top_offset);
    
    //check meets minimum criteria
    if (min_height != null) {
        if (newHeight < min_height)
            newHeight = min_height;
    }
    
    //apply height if id found
    var lyr = document.getElementById(panel_id);

    if (lyr) {
        lyr.style.height = (newHeight + "px");
    }
}
