////////////////////////////////////////////////////////////////////////////////
// flash-javascript connection setup

var flashLoaded = false;
var flashInterval;

// get the flash object
function thisMovie(movieName) {
	var isIE = navigator.appName.indexOf("Microsoft") != -1;
	return (isIE) ? window[movieName] : document[movieName];
}

// called from flash to initialize js
function initFlash() {
	flashLoaded = true;
}

// called from flash to close video
function closeFlash() {
	writeCookie("visited","0",24);
	hideFlashBox();
}

// make call to flash to play
function playVideo() {
	clearInterval(flashInterval);
	flashInterval = setInterval("waitToPlay()",1000);
}

// make call to flash to pause
function pauseVideo() {
	if (flashLoaded) {
		thisMovie("fbFlashObj").pauseVideo();
	}
}

// make call to flash to stop
function stopVideo() {
	if (flashLoaded) {
		thisMovie("fbFlashObj").stopVideo();
	}
}

// wait for flash to load - then play it
function waitToPlay() {
	if (flashLoaded) {
		clearInterval(flashInterval);
		thisMovie("fbFlashObj").playVideo();
	}
}

// wait for flash to load - then pause it
function waitToPause() {
	if (flashLoaded) {
		clearInterval(flashInterval);
		thisMovie("fbFlashObj").pauseVideo();
	}
}

////////////////////////////////////////////////////////////////////////////////
// flashBox functions

var fadeInterval;    // used to fade the mask
var minInterval;     // used to move the minimized box
var maskOpacity = 0; // mask opacity
function trackClick(cat,image) {


	// If DART parameter is passed, create the DART tag:
	if(cat) {
		var axel = Math.random()+"";
		var a = axel * 10000000000000;
		var img = document.createElement("img");
		img.src = 'http://ad.doubleclick.net/activity;src=375714;type=lfg20368;cat='+cat+';ord='+ a + '?';
		img.width = 1;
		img.height = 1;
		img.border = 0;
		document.getElementById("trackContainer").appendChild(img);	

	//	alert('DART Category: '+cat);
	}
	
	// If image parameter is passed, create the image:
	if(image) {
		var img2 = document.createElement("img");	
		img2.src = 'http://www.lfg.com/lfg/icc/img/hff/'+image+'.bmp';	
		img2.width = 1;
		img2.height = 1;
		img2.border = 0;	
		document.getElementById("trackContainer").appendChild(img2);
		
	//	alert('IMAGE: '+image+'.bmp');			
	}
				
}


function showFlashBox(dartCat,trackImg) {

	// call to show the hub L1 tabs
	lyrParentDiv.show();

	var maskDiv = document.getElementById("maskDiv");
	var progressDiv = document.getElementById("progressDiv");
	var flashBoxDiv = document.getElementById("flashBoxDiv");
	
	hideAllSelectObjects(); // hide the select form controls
	
	if(document.getElementById("calculatorDiv")){			
		document.getElementById("calculatorDiv").style.visibility="hidden";
	
	}
	
	maskDiv.className = "maskVisible";
	flashBoxDiv.className = "flashBoxVisible";
	trackClick(dartCat,trackImg);
	maintPos();
	clearInterval(fadeInterval);
	fadeInterval = setInterval("fadeMask(9,3,maintPos)",1);
	playVideo();
	
	hideMinimizedBox();
	writeCookie("visited","1",24);
	
}

function hideFlashBox() {
	var maskDiv = document.getElementById("maskDiv");
	var progressDiv = document.getElementById("progressDiv");
	var flashBoxDiv = document.getElementById("flashBoxDiv");
	
	showAllSelectObjects();
	
	stopVideo();
	
	flashBoxDiv.className = "flashBoxHidden";
	flashBoxDiv.style.top = "0px";
	flashBoxDiv.style.left = "0px";

	progressDiv.className = "progressHidden";
	
	if(document.getElementById("calculatorDiv")){			
		document.getElementById("calculatorDiv").style.visibility="visible";
	
	}
	// new - fade out the mask
	clearInterval(fadeInterval);
	fadeInterval = setInterval("fadeMask(0,-3,null)",1);
	
	/*
	maskDiv.className = "maskHidden";
	setMaskOpacity(0);
	window.onscroll = null;
	window.onresize = null;
	*/
	
	hideMinimizedBox();
	writeCookie("visited","0",24);
}

function minimizeFlashBox() {
	var maskDiv = document.getElementById("maskDiv");
	var progressDiv = document.getElementById("progressDiv");
	var flashBoxDiv = document.getElementById("flashBoxDiv");
	
	showAllSelectObjects();
	
	pauseVideo();
	
	flashBoxDiv.className = "flashBoxHidden";
	flashBoxDiv.style.top = "0px";
	flashBoxDiv.style.left = "0px";

	progressDiv.className = "progressHidden";
	
	if(document.getElementById("calculatorDiv")){			
		document.getElementById("calculatorDiv").style.visibility="visible";
	
	}
	
	// new - fade out the mask
	clearInterval(fadeInterval);
	fadeInterval = setInterval("fadeMask(0,-3,adjustMin)",1);
	/*
	maskDiv.className = "maskHidden";
	setMaskOpacity(0);
	*/
	
	showMinimizedBox();
	writeCookie("visited","1",24);
}

function minimizeFlashBoxURL(inURL) {
	var maskDiv = document.getElementById("maskDiv");
	var progressDiv = document.getElementById("progressDiv");
	var flashBoxDiv = document.getElementById("flashBoxDiv");
	
	showAllSelectObjects();
	
	pauseVideo();
	
	flashBoxDiv.className = "flashBoxHidden";
	flashBoxDiv.style.top = "0px";
	flashBoxDiv.style.left = "0px";

	progressDiv.className = "progressHidden";
	
	// new - fade out the mask
	clearInterval(fadeInterval);
	fadeInterval = setInterval("fadeMask(0,-3,adjustMin)",1);
	/*
	maskDiv.className = "maskHidden";
	setMaskOpacity(0);
	*/
	
	showMinimizedBox();
	writeCookie("visited","1",24);
	window.location = inURL;
}

function posToCenter(elem) {
	
	var scrollPos = new getScrollPos();
	var pageSize = new getPageSize();
	var emSize = new getElementSize(elem);
	var x = Math.round(pageSize.width/2) - (emSize.width/2) + scrollPos.scrollX;
	var y = Math.round(pageSize.height/2) - (emSize.height/2) + scrollPos.scrollY;
	//alert("p:"+x+","+y);
	elem.style.left = x+'px';
	elem.style.top = y+'px';
}

function maintPos() {
	
	var maskDiv = document.getElementById("maskDiv");
	var progressDiv = document.getElementById("progressDiv");
	var flashBoxDiv = document.getElementById("flashBoxDiv");
	var pagesize = new getPageSize();
	var scrollPos = new getScrollPos();
	var ua = navigator.userAgent;
	if(ua.indexOf("MSIE ") != -1) { maskDiv.style.width = pagesize.width+'px'; } 
	if(ua.indexOf("Opera/9") != -1) { maskDiv.style.height = document.body.scrollHeight+'px'; }
	else { maskDiv.style.height = pagesize.height+scrollPos.scrollY+'px';}
	posToCenter(progressDiv);
	posToCenter(flashBoxDiv);
	
}

function adjustMin() {
	var flashBoxMinDiv = document.getElementById("flashBoxMinDiv");
	var pagesize = new getPageSize();
	var scrollPos = new getScrollPos();
	var emSize = new getElementSize(flashBoxMinDiv);
	var ua = navigator.userAgent;
	if(ua.indexOf("Opera/9") != -1) { flashBoxMinDiv.style.top = (document.body.scrollHeight-emSize.height)+'px'; }
	else { flashBoxMinDiv.style.top = (pagesize.height+scrollPos.scrollY-emSize.height)+'px'; }
}

function setMaskOpacity(value) {
	var maskDiv = document.getElementById("maskDiv");
	maskOpacity = value;
	maskDiv.style.opacity = value/10;
	maskDiv.style.filter = 'alpha(opacity='+(value*3)+')';
}

function fadeMask(target,increment,scrollAction) {
	if (Math.abs(target - maskOpacity) < 0.1) {
		clearInterval(fadeInterval);
		setMaskOpacity(target);
		if (target == 0) {
			var maskDiv = document.getElementById("maskDiv");
			maskDiv.className = "maskHidden";
			setMaskOpacity(0);
		}
		window.onscroll = scrollAction;
		window.onresize = scrollAction;
	} else {
		var maskDiv = document.getElementById("maskDiv");
		var newOpacity = maskOpacity + increment;
		setMaskOpacity(newOpacity);
	}
}

function showMinimizedBox() {
	writeCookie("visited","1",24);
	var flashBoxMinDiv = document.getElementById("flashBoxMinDiv");
	flashBoxMinDiv.className = "flashBoxMinVisible";
	//adjustMin();
	// new - move min box to bottom
	posToCenter(flashBoxMinDiv);
	clearInterval(minInterval);
	minInterval = setInterval("moveMinimizedBox()",1);
}

function popMinimizedBox() {
	var flashBoxMinDiv = document.getElementById("flashBoxMinDiv");
	flashBoxMinDiv.className = "flashBoxMinVisible";
	adjustMin();
	window.onscroll = adjustMin;
	window.onresize = adjustMin;
}

function moveMinimizedBox() {
	var flashBoxMinDiv = document.getElementById("flashBoxMinDiv");
	var pagesize = new getPageSize();
	var scrollPos = new getScrollPos();
	var emSize = new getElementSize(flashBoxMinDiv);
	var ua = navigator.userAgent;
	
	var tx = 0;
	var ty;
	if(ua.indexOf("Opera/9") != -1) { ty = (document.body.scrollHeight-emSize.height); }
	else { ty = (pagesize.height+scrollPos.scrollY-emSize.height); }
	
	//alert("ty:"+ty);
	
	var x = flashBoxMinDiv.style.left;
	var y = flashBoxMinDiv.style.top;
	
	x = Number(x.substring(0,x.indexOf("px")));
	y = Number(y.substring(0,y.indexOf("px")));
		
	if (Math.abs(tx-x) < 0.1 && Math.abs(ty-y) < 0.1) {
		clearInterval(minInterval);
		flashBoxMinDiv.style.left = tx;
		flashBoxMinDiv.style.top = ty;
		window.onscroll = adjustMin;
		window.onresize = adjustMin;
	} else {
		var nx = x + ((tx - x) / 2);
		var ny = y + ((ty - y) / 2);
		//alert("nxny:"+nx+","+ny);
		flashBoxMinDiv.style.left = (x + ((tx - x) / 2)) + "px";
		flashBoxMinDiv.style.top = (y + ((ty - y) / 2)) + "px";
	}
	
}

function hideMinimizedBox() {
	var flashBoxMinDiv = document.getElementById("flashBoxMinDiv");
	flashBoxMinDiv.className = "flashBoxMinHidden";
	flashBoxMinDiv.style.top = "0px";
}

function getScrollPos() {
	var docElem = document.documentElement;
	this.scrollX = self.pageXOffset || (docElem&&docElem.scrollLeft) || document.body.scrollLeft;
	this.scrollY = self.pageYOffset || (docElem&&docElem.scrollTop) || document.body.scrollTop;
}

function getPageSize() {
	var docElem = document.documentElement
	this.width = self.innerWidth || (docElem&&docElem.clientWidth) || document.body.clientWidth;
	this.height = self.innerHeight || (docElem&&docElem.clientHeight) || document.body.clientHeight;
}

function getElementSize(elem) {
	this.width = elem.offsetWidth || elem.style.pixelWidth;
	this.height = elem.offsetHeight || elem.style.pixelHeight;
	if (this.width == undefined) this.width = 0;
	if (this.height == undefined) this.height = 0;
}

function hideAllSelectObjects() {
	var select_array = document.getElementsByTagName("select");
	var numSelects = select_array.length;
	for (var i=0; i<numSelects; i++) {
		select_array[i].style.display = "none";	
	}
}

function showAllSelectObjects() {
	var select_array = document.getElementsByTagName("select");
	var numSelects = select_array.length;
	for (var i=0; i<numSelects; i++) {
		select_array[i].style.display = "";	
	}
}





