/*
	Javascript Document
	Document Name:  functions.js
	
	Description : New Functions JS library that extends scriptaculous and prototype
	
	Client : The Company
	Project: Alternate Pages
	Date:	12.20.2007
	
	Author: Fuse Developments, Inc. Giancarlo Gomez
*/
/*
*	function closeScreen
*	This functions closes a form screen
*/
function closeScreen(theScreen,theLink){
	// If theLink variable is passed show it
	if (theLink!=undefined && theLink!=''){
		$(theLink).show(); 
	}
	// array of possible divs to hide and then run thru each
	var divArr = $w('unExists unExists2 unExists3 cnExists');
	divArr.each(function(i,index){
		if ($(i)!=undefined){
		$(i).hide();
	}							
	});
	// array of possible fileds to clear class on and then run thru each
	var fldArr = $w('LOGIN job_id');
	fldArr.each(function(i,index){
		if ($(i)!=undefined){
		$(i).className='';
	}							
	});
	// Hide the Form
	if (!Prototype.Browser.IE){
		new Effect.BlindUp(theScreen, {duration: 0.2}); 
	}else{
		$(theScreen).hide();	
	}
}
/*
*	function showScreen
*	This functions opens a form screen
*/
function showScreen(theScreen,theLink,theFrm,scrollPos){
	// If theLink variable is passed hide it
	//consoleDump(theScreen+ ' ' + theLink+ ' ' + theFrm+ ' ' + scrollPos)
	if (theLink!=undefined && theLink!=''){
		$(theLink).hide(); 
		
	}
	// Get All text inputs in theFrm if passed and clear the values
	if (theFrm!=undefined && theFrm!=''){
		var iArray=$$('#' + theFrm + ' input[type=text]');
		iArray=iArray.concat($$('#' + theFrm + ' input[type=password]')); // push password elements as well
		iArray=iArray.concat($$('#' + theFrm + ' textarea')); // push textarea elements as well
		//consoleDump(iArray.length);
		iArray.each(function(i,index){
			//consoleDump(i);
			i.value='';							 
		})
	}
	// Show the Form - No animation for IE because it is a horrible browser
	if (!Prototype.Browser.IE){
		new Effect.BlindDown(theScreen,{
				duration: 0.2, 
				scaleFrom:5,
				afterFinish:function(){
						if (scrollPos!=undefined){
							Element.scrollTo(scrollPos)
						}
					}
				});
	}else{
		$(theScreen).show();
		if (scrollPos!=undefined){
			Element.scrollTo(scrollPos)
		}
	}
}
// Console Dump Functions
function consoleDump(text){
	if(window.console) {
		window.console.log(text);
	}
}

// PAGE FUNCTIONS
var curID = 0;
var noAjax = 0;

function showSearch(){
	if ($('searchFrm0').getStyle('display')=='none'){
		showScreen('searchFrm0');
		$('searchLink').addClassName('open');
	}else{
		closeScreen('searchFrm0');	
		$('searchLink').removeClassName('open');
	}
}

function mouseOver(theID){
	if (!noAjax && theID != curID){
		// curID = theID;
		// consoleDump(theID);
		// change the map and select the item in the form
		if (theID > 0){
			// $('schCity').options[theID].selected=true;
			// we placed specific change to the background style via the inline style element as IE crap 6 failed with class changes
			$('c2').setStyle('background-position:-' + theID*200 + 'px 0');
		}else{
			$('c2').setStyle('background-position:0 0');	
		}
		// update the image links
		for (var i=1; i<=6; i++){
			if (i==theID){
				$('l'+i).addClassName('over');
			}else{
				$('l'+i).removeClassName('over');
			}
		}
		// Get Listings 
		// getListings(theID);
	}
}

function mouseOut(theID){
	if (curID>0){
		// if the mouseout action happens on a dif div then remove the class and set the current one back to its over state
		if (curID != theID){
			$('l'+curID).addClassName('over');
			$('l'+theID).removeClassName('over');
			$('c2').setStyle('background-position:-' + curID*200 + 'px 0');
		}
	}else{
		// if cur ID is 0 just set it back to normal
		$('l'+theID).removeClassName('over');
		$('c2').setStyle('background-position:0 0');
	}		
}

function mouseClick(theID){
	if (!noAjax && theID != curID){
		curID = theID;
		//consoleDump(theID);
		// change the map and select the item in the form
		if (theID > 0){
			$('schCity').options[theID].selected=true;
			// we placed specific change to the background style via the inline style element as IE crap 6 failed with class changes
			$('c2').setStyle('background-position:-' + theID*200 + 'px 0');
		}else{
			$('c2').setStyle('background-position:0 0');	
		}
		// update the image links
		for (var i=1; i<=6; i++){
			if (i==theID){
				$('l'+i).addClassName('over');
			}else{
				$('l'+i).removeClassName('over');
			}
		}
		// Get Listings 
		getListings(theID);
	}
}

function getListings(theID){
	// url
	var url = '/includes/ajax/search.cfc?wsdl';
	// Parameters
	var pars='method=getListings&theID=' + theID;;
	// New Ajax Request
	new Ajax.Request(url, {
		method:'post', // changed method to post due to IE7 cache bug
		parameters: pars,
		onCreate: function(){	
			$('loading').show();
			$('featuredListings').hide();
		},
		onSuccess:  function (transport) {						 
			// Set the clear text
			var field = transport.responseText.stripTags(); // FIX APPLIED - use stripTags to remove all wddx tags as it causes problems in the re-display of data
			// rewrite html
			field = field.replace(/\&lt;/g, "<");
			field = field.replace(/\&gt;/g, ">");
			// update screen
			$('featuredListings').update(field);
							
		},
		onComplete: function(){
			updateDom.delay(1);
		}
	});
	// End Ajax Call	
}

function updateDom(){
	$('loading').hide();
	$('featuredListings').show();	
}



/*
*********************************
**********************************
**********************************
*	NEW 
**********************************
**********************************
**********************************
*/
/*
**********************************
GENERIC WINDOW OBJECT VARIABLES
**********************************
*/
var curGenWin = '';

/*
*
*	function openGenWin
*
*	this function is used to open a generic popup window (inside the browser)
*
*	@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
*
*/
function openGenWin(win,container){
	
	var curPageSize = Lightbox.prototype.getPageSize();
	var arrayPageScroll = document.viewport.getScrollOffsets();
	var winTop = arrayPageScroll[1] + (document.viewport.getHeight()/2 - $(win).getHeight()/2);
    var winLeft = 0;
	var winWidth = $(win).getWidth()/2;
	var bg = $('genericPopUpBG');
	var w = $(win);
	
	
	// set left placement value
	if (container != undefined && w.getOffsetParent().id == container){
		winLeft = $(container).getWidth()/2 - winWidth;
	}else{
		winLeft = curPageSize[0]/2 - winWidth;	
	}
	
	if (Prototype.Browser.IE && parseFloat(navigator.appVersion) < 7){
	
		var theSelects = $$('#qp_content select','#searchFrm1 select');
		
		theSelects.each(function(e){
							 e.setStyle({'visibility':'hidden'});
							 });
		
	}
	
	// close a current open generic window if one exists
	if (curGenWin != ''){
	
		$(curGenWin).hide();
	
	}
	// set the current open generic window
	curGenWin = win;	
	
	bg.setStyle({zIndex:1,width:curPageSize[0] + 'px',height:curPageSize[1] + 'px'}).appear({duration:.25,from:0,to:.5});
	// set the top and left properties and fade in the window	
    w.setStyle({zIndex:100,top: winTop + 'px', left: winLeft + 'px' }).appear({duration:.25,queue:'end'});
		
}

/*
*
*	function closeGenWin
*
*	this function is used to close a generic popup window (inside the browser)
*
*	@win	=	name of window
*
*/
function closeGenWin(win,fnc){
	
	var bg = $('genericPopUpBG');
	
	
	
	// reset the current open generic window
	curGenWin = '';
	
	// close the window
	$(win).fade({duration:.25});
	bg.fade({duration:.25,queue:'end',afterFinish:function(){
			   if (fnc != undefined){
				eval(fnc+'()');;
			   }
			   
				if (Prototype.Browser.IE && parseFloat(navigator.appVersion) < 7){
				
					var theSelects = $$('#qp_content select','#searchFrm1 select');
					
					theSelects.each(function(e){
											 e.setStyle({'visibility':'visible'});
				});
			}
	}});

}