/**
* @package Search GUI
* @version 1.0 (11/9/2008)
* @copyright (C) 2008 Digital Hive
* @author John McClumpha john@digitalhive.com.au
*/

$(document).ready(function(){

	var $defaultMsgs = Array();
	$defaultMsgs['p_property'] = 'property type';
	$defaultMsgs['p_location'] = 'location';
	$defaultMsgs['p_property_email'] = 'property type';
	$defaultMsgs['p_location_email'] = 'location';
	
	// determine which checkboxes are checked at load and match the fakes (also hide the checkboxes)
	$("input:checkbox").each( function() {
		if($(this).attr('checked')) {
			$(this).siblings('.check').addClass('checked'); 
		}
		$(this).hide();
	});
	
	// determine which radio boxes are checked at load and match the fakes (also hide the checkboxes)
	$("input:radio").each( function() {
		if($(this).attr('checked')) {
			$(this).siblings('.radio').addClass('radioed'); 
		}
		$(this).hide();
	});
	
	// hide all radio boxes at load
/*	$("input:radio").each( function() {
		$(this).hide();
	});
	*/
	// replace the for attributes with rel attributes
	$("label").each( function() {
		$(this).attr('rel',$(this).attr('for'));
		$(this).removeAttr('for');
	  });
	
	//. function to submit the form
	$("#home_search").click( function() {
		document.search_form.submit();
	});
	
	$("#nav_search").click( function() {
		document.search_form.submit();
	});
	// function to display the dialog box (called when search field clicked)
	$(".s_field").each( function(){
		$(this).click(function(){
			// hide all the visible dialog boxes (just in case any are visible)
			$(".dialog").each( function(){
			if($(this).css('display') != 'none') {
					updateFacelistCheckBox();
					$(this).hide(0,showOpts($(this)));
				}
			});
			// reposition and fade in the dialog box
			var $dialogObj = $('#'+$(this).attr('rel'));
			$dialogWidth = parseInt($dialogObj.css('width'));
			$dialogObj.css('left',$(this).offset().left - ($dialogWidth/2) + 80);
			var popheight = $dialogObj.height();
			if($(this).offset().top - (popheight/2) > 155) {
				$dialogObj.css('top',$(this).offset().top - (popheight/2));
			} else {
				$dialogObj.css('top', 155);
			}
//			($('#'+$(this).attr('rel'))).fadeIn('fast');
			$dialogObj.fadeIn('fast');		
		});
	});
	
	// function called when fake checkboxes are clicked
	$(".check").each( function() {
		$(this).click(function(){
			var $thisBox = $('#'+$(this).attr('rel'));
			if($(this).hasClass('checked')) {
				// turn OFF the check
				$('#bit-'+$(this).attr('rel')).remove();
				$(this).removeClass('checked');
				if($thisBox.attr('checked')) { // required for ie
					$('#'+$(this).attr('rel')).trigger("click");
				}

			} else {
				// turn ON the check
				$(this).addClass('checked');
				if(!$thisBox.attr('checked')) { // required for ie
					$('#'+$(this).attr('rel')).trigger("click");
				}
			}

		});
	});
	
	// function called when fake radio boxes are clicked
	$(".radio").each( function() {
		$(this).click(function(){
			var $thisBox = $('#'+$(this).attr('rel'));
			$(this).parents('ol').contents().find(".radio").each( function() {
				$(this).removeClass('radioed');
		   });
			if(!$(this).hasClass('radioed')) {
				// turn ON the check
				$(this).addClass('radioed');
				if(!$thisBox.attr('radioed')) { // required for ie
					$('#'+$(this).attr('rel')).trigger("click");
				}
			}

		});
	});
	// function called when "ok" buttons are clicked
	$(".btn_ok").each( function() {
		$(this).click(function() {
			$dialogObj = $(this).parents('.dialog');
			if($($dialogObj).attr('id') == 'p_price' || $($dialogObj).attr('id') == 'p_price_email'){
				$dialogObj.fadeOut('fast', showPriceOpts($dialogObj));
			} else {
				$dialogObj.fadeOut('fast', showOpts($dialogObj));
				updateFacelistCheckBox();
			}
		});
	});
	
	// function to update the display based upon which checkboxes were changed
	function showOpts($dialogObj) {
		var $retval = new Array();
		// traverse all checkboxes within the dialog being hidden
		$($dialogObj).contents().find("input:checkbox").each( function() {
			if($(this).attr('checked')) {
				if($(this).val() != "Existing Property" && $(this).val() != "New Development" && $(this).val() != "Off the Plan") {
					// add then to a display array
					$retval.push($(this).val());
				}
			}
		});

		if($retval.length > 0) {
			// put the content of the display array into the a tag
			if($retval.length > 2) {
				var $displayVal = $retval[0]+', '+$retval[1]+'...';
			} else {
				var $displayVal = $retval.join(', ');
			}
		$('[rel='+$($dialogObj).attr('id')+']').html($displayVal);
		} else {
			$('[rel='+$($dialogObj).attr('id')+']').html($defaultMsgs[$($dialogObj).attr('id')]);
		}
	}

	// function to update the display based upon which price radio items were selected
	function showPriceOpts($dialogObj) {
		var $retval = new Array();
		// traverse all radio buttons within the dialog being hidden
		$(".prop_price_from").find("input:radio").each( function() {
																 
			if($(this).attr('checked')) { 
				$retval['from'] = $(this).val();
			}
		});
		$(".prop_price_to").find("input:radio").each( function() {
			if($(this).attr('checked')) { 
				$retval['to'] = $(this).val();
			}
		});
		$displayVal = '';
		if (eval($retval['from']) >= 10) {
			$displayVal = '$'+$retval['from']+'million plus';
		} else if($retval['from'] && $retval['to']) {
				if(eval($retval['from']) > eval($retval['to'])) { // are they arse about?
					$displayVal = '$'+$retval['to']+'million to $'+$retval['from']+'million';
				} else {
					$displayVal = '$'+$retval['from']+'million to $'+$retval['to']+'million';
					}
		} else {
			if($retval['from']) {
				$displayVal = '$'+$retval['from']+'million and over';
			} else if($retval['to']) {
				$displayVal = 'up to $'+$retval['to']+'million';
			}
		}
			if($displayVal != '') {
			// put the content of the display array into the a tag
			$('[rel='+$($dialogObj).attr('id')+']').html($displayVal);
			} else {
			$('[rel='+$($dialogObj).attr('id')+']').html($defaultMsgs[$($dialogObj).attr('id')]);
			}
		}

	function updateFacelistCheckBox()
	{
	var $retval = new Array();
		//get all tokens
		$(".token").each( function() {
			$count = 0;
			$id = $(this).attr('id');
			$newRel = (new String($id).split("-"))[1];/*.sort().toString();*/
			$newLocalId = (new String($newRel).split("_"))[1];/*.sort().toString();*/
			$newLocalString = $("#" +$id).attr('rel');
			$newLocalArray = new String($newLocalString).split(" ");
			//get Postcode
			
//			alert($newLocalArray);
			if((($newLocalArray[1]).length) == 3){
				$location = $newLocalArray[0];
				$state = $newLocalArray[1];
				$postcode = $newLocalArray[2];
			}else if((($newLocalArray[2]).length) == 3){
				$location = $newLocalArray[0] + " " + $newLocalArray[1];
				$state = $newLocalArray[2];
				$postcode = $newLocalArray[3];
			}else if((($newLocalArray[3]).length) == 3){
				$location = $newLocalArray[0] + " " + $newLocalArray[1] + " " + $newLocalArray[2];
				$state = $newLocalArray[3];
				$postcode = $newLocalArray[4];
			}else if((($newLocalArray[4]).length) == 3){
				$location = $newLocalArray[0] + " " + $newLocalArray[1] + " " + $newLocalArray[2] + " " + $newLocalArray[3];
				$state = $newLocalArray[4];
				$postcode = $newLocalArray[5];
			}
			
			
			
			//alert($newLocalString);
			$(".check").each( function() 
			{
				if($(this).attr('rel') == $newRel)
				{		
				//alert('entered');
				$retval.push($newLocalString);	
					$(this).addClass('checked');
					if(!$(this).hasClass('checked')) { // required for ie
						$('#'+$(this).attr('rel')).trigger("click");
					}else{
					//alert('box checked');
					}
				$count++;
				}
			
			
			
			});
			$dialogObj = $(this).parents('.dialog');
			//append the li list with new checkbox
			if($count<1)
			{
			//alert("attempting to add check");
			$html = "<li><input type=\"checkbox\" id=\""+$newRel+"\" value=\""+$location+", "+$state+"\" name=\"location["+$newLocalId+"]\" style=\"display: none;\"/><label class=\"check\" rel=\""+$newRel+"\">"+$location+", "+$state+"</label></li>";
			$("#prop_location").append($html);
			
			$(".check").each( function() 
			{
			//Add the functionality to the new checkbox
				if($(this).attr('rel') == $newRel)
				{	
				//alert("found");	
					$(this).click(function(){
						var $thisBox = $('#'+$(this).attr('rel'));
						if($(this).hasClass('checked')) {
								// turn OFF the check
								$('#bit-'+$(this).attr('rel')).remove();
								$(this).removeClass('checked');
							if($thisBox.attr('checked')) { // required for ie
								$('#'+$(this).attr('rel')).trigger("click");
							}

						} else {
							// turn ON the check
							$(this).addClass('checked');
							if(!$thisBox.attr('checked')) { // required for ie
								$('#'+$(this).attr('rel')).trigger("click");
							}
						}

				});
				
				//Trigger new functionlity
				$(this).trigger("click");
					$retval.push($newLocalString);
				}
			
			});
		}
			
		});
	
		if($retval.length > 0) {
			// put the content of the display array into the a tag
			if($retval.length > 2) {
				var $displayVal = $retval[0]+', '+$retval[1]+'...';
			} else {
				var $displayVal = $retval.join(', ');
			}
		$('[rel='+$($dialogObj).attr('id')+']').html($displayVal);
		} else {
			//$('[rel='+$($dialogObj).attr('id')+']').html($defaultMsgs[$($dialogObj).attr('id')]);
		}
	
	}

	showPriceOpts($('#p_price'));
	showOpts($('#p_property'));
	showOpts($('#p_location'));
	showOpts($('#p_feature'));
	showPriceOpts($('#p_price_email'));
	showOpts($('#p_property_email'));
	showOpts($('#p_location_email'));
	

});