// Default JavaScript

document.getElementsByClass = function (cn) {
	var arrNodes = new Array();
	var reClass = new RegExp("\\b"+ cn +"\\b");
	var DomElements = this.getElementsByTagName('*');
	for (var i=0; i < DomElements.length; i++) {
		var classNames = DomElements[i].className;
		if (reClass.test(classNames)) arrNodes.push(DomElements[i]);
	}
	return arrNodes;
}

document.getElementsByIdAndTag = function (id) {
	var arrNodes = new Array();
	var reClass = new RegExp("\\b"+ id.replace('|',"\\|") +"\\b");
	var tagName = (arguments.length>1) ? arguments[1] : "*";
	var DomElements = this.getElementsByTagName(tagName);
	for (var i=0; i < DomElements.length; i++) 
		if (reClass.test(DomElements[i].id)) arrNodes.push(DomElements[i]);
	return arrNodes;
}

function getSibling (obj, order) {
	do obj = eval("obj."+ order +"Sibling");
	while (obj && obj.nodeType != 1);
	return obj;
}

// Returns the object from which an event(e) is triggered.
function EventObject (e) 
{
	var source = new Object();
	if (!e) var e = window.event;
	if (e.target) source = e.target;
		else if (e.srcElement) source = e.srcElement;
	if (targ.nodeType==3) source = source.parentNode;
	return source;
}

function findPosition (obj,axis)
{
	var currentPosition = 0;
	var position = (axis == "left") ? "offsetLeft" : "offsetTop";
	if (obj.offsetParent)
		do { currentPosition += eval("obj." + position); }
			while (obj = obj.offsetParent);
	return currentPosition;
}

function toggleDropDownList (listObjId,fromBody)
{
	var ListObject = document.getElementById(listObjId);
	var SelectionObject = getSibling(ListObject,"previous");
	SelectionObject = getSibling(SelectionObject,"previous");
	var ListObjectDisplayed = (ListObject.style.display == "block");	
	ListObject.style.display = (ListObjectDisplayed) ? "none" : "block";
	ListObject.style.top = findPosition(SelectionObject,"top") + SelectionObject.offsetHeight - 6 +"px"; 
}


/* BEGIN: Functions used by the tabs forms */

function jobsTabSearch()
{	
	var jobsTabForm = document.jobstabform;
	if (jobsTabForm.JobsKeywordSearch.value == "Keyword (ex: sales, excel, nursing) or Web ID")
	{
		jobsTabForm.JobsKeywordSearch.value = "";
	}
	if (jobsTabForm.JobsLocationSearch.value == "City (optional)")
	{
		jobsTabForm.JobsLocationSearch.value = "";
	}					
	jobsTabForm.submit();				
}

function autosTabSearch()
{	
	var autostabform = document.autostabform;
	if (autostabform.AutosKeywordSearch.value == "Keyword (ex: 2003 Civic, leather, ABS) or Web ID")
	{
		autostabform.AutosKeywordSearch.value = "";
	}		
}

function classifiedsTabSearch()
{	
	var classifiedstabform = document.classifiedstabform;
	if (classifiedstabform.ClassifiedsKeywordSearch.value == "enter Keyword or Web ID")
	{
		classifiedstabform.ClassifiedsKeywordSearch.value = "";
	}			
}

function realEstateTabSearch()
{
	var realEstateTabForm = document.realestatetabform;
	if (realEstateTabForm.RealestateKeywordSearch.value == "Search by Keyword / Web ID / City / Zip Code")
	{
		realEstateTabForm.RealestateKeywordSearch.value = "";
	}	
	var finderValue = "";
	for ( var r = 0; r < realEstateTabForm.finder.length; r++ )
	{
		if (realEstateTabForm.finder[r].checked) 
		{
			break;
		}						
	}	
	var ad_type = "";
	finderValue = realEstateTabForm.finder[r].value;	

	if (finderValue == "buy") {
		ad_type = "residential-listing,vacation-listing,new-homes-listing";
	} else if (finderValue == "new") {
		ad_type = "new-homes-listing";
	} else if (finderValue == "rent") {
		ad_type = "residential-rentals-listing,vacation-rentals-listing";
	} else if (finderValue == "fcl") {
		ad_type = "foreclosure-listing";
	} else if (finderValue == "apt") {
		ad_type = "community-rentals-listing";
	} else { 
		ad_type = "";
	}

	if (finderValue == "apt") { var finder = "rent"; } else { var finder = realEstateTabForm.finder[r].value; }

	location.href = "http://realestate." + realEstateTabForm.property.value + "/?classification=" + realEstateTabForm.classification.value + "&temp_type=" + realEstateTabForm.temp_type.value + "&finder=" + finder + "&ad_type=" + ad_type + "&tp=" + realEstateTabForm.tp.value + "&property=" + realEstateTabForm.property.value + "&keywords=" + realEstateTabForm.keywords.value;

	return false;
}

/* END: Functions used by the tabs forms */


//* START: JQUERY SCRIPTS *//

// Ajax for loading content on demand in a specific container
// whichArea - use ID and class names
// whichURL - use absolute path or full URL(must be from the same domain or it will not work)
// whichImg - (optional) - use html code for the "loading" image, ie. <img src="/images/loading.gif" alt="loading">
var loadContent = function(whichURL,whichArea,whichImg,whichTimeout) {	
		//default "loading" image
		if (!whichImg) { var whichImg = '<p class="center"><img src="/08design/images/loading.gif" width="50" height="50" /></p>'; }
		if (!whichTimeout) { var whichTimeout = 1000; }
		$(whichArea).html(whichImg);
		//alert(whichTimeout);
		setTimeout( function(){	$(whichArea).load(whichURL); }, whichTimeout);		
}

var loadingImg = function (el_holder) {
  $(el_holder).html('<p class="center"><img src="/08design/images/loading.gif" width="50" height="50" /></p>');  
}

//get the position of the placeholder element  
//mbox : the container to be positioned
//el   : element used for positioning
//el_x : position of another container (optional)
var positionBelow = function(mbox,el,el_x) {  		
	var el_pos = jQuery(el).offset();    
	var el_pos_h = jQuery(el).height();   	
	if (el_x) { var el_pos_x = jQuery(el_x).offset(); }
	else { el_pos_x = el_pos; }		
	var mbox_left = el_pos_x.left + 10 + "px";
	var mbox_top = el_pos.top + el_pos_h +"px";	
	//show the menu directly below the placeholder  
	jQuery(mbox).css( { 
					left: mbox_left, 
					top: mbox_top
	} );
}

// IE6 Hover state for green buttons
jQuery(function ($) {
	$(".imageGo_toprail").hover(
		function () { $(this).addClass("imageGo_toprail_mo"); },
		function () { $(this).removeClass("imageGo_toprail_mo"); }
	);
	$(".imageGo").hover(
		function () { $(this).addClass("imageGo_mo"); },
		function () { $(this).removeClass("imageGo_mo"); }
	);
	$(".imageSearch").hover(
		function () { $(this).addClass("imageSearch_mo"); },
		function () { $(this).removeClass("imageSearch_mo"); }
	);
});

// Tabs Widget
jQuery(function ($) {
	$('table.tab_table td a').click(function () {
		//selectors for tab elements	
		var $tabParent = $(this).parents('.tabs_widget');
		var $tabContents = $(this).parents('.tabs_widget').children('.tab_contents').children('div'); 
		var $tabCells = $(this).parent().parent().children('td'); 		
		
		//community_pages - load entertainment tab contents via ajax.  Use function loadContent()
		if ($tabParent.attr("ID")) {
			var tabModule = $tabParent.attr("ID").replace(/\_tabs$/i,"").toLowerCase();
			if (tabModule=='entertainment') {
				var tabContentArea = $tabParent.children('.tab_contents');			
				var tabName = this.hash.replace(/_tab$/i,"").toLowerCase().replace(/\#/g,"");
				var tabPath =  "/modules/community_pages/" + tabModule + "_tabs_" + tabName + ".ssf";						 		
				loadContent(tabPath,tabContentArea);
			}
		}
		
		//unselect other tabs first
		$tabCells.not('.first_tab,.last_tab').removeClass();
		$tabCells.removeClass('fix_background');
		$tabContents.hide();
		
		//decide which tab to select
		if ($(this).parent().is('.first_tab')) { 
			//FIRST tab	
			$tabCells.removeClass('last_tab_active');			
			$(this).parent().addClass('first_tab_active');
		} else if ($(this).parent().is('.last_tab')) { 
			//LAST tab
			$tabCells.removeClass('first_tab_active');
			$(this).parent().addClass('last_tab_active');
			$(this).parent().prev().addClass('fix_background');	//remove the right border and extra padding on tab before this
		} else if ($(this).parent().not('.first_tab,.last_tab')) { 
			//MIDDLE tabs
			$tabCells.removeClass('first_tab_active last_tab_active');
			$(this).parent().addClass('tab_active');
			$(this).parent().prev().addClass('fix_background');//remove the right border and extra padding on tab before this		
		} else {
			//nothing
		}
				
		//show content for this tab				
		$tabContents.filter(this.hash).show();		
		return false;
		
	}).parent().filter('.first_tab').children('a').click();	//auto click first tab on page load
});	

//Weather Search Overlay
jQuery(function ($) {								 
	// make sure elements exist
	if (document.getElementById('Overlay_Weather_Search')) { 		
		var DefaultForm = $('#Weather_Search_Form').html();	//remember the default form fields
		// open weather search
		$('#Weather_Change_Location').click(function(){
				$('#Overlay_Weather_Search').jqm({overlay: 0.01,toTop: true}).jqmShow();
				positionBelow('#Overlay_Weather_Search','#Weather_Change_Location','#Module-Weather');
				return false;
 	  });		
		//close weather search bubble and reset form
		$('#Overlay_Weather_Search .x_close a').click(function(){ 
				$('#Overlay_Weather_Search').jqmHide(); 
				$('#Weather_Search_Form').html(DefaultForm); //reset form
				$('#Weather_Search_Form .imageSearch').attr('value',''); //IE fix for 'Submit Query' text over button.
				return false; 
		});		
		//Ajax for submitting form
		$('#Weather_Search_Form').submit(function() { 																							
					var FormDataString = 'search='+ $('#Weather_Search_Field').attr('value');				
					$.ajax({
							url: $(this).attr('action'),
							type: 'GET',
							datatype: 'http',
							data: FormDataString,
							async: false,
							cache: false,
							timeout: 30000,
							error: function(){
									return true;
							},
							success: function(html){ 									
									loadingImg('#Weather_Search_Form'); //Animated gif. This function is defined in default.js
									setTimeout(function() { $('#Weather_Search_Form').html(html);	}, 1250); //wait 1.25 sec before loading results
									return false;
							}							
					}); 
					return false;					
		});
	}		
});	

//* END: JQUERY SCRIPTS *//

