/*Rich Media JavaScript: 
iFrame Related Routines

VS - 01.21.10
//Edited 04.15.10 - adding login panel code
//Edited 07.15.10 - added nuture fix

*/

var contentURL;
var winBackContent;
var theBigIframe;
var divName1 = 'rm_iFrameDiv';
var divName2 = 'iFrameLayer';
var wb_divName1 = 'rm_winBack';
var wb_divName2 = 'winBL';
var defaultWidth = 978;
var iFrameID1 = 'rwIframe';
var iFrameID2 = 'rwIframe2';
var $overlay_wrapper;
var $preloaderDIV;
var iemove;
var doRefresh = false;



function rm_placeIframe(theURL, aZ, aWidth, aX, aY) {
	contentURL = theURL;
	theZ = aZ;
	theZ2 = theZ+1;
	
	iFrameID1 = 'loginPanel';
	layerWidthINT = aWidth;
	divName2 = "loginDiv";
	
	//Find out if the div is defined (nuture fix 07.15.10);
	tDivID = arguments[5];
	
	//Make the iFrame content
	tContent = "<iframe id='" + iFrameID1 + "' name='" + iFrameID1 + "' frameborder='0'   height ='0' width=" + layerWidthINT + " src='" + contentURL +"' scrolling='no'></iframe>";
	//height='120'
	
	
	//Make the Div Content;	
	theLayerCode = "";
	theLayerCode += '<div id="siteDiv">';
	theLayerCode += '<div id="' + divName2 + '">';
	theLayerCode += tContent;
	theLayerCode += '</div>';
	theLayerCode += '</div>';
	
	//Add the Div Style (needs to be dynamic to center properly)
	leftpush = 700;
	theLayerCode += '<style type="text/css">';
	theLayerCode += '#siteDiv {position:relative; display:visible;z-index: ' + theZ + ';background-color: #444400;}';
	theLayerCode += '#' + divName2 + ' {position:absolute; top: ' + aY + 'px; left: ' + aX + 'px; background-color: #fadd99; padding:0px; visibility: visible; overflow:hidden; display:block; width:' + layerWidthINT + 'px;}'
	theLayerCode += '</style>';
	
	//alert(theLayerCode);
	//$overlay_panel = $(theLayerCode).appendTo( $('BODY') );	
	//$overlay_panel = $(theLayerCode).prependTo($("td:first + td"));
	
	if (tDivID) {
		//alert('div defined');
		$overlay_panel = $(theLayerCode).prependTo($("#" + tDivID));
	} else {
		//alert('div not defined');
		$overlay_panel = $(theLayerCode).prependTo($(".Stage"));
	}
	

	$('iframe').load(function() {	  
		//this resizes the iframe that's loaded, and sets the gray layer to fill the page.
		//have to use scrollHeight since IE returns 1 for offsetHeight no matter what.
		var iFrameHeight = ie ? this.contentWindow.document.body.scrollHeight : this.contentWindow.document.body.offsetHeight;
		this.style.height = iFrameHeight + 'px';
	}
	);

}


function rm_unplaceIframe() {
	//Delete the layers
	$overlay_panel.remove();
}


function rm_displayIframe(theURL, theZ) {
	contentURL = theURL;
	theOnClickString = arguments[2];
	
	if (!theOnClickString) {
		theOnClickString = "";
	} else {
		theOnClickString = 'onclick="' + theOnClickString + '" style="cursor: pointer;"';
	}
	
	$overlay_wrapper = $('<div id="' + divName1 + '"' + theOnClickString + ' class="rm_overlay";"></div>').appendTo( $('BODY') );
	
	$overlay_wrapper.css("opacity",0);
	$overlay_wrapper.css("display","block");
	$overlay_wrapper.css("height",getTrueHeight());
	$overlay_wrapper.css("z-index",theZ);
	$overlay_wrapper.fadeTo(600, 0.8, makeIframe);
}


function makeIframe() {
	layerWidthINT = parseInt(defaultWidth);
	theZ2 = parseInt($overlay_wrapper.css("z-index")) + 1;
	
	
	//place preloader
	$preloaderDIV = $('<div id="preloaderLayer" class="preLoaderLayerCSS" style="z-index: ' + theZ2 + ';"><img src="/global/images/rm_images/rm_blue_preloader.gif"></div>').appendTo( $('BODY') );
	$preloaderDIV.css("width", 50);
	$preloaderDIV.css("height", 50);
	$preloaderDIV.css("margin-top",50/2 * -1);
	$preloaderDIV.css("margin-left",50/2 * -1);
	$preloaderDIV.css("padding-left", 10);
	$preloaderDIV.css("padding-top", 10);
	$preloaderDIV.css("background-color", "#FFFFFF");

	
	
	
	//Make the iFrame content
	tContent = "<iframe id='" + iFrameID1 + "' name='" + iFrameID1 + "' frameborder='0' height='1' width=" + layerWidthINT + " src='" + contentURL +"' scrolling='no'></iframe>";
	
	//Make the Div Content;	
	theLayerCode = "";
	theLayerCode += '<div id="' + divName2 + '">';
	theLayerCode += tContent;
	theLayerCode += '</div>';
	
	//Add the Div Style (needs to be dynamic to center properly)
	theLayerCode += '<style type="text/css">';
	theLayerCode += '#' + divName2 + ' {position:absolute; top: 90px; left: 50%; margin-left: -' + layerWidthINT/2 + 'px; background-color: #FFFFFF; padding:0px; z-index: ' + theZ2++ + '; visibility: visible; height: 0px; overflow:hidden; display:block; width:' + layerWidthINT + 'px;}'
	theLayerCode += '</style>';
	
	$overlay_panel = $(theLayerCode).appendTo( $('BODY') );
	
	
	$('iframe').load(function() {
							  
		//remove the preloader:
		$preloaderDIV.remove();
							  
		//this resizes the iframe that's loaded, and sets the gray layer to fill the page.
		//have to use scrollHeight since IE returns 1 for offsetHeight no matter what.
		var iFrameHeight = ie ? this.contentWindow.document.body.scrollHeight : this.contentWindow.document.body.offsetHeight;
		this.style.height = iFrameHeight + 'px';
		
		//Set the formLayer to display none and set it's height to the form size
		$('#' + divName2).css("display","none");
		$('#' + divName2).css("height",iFrameHeight);
			
		newHeight = Math.max(getTrueHeight(), iFrameHeight + 90);
		$('#' + divName1).css("height", newHeight);

		//Expand the form div exposing the iFrame
		$('#' + divName2).slideDown(1000);
	}
	);
	
}




function rm_winBackiFrame(theURL, theZ) {
	winBackContent = theURL;
	theOnClickString = arguments[2];
	
	if (!theOnClickString) {
		theOnClickString = "";
	} else {
		theOnClickString = ' onclick="' + theOnClickString + '" style="cursor: pointer;"';
	}
	
	$overlay_wrapper2 = $('<div id="' + wb_divName1 + '"' + theOnClickString + ' class="rm_overlay"></div>').appendTo( $('BODY') );
	$overlay_wrapper2.css("z-index", theZ);
	$overlay_wrapper2.css("opacity",0);
	$overlay_wrapper2.css("display","block");
	
	//Set the Black Layer to the proper height...
	if ($overlay_wrapper) {
		iFrameHeight = $overlay_wrapper.height();
	} else {
		iFrameHeight = 0;
	}
	newHeight = Math.max(getTrueHeight(), iFrameHeight);

	$overlay_wrapper2.css("height",newHeight);
	$overlay_wrapper2.fadeTo(200, 0.8, makewinBackIframe);

}



function makewinBackIframe() {

	theZ2 = parseInt($overlay_wrapper2.css("z-index")) + 1;
	
	//Make the iFrame content
	tContent = "<iframe id='" + iFrameID2 + "' name='" + iFrameID2 + "' frameborder='0' width='1' height='1' src='" + winBackContent +"' scrolling='no'></iframe>";
	
	//Make the Div Content;	
	theLayerCode = "";
	theLayerCode += '<div id="' + wb_divName2 + '">';
	theLayerCode += tContent;
	theLayerCode += '</div>';
	
	//Add the Div Style (needs to be dynamic to center properly)
	theLayerCode += '<style type="text/css">';
	theLayerCode += '#' + wb_divName2 + ' {position:fixed;top: 50%;left: 50%;margin-top: -2px; margin-left: -2px; background-color: #FFFFFF; padding:0px; z-index: ' + theZ2 + '; visibility: visible; overflow:hidden; display:block; height: 0px; width: 1px}'
	theLayerCode += '</style>';
	
	//Add the div to the overlay_panel 
	$overlay_panel2 = $(theLayerCode).appendTo( $('BODY') );
	

	$('iframe').load(function() {
		
		//this resizes the iframe that's loaded, and sets the gray layer to fill the page.
		var iFrameHeight = ie ? this.contentWindow.document.body.scrollHeight : this.contentWindow.document.body.offsetHeight;
		this.style.height = iFrameHeight + 'px';

		var iFrameWidth = Math.max(this.contentWindow.document.body.scrollWidth, this.contentWindow.document.body.offsetWidth);
		
		//This solves the width issue with FF... I dunno why it needs to be this way, but it does...
		if (this.contentDocument) {
			 iFrameWidth = 0;
			 var children = this.contentDocument.body.childNodes;
			 
			 iFrameWidth = reCursiveOffset(children, children.length);
		 
		}
		
		this.style.width = iFrameWidth + 'px';
		
		$overlay_panel2.css("display","none");
		$overlay_panel2.height(iFrameHeight);
		$overlay_panel2.width(iFrameWidth);
		
		$overlay_panel2.css("margin-top",iFrameHeight/2 * -1);
		$overlay_panel2.css("margin-left",iFrameWidth/2 * -1);
		
		ie6layerSet();
		
		//Animate the form down...
		var $answer = $('#' + wb_divName2); 	
		$answer.fadeIn(300);  
							  
	}
	)
	
}


function rm_removeIframe() {
	//Delete the layers
	$overlay_wrapper.remove();
	$overlay_panel.remove();
	if (doRefresh) {
		window.location.reload();
	} 
}

function rm_fadeOutIframe2() {
	$overlay_wrapper.fadeOut(300, rm_removeIframe);
}

function rm_fadeOutIframe() {
	arg = arguments[0];
	if (arg) {
		doRefresh = true;	
	} else {
		doRefresh = false;
	}
	$('#' + divName2).slideUp(200, rm_fadeOutIframe2);
}


function rm_fadeOutwinBackiFrame() {
	removeIE6interval();
	$('#winBL').fadeTo(100, 0, rm_fadeOutwinBackiFrame2);
}

function rm_fadeOutwinBackiFrame2() {
	$overlay_wrapper2.fadeTo(200, 0, rm_removewinBackiFrame);
}

function rm_removewinBackiFrame() {
	$overlay_wrapper2.remove();
	$overlay_panel2.remove();
}

function rm_removeAlliFrames() {
		rm_fadeOutwinBackiFrame();
		rm_fadeOutIframe();
}



function rm_adjustIframeSize(theFrame) {
	//From the iFrame, this function should be called like:
	//parent.rm_adjustIframeSize(this);
	
	var iFrameHeight = theFrame.$('table').height();

	//Set the Iframe height
	$('#' + iFrameID1).height(iFrameHeight);
	//Set the Iframe layer height
	$('#' + divName2).css("height",iFrameHeight);
	
	//Set the Black Background Layer to the proper height...
	newHeight = Math.max(getTrueHeight(), iFrameHeight + 90);
	$('#' + divName1).css("height", newHeight);
	
}


function rm_adjustIframeSize2() {
	//From the iFrame
	
	if (BrowserDetect.browser == "Chrome" || BrowserDetect.browser == "Safari") {
		//Must shrink frames/layers since offsetHeight Doesn't change.
		//Might cause a flicker
		$('#' + iFrameID1).height(10);
		$('#' + divName2).css("height",10);
		$('#' + divName1).css("height", 10);
	}

	$iFrameDoc = window.frames[iFrameID1].document.body;
	
	//alert($iFrameDoc);
	//alert($iFrameDoc.scrollHeight);
	//alert($iFrameDoc.offsetHeight);
	//alert(BrowserDetect.browser);
	
	//Below returns data, but no height for the table or body...
	//$iFrameDoc2 = window.frames[iFrameID1].document
	//alert(this.$(document));
	//alert(this.$('#testTable').height());
	
	
	if (BrowserDetect.browser == "Explorer") {
		var iFrameHeight = $iFrameDoc.scrollHeight;
	} else {
		var iFrameHeight = $iFrameDoc.offsetHeight;
	}

	//Set the Iframe height
	$('#' + iFrameID1).height(iFrameHeight);
	//Set the Iframe layer height
	$('#' + divName2).css("height",iFrameHeight);
	
	//Set the Black Background Layer to the proper height...
	newHeight = Math.max(getTrueHeight(), iFrameHeight + 90);
	$('#' + divName1).css("height", newHeight);
	
}



function getTrueHeight() {
	var trueHeight;
	pageHeight = document.getElementsByTagName("body")[0].offsetHeight;
	browserHeight = browserWindowSize()[1];
	//alert(pageHeight + ' - ' + browserHeight);
	if (pageHeight > browserHeight) {
		trueHeight = pageHeight;
	} else {
		trueHeight = browserHeight;
	}
	
	return (trueHeight);
}




//IE 6 Fixes Code:
function ie6layerSet() {
	//This must be called on Body onLoad...  resets the layer for IE6
	if ((BrowserDetect.browser == "Explorer" && BrowserDetect.version == 6)) {
		//set the layer to absolute for IE6 		
		$overlay_panel2.css("position","absolute");
		//Set interval for IE6 to move the layer 
		iemove = window.setInterval(move_box, 200);
	}	
}


function removeIE6interval() {
	if (BrowserDetect.browser == "Explorer" && BrowserDetect.version == 6) {	
		clearInterval(iemove); //Clear the IE6 Interval
	}
}

function move_box() {
	//half the layer height as offset is the top of the screen.
	//add half the browserWindowSize minus the layer height and it's centered
   var offset = ((392 / 2) + ((browserWindowSize()[1] - 392) / 2)); 
   var ie6top = (document.documentElement.scrollTop + offset);
   $overlay_panel2.css("top",ie6top);
}

function refreshThePage() {
	window.location.reload();	
}



function reCursiveOffset(theChildren, theLength) {
	//This recursive function goes through every element on the page and returns the largest width.
	//Might be slow on larger pages.... 
	
	var newWidth = 0;
		 
	 for(var i=0;i<theLength;i++){		
	 
	 	if (theChildren[i].childNodes.length > 0) {
			tempWidth = reCursiveOffset(theChildren[i].childNodes, theChildren[i].childNodes.length);
			if (tempWidth>newWidth) newWidth = tempWidth;
		}
	 
		 if(theChildren[i].offsetWidth && theChildren[i].offsetWidth>newWidth) newWidth = theChildren[i].offsetWidth;
	
	 }
	
	return newWidth;
}

var redirect;

function rm_removeIframe_2() {
	//Delete the layers
	$overlay_wrapper.remove();
	$overlay_panel.remove();
	if (doRefresh) {
		window.location.assign(redirect);
	} 
}

function rm_fadeOutIframe2_2() {
	$overlay_wrapper.fadeOut(300, rm_removeIframe_2);
}

function rm_fadeOutIframe_2(refresh, url) {
    if (refresh == null || refresh == ""){
		doRefresh = false;	
	} else {
		doRefresh = true;
	}
	if(url)
	{
	    redirect = url;
	}
	$('#' + divName2).slideUp(200, rm_fadeOutIframe2_2);
}


