//Global Lightwindow:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
/*
Note: globalWin utilizes the globalBase element that is created with the DE global header and footer
- to init an instance of the global light window, call globalWin('path-to-file','title of light window',true or false to hide embedded elements)
- globalWin will adapt to the size of the file being called.  The file should not contain body or html tags or links to styles.  Stylesheets, etc should be loaded into the parent document.
- a height and width must be defined in the top node-element of the file being loaded into the light window
*/

//check for IE
var IE = document.all?true:false;
embedHide = 0;


// JavaScript Document
function deGlobalWin(params) {
	
			pG = params;
		
			//hide flash objects, these get shown again in the clearWin function
			if($$("object")){
				var flashEls = $$("object");
				for (var i=0; i<flashEls.length; i++) {
					flashEls[i].style.visibility = "hidden";
				}
			}
			
			//hide iframes, these get shown again in the clearWin function
			if($$("iframe")){
				var frames = $$("iframe");
				for (var i=0; i<frames.length; i++) {
					frames[i].style.visibility = "hidden";
				}
			}
			
			//if IE remove els by default	
			if	(IE) {removeEls('hide');}
			
			
			////////
			//create the screen
			var win = "<div id='globalWin' style='display:none;' onclick='clearWin();' align='center'></div>";
			
			////////
			//insert into DOM we're currently using globalBase, but this can be any element that is at the top of your document
			
			if(document.body.id){
				var bigID = document.body.id;
			}else{
				var bigID = "bigID";
				document.body.id = bigID;
			}
			
			$(bigID).insert(win);
			$('globalWin').appear({ duration: .6 });
			
			
			////////
			//set screen's width and height
			var newDim = document.body.scrollHeight
			var newDim2 = document.viewport.getHeight()

			if(newDim < newDim2){newDim = newDim2;}
			
			$('globalWin').style.height = newDim+"px";
			$('globalWin').style.width = "100%";
			
			
			
			////////
			//create the content window
			var conWin = "<div id='conWin' align='center' style='display:none;'><div id='winLoader'>Loading...<br><a href='#' onclick='clearWin();' id='winCancel'>cancel</a></div><div id='conWinInner' style='top:40px;visibility:hidden;' align='center'></div></div>";
			//insert into DOM
			$('globalWin').insert({before: conWin});
			$('conWinInner').style.visibility = "hidden";
			
			
			////////
			//SET LOADER, check to see if loader is needed defaults to true
			if(params.loader==false){
				$('winLoader').hide();
			}
			
			////////
			//position loader in the center of screen
			resizeWinLoader();
			
			////////
			//DISPLAY loader and screen
			$('conWin').appear({ duration: .6 });
			
		
		
			////////
			//set resize ans scroll observers to keep window centered
			Event.observe(window, 'scroll', function() {
				if ($('globalWin')){										 
					resizeWin(1);
				}
			});
		
		
			Event.observe(window, "resize", function() {
				if ($('globalWin')){										 
					resizeWin(1);
				}
			});
		
		
			////////
			//AJAX OR DIV  --  is this an ajax call or are we get innerhtml from a div
			if(params.div==undefined){
				//creat the content window
				new Ajax.Updater('conWinInner', params.path, {
					parameters: {  },
					evalScripts: true,
					onComplete: function(){
						showme();
					}
				});
			}else{
				var winFill = $(params.div).innerHTML;
				$('conWinInner').update(winFill);
				setTimeout("showme();",750);
			}
	}


	////////////////
	//no evaluate the hash parameters and display the window
	function showme(){

			////////
			//This just strips out some tags that make things unsightly
			var badTag = $$('#conWinInner title');
			var badTag2 = $$('#conWinInner meta');
			
			for (var i=0; i<badTag.length; i++) {
				Element.remove(badTag[i]);
			}
			for (var i=0; i<badTag2.length; i++) {
				Element.remove(badTag2[i]);
			}
			////////
			
			if(pG.hasOwnProperty('width')){
				$('conWinInner').setStyle({width:pG.width});
			}else{
				//get content width
				var conWidth = $('conWinInner').firstDescendant().getWidth();
				if(conWidth!=0){
					$('conWinInner').style.width = conWidth+"px";
				}else{
					$('conWinInner').style.width = "400px";
				}
			}	
			
			
			
			
			
			////////
			//Set pretty much any inline css you desire...  fill more in here if you'd like http://www.w3.org/TR/DOM-Level-2-Style/css.html#CSS-ElementCSSInlineStyle
			if(pG.hasOwnProperty('background')){$('conWinInner').setStyle({background:pG.background});}
			
			if(pG.hasOwnProperty('border')){$('conWinInner').setStyle({border:pG.border});}
			
			
			
			if(pG.hasOwnProperty('height')){$('conWinInner').setStyle({height:pG.height});}
			
			if(pG.hasOwnProperty('overflow')){$('conWinInner').setStyle({overflow:pG.overflow});}
			

			
			////////
			//insert the title - header for light box content
			var closeBthClass = "";
			if(pG.hasOwnProperty('title')){
				var titleWin = "<h4 class='win' id='titleHandle' align='left'> &nbsp;"+pG.title+"</h4>";
				$('conWinInner').insert({top: titleWin});
			}else{
			////////
			//Alternate close button style
				var closeBthClass = "grayCloseWin";
			}
			

			////////
			//insert close window link
			if(pG.closebtn!=false){
				
				var closeWin = "<a class='closeWin "+closeBthClass+"' onclick='clearWin();'>X CLOSE</a>";
				$('conWinInner').insert({top: closeWin});
			}
			
			
			////////
			//Let's get display this thing already...  call the resize function to get center the window, resize() is also called by scroll and resize window events
			resizeWin();
			$('winLoader').fade({duration:.5,scope: 'present',queue: 'front'})
			$('conWinInner').style.display = "none";
			$('conWinInner').style.visibility = "";
			$('conWinInner').appear({scope: 'present',queue: 'end',duration:.5 });
			Element.remove($('winLoader'));
			
			////////
			//make it dragable
			if(pG.drag==true||pG.drag=='full'){
				if ($('titleHandle') && pG.drag!='full'){
					new Draggable('conWinInner', {scroll: window,handle: 'titleHandle',onDrag:function(){
						Event.stopObserving(window, 'scroll');
						Event.stopObserving(window, 'resize');
						}
					});
					$('titleHandle').style.cursor="move";
				}else{
					new Draggable('conWinInner', {scroll: window,onDrag:function(){
						Event.stopObserving(window, 'scroll');
						Event.stopObserving(window, 'resize');
						}
					});
					
				}
			}		
	}
		
	



	function unRegisterResize(){
		Event.stopObserving(window, 'scroll');
		Event.stopObserving(window, 'resize');	
	}



	function removeEls(act) {
		if (act == 'hide'){
		
			$$('select').each(function(s,index) {
					s.hide();
			});
		
		} else {
		
			$$('select').each(function(s,index) {
				s.show();
			});
			
		}
		
	}



	function resizeWin(observeCall) {
		//set a default for now
		var element = 'conWinInner';
		if($(element) != null) {
			if(typeof window.innerHeight != 'undefined') {
				
				yWithScroll = document.body.scrollHeight;
				var maxTop = Math.round(yWithScroll - $(element).getHeight());
				
				var newHeight = Math.round(document.viewport.getScrollOffsets().top + 
				((window.innerHeight - $(element).getHeight()))/2);
	
				if (newHeight < 0) {
					var newHeight = 1;
				} else if (newHeight > maxTop){//if the new position is great than the height of the lightwindow - body height
					var newHeight = maxTop - 1;
				}
				
				if (window.innerHeight < $(element).getHeight() && observeCall != 1){
					$(element).style.top = newHeight+'px';
				} else if (window.innerHeight > $(element).getHeight()){
					$(element).style.top = newHeight+'px';
				}
					
			} else {//this is IE primarily 
				
				yWithScroll = document.body.scrollHeight;
				var maxTop = Math.round(yWithScroll - $(element).getHeight());
				
				var newHeight = Math.round(document.viewport.getScrollOffsets().top + 
				((document.viewport.getHeight() - $(element).getHeight()))/2)
				
				if (newHeight < 0) {
					var newHeight = 1;
				} else if (newHeight > maxTop){
					var newHeight = maxTop - 1;
				}
			
				if (document.viewport.getHeight() < $(element).getHeight() && observeCall != 1){
					$(element).style.top = newHeight+'px';
				} else if (document.viewport.getHeight() > $(element).getHeight()){
					$(element).style.top = newHeight+'px';
				}
				
			}
		}
	}

	function resizeWinLoader(observeCall) {
		//set a default for now
		var element = 'winLoader';
		if($(element) != null) {
			if(typeof window.innerHeight != 'undefined') {
				
				yWithScroll = document.body.scrollHeight;
				var maxTop = Math.round(yWithScroll - $(element).getHeight());
				
				var newHeight = Math.round(document.viewport.getScrollOffsets().top + 
				((window.innerHeight - $(element).getHeight()))/2);
	
				if (newHeight < 0) {
					var newHeight = 1;
				} else if (newHeight > maxTop){//if the new position is great than the height of the lightwindow - body height
					var newHeight = maxTop - 1;
				}
				
				if (window.innerHeight < $(element).getHeight() && observeCall != 1){
					$(element).style.top = newHeight+'px';
				} else if (window.innerHeight > $(element).getHeight()){
					$(element).style.top = newHeight+'px';
				}
					
			} else {//this is IE primarily 
				
				yWithScroll = document.body.scrollHeight;
				var maxTop = Math.round(yWithScroll - $(element).getHeight());
				
				var newHeight = Math.round(document.viewport.getScrollOffsets().top + 
				((document.viewport.getHeight() - $(element).getHeight()))/2)
				
				if (newHeight < 0) {
					var newHeight = 1;
				} else if (newHeight > maxTop){
					var newHeight = maxTop - 1;
				}
			
				if (document.viewport.getHeight() < $(element).getHeight() && observeCall != 1){
					$(element).style.top = newHeight+'px';
				} else if (document.viewport.getHeight() > $(element).getHeight()){
					$(element).style.top = newHeight+'px';
				}
				
			}
		}
	}




	function clearWin() {
		Element.remove($('globalWin'));
		Element.remove($('conWin'));
		
		var flashEls = $$("object");
		for (var i=0; i<flashEls.length; i++) {
			flashEls[i].style.visibility = "visible";
		}
		
		if($$("iframe")){
			var frames = $$("iframe");
			for (var i=0; i<frames.length; i++) {
				frames[i].style.visibility = "visible";
			}
		}
		
		
		if	(IE) {
			removeEls('show');
		}	
		
		//special exception for player to hide embedded media
		if (embedHide == 1){
			embedHide = 1;
			$('vidWindow').style.position = "relative";
			$('vidWindow').style.left = "0px";
		}
		
		
	}