/********************************************/
/*              PMG LIGHTBOX                */
/********************************************/
var gPanel = {};
function initPanels(){
	
	var lDiv = document.getElementById("window_immobilize");
	if(!lDiv){
		var lDiv = document.createElement('div');
		lDiv.id = "window_immobilize";
		// "internet explorer cannot open the internet site" "operation aborted"
		// IE ERROR //
		//It is not possible to append to the BODY element from script that isn’t a direct child to the BODY element //
		//document.body.appendChild(lDiv);
		// It is also a problem with IE 6 when appending to a tag that hasn't rendered the close tag yet //
	}
	
	for(var x in gPanels){
		var lPanel = document.getElementById(gPanels[x].panel + "_content");
		// make sure that the panel exists //
		if(lPanel){
			gPanel[x] = new PMG_panel(gPanels[x]);
			// init the global alerts only //
			// local alerts are initialized in the onclick //
			if(!gPanels[x].local){
				gPanel[x].content.innerHTML = initPanelContent({html:gPanel[x].content.innerHTML,style:gPanel[x].p_style,obj:gPanel[x]});
			}
		}
	}
	
	// Attach the listener to the onScroll() //
	// This allows the light box to change size if the text is resized //
	if(window.attachEvent){
		//window.attachEvent("onscroll", Alert_resize);
		//window.attachEvent("onresize", Alert_resize);
	}else{
		//window.addEventListener("scroll", Alert_resize, false);
		//window.addEventListener("resize", Alert_resize, false);
	}
}

function initPanelContent(p){
		var ws = '';
		ws += '<table class="alert_table" cellpadding="0" cellspacing="0" border="0">';
		var lStyle = PMG_background({graphic:"lightboxes/" + p.style + "/tl",ext:"png",scale:"scale",props:"no-repeat"});
		ws += '<tr><td class="alert_tl" style="' + lStyle + ';">';
		var lStyle = PMG_background({graphic:"lightboxes/" + p.style + "/t",ext:"png",scale:"scale",props:"repeat-x"});
		ws += '</td><td class="alert_top" style="' + lStyle + ';"></td>';
		var lStyle = PMG_background({graphic:"lightboxes/" + p.style + "/tr",ext:"png",scale:"scale",props:"no-repeat"});
		ws += '<td class="alert_tr" style="' + lStyle + ';"></td></tr>';
		
		// CONTENT //
		var lStyle = PMG_background({graphic:"lightboxes/" + p.style + "/l",ext:"png",scale:"scale",props:"repeat-y"});
		ws += '<tr><td class="alert_left" style="' + lStyle + ';width:15px;"></td>';
		//var lStyle = PMG_background({graphic:"lightboxes/default",ext:"png",scale:"scale",props:"repeat"});
		//ws += '<td id="alert_container_' + p.style + '" style="' + lStyle + ';" class="alert_content_td">';
		ws += '<td id="alert_container_' + p.obj.name + '" class="alert_content_td">';
		ws += '<div id="' + p.obj.name + '_drag" class="alert_close_container">';
		
		var lCoseButton = (p.obj.p_close_button)? "alert_close_" + p.obj.p_close_button : "alert_close";
		ws += '<a id="alert_close_' + p.obj.name + '" href="javascript:void(0);" class="' + lCoseButton +'" onclick=""></a></div>';
		ws += p.html + '</td>';
		var lStyle = PMG_background({graphic:"lightboxes/" + p.style + "/r",ext:"png",scale:"scale",props:"repeat-y;"});
		ws += '<td class="alert_right" style="' + lStyle + ';"></td></tr>'
	    
		// Add the bottom //
		var lStyle = PMG_background({graphic:"lightboxes/" + p.style + "/bl",ext:"png",scale:"scale",props:"no-repeat"});
		ws += '<tr><td class="alert_bl" style="' + lStyle + ';"></td>';
		var lStyle = PMG_background({graphic:"lightboxes/" + p.style + "/b",ext:"png",scale:"scale",props:"repeat-x"});
		ws += '<td class="alert_bottom" style="' + lStyle + ';"></td>';
		var lStyle = PMG_background({graphic:"lightboxes/" + p.style + "/br",ext:"png",scale:"scale",props:"no-repeat;"});
		ws += '<td class="alert_br" style="' + lStyle + ';"></td></tr>'
		ws += '</table>'
		
		return ws;
}

// ******************************************** //
// POPUP ALERT   
// ******************************************** //
function PMG_panel(p){

	this.disable = document.getElementById("window_immobilize");
	this.container = document.getElementById("full_screen_container_sfw");
	this.site_container = document.getElementById("site_container_sfw");
	//this.title_bar = document.getElementById("alert_title_bar");
	this.p_enabled = false;
	this.p_open = false;
	this.p_initialized = false;
	this.p_local = p.local || false;
	this.p_top = p.top || false; // default will center popup in window //
	this.p_style = p.cssStyle || "default";
	this.p_script = p.script || null;
	this.p_leaveFields = p.leaveFields || false;
	this.p_hide_close = p.hide_close || false; // hide the close button //
	this.p_close_button = (p.closeButton)? p.closeButton : false; // allow custom close button /
	this.panel = document.getElementById("alert_container");
	this.content = (typeof(p.local) != "undefined")? document.getElementById("local_content") : document.getElementById(p.panel + "_content");
	//this.title = (typeof(p.local) != "undefined")? document.getElementById("local_title") : document.getElementById(p.panel + "_title");
	this.immobilize = p.immobilize;
	this.p_immobilize_image = p.immobilize_image || gPaths.images + "lightboxes/window_immobilize.png";
	this.dragable = false; //p.dragable; force all drag features off //
	this._width = p.width;
	this.name = p.panel;
	this.location = p.location || 'center';
	this.buttons = {};//document.getElementById("alert_buttons").childNodes;
	this.previous_panel = p.previous_panel || null;
	this.clear_forms = p.clearForms || false;
	this.access_key = p.accessKey || false;
	this.hideOnOpen = (p.hideOnOpen)? document.getElementById(p.hideOnOpen) : false; // prop to hide site under the lightbox //
	
	this.closePanel = function(){
		this.p_enabled = false;
		this.p_open = false;
		this.showHideForms(true);
		this.assignKey();
		
		//this.title.style.display = "none";
		this.content.style.display = "none";
		this.panel.style.display = "none";
		this.panel.style.visibility = "hidden";
		this.disable.style.display = "none";
		this.disable.style.visibility = "hidden";
		
		// Clear the forms //
		this.clearFormsMe();
		
		if(window.attachEvent){
			window.detachEvent("onscroll", gLightBox.onScroll);
			window.detachEvent("onresize", gLightBox.onResize);
		}else{
			window.removeEventListener("scroll", gLightBox.onScroll, false);
			window.removeEventListener("resize", gLightBox.onResize, false);
		}
				
		// execute additional script //
		if(this.p_script){
			this.p_script();
		}
	
		if(this.previous_panel){
			if(gPanel[this.previous_panel]){
				gPanel[this.previous_panel].openPanel();
			}else{
				alert(this.previous_panel + "does not exist in gPanels object");
			}
		}
	}
	
	// This function will clear the forms dialog //
	this.clearFormsMe = function(){
		if(this.clear_forms){
			var lInputs = this.content.getElementsByTagName('input');
			if(lInputs.length){
				for(var x = 0; x<lInputs.length; x++){
					switch(lInputs[x].type){
						case "text":
						case "password":
							lInputs[x].value = "";
							break;
						case "radio":
						case "checkbox":
							lInputs[x].checked = false;
							break;
					}
				}
			}
			
			var lSpans = this.content.getElementsByTagName('span');
			if(lSpans.length){
				for(var x = 0; x<lSpans.length; x++){
					if(lSpans[x].className == "form_validation_text"){
						lSpans[x].style.display = "none";
					}
				}
			}			
		}
	}
	
	// this function will allow the Enter Key to execute the LightBox //
	// this.access_key needs to be set to the button id //
	this.assignKey = function(pEnable){
		if(this.access_key){
			var lButton = document.getElementById(this.access_key);
			if(lButton){
				if(pEnable){
					// store the current keypress //
					this.pSave_onkeypress = document.onkeypress;
					gForms.enterKey = lButton;
					document.onkeypress = gForms.assignEnterKey;
				}else{
					// restore the current keypress //
					document.onkeypress = this.pSave_onkeypress;
				}
			}
		}
	}
	
	// IE 6 needs to hide all drop down forms //
	// this code is causing some funky behavior need to revisit //
	this.showHideForms = function(p_show){
		// check for dropdown form items to hide in IE 6 only //
		if(gBrowser.ie6){
			var lForms = document.body.getElementsByTagName('select');
			var lShow = (p_show == true)? "visible" : "hidden";
			if(lForms.length){
				for(var x = 0; x<lForms.length; x++){
					lForms[x].style.visibility = lShow;
				}
			}
		}
	}

	this.checkFormLocation = function(pCoords){
		//gDebug.getVar(pCoords);
		//gDebug.getVar(this.p_rect)
		var lChange = false;
		if((pCoords.left > this.p_rect.left) & (pCoords.left < this.p_rect.right)){
			if((pCoords.top > this.p_rect.top) & (pCoords.top < this.p_rect.bottom)){
				lChange = true;
			}
		}
		
		return lChange;
	}
	
	// Called in the window.onScroll() to resize the lightbox //
	this.resizeDisable = function(){
		if(this.p_enabled){
			var lHeight = (document.documentElement.clientHeight)? document.documentElement.clientHeight : document.body.clientHeight;
			if(this.immobilize){
				var lScroll = document.body.scrollTop || document.documentElement.scrollTop;
				this.checkDisableExists();
				this.disable.style.width = (this.container.clientWidth-1) + "px";
				this.disable.style.height = (lHeight + lScroll) + "px";
			}
		}
	}
	
	this.centerPanel = function(pScroll){

		switch(this.location){
			case 'mouse':
				// alert(window.event.srcElement.offsetTop)
				this._top = 100;
				this._left = 100;
				
				break;
			default:
				//var lAvailableHeight = (window.innerHeight)? Math.round((window.innerHeight/2) + window.pageYOffset) : (document.documentElement.clientHeight/2) + document.documentElement.scrollTop;
				//var lContentHeight = (this.site_container.clientHeight/2)
				this._left = (document.body.clientWidth/2) - (this.panel.clientWidth/2);
				if(this.p_top){
					this._top = this.p_top;
				}else{
					this._top = (window.innerHeight)? ((window.innerHeight/2)-(this.panel.clientHeight/2) + window.pageYOffset) : (document.documentElement.clientHeight/2)-(this.panel.clientHeight/2) + document.documentElement.scrollTop;
					//document.documentElement.cumulativeScrollOffset
					//scrollHeight
				}
				break;
		}
		this.panel.style.left = this._left + "px";
		if(this._top < 25){
			this._top = 100;
		}else{
			this._top = (this._top-36);
		}
		//this.panel.style.top = (this._top < 25)? "100px" : (this._top-36) + "px";
		this.panel.style.top = this._top + "px";
		this.panel.style.visibility = "visible";
		this.p_open = true;
		this.p_rect = {left:this._left,top:this._top,right:(this.panel.clientWidth+this._left),bottom:(this.panel.clientHeight+this._top)};
		//gDebug.getVar(this.p_rect)
	}

	this.checkDisableExists = function(){
		// Add the immobilize window //
		if(!this.disable){
			this.disable = document.createElement('div');
			this.disable.id = "window_immobilize";
			var lContainer = document.getElementById("full_screen_container_sfw");
			lContainer.appendChild(this.disable);
			//document.body.appendChild(this.disable);
		}
	}
	
	this.openPanel = function(){
		// IE 7 bug work around - with Flash video //
		if(this.p_restore){
			this.content.innerHTML = this.p_restore;
		}
		gLightBox.close_AllPanels();

		this.panel.style.width = this._width + "px";
		// display needs to be set in order to calculate width and height //
		this.panel.style.display = "block";
		this.content.style.display = "block";
		// local popup only swap content and title //
		if(this.p_local){
			this.content.innerHTML = initPanelContent({html:document.getElementById(this.name + "_content").innerHTML,style:this.p_style});
		}else{
		}
	
		// Show the panel //
		this.centerPanel();
		this.showHideForms();
		// pause video if playing //
		gLightBox.pauseAV();
		
		// do lightbox assigned in config_pmg.js //
		if(this.immobilize){
			this.checkDisableExists();
			// set the transparency for IE only //
			if((gBrowser.vendor == "IE") && !gBrowser.support_png){
				//this.disable.style.filter = 'alpha(opacity=50)';
				this.disable.style.background = "none";
				this.disable.style.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="' + this.p_immobilize_image + '",sizingMethod="scale")';
			}
			this.p_enabled = true;
			this.resizeDisable();
			this.disable.style.display = "block";
			//this.disable.style.width = "99.9%";
			this.disable.style.visibility = "visible";			
		}
		
		if(this.hideOnOpen){
			this.hideOnOpen.style.visibility = "hidden";
		}
		
		// Close function //
		var lClose = {};
		lClose.onclick = function test_close(){
			var lPanel = p.panel;
			if(p.local){
				lPanel = p.local;
			}
			//if(gBrowser.vendor == "IE"){location.reload();}
			gPanel[lPanel].closePanel();
		}
		
		// assign the close panel to the X //
		var lX = document.getElementById("alert_close_" + this.name);
		if(lX){
			// hide the close button //
			if(this.p_hide_close){
				lX.style.display = "none";
			}
			// allow for custon close scripts //
			if(this.p_script){
				//lClose.onclick = this.p_script; 
			}
			
			lX.onclick = lClose.onclick;
		}
		
		// BUTTONS //
		for(var x in this.buttons){
			if(this.buttons[x].id){
				this.buttons[x].onclick = function(){
					//gPanel[p.panel].closePanel();
					lClose.onclick();
					return false;
				}
			}
		}
		lClose.onmousedown = function(ev){
			// prevent the X from draging the window //
			if(gBrowser.vendor == "IE"){
				event.cancelBubble = true;
			}else{
				ev.stopPropagation();
			}
		}
		// make the panel dragable not working SSK 20081205 //
		if(this.dragable){
			document.onmousemove = move_Alert;
			//alert(this.name)
			this.drag_div = document.getElementById(this.name + "_drag");
			this.drag_div.style.cursor = "move";
			//this.title_bar.style.cursor = "move";
			//makeDraggable(this.title_bar);
			makeDraggable(this.drag_div);
		}else{
			//this.title_bar.style.cursor = "default";
			//this.drag_div.style.cursor = "default";
		}
		
		
		// This allows the lightbox to change size //
		if(window.attachEvent){
			window.attachEvent("onscroll", gLightBox.onScroll);
			window.attachEvent("onresize", gLightBox.onResize);
		}else{
			window.addEventListener("scroll", gLightBox.onScroll, false);
			window.addEventListener("resize", gLightBox.onResize, false);
		}
		
		this.assignKey(true);
		
	}
}

// This function allows a sigle page to have custom popups //
// It must be added to the onload function in order to work properly //
function formatPagePanels(){

	var lDisable = document.getElementById("window_immobilize");
	var lContainer = document.getElementById("full_screen_container_sfw");	

	if(!lDisable){
		lDisable = document.createElement('div');
		lDisable.id = "window_immobilize";
		lContainer.appendChild(lDisable);
	}

	// this is to add a single .aspx pages gPanels //	
	var lHolder = document.getElementById('alert_container').parentNode;
	var lContent = document.getElementById("alert_container");
	lHolder.removeChild(lContent);
	lContainer.appendChild(lContent);
	
	initPanels();

}



function formatSinglePanel(obj){
	
	if(typeof(obj) != "object" || typeof(obj.panels) != "object"){
		alert("obj.panels need to be an array. Called in formatSinglePanel()");
		return;
	}

	var lAppend = false;
	if(!document.getElementById("window_immobilize")){
		var lCreate = document.createElement('div');
		lCreate.id = "window_immobilize";
		lAppend = true;
	}
	
	var lAlert = document.getElementById("alert_container");
	if(!lAlert){
		lAlert = document.createElement('div');
		lAlert.id = "alert_container";
	}

	var lContainer = document.getElementById("full_screen_container_sfw");	
	

	
	for(var x = 0; x<obj.panels.length; x++){
		// this is to add a single .aspx pages gPanels //	
		var lPanel = obj.panels[x];
		var lContent = document.getElementById(lPanel + "_replace");
		if(!lContent){return;}
		var lHolder = lContent.parentNode;
		var lHTML = lContent.innerHTML;
		lContent.innerHTML = "";
		lHolder.removeChild(lContent);
		//lContainer.appendChild(lContent);
		
		// Create the Content Divs //
		var lDiv = document.createElement('div');
		lDiv.id = lPanel + "_content";
		lDiv.className = "alert_content";
		lDiv.innerHTML = lHTML;
		lAlert.appendChild(lDiv);
		//alert(document.getElementById("window_immobilize").parentNode.nodeName)
	}
	
	if(lAppend){
		document.body.appendChild(lCreate);
		document.body.appendChild(lAlert);
	}

	
	initSinglePanel(obj.panels)
	
}

function testing(e,obj){
	//var ev = (e)? e.target : window.event.srcElement;
	alert(e)

}

var gPopups = {};
gPopups.pCurrent = null;
gPopups.init = function(ev,obj){
	if(ev.pageX || ev.pageY){
		var M = {x:ev.pageX, y:ev.pageY};
	}else{
		var M = {};
		M.x = (ev.clientX + document.body.scrollLeft - document.body.clientLeft);
		M.y = (ev.clientY + document.body.scrollTop  - document.body.clientTop);
	}

	if(!gPopups[obj.id]){
		var lButton = new createDynamicPopup(ev,obj);
		gPopups[obj.id] = lButton;
	}
}


function createDynamicPopup(ev,p){
	
	this.p = p;
	this.p.p_event = ev;
	this.p.pUse_iFrame = gBrowser.ie6;
	var lDiv = document.getElementById(this.p.id + "_popup_content");
	this.p.p_text = (lDiv)? lDiv.innerHTML : this.p.title;
	this.p.title = "";
	
	this.p.onmouseover = function(event){
		if(event){
			this.p_event = event;
		}
		
		if(this.p_event.pageX || this.p_event.pageY){
			var M = {x:this.p_event.pageX, y:this.p_event.pageY};
		}else{
			var M = {};
			var lOffsetTop = (document.documentElement)? document.documentElement.scrollTop : document.body.scrollTop;
			var lOffsetLeft = (document.documentElement)? document.documentElement.scrollLeft : document.body.scrollLeft;
			//document.documentElement.scrollLeft
			
			M.x = (this.p_event.clientX + lOffsetLeft);
			M.y = (this.p_event.clientY + lOffsetTop);
		}
		
		this.showMe(M);
	}
	
	// IE 6 needs an iFrame @#$! //
	this.p.initMe = function(){
		var iFrame = document.getElementById(this.id + "_popup_iframe");
		if(iFrame && this.pUse_iFrame){
			this.p_frame = iFrame;
		}else{
			// create the iFrame in IE6 only//
			if(this.pUse_iFrame){
				var iFrame = document.createElement('iframe');
				iFrame.id = this.pPanel + "_popup_iframe";
				iFrame.className = "dynamic_iframe";
				iFrame.style.position = "absolute";
				iFrame.style.left = "0px";
				iFrame.style.top = "0px";
				iFrame.style.visibility = "hidden";
				iFrame.style.display = "none";
				iFrame.style.overflow = "hidden";
				iFrame.scrolling = "no";
				iFrame.frameBorder = "0";
				iFrame.src = "/blank.html";
				
				document.body.appendChild(iFrame);
				this.p_frame = iFrame;
			}
			
			var iPOP = document.createElement('div');
			iPOP.id = this.id + "_dynamic_popup";
			iPOP.className = "dynamic_popup";
			iPOP.style.position = "absolute";
			iPOP.style.left = "0px";
			iPOP.style.top = "0px";
			iPOP.style.visibility = "hidden";
			iPOP.style.display = "none";
			iPOP.style.overflow = "hidden";
			this.p_content = iPOP;
			document.body.appendChild(iPOP);
			
		}
		
		this.onmouseover()
	}
		
	this.p.showMe = function(pLoc){
		//alert(this.p_content.id)
		//gDebug.getVar(pLoc)
		
		if(pLoc){
			var lAmount = 25;
			this.p_content.innerHTML = this.p_text;
			this.p_content.style.display = "block";

			var lOffsetY = this.p_content.clientHeight;
			this.p_content.style.left = pLoc.x + "px";
			this.p_content.style.top = (pLoc.y-(lOffsetY+lAmount)) + "px";
			
			if(this.p_frame){
				//alert(this.p_frame)
				this.p_frame.style.display = "block";
				this.p_frame.style.height = this.p_content.clientHeight;
				this.p_frame.style.width = this.p_content.clientWidth;
				this.p_frame.style.left = pLoc.x + "px";
				this.p_frame.style.top = (pLoc.y-(lOffsetY+lAmount)) + "px";
				this.p_frame.style.visibility = "visible";
			}
			
			this.p_content.style.visibility = "visible";
			
		}else{
			
			if(this.p_frame){
				this.p_frame.style.visibility = "hidden";
			}
			
			this.p_content.style.visibility = "hidden";
		}
	}
	
	this.p.onmouseout = function(){
		this.showMe(false);
	}

	
	this.p.initMe();
	
}

// This is needed for IE only Firefox by default will pause the AV //
// pID passes the ID of the flash control //
function closeAV_Panel(pID){
	if(gBrowser.vendor == "IE"){
		var lFlash = (pID)? document.getElementById(pID) : document.getElementById("flv_id");
		if(lFlash && lFlash.pauseVideo){
			lFlash.pauseVideo();
		}else{
			// IE 7 bug - allows for the video or audio to stop playing //
			/*
			this.p_restore = this.content.innerHTML;
			if(this.p_restore){
				this.content.innerHTML = "";
			}
			*/
		}
	}
}

function GetInnerSize() {
	var x,y;
	if (self.innerHeight) // all except Explorer
	{
		x = self.innerWidth;
		y = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight)
		// Explorer 6 Strict Mode
	{
		x = document.documentElement.clientWidth;
		y = document.documentElement.clientHeight;
	}
	else if (document.body) // other Explorers
	{
		x = document.body.clientWidth;
		y = document.body.clientHeight;
	}
	return [x,y];
}

// allows for a single panel to be added to the global panels array //
function initSinglePanel(p){
	var lPanels = p;
	
	if(typeof(p) != "object"){
		lPanels = [p];
	}
	
	for(var x =0; x<lPanels.length; x++){
		gPanel[lPanels[x]] = new PMG_panel(gPanels[lPanels[x]]);
		// init the global alerts only //
		// local alerts are initialized in the onclick //
		if(!gPanel[lPanels[x]].local){
			gPanel[lPanels[x]].content.innerHTML = initPanelContent({html:gPanel[lPanels[x]].content.innerHTML,style:gPanel[lPanels[x]].p_style,obj:gPanel[lPanels[x]]});
		}
	}
}

// ********************************* //
//   Opens a gPanel with an iFrame   //
// ********************************* //
function PMG_iFrame(p){
	
	var iFrame = document.getElementById("iframe_" + p.id);
	//var iSrc = p.loc; // PM - needed to add to reload the iFrame
	// create the iFrame //
	if(!iFrame){
		var iFrame = document.createElement("iframe");
		iFrame.id = "iframe_" + p.id;
		iFrame.style.position = "relative";
		iFrame.style.left = "0px";
		iFrame.style.top = "0px";
		iFrame.style.visibility = "visible";
		iFrame.style.display = "block";
		iFrame.style.overflow = "hidden";
		iFrame.scrolling = "no";
		iFrame.frameBorder = "0";
		iFrame.src = "/common/popup.html";
		//iFrame.src = p.loc;

		// Check to see if the immobilize div exists //
		var lAppend = false;
		if(!document.getElementById("window_immobilize")){
			var lWindow = document.createElement('div');
			lWindow.id = "window_immobilize";
			lAppend = true;
		}
		
		// Check to see if the alert container exists //
		var lAlert = document.getElementById("alert_container");
		if(!lAlert){
			lAlert = document.createElement('div');
			lAlert.id = "alert_container";
		}
		
		// creat a reference to the main site container //
		var lContainer = document.getElementById("full_screen_container_sfw");	
	
		// Create the Content Divs //
		var lDiv = document.createElement('div');
		lDiv.id = p.id + "_content";
		lDiv.className = "alert_content";
		lDiv.appendChild(iFrame);
		//iFrame.src = "planner_iframe.aspx";
		lAlert.appendChild(lDiv);
		//alert(document.getElementById("window_immobilize").parentNode.nodeName)
	
		if(lAppend){
			document.body.appendChild(lWindow);
			document.body.appendChild(lAlert);
		}
		// IE need this after the blank.html file is loaded //
		iFrame.src = (p.loc)? p.loc : "http://www.google.com";
		initSinglePanel([p.id])
	} else {
		//iFrame.src = "http://www.google.com";
		//iFrame.src = iSrc; // Need to reload the iFrame
		//iFrame.src = p.loc;
	}
	
	if(iFrame.src != p.loc){
		iFrame.src = p.loc;
	}
	gPanel[p.id].openPanel();
	//alert("PMG_iFrame('" + pObj.id + "') \nWe will use an iFrame to display this content.")
}


gLightBox.init = function(p){
	if(typeof(p) != "object"){
		alert("Object not passed to gLightBox.init()");
		return;
	}
	
	// check if the panel already exists //
	if(gPanel[p.div]){
		gPanel[p.div].openPanel();
	}else{
		// create and launch the panel //
		if(!gPanels[p.div]){alert(p.div + " not defined in gPanels"); return;}
		var lContent = document.getElementById(p.div);
		if(!lContent && !gPanels[p.div].db){
			alert(p.div + " not found on page\n" + location.href); 
			return;
		}
		
		// Check to see if the immobilize div exists //
		var lAppend = false;
		if(!document.getElementById("window_immobilize")){
			var lCreate = document.createElement('div');
			lCreate.id = "window_immobilize";
			lAppend = true;
		}
		
		// Check to see if the alert container exists //
		var lAlert = document.getElementById("alert_container");
		if(!lAlert){
			lAlert = document.createElement('div');
			lAlert.id = "alert_container";
		}
	
		if(gPanels[p.div].db){
			var lDiv = document.createElement('div');
			lDiv.id = p.div + "_content";
			lDiv.className = "alert_content";
			lAlert.appendChild(lDiv);
			gLightBox.props.db = p.div;
			//gLightBox.getDatabase_content(p);

			Sys.Net.WebServiceProxy.invoke('/GlobalMethods.asmx', 'GetGenericLightBox', true, {"strTitle":p.div} , gLightBox.populateLightBox);
			/*
			if(PageMethods && PageMethods.GetGenericLightBox){
				PageMethods.GetGenericLightBox(p.div, gLightBox.populateLightBox)
				
			}else{
				alert("PageMethods.GetGenericLightBox() not defined in " + unescape(location.href));
				return;
			}
			*/
		}else{
			// creat a reference to the main site container //
			var lContainer = document.getElementById("full_screen_container_sfw");	
				
			// Get the parent of the content div //	
			var lHolder = lContent.parentNode;
			var lClone = lContent.cloneNode(true);
			lClone.style.display = "block";
			var lHTML = lContent.innerHTML;
			lHolder.removeChild(lContent);
	
			// Create the Content Divs //
			var lDiv = document.createElement('div');
			lDiv.id = p.div + "_content";
			lDiv.className = "alert_content";
			lDiv.appendChild(lClone);
			lAlert.appendChild(lDiv);
		}
		
		if(lAppend){
			var lMashBaby = document.getElementById("full_screen_container_sfw");
			if(lMashBaby){
				lMashBaby.appendChild(lCreate);
				lMashBaby.appendChild(lAlert);
				//document.body.appendChild(lCreate);
				//document.body.appendChild(lAlert);
			}
		}
		
		initSinglePanel([p.div])
		if(gPanels[p.div].db){
			// this is needed to make sure the content is centered //
			gPanel[p.div].centerPanel();
			setTimeout('gPanel["' + p.div + '"].openPanel()',100);
		}else{
			gPanel[p.div].openPanel();
		}
	}
	
}


gLightBox.populateLightBox = function(pResult){


	if(gLightBox.props.db){
		var lButtons = [{id:"close",label:"Close"}];
		
		if(gPanels[gLightBox.props.db].buttons){
			lButtons = gPanels[gLightBox.props.db].buttons;
		}

		if(lButtons.length){
			var lButton_wrapper = document.createElement('div');
			if(gPanels[gLightBox.props.db].button_loc){
				lButton_wrapper.className = "button_container_" + gPanels[gLightBox.props.db].button_loc;
			}else{
				lButton_wrapper.className = (lButtons.length == 1)? "button_container_center" : "button_container_right";
			}
			// Create the Buttons //
			for(var x = 0; x<lButtons.length; x++){
				//gDebug.getVar(lButtons[x]);
				var lA = document.createElement('a');
				lA.p_id = gLightBox.props.db
				lA.id = gLightBox.props.db + "_" + lButtons[x].id.toLowerCase();
				lA.className = lButtons[x].className || "orangeButton";
				lA.href = "javascript:void(0);";
				lA.style.marginLeft = (lButtons.length == 1)? "0px" : "10px";
				// all buttons need an id and a label //
				if(lButtons[x].id){
					if(!lButtons[x].action){
						// default close button //
						lA.onclick = function(){
							gPanel[this.p_id].closePanel();
						}
					}else{
						// this is for custom "Submit" button //
						lA.onclick = eval(lButtons[x].action);
					}
				}
				
				var lText = document.createTextNode(lButtons[x].label);
				lA.appendChild(lText);
				lButton_wrapper.appendChild(lA);
			}
		}

		var lContainer = document.getElementById("alert_container_" + gLightBox.props.db);
		var lDiv_content = document.createElement('div');
		lDiv_content.id = gLightBox.props.db;
		lDiv_content.innerHTML = pResult;
		lContainer.appendChild(lDiv_content);
		
		if(lButtons.length){
			lContainer.appendChild(lButton_wrapper);
		}
		
		// see if any bullets need formatting //
		if(lDiv_content.innerHTML.indexOf("bullets_wrapper") != -1){
			initBullets({div:gLightBox.props.db});
		}
		
		
	}else{
		//alert(result)
	}
}

// this function resizes the screen //
gLightBox.onScroll = function(){
	for(var x in gPanel){
		if(gPanel[x].p_enabled){
			gPanel[x].resizeDisable();
			break;
		}
	}
}

// centers the alert lightbox in the window //
gLightBox.onResize = function(){
	for(var x in gPanel){
		if(gPanel[x].p_enabled){
			gPanel[x].resizeDisable(true);
			gPanel[x].centerPanel();
			break;
		}
	}
}

// called when a lightbox is closed //
gLightBox.close_AllPanels = function(){
	for(var x in gPanel){
		if(gPanel[x].p_open){
			if(typeof(gPanel[x].closePanel) == "function"){
				gPanel[x].closePanel();
			}
		}
	}

	//document.onmousemove = "";
	//document.onmouseup = "";
	//gDND_Object = null;
}

// this is called when a lightbox is opened //
gLightBox.pauseAV = function(pID){
	var lFlash = (pID)? document.getElementById(pID) : document.getElementById("flv_id");
	if(lFlash && lFlash.pauseVideo){
		lFlash.pauseVideo();
	}
}

gLightBox.loadingData = function(){
	document.body.style.cursor = "wait";
}

gLightBox.loadingData_Done = function(){
	
}


/***********************************************/
/************  GENERIC FUNCTIONS  **************/
/***********************************************/
function showPanel(pObj){
	
	var lPreviousPanel = null;
	
	if(pObj.previousPanel){
		lPreviousPanel = pObj.previousPanel;
	}
	
	gLightBox.init({div:pObj.panel});
	gPanel[pObj.panel].previous_panel = lPreviousPanel;
	
	if(pObj.panel == "coach_calls"){
		var so = new SWFObject(gPaths.flash + "av_player/av_player.swf?p_type=audio&p_auto_play=false", "flash_mp3_id", "350", "50", "8");
		so.addParam("salign", "tl");
		//so.write("flash_audio");
		checkFlashValid({obj:so,div:"flash_audio"});
		if(gHpp_Popups && gHpp_Popups.hpp_coach){
			gHpp_Popups.hpp_coach.closePanel();
			//initBullets({div:"coach_calls"});
		}
	}
}

// This function is called from the iFram onload //
function initHeights(containers){
    // defaults if not passed //
	if (!containers){
        containers = {};
    }
	
	containers.iframe_id = containers.iframe_id || 'iframe_view';
    containers.main_id = containers.main_id || 'main_container';
    containers.content_id = containers.content_id || 'content_wrapper';

    var iFrameID = parent.document.getElementById(containers.iframe_id);
    var mainID = document.getElementById(containers.main_id);
    var contentID = document.getElementById(containers.content_id);
	
	//alert("iFrameID = " + iFrameID + "\nmainID = " + mainID + "\ncontentID = " + contentID)
	if(iFrameID && mainID && contentID){
		// Change the heights of the wrapper and iframe to the accomodate all of the content, this will remove the need for the inner scrollbar
		mainID.style.height = contentID.clientHeight + 5 + 'px';
		iFrameID.style.height = contentID.clientHeight + 'px';
		// we may want to call this too //
		if(parent.gPanel && parent.gPanel.recipe_view){
			parent.gPanel.recipe_view.centerPanel();
		}
	}
}

function initLibraryVideo(){

	if(gVideos[gProps.library.category] && gVideos[gProps.library.category].video){
		
		var lContainer = document.getElementById("library_video_link_wrapper");
		var lGraphic = document.getElementById("img_library_video_link");
		var lLink = document.getElementById("library_video_link");
		if(lContainer){
		
			gPanels.library_video = {};
			gPanels.library_video.panel = "library_video";
			gPanels.library_video.immobilize = true;
			gPanels.library_video.dragable = false;
			gPanels.library_video.cssStyle = "blue";
			gPanels.library_video.width = 400;
			gPanels.library_video.script = closeAV_Panel;
			
			
			
			lGraphic.src = gPaths.images + "library/vid_" + gProps.library.category + ".jpg";
			lGraphic.onclick = function(){
				//alert(gVideos[gProps.library.category].video);
				if(gPanel.library_video){
					gPanel.library_video.openPanel();
				}else{
					var lCreate = document.createElement("div");
					lCreate.id = "library_video";
					lCreate.style.textAlign = "center";
					lCreate.className = "alert_content";
					// create the flash wrapper //
					var lDiv = document.createElement("div");
					lDiv.id = "library_video_wrapper";
					
					var lBlausen = document.createElement("div");
					lBlausen.id = "blausen";
					
					lCreate.appendChild(lDiv)
					lCreate.appendChild(lBlausen)
					document.body.appendChild(lCreate);
				
					var lPath = '';
					lPath += gPaths.flash + "av_player/av_player.swf?";
					lPath += "p_video=" + gPaths.video + gProps.library.category + ".flv";
					var so = new SWFObject(lPath, "flv_id", "320", "265", "8", "#000000");
					
					gLightBox.init({div:'library_video'});
					checkFlashValid({obj:so,div:"library_video_wrapper"});
					//so.write("library_video_wrapper");
					
					
					if(gBrowser.vendor == "IE"){
						var lFlash = document.getElementById("flv_id");
						if(lFlash){
							window.flv_id = lFlash;
						}
						//window.flv_id = document.forms[0]["flv_id"];
					}

					
				}
			}
			
			lLink.onclick = lGraphic.onclick;
			
			//gLightBox.init({div:'library_video'})
			
			//alert(lGraphic)
			
			lContainer.style.display = "block";
		}
		
	}
}
