// JavaScript Document

// runs any function once the entire page has loaded, cross-browser compatible
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

// find object's position on the page
function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

function MM_preloadImages() { //v3.0
var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

// adjusts column heights, width, etc.
function adjustPage(){
	var i = 1;
	var topBlock;
	var rightSquareBlock;
	var rightCol;
	var leftCol;
	var altLeftCol;
	var altLeftColRight;
	
	if(document.getElementById('inner-page-main')){
		while(document.getElementById('content-block' + i)){
			topBlock = document.getElementById('content-block' + i);
			rightSquareBlock = document.getElementById('right-square-block' + i);
			if(rightSquareBlock){
				var topBlockCoords = findPos(topBlock);
				var topBlockBottomPos = topBlockCoords[1] + topBlock.offsetHeight;
		
				var rightSquareBlockCoords = findPos(rightSquareBlock);
				var rightSquareBlockHeight = topBlockBottomPos - rightSquareBlockCoords[1];
				rightSquareBlock.style.height = rightSquareBlockHeight + 'px';
			}
		
			rightCol = document.getElementById('right-col'+i);
			if(!rightCol){
				leftCol = document.getElementById('left-col'+i);
				if(leftCol){
					leftCol.style.width = '705px';	
				}
				altLeftCol = document.getElementById('left-col-alt'+i);
				altLeftColRight = document.getElementById('left-col-alt-right'+i);
				if(altLeftCol && altLeftColRight){
					altLeftCol.style.width = '705px';
					altLeftColRight.style.width = '584px';
				}
			}
			i++;
		}
	}
	else{
		rightCol = document.getElementById('right-col');
		leftCol = document.getElementById('left-col');
		rightSquareBlock = document.getElementById('right-square-block');
		if(rightCol){
			if(rightSquareBlock && (leftCol.offsetHeight > rightCol.offsetHeight)){
				var leftColCoords = findPos(leftCol);
				var leftColBottomPos = leftColCoords[1] + leftCol.offsetHeight;
			
				var rightSquareBlockCoords = findPos(rightSquareBlock);
				var rightSquareBlockHeight = leftColBottomPos - rightSquareBlockCoords[1];
				rightSquareBlock.style.height = rightSquareBlockHeight + 'px';
			}
		}
		else {
			if(leftCol){
				leftCol.style.width = '705px';
			}
		}
	}
}

//adjusts dynamic flash header
function adjustHeader(){
	var header = document.getElementById('dynamic-header');
	if(header){
		var dynHdrW = header.offsetWidth;
		var offset = (dynHdrW - 12) % 24;
		if(offset > 5){
			var adjustValue = 24 - offset;
			header.style.width = Number(dynHdrW + adjustValue) + 'px';
		}	
	}
}
//initializes the flash header
function initSIFR(){
	if(typeof sIFR == "function" && document.getElementById('dynamic-header') && document.getElementById('inner-page-header')){
		sIFR.replaceElement(named({sSelector:"#inner-page-header h1", sFlashSrc:"/flash/tradegothicHead.swf", sColor:"#adc32a", sBgColor:"#000000", sWmode:"transparent", nPaddingTop:22, nPaddingBottom:0, sFlashVars:"fontSize=26"}));
		adjustHeader();
	}
	if(typeof sIFR == "function" && document.getElementById('dynamic-header') && document.getElementById('secondary-home-page-header')){
		sIFR.replaceElement(named({sSelector:"#secondary-home-page-header h1", sFlashSrc:"/flash/tradegothicHead.swf", sColor:"#adc32a", sBgColor:"#000000", sWmode:"transparent", nPaddingTop:0, nPaddingBottom:0, sFlashVars:"fontSize=26"}));
	}
}

//initializes advice pop-ups
function initAdvice(){
	var i = 1;
	var btn;
	var position;
	var popup;
	while(document.getElementById('tip-button' + i)){
		btn = document.getElementById('tip-button' + i);
		btn.order = i;
		btn.over = false;
		position = findPos(btn);
		btn.x = position[0];
		btn.y = position[1];
		if(document.getElementById('advice-popup' + i)){
			popup = document.getElementById('advice-popup' + i);
			popup.over = false;
			btn.onmouseover = showAdvice;
			btn.onmouseout = hideAdvice;
			popup.onmouseover = showAdvicePopup;
			popup.onmouseout = hideAdvicePopup;
		}
		i++;
	}
}
function showAdvice(){
	var popup;
	var xOffset;
	this.over = true;
	if(document.getElementById('advice-popup' + this.order)){
		popup = document.getElementById('advice-popup' + this.order);
		popup.style.display = 'block';
		xOffset = popup.offsetWidth - 99;
		popup.style.top = this.y + 19 + 'px';
		popup.style.left = this.x - xOffset + 'px';	
	}
}
function hideAdvice(){
	var popup;
	this.over = false;
	if(!document.getElementById('advice-popup' + this.order).over){
		popup = document.getElementById('advice-popup' + this.order);
		popup.style.display = 'none';
	}
}
function showAdvicePopup(){
	this.over = true;
	this.style.display = 'block';
}
function hideAdvicePopup(){
	this.over = false;
	this.style.display = 'none';
}

addLoadEvent(initSIFR);
addLoadEvent(adjustPage);
addLoadEvent(initAdvice);

function ShowPrintPop(ContentId)
{
    ShowPopupWithOptions("PrintPreview.aspx?id="+ContentId,"PrintPreview",800,600,1,1,0,0,1);
}

function ShowPopupWithOptions ( pszURL, pszWinName, pnWidth, pnHeight, pbIsResize, pbIsScroll, pbMenuBar, pbToolbar, pbStatusBar ) 
{
    var szMyURL = pszURL;
    var szMyWindowName = pszWinName;
    var nMyWidth = pnWidth;
    var nMyHeight = pnHeight;
    if (pbIsResize) pbIsResize = 'Yes'
    if (pbIsScroll) pbIsScroll = 'Yes'
    if (pbMenuBar) pbIsResize = 'Yes'
    if (pbToolbar) pbIsScroll = 'Yes'
    if (pbStatusBar) pbIsResize = 'Yes'

    var bMyResizable = pbIsResize;
    var bMyScrollBars = pbIsScroll;
    var bMyMenuBar = pbMenuBar;
    var bMyToolBar = pbToolbar;
    var bMyStatusBar = pbStatusBar;

    if (document.all) var xMax = screen.width, yMax = screen.height;
    else
    if (document.layers) var xMax = window.outerWidth, yMax = window.outerHeight;
    else var xMax = 640, yMax=480;

    var xOffset = (xMax - nMyWidth)/2, yOffset = (yMax - nMyHeight)/2;

    var winPopUp =window.open(szMyURL,szMyWindowName,'width=' + nMyWidth + ',height=' + nMyHeight + ',screenX= ' + xOffset + ',screenY=' + yOffset + ',top=' + yOffset + ',left=' + xOffset + ',scrollbars=' + bMyScrollBars + ',resizable=' + bMyResizable + ',menubar=' + bMyMenuBar + ',toolbar=' + bMyToolBar + ',status=' + bMyStatusBar + '');
    winPopUp.focus();
}