// 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)

*	An Object is passed an below are the parameters that are used
*
*	@win		name of window
*	@container	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
*	@overlay	the overlay div - defaults to genericPopUpBG
*	@top		if this number is passed, it sets the window at a fixed top value
*	@left		if this number is passed, it sets the window at a fixed left value
*	@animate	boolean that controls whether or not to run animation
*	@reposition	boolean that controls whether or not to reposition	
*	@addCloser	boolean that controls whether or not to add a listener to the bg to close the window
*	@func		A function to pass to the auto close function
*
*	pass any 
*
*/
function openGenWin(o){
		
	var curPageSize = Lightbox.prototype.getPageSize();
	var arrayPageScroll = document.viewport.getScrollOffsets();
	
	var win = $(o.win);
	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(o.left == undefined || o.reposition == undefined || o.reposition == true){
		// set left placement value
		if (o.container != undefined && o.container != null && o.container != '' && win.getOffsetParent().id == o.container){
			winLeft = $(c).getWidth()/2 - winWidth;
		}else{
			winLeft = curPageSize[0]/2 - winWidth;	
		}
	}else if (o.left != undefined && !isNaN(o.left)){
		winLeft = o.left;
	}
	
	// 2
	// set up positioning from top if passed or auto setup
	if (o.top && !isNaN(o.top)){
		winTop = o.top;
	}else{
		winTop = arrayPageScroll[1] + (document.viewport.getHeight()/2 - win.getHeight()/2)
	}
	
	// 3
	// set default overlay if other not passed
	if (o.overlay == undefined){
		o.overlay = 'genericPopUpBG';			
	}	
	
	// make sure the overlay exists
	if (!$(o.overlay)){
		var d  = '<div id="'+o.overlay+'" style="display:none;"></div>';
		// insert overlay window
		$$('body')[0].insert(d);	
	}
	// set the current bg overlay
	var bg = $(o.overlay);
	curGenBG = o.overlay;
	
	// 4
	// by default set animation to true
	if (o.animate == undefined){
		o.animate = 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 != o.win){	
		$(curGenWin).hide();	
	}
	
	// 7
	// only animate if not the same window and the BG is already active
	if (curGenWin != o.win && bg.getStyle('display') == 'none'){
		bg.setStyle({zIndex:99,width:curPageSize[0] + 'px',height:curPageSize[1] + 'px'});
		if (o.animate){
			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 (o.animate){
		win.appear({duration:.25,queue:'end'});
	}else{
		win.show();	
	}
	
	// 9
	// set the current open generic window
	curGenWin = o.win;	
	
	// 10
	// set the animation state
	doAnim = o.animate;
	
	// 11 
	// add a listener to the bg to close the window if clicked on
	if(o.addCloser == true){
		bg.observe('click',function(){closeGenWin(o.win)});
	}
	
	// 12
	// add the close function if passed
	if (o.func != 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
	bg.stopObserving('click');
	
	if (doAnim){
		// close the window
		win.fade({duration:.25});
		bg.fade({duration:.25,queue:'end',afterFinish:function(){
				   if (f != undefined){
					eval(f+'()');
				   }
				   showHideElements(true);
		}});
	}else{
		if (f != undefined){
			eval(f+'()');
		}
		win.hide();
		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'});
		 	});
		}	
		
	}
	
}

/*
**********************************
ALERT BOX
**********************************
*
*	This function creates and reuses the alert_win div
*	It is dependant of openGenWin
*
*	obj Object
*
*	obj.message				the message to display
*	obj.doConfirm			the name of the form to submit or function to run
*	obj.doConfirmArgs		argument to pass if we are sending a function
*	obj.doConfirmLabel		the label for the confirm button |	default "Yes"
*	obj.doCancelLabel		the label for the cancel button  |	default "No"
*	obj.submitButtons		the buttons to change on a submit
*/
function alertBox(obj){
	var a = $('alert_win');
	// insert the alert window if none exists
	if(!a){
		// insert the window
		$$('body')[0].insert('<div class="genericAlert" id="alert_win" style="display:none;"></div>');
		a = $('alert_win');
	}
	// set up defaults
	if(obj.doConfirmLabel == undefined){
		obj.doConfirmLabel = "Yes";	
	}
	if(obj.doCancelLabel == undefined){
		obj.doCancelLabel = "No";	
	}
	// create the alert
	var d = '<p>' + obj.message + '</p><p style="text-align:center; margin-top:1em;">';
	// add the confirm event
	if(obj.doConfirm != undefined){
		d = d + '<span class="uiBtn inline" id="alert_win_confirm"><a href="#">' + obj.doConfirmLabel + '</a></span> ';
	}
	// add the close event
	d = d + '<span class="uiBtn inline" id="alert_win_cancel"><a href="#">' + obj.doCancelLabel + '</a></span>';
	// close the alert
	d = d + '</p>';
	a.update(d);
	// add listeners
	if(obj.doConfirm != undefined){
		$('alert_win_confirm').on("click",function(event){
			if(obj.submitButtons != undefined){
				//showHideSimple(obj.submitButtons);
			}
			try{
				if($(obj.doConfirm) && $(obj.doConfirm).constructor.toString().indexOf("HTMLFormElement")>=0){
					// is form so submit
					$(obj.doConfirm).submit();
				}else if(typeof eval(obj.doConfirm) == 'function'){
					// run a function if passed
					var f = new Function(obj.doConfirm + '('+ ((obj.doConfirmArgs == undefined)?'':obj.doConfirmArgs) +')');
					// run function
					f();
				}
			}
			catch(err){
				// dont do anything	
			}
			event.stop();
		});		
	}
	$('alert_win_cancel').on("click",function(event){
		closeGenWin('alert_win');
		a.hide().update('');
		event.stop();
	});
	// open the alert window
	openGenWin({win:'alert_win',overlay:'genericPopUpBG2',animate:false});			
}
