// JavaScript Document

/*
**********************************
WINDOW OBJECT VARIABLES
**********************************
*/
var WindowObjectReference = null; // global variable
var PreviousUrl;	
/*
*	function openWin (Opens a centered window object)
*
*	@url = URL String (Required)
*	@win = Window Name (Required)
*	@myWidth = Window Width (Required)
*	@myHeight = Window Height (Required)
*	@scrollBars = Yes/No Value (Required)		
*/
function openWin(url,win,myWidth,myHeight,scrollBars){
	var tHeight=screen.height/2;
	var tWidth=screen.width/2;
	var top=tHeight-myHeight/2;
	var left=tWidth-myWidth/2;
	var Feats="width="+myWidth+",height="+myHeight+",top="+top+",left="+left+", screenY=" +top+",screenX="+left+",scrollbars="+scrollBars+", toolbar=no, status=yes, resizable=no,alwayRaised=yes";
	if(WindowObjectReference == null || WindowObjectReference.closed)
		{
			WindowObjectReference=window.open(url,win,Feats);
			WindowObjectReference.focus();
		}
		else if(previousUrl !=url)
		{
			WindowObjectReference=window.open(url,win,Feats);
			WindowObjectReference.focus();
		}
		else
		{
			WindowObjectReference.focus();
		}
	previousUrl=url;
}

/*
**********************************
GENERIC WINDOW OBJECT VARIABLES
**********************************
*/
var curGenWin = '';
var curGenBG = '';
var curGenCloseFunc = '';
var doAnim = true;

/*
*
*	function openGenWin
*
*	this function is used to open a generic popup window (inside the browser)
*
*	@w	name of window
*	@c	this is used to pass the name of a container to use to evaluate
*		left positioning. If not passed we base it on the document
*	@o	the overlay div - defaults to genericPopUpBG
*	@ft	if this number is passed, it sets the window at a fixed top value
*	@fl	if this number is passed, it sets the window at a fixed left value
*	@a	boolean that controls whether or not to run animation
*	@m	boolean that controls whether or not to reposition	
*	@l	boolean that controls whether or not to add a listener to the bg to close the window
*	@fn	A function to pass to the auto close function
*
*	pass any 
*
*/
function openGenWin(w,c,o,ft,fl,a,m,l,fn){
	
	var curPageSize = Lightbox.prototype.getPageSize();
	var arrayPageScroll = document.viewport.getScrollOffsets();
	
	var win = $(w);
	var winTop = 0;
    var winLeft = 'auto';
	var winWidth = win.getWidth()/2;
	
	// 1
	// only center align if reposition is requested or set abs left if passed
	if(fl == undefined || m == undefined || m == true){
		// set left placement value
		if (c != undefined && c != null && c != '' && win.getOffsetParent().id == c){
			winLeft = $(c).getWidth()/2 - winWidth;
		}else{
			winLeft = curPageSize[0]/2 - winWidth;	
		}
	}else if (fl != undefined && !isNaN(fl)){
		winLeft = fl;
	}
	
	// 2
	// set up positioning from top if passed or auto setup
	if (ft && !isNaN(ft)){
		winTop = ft;
	}else{
		winTop = arrayPageScroll[1] + (document.viewport.getHeight()/2 - win.getHeight()/2)
	}
	
	// 3
	// set default overlay if other not passed
	if (o == undefined){
		o = 'genericPopUpBG';			
	}	
	// make sure the overlay exists
	if (!$(o)){
		var d  = '<div id="'+o+'" style="display:none;"></div>';
		// insert overlay window
		$$('body')[0].insert(d);	
	}
	// set the current bg overlay
	var bg = $(o);
	curGenBG = o;
	
	// 4
	// by default set animation to true
	if (a == undefined){
		a = true;	
	}
	
	// 5 
	// hide the elements
	showHideElements(false);
	
	
	// 6
	// close a current open generic window if one exists or it is not the same one
	if (curGenWin != '' && curGenWin != w){	
		$(curGenWin).hide();	
	}
	
	// 7
	// only animate if not the same window and the BG is already active
	if (curGenWin != w && bg.getStyle('display') == 'none'){
		bg.setStyle({zIndex:99,width:curPageSize[0] + 'px',height:curPageSize[1] + 'px'});
		if (a){
			bg.appear({duration:.25,from:0,to:.5});
		}else{
			// only apply opacity if background has color
			// added this due to IE bug not allowing click on element if opacity is attempted when it has no background set
			var bc = bg.getStyle('backgroundColor');
			if(bc!='transparent' && bc!='none'){
				bg.setOpacity(.5);
			}
			bg.show();				
		}		
	}
	
	// 8
	// set the top and left properties and fade in the window	
	win.setStyle({zIndex:100,top: winTop + 'px', left: (isNaN(winLeft) ? winLeft : winLeft + 'px') })
	if (curGenWin != w){
		if (a){
			win.appear({duration:.25,queue:'end'});
		}else{
			win.show();	
		}
	}
	
	// 9
	// set the current open generic window
	curGenWin = w;	
	
	// 10
	// set the animation state
	doAnim = a;
	
	// 11 
	// add a listener to the bg to close the window if clicked on
	if(l == true){
		bg.observe('click',function(){closeGenWin(w)});
	}
	
	// 12
	// add the close function if passed
	if (fn != undefined){
		curGenCloseFunc = fn;
	}
		
}

/*
*
*	function closeGenWin
*
*	this function is used to close a generic popup window (inside the browser)
*
*	@w	name of window
*	@f	funtion to run afterwards
*
*/
function closeGenWin(w,f){
	
	var bg = $(curGenBG);	
	var win = $(w);
	// set a close function if one not passed and a global one was set
	if (f == undefined && curGenCloseFunc != ''){
		f = curGenCloseFunc;	
	}
	
	// reset the current open generic window, background and close function
	curGenWin = '';
	curGenBG = '';
	curGenCloseFunc = '';
	
	// stop observing click on bg
	if (bg != null) {
		bg.stopObserving('click');
	}
	
	if (doAnim){
		// close the window
		win.fade({duration:.25});
		if (bg != null) {
			bg.fade({duration:.25,queue:'end',afterFinish:function(){
					   if (f != undefined){
						eval(f+'()');
					   }
					   showHideElements(true);
			}});
		}
	}else{
		if (f != undefined){
			eval(f+'()');
		}
		win.hide();
		if (bg != null) {
			bg.hide();
		}
	}

}

function showHideElements(a){
	
	if (a){
		
		// show all objects, embeds and selects
		$$('object', 'embed').each(function(node){ node.style.visibility = 'visible' });				   
		if (Prototype.Browser.IE && parseFloat(navigator.appVersion) < 7){		
			var theSelects = $$('#qp_content select','#searchFrm1 select');			
			theSelects.each(function(e){
				e.setStyle({'visibility':'visible'});
			});				
		}
			
	}else{
		
		// hide all objects, embeds and selects
		$$('object', 'embed').each(function(node){ node.style.visibility = 'hidden' });
		if (Prototype.Browser.IE && parseFloat(navigator.appVersion) < 7){		
			var theSelects = $$('#qp_content select','#searchFrm1 select');			
			theSelects.each(function(e){
				e.setStyle({'visibility':'hidden'});
		 	});
		}	
		
	}
	
}
