$.fn.trimval = function() {
	return $(this).val().toString().replace(/^\s+|\s+$/g,"");
};

$(document).ready(function() {
	$('.btn_over').mouseover(function(){
		$(this).attr('src', $(this).attr('src').toString().replace('.g','_over.g'));
		$(this).attr('src', $(this).attr('src').toString().replace('.j','_over.j'));
		$(this).attr('src', $(this).attr('src').toString().replace('.p','_over.p'));
    }).mouseout(function(){
		$(this).attr('src', $(this).attr('src').toString().replace('_over.g','.g'));
		$(this).attr('src', $(this).attr('src').toString().replace('_over.j','.j'));
		$(this).attr('src', $(this).attr('src').toString().replace('_over.p','.p'));
    });
	
			$('#slideshow').cycle({ 
			fx:     'fade', 
			speed:  'fast', 
			timeout: 4000,
			pager:  '#slideshow_pager' 
		});
			
	// mortgage calculator
	
	$('#cal-link').bind('click',function(event){
			event.preventDefault();
			var url = "http://www9.property.hk/mortgage.php?isFrame=1&lang=E";
		    window.open (url,"mywindow","location=0,status=0,toolbar=0,scrollbars=1,menubar=0,resizable=0,width=700,height=450");	
		 })
	
	
	
	//Save Enquiry
	
	$('a[name^=save-]').bind('click',function(event){
			event.preventDefault();
			var element = $(this);
		    $.get(this.href,{},function(response){ 
				if(response == "Saved"){
					//alert(response);
					element.parent().html('<a href="my_enquiry.php" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage(\'Image37\',\'\',\'images/my_over.png\',1)"><img src="images/my.png" name="Image37" width="103" height="20" border="0" class="btn_over"></a>');
				} else {
					alert(response);
				}
		 	   //$('#response').html(response)
		    })	
		 })

	//Remove Enquiry
	
	$('a[name^=remove-]').bind('click',function(event){
			//alert("remove");
			event.preventDefault();
			var element = $(this);
			var parent = $(this).parents("div.area").eq(0);

		    $.get(this.href,{},function(response){ 
				if(response == "Removed"){
					$(parent).remove();
				} else if(response == "Empty") {
					$(parent).html('<div class="image"><p>No Enquiry item in your basket.<p>You can click on the button "save to enquiry" <img src="images/save.png" alt="Save to enquiry" align="absmiddle"> to save your interested property in your basket, and send them to us through this page. We will contact you as soon as we received your enquiry.</div>');
					$('[name=action]').remove();
				} else {
					alert(response);
				}
				//alert(response);
		 	   //$('#response').html(response)
		    })	
		 })

/*	$('').click(function() {
		pos = this.attr("href");

	});*/

	
	/* Quick Search */
	
	$('#quicksearch').submit(function() {
		if ($("input[name=q]").val() == "Quick Search"){
			location.href="buy.php";
		}
		return false;
	});
});
/* Edwin's Flash Map */
	var id = 0;
	function mapSwfInit_buy(){
		mapStartFlash_buy();
	}
	function mapSwfInit_rent(){
		mapStartFlash_rent();
	}
	
	function mapSendToSwf(){
		var xmlDoc;
		xmlDoc='<root>';
		xmlDoc+="<point id=\'"+id+"\'/>";
		xmlDoc+='</root>';
		swf = document.getElementById("mapSWF");
		swf.SetVariable("inputxml", xmlDoc);	
		id++;
	}
	
	//function reloadFlash(){
		//document.getElementById("flashcontent").innerHTML='';
		//startFlash();
	//}	

	function mapStartFlash_buy(){		
		
		var so = new SWFObject("swf/Container2.swf", "mapSWF", "604px", "379px", "8.0.22",  null);
		so.useExpressInstall('swf/expressinstall.swf');
		//so.addParam("scale", "noscale");
		so.addParam("wmode", "window"); 
		so.addVariable("swLiveConnect", "true");
		so.addParam("allowScriptAccess", "sameDomain");
		so.addVariable("dataType", "swf/buy.xml");
		//so.addVariable("count", 1);
		so.addVariable("inputxml", "<root/>");
		//so.addVariable("MSG", getQueryParamValue("m"));
		//so.setAttribute('xiRedirectUrl', 'http://www.projectDomain.com/upgradefinished.html');	// Default will redirect back to the original webSite.
		so.write("flashcontent");
		var macmousewheel = new SWFMacMouseWheel( so );
	}
	
	function mapStartFlash_rent(){		
		
		var so = new SWFObject("swf/Container2.swf", "mapSWF", "604px", "379px", "8.0.22",  null);
		so.useExpressInstall('swf/expressinstall.swf');
		//so.addParam("scale", "noscale");
		so.addParam("wmode", "window"); 
		so.addVariable("swLiveConnect", "true");
		so.addParam("allowScriptAccess", "sameDomain");
		so.addVariable("dataType", "swf/rent.xml");
		//so.addVariable("count", 1);
		so.addVariable("inputxml", "<root/>");
		//so.addVariable("MSG", getQueryParamValue("m"));
		//so.setAttribute('xiRedirectUrl', 'http://www.projectDomain.com/upgradefinished.html');	// Default will redirect back to the original webSite.
		so.write("flashcontent");
		var macmousewheel = new SWFMacMouseWheel( so );
	}
	
	// Name: createXMLDocument
	// Input: String
	// Output: XML Document
	/*$.createXMLDocument = function(string)
	{
		var browserName = navigator.appName;
		var doc;
		if (browserName == 'Microsoft Internet Explorer')
		{
			doc = new ActiveXObject('Microsoft.XMLDOM');
			doc.async = 'false'
			doc.loadXML(string);
		} else {
			doc = (new DOMParser()).parseFromString(string, 'text/xml');
		}
		return doc;
	}*/
	function test(xmlString){
		//alert(xmlString);
		var points = xmlString.split(",");
		
		$("#district").dropdownchecklist("destroy"); // Destroy Drop down check list
		try // Parse XML, if available
		{
			$('#district option').attr("selected",""); 

			//var xmlDoc = $.createXMLDocument(xmlString);
	
			//$("point", xmlDoc).each(function() {
			$.each(points, function (n, val){
				$('#district option[value='+val+']').attr("selected","selected"); 
			});
			/*$(points).each(function() {
				var point = $(this);
				var id = point.attr("id");
				var id = $(this);
				$('select#district option[value='+id+']').attr("selected","selected"); 
			});*/
		} catch(e) { alert("error : " + e.description); }
		
		create_dropdownlist(); // Recreate Drop down check list
	}
	
	function create_dropdownlist(){
		$("#district").dropdownchecklist({ width: 150 , maxDropHeight: 100 }).change(function () {
			// Send to SWF
			var xmlDoc;
			xmlDoc='<root>';
			
			$('option:selected',this).each(function() {
				xmlDoc+="<point id=\'"+$(this).val()+"\'/>";
			});
			xmlDoc+='</root>';
			swf = document.getElementById("mapSWF");
			swf.SetVariable("inputxml", xmlDoc);
			// end of change	
		});
	}
	
	function indexSwfInit(){
		indexStartFlash();
	}
	function indexSendToSwf(){
		var xmlDoc;
		xmlDoc='<root>';
		xmlDoc+="<point id=\'0\' name=\'testing1234\' dist=\'testing2345\' spUrl=\'textingPhoto\' lpUrl=\'textingLargePhoto\' rent=\'10,000,000\' sale=\'10,000,000\'/>";
		xmlDoc+="<point id=\'1\' name=\'testing1234\' dist=\'testing2345\' spUrl=\'textingPhoto\' lpUrl=\'textingLargePhoto\' rent=\'10,000,000\' sale=\'10,000,000\'/>";
		xmlDoc+="<point id=\'2\' name=\'testing1234\' dist=\'testing2345\' spUrl=\'textingPhoto\' lpUrl=\'textingLargePhoto\' rent=\'10,000,000\' sale=\'10,000,000\'/>";
		xmlDoc+='</root>';
		//xmlDoc='<root>';
		//xmlDoc+="<point id=\'"+id+"\'/>";
		//xmlDoc+="<point id=\'"+id+"\'/>";
		//xmlDoc+="<point id=\'"+id+"\'/>";
		//xmlDoc+='</root>';
		//swf = document.getElementById("mapSWF");
		//swf.SetVariable("inputxml", xmlDoc);
		return xmlDoc
	}
	
	function indexStartFlash(){		
		var so = new SWFObject("swf/indexBanner.swf", "mapSWF", "907px", "269px", "8.0.22",  null);
		so.useExpressInstall('swf/expressinstall.swf');
		//so.addParam("scale", "noscale");
		so.addParam("wmode", "transparent"); 
		//so.addVariable("swLiveConnect", "true");
		so.addParam("allowScriptAccess", "sameDomain");
		so.addVariable("dataType", "buy.xml");
		so.addVariable("xml", indexSendToSwf());
		//so.addVariable("MSG", getQueryParamValue("m"));
		//so.setAttribute('xiRedirectUrl', 'http://www.projectDomain.com/upgradefinished.html');	// Default will redirect back to the original webSite.
		so.write("flashcontent");
		var macmousewheel = new SWFMacMouseWheel( so );
	}