/**
 * jCarousel - Riding carousels with jQuery
 *   http://sorgalla.com/jcarousel/
 *
 * Copyright (c) 2006 Jan Sorgalla (http://sorgalla.com)
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses.
 *
 * Built on top of the jQuery library
 *   http://jquery.com
 *
 * Inspired by the "Carousel Component" by Bill Scott
 *   http://billwscott.com/carousel/
 */
eval(function(p,a,c,k,e,r){e=function(c){return(c<a?'':e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--)r[e(c)]=k[c]||e(c);k=[function(e){return r[e]}];e=function(){return'\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p}('(9($){$.1s.A=9(o){z 4.14(9(){2H r(4,o)})};8 q={W:F,23:1,1G:1,u:7,15:3,16:7,1H:\'2I\',24:\'2J\',1i:0,B:7,1j:7,1I:7,25:7,26:7,27:7,28:7,29:7,2a:7,2b:7,1J:\'<N></N>\',1K:\'<N></N>\',2c:\'2d\',2e:\'2d\',1L:7,1M:7};$.A=9(e,o){4.5=$.17({},q,o||{});4.Q=F;4.D=7;4.H=7;4.t=7;4.R=7;4.S=7;4.O=!4.5.W?\'1N\':\'2f\';4.E=!4.5.W?\'2g\':\'2h\';8 a=\'\',1d=e.J.1d(\' \');1k(8 i=0;i<1d.K;i++){6(1d[i].2i(\'A-2j\')!=-1){$(e).1t(1d[i]);8 a=1d[i];1l}}6(e.2k==\'2K\'||e.2k==\'2L\'){4.t=$(e);4.D=4.t.18();6(4.D.1m(\'A-H\')){6(!4.D.18().1m(\'A-D\'))4.D=4.D.B(\'<N></N>\');4.D=4.D.18()}X 6(!4.D.1m(\'A-D\'))4.D=4.t.B(\'<N></N>\').18()}X{4.D=$(e);4.t=$(e).2M(\'>2l,>2m,N>2l,N>2m\')}6(a!=\'\'&&4.D.18()[0].J.2i(\'A-2j\')==-1)4.D.B(\'<N 2N=" \'+a+\'"></N>\');4.H=4.t.18();6(!4.H.K||!4.H.1m(\'A-H\'))4.H=4.t.B(\'<N></N>\').18();4.S=$(\'.A-11\',4.D);6(4.S.u()==0&&4.5.1K!=7)4.S=4.H.1u(4.5.1K).11();4.S.V(4.J(\'A-11\'));4.R=$(\'.A-19\',4.D);6(4.R.u()==0&&4.5.1J!=7)4.R=4.H.1u(4.5.1J).11();4.R.V(4.J(\'A-19\'));4.H.V(4.J(\'A-H\'));4.t.V(4.J(\'A-t\'));4.D.V(4.J(\'A-D\'));8 b=4.5.16!=7?1n.1O(4.1o()/4.5.16):7;8 c=4.t.2O(\'1v\');8 d=4;6(c.u()>0){8 f=0,i=4.5.1G;c.14(9(){d.1P(4,i++);f+=d.T(4,b)});4.t.y(4.O,f+\'U\');6(!o||o.u===L)4.5.u=c.u()}4.D.y(\'1w\',\'1x\');4.R.y(\'1w\',\'1x\');4.S.y(\'1w\',\'1x\');4.2n=9(){d.19()};4.2o=9(){d.11()};4.1Q=9(){d.2p()};6(4.5.1j!=7)4.5.1j(4,\'2q\');6($.2r.2s){4.1e(F,F);$(2t).1y(\'2P\',9(){d.1z()})}X 4.1z()};8 r=$.A;r.1s=r.2Q={A:\'0.2.3\'};r.1s.17=r.17=$.17;r.1s.17({1z:9(){4.C=7;4.G=7;4.Y=7;4.12=7;4.1a=F;4.1f=7;4.P=7;4.Z=F;6(4.Q)z;4.t.y(4.E,4.1A(4.5.1G)+\'U\');8 p=4.1A(4.5.23);4.Y=4.12=7;4.1p(p,F);$(2t).1R(\'2u\',4.1Q).1y(\'2u\',4.1Q)},2v:9(){4.t.2w();4.t.y(4.E,\'2R\');4.t.y(4.O,\'2S\');6(4.5.1j!=7)4.5.1j(4,\'2v\');4.1z()},2p:9(){6(4.P!=7&&4.Z)4.t.y(4.E,r.I(4.t.y(4.E))+4.P);4.P=7;4.Z=F;6(4.5.1I!=7)4.5.1I(4);6(4.5.16!=7){8 a=4;8 b=1n.1O(4.1o()/4.5.16),O=0,E=0;$(\'1v\',4.t).14(9(i){O+=a.T(4,b);6(i+1<a.C)E=O});4.t.y(4.O,O+\'U\');4.t.y(4.E,-E+\'U\')}4.15(4.C,F)},2T:9(){4.Q=1g;4.1e()},2U:9(){4.Q=F;4.1e()},u:9(s){6(s!=L){4.5.u=s;6(!4.Q)4.1e()}z 4.5.u},2V:9(i,a){6(a==L||!a)a=i;6(4.5.u!==7&&a>4.5.u)a=4.5.u;1k(8 j=i;j<=a;j++){8 e=4.M(j);6(!e.K||e.1m(\'A-1b-1B\'))z F}z 1g},M:9(i){z $(\'.A-1b-\'+i,4.t)},2x:9(i,s){8 e=4.M(i),1S=0,2x=0;6(e.K==0){8 c,e=4.1C(i),j=r.I(i);1q(c=4.M(--j)){6(j<=0||c.K){j<=0?4.t.2y(e):c.1T(e);1l}}}X 1S=4.T(e);e.1t(4.J(\'A-1b-1B\'));1U s==\'2W\'?e.2X(s):e.2w().2Y(s);8 a=4.5.16!=7?1n.1O(4.1o()/4.5.16):7;8 b=4.T(e,a)-1S;6(i>0&&i<4.C)4.t.y(4.E,r.I(4.t.y(4.E))-b+\'U\');4.t.y(4.O,r.I(4.t.y(4.O))+b+\'U\');z e},1V:9(i){8 e=4.M(i);6(!e.K||(i>=4.C&&i<=4.G))z;8 d=4.T(e);6(i<4.C)4.t.y(4.E,r.I(4.t.y(4.E))+d+\'U\');e.1V();4.t.y(4.O,r.I(4.t.y(4.O))-d+\'U\')},19:9(){4.1D();6(4.P!=7&&!4.Z)4.1W(F);X 4.15(((4.5.B==\'1X\'||4.5.B==\'G\')&&4.5.u!=7&&4.G==4.5.u)?1:4.C+4.5.15)},11:9(){4.1D();6(4.P!=7&&4.Z)4.1W(1g);X 4.15(((4.5.B==\'1X\'||4.5.B==\'C\')&&4.5.u!=7&&4.C==1)?4.5.u:4.C-4.5.15)},1W:9(b){6(4.Q||4.1a||!4.P)z;8 a=r.I(4.t.y(4.E));!b?a-=4.P:a+=4.P;4.Z=!b;4.Y=4.C;4.12=4.G;4.1p(a)},15:9(i,a){6(4.Q||4.1a)z;4.1p(4.1A(i),a)},1A:9(i){6(4.Q||4.1a)z;i=r.I(i);6(4.5.B!=\'1c\')i=i<1?1:(4.5.u&&i>4.5.u?4.5.u:i);8 a=4.C>i;8 b=r.I(4.t.y(4.E));8 f=4.5.B!=\'1c\'&&4.C<=1?1:4.C;8 c=a?4.M(f):4.M(4.G);8 j=a?f:f-1;8 e=7,l=0,p=F,d=0;1q(a?--j>=i:++j<i){e=4.M(j);p=!e.K;6(e.K==0){e=4.1C(j).V(4.J(\'A-1b-1B\'));c[a?\'1u\':\'1T\'](e)}c=e;d=4.T(e);6(p)l+=d;6(4.C!=7&&(4.5.B==\'1c\'||(j>=1&&(4.5.u==7||j<=4.5.u))))b=a?b+d:b-d}8 g=4.1o();8 h=[];8 k=0,j=i,v=0;8 c=4.M(i-1);1q(++k){e=4.M(j);p=!e.K;6(e.K==0){e=4.1C(j).V(4.J(\'A-1b-1B\'));c.K==0?4.t.2y(e):c[a?\'1u\':\'1T\'](e)}c=e;8 d=4.T(e);6(d==0){2Z(\'30: 31 1N/2f 32 1k 33. 34 35 36 37 38 39. 3a...\');z 0}6(4.5.B!=\'1c\'&&4.5.u!==7&&j>4.5.u)h.3b(e);X 6(p)l+=d;v+=d;6(v>=g)1l;j++}1k(8 x=0;x<h.K;x++)h[x].1V();6(l>0){4.t.y(4.O,4.T(4.t)+l+\'U\');6(a){b-=l;4.t.y(4.E,r.I(4.t.y(4.E))-l+\'U\')}}8 n=i+k-1;6(4.5.B!=\'1c\'&&4.5.u&&n>4.5.u)n=4.5.u;6(j>n){k=0,j=n,v=0;1q(++k){8 e=4.M(j--);6(!e.K)1l;v+=4.T(e);6(v>=g)1l}}8 o=n-k+1;6(4.5.B!=\'1c\'&&o<1)o=1;6(4.Z&&a){b+=4.P;4.Z=F}4.P=7;6(4.5.B!=\'1c\'&&n==4.5.u&&(n-k+1)>=1){8 m=r.10(4.M(n),!4.5.W?\'1r\':\'1Y\');6((v-m)>g)4.P=v-g-m}1q(i-->o)b+=4.T(4.M(i));4.Y=4.C;4.12=4.G;4.C=o;4.G=n;z b},1p:9(p,a){6(4.Q||4.1a)z;4.1a=1g;8 b=4;8 c=9(){b.1a=F;6(p==0)b.t.y(b.E,0);6(b.5.B==\'1X\'||b.5.B==\'G\'||b.5.u==7||b.G<b.5.u)b.2z();b.1e();b.1Z(\'2A\')};4.1Z(\'3c\');6(!4.5.1H||a==F){4.t.y(4.E,p+\'U\');c()}X{8 o=!4.5.W?{\'2g\':p}:{\'2h\':p};4.t.1p(o,4.5.1H,4.5.24,c)}},2z:9(s){6(s!=L)4.5.1i=s;6(4.5.1i==0)z 4.1D();6(4.1f!=7)z;8 a=4;4.1f=3d(9(){a.19()},4.5.1i*3e)},1D:9(){6(4.1f==7)z;3f(4.1f);4.1f=7},1e:9(n,p){6(n==L||n==7){8 n=!4.Q&&4.5.u!==0&&((4.5.B&&4.5.B!=\'C\')||4.5.u==7||4.G<4.5.u);6(!4.Q&&(!4.5.B||4.5.B==\'C\')&&4.5.u!=7&&4.G>=4.5.u)n=4.P!=7&&!4.Z}6(p==L||p==7){8 p=!4.Q&&4.5.u!==0&&((4.5.B&&4.5.B!=\'G\')||4.C>1);6(!4.Q&&(!4.5.B||4.5.B==\'G\')&&4.5.u!=7&&4.C==1)p=4.P!=7&&4.Z}8 a=4;4.R[n?\'1y\':\'1R\'](4.5.2c,4.2n)[n?\'1t\':\'V\'](4.J(\'A-19-1E\')).20(\'1E\',n?F:1g);4.S[p?\'1y\':\'1R\'](4.5.2e,4.2o)[p?\'1t\':\'V\'](4.J(\'A-11-1E\')).20(\'1E\',p?F:1g);6(4.R.K>0&&(4.R[0].1h==L||4.R[0].1h!=n)&&4.5.1L!=7){4.R.14(9(){a.5.1L(a,4,n)});4.R[0].1h=n}6(4.S.K>0&&(4.S[0].1h==L||4.S[0].1h!=p)&&4.5.1M!=7){4.S.14(9(){a.5.1M(a,4,p)});4.S[0].1h=p}},1Z:9(a){8 b=4.Y==7?\'2q\':(4.Y<4.C?\'19\':\'11\');4.13(\'25\',a,b);6(4.Y!==4.C){4.13(\'26\',a,b,4.C);4.13(\'27\',a,b,4.Y)}6(4.12!==4.G){4.13(\'28\',a,b,4.G);4.13(\'29\',a,b,4.12)}4.13(\'2a\',a,b,4.C,4.G,4.Y,4.12);4.13(\'2b\',a,b,4.Y,4.12,4.C,4.G)},13:9(a,b,c,d,e,f,g){6(4.5[a]==L||(1U 4.5[a]!=\'2B\'&&b!=\'2A\'))z;8 h=1U 4.5[a]==\'2B\'?4.5[a][b]:4.5[a];6(!$.3g(h))z;8 j=4;6(d===L)h(j,c,b);X 6(e===L)4.M(d).14(9(){h(j,4,d,c,b)});X{1k(8 i=d;i<=e;i++)6(i!==7&&!(i>=f&&i<=g))4.M(i).14(9(){h(j,4,i,c,b)})}},1C:9(i){z 4.1P(\'<1v></1v>\',i)},1P:9(e,i){8 a=$(e).V(4.J(\'A-1b\')).V(4.J(\'A-1b-\'+i));a.20(\'3h\',i);z a},J:9(c){z c+\' \'+c+(!4.5.W?\'-3i\':\'-W\')},T:9(e,d){8 a=e.2C!=L?e[0]:e;8 b=!4.5.W?a.1F+r.10(a,\'2D\')+r.10(a,\'1r\'):a.2E+r.10(a,\'2F\')+r.10(a,\'1Y\');6(d==L||b==d)z b;8 w=!4.5.W?d-r.10(a,\'2D\')-r.10(a,\'1r\'):d-r.10(a,\'2F\')-r.10(a,\'1Y\');$(a).y(4.O,w+\'U\');z 4.T(a)},1o:9(){z!4.5.W?4.H[0].1F-r.I(4.H.y(\'3j\'))-r.I(4.H.y(\'3k\')):4.H[0].2E-r.I(4.H.y(\'3l\'))-r.I(4.H.y(\'3m\'))},3n:9(i,s){6(s==L)s=4.5.u;z 1n.3o((((i-1)/s)-1n.3p((i-1)/s))*s)+1}});r.17({3q:9(d){z $.17(q,d||{})},10:9(e,p){6(!e)z 0;8 a=e.2C!=L?e[0]:e;6(p==\'1r\'&&$.2r.2s){8 b={\'1w\':\'1x\',\'3r\':\'3s\',\'1N\':\'1i\'},21,22;$.2G(a,b,9(){21=a.1F});b[\'1r\']=0;$.2G(a,b,9(){22=a.1F});z 22-21}z r.I($.y(a,p))},I:9(v){v=3t(v);z 3u(v)?0:v}})})(3v);',62,218,'||||this|options|if|null|var|function||||||||||||||||||||list|size||||css|return|jcarousel|wrap|first|container|lt|false|last|clip|intval|className|length|undefined|get|div|wh|tail|locked|buttonNext|buttonPrev|dimension|px|addClass|vertical|else|prevFirst|inTail|margin|prev|prevLast|callback|each|scroll|visible|extend|parent|next|animating|item|circular|split|buttons|timer|true|jcarouselstate|auto|initCallback|for|break|hasClass|Math|clipping|animate|while|marginRight|fn|removeClass|before|li|display|block|bind|setup|pos|placeholder|create|stopAuto|disabled|offsetWidth|offset|animation|reloadCallback|buttonNextHTML|buttonPrevHTML|buttonNextCallback|buttonPrevCallback|width|ceil|format|funcResize|unbind|old|after|typeof|remove|scrollTail|both|marginBottom|notify|attr|oWidth|oWidth2|start|easing|itemLoadCallback|itemFirstInCallback|itemFirstOutCallback|itemLastInCallback|itemLastOutCallback|itemVisibleInCallback|itemVisibleOutCallback|buttonNextEvent|click|buttonPrevEvent|height|left|top|indexOf|skin|nodeName|ul|ol|funcNext|funcPrev|reload|init|browser|safari|window|resize|reset|empty|add|prepend|startAuto|onAfterAnimation|object|jquery|marginLeft|offsetHeight|marginTop|swap|new|normal|swing|UL|OL|find|class|children|load|prototype|0px|10px|lock|unlock|has|string|html|append|alert|jCarousel|No|set|items|This|will|cause|an|infinite|loop|Aborting|push|onBeforeAnimation|setTimeout|1000|clearTimeout|isFunction|jcarouselindex|horizontal|borderLeftWidth|borderRightWidth|borderTopWidth|borderBottomWidth|index|round|floor|defaults|float|none|parseInt|isNaN|jQuery'.split('|'),0,{}))

function trim(stringToTrim) {
    return stringToTrim.replace(/^\s+|\s+$/g,"");
}

jQuery(document).ready(function() {
	
	jQuery('#propertycarousel').jcarousel({
		// Configuration goes here
		itemLoadCallback: carousel_itemLoadCallback,
		size: setInitCarouselSize(),
		scroll: 5,
		start: setInitStartPosition(),
		itemVisibleInCallback: itemVisibleIn		
	});
});

function setInitCarouselSize()
{
	//var no_props_found = jQuery('#propertycarousel > ul > li').size();
	return $('#result_count').val();
}

function setInitStartPosition()
{
	return $('#searchStartPoint').val();
}

function itemVisibleIn(carousel, li, index, state)
{
    var resultcount = $('#result_count').val();
    $('#scrollmsg').html('&nbsp;&nbsp;&nbsp;&nbsp;' + carousel.first + '&nbsp;&nbsp; &mdash; &nbsp;&nbsp;' + carousel.last + ' of&nbsp;&nbsp;&nbsp;&nbsp;<br>' + resultcount + ' results');
}


function carousel_itemLoadCallback(carousel, state)
{
	// Check if the requested items already exist
	if (carousel.has(carousel.first, carousel.last)) {
		// may have to stop this "caching because the search parameters might have changed.
		//alert('first: ' + carousel.first + ', last: ' + carousel.last);
		return;
	}
	
	//alert('carousel_itemLoadCallback called');
	
	jQuery.get(
		'/ajax/propertysearch',
		{
			country: $("#searchcountry").val() ,
			city: $("#searchcity").val(),
			district: $("#searchdistrict").val(),
			minrate: $("#searchminrate").val(),
			maxrate: $("#searchmaxrate").val(),
			beds: $("#searchbeds").val(),
			start: carousel.first,
			location: $("#searchlocation").val(),
			rating: $("#searchrating").val(),
			minstay: $("#searchminstay").val(),
			wheelchair: $("#searchwheelchair").val(),
			parkingfacilities: $("#searchparkingfacilities").val(),
			petspolicy: $("#searchpetspolicy").val(),
			airconditioning: $("#searchairconditioning").val(),
			broadband: $("#searchbroadband").val(),
			postcode: $("#searchpostcode").val(),
			washerdryer: $("#searchwasherdryer").val(),
			type: $("#searchtype").val()
		},
		function(xml) {
			mycarousel_itemAddCallback(carousel, carousel.first, carousel.last, xml);
		},
		'xml'
	);

};

function mycarousel_itemAddCallback(carousel, first, last, xml)
{
	var queryString = 'searchcountry=' + $("#searchcountry").val() + '&searchcity=' + $("#searchcity").val() + '&searchdistrict=' + $("#searchdistrict").val() + '&searchminrate=' + $("#searchminrate").val() + '&searchmaxrate=' + $("#searchmaxrate").val() + '&searchbeds=' + $("#searchbeds").val() + '&searchstart=' + carousel.first + '&searchlocation=' + $("#searchlocation").val() + '&searchrating=' + $("#searchrating").val() + '&searchminstay=' + $("#searchminstay").val() + '&searchwheelchair=' + $("#searchwheelchair").val() + '&searchparkingfacilities=' + $("#searchparkingfacilities").val() + '&searchpetspolicy=' + $("#searchpetspolicy").val() + '&searchairconditioning=' + $("#searchairconditioning").val() + '&searchbroadband=' + $("#searchbroadband").val() + '&searchpostcode=' + $("#searchpostcode").val() + '&searchwasherdryer=' + $("#searchwasherdryer").val() + '&searchtype=' + $("#searchtype").val();
	
	// Set the size of the carousel
	//carousel.size(parseInt(jQuery('total', xml).text()));

	jQuery('prop', xml).each(function(i) {
		carousel.add(first + i, '<div style="width:145px;height:106px;"><div style="font-size:12px;padding:3px 0 0 5px;float:left;">' + (first + i) + '</div><div style="padding:3px 5px 0 0;width:100px;float:right;"><div><img border="0" width="97px" heighht="75px" src="/images/properties-images/thumb/' + jQuery(this).find('prop_img').text() + '"></div><div style="color:#467CB0;font-size:10px;"><a style="cursor: pointer;text-decoration:none;" border="0" href="/serviced-apartment-property-' + jQuery(this).find('prop_id').text() + '?' + queryString + '&searchstart=' + (first + i) + '"><span style="color:#467CB0;font-size:10px;">' + jQuery(this).find('prop_name').text() + '</span></a></div></div></div>');
	});


//	jQuery('prop', xml).each(function(i) {
//		carousel.add(first + i, '<div style="width:145px;height:106px;"><a style="cursor: pointer;text-decoration:none;" border="0" href="javascript:loadpropertyprofile(' + jQuery(this).find('prop_id').text() + ');"><div style="font-size:18;padding:3px 0 0 5px;float:left;">' + (first + i) + '</div><div style="padding:3px 5px 0 0;width:100px;float:right;"><div><img border="0" width="97px" heighht="75px" src="/images/properties-images/thumb/' + jQuery(this).find('prop_img').text() + '"></div><div style="color:#467CB0;font-size:10px;">' + jQuery(this).find('prop_name').text() + '</div></div></a></div>');
//	});
	
//	$.post('/ajax/propertyprofile?propertyid=' + $(xml).find('prop_id').first().text(), function(data) {
//	  $('#propertytab-frag-1').html(data);
//	});

};

function loadpropertyprofile(id)
{
	$.post('/ajax/propertyprofile?propertyid=' + id, function(data) {
	  $('#propertytab-frag-1').html(data);
	});
}

function load360(propid,tournumber)
{
	var controlparam = "";
	
         if (isIE)
         {
		controlparam = "1";
         }
         else
         {
		if ( hasProductInstall && !hasRequestedVersion )
		{
			controlparam = "2";
		} else if (hasRequestedVersion) 
		{
			controlparam = "3";
		} 
		else 
		{
			controlparam = "4";
		}
         }         
         
	$.post('/ajax/virtualtours?propertyid=' + propid + '&tour=' + tournumber + '&controlparam=' + controlparam, function(data) {
	  $('#maintourwin').html(data);
	});
}

function resetcountrydependants()
{
	loadcities('','');
	resetlocation(true);
}


function loadcities(city_name, district_name)
{
	if (trim($('#searchcountry').val()) != "")
	{
		$.getJSON("/ajax/cities",{countryid: $('#searchcountry').val(), ajax: 'true'}, function(data)
		{
			var options = '<option value="">All cities</option>';
			for (var i = 0; i < data.length; i++)
			{
				if (city_name != null && city_name != "" && city_name == data[i]['city_name'])
					options += '<option selected="selected" value="' + data[i]['city_id'] + '">' + data[i]['city_name'] + '</option>';
				else
					options += '<option value="' + data[i]['city_id'] + '">' + data[i]['city_name'] + '</option>';
			}

			$("select#searchcity").html(options);
			$("select#searchcity").attr("enabled","enabled");

			// Load districts
			loaddistricts(district_name);
		});
	}
	else
	{
		$("select#searchcity").html('<option value="">All cities</option>');
		$("select#searchcity").attr("disabled","disabled");
	}	
}

function loaddistricts(district_name)
{
	if (trim($('#searchcity').val()) != "")
	{
		$.getJSON("/ajax/districts",{cityid: $('#searchcity').val(), ajax: 'true'}, function(data)
		{
			var options = '<option value="">All districts</option>';
			for (var i = 0; i < data.length; i++)
			{
				if (district_name != null && district_name != "" && district_name == data[i]['district_name'])
					options += '<option selected="selected" value="' + data[i]['district_id'] + '">' + data[i]['district_name'] + '</option>';
				else
					options += '<option value="' + data[i]['district_id'] + '">' + data[i]['district_name'] + '</option>';

				
			}

			$("select#searchdistrict").html(options);
			$("select#searchdistrict").attr("enabled","enabled");
		});
	}
	else
	{
		$("select#searchdistrict").html('<option value="">All districts</option>');
		//$("select#searchdistrict").attr("disabled","disabled");
	}
}


function resetcitydependants()
{
	loaddistricts('');
	resetlocation(true,true);
}

function resetdistrictdependants()
{
	resetlocation(true,true,true);
}

function resetlocation(country_reset,city_reset,district_reset)
{
	var new_location = "";
	var country_name = $("select#searchcountry").val();
	var city_name = $("select#searchcity").val();
	var district_name = $("select#searchdistrict").val();
	
	if (( country_name != "") && (country_name != "0") && country_reset)
	{
		new_location += $("select#searchcountry :selected").text();
		
		if (( city_name != "") && (city_name != "0") && city_reset)
		{
			new_location += ' - ' + $("select#searchcity :selected").text();
			
			if (( district_name != "") && (district_name != "0") && district_reset)
			{
				new_location += ' - ' + $("select#searchdistrict :selected").text();
			}
		}
	}

	$("input#searchlocation").val(new_location);

}

function resetcountrycitydistrict()
{
	//$("select#searchcountry").val('');
	//loadcities('','');
}


function changedropdown(control_name, control_text)
{
	$("#" + control_name + " option").each(function(i){

		if($(this).text() == control_text)
		{
			$("#" + control_name).val($(this).val());
			$("#" + control_name).attr("enabled","enabled");
			//return false;
		}
	});
}

function initialiseSearch()
{
	var searchtype_value = $('#searchtype').val();
	
	// alert('search type is ' + searchtype_value);
	
	if (searchtype_value == 'basic')
	{
		$('.advancedsearchitem').hide();
		$('.advancedsearchitem').css('display','none');
	}
	else
	{
		$('.advancedsearchitem').show();
		$('.advancedsearchitem').css('display','block');

		$('.searchlink1').hide();
		$('.searchlink1').css('display','none');

		$('div#advanced-form').show();
		$('div#advanced-form').css('display','block');
	}	
	
	
        $(".formtoggle").click(function(){
        	
        	 var searchtype_value = $('#searchtype').val();
        	 
        	 if (searchtype_value == 'basic')
        	 {
        	 	$('#searchtype').val('advanced');

			$('.searchlink1').hide();
			$('.searchlink1').css('display','none');

			$('.advancedsearchitem').show();
			$('.advancedsearchitem').css('display','block');			
		 } 	
        	 else
        	 {
        	 	$('#searchtype').val('basic');

			$('.advancedsearchitem').hide();
			$('.advancedsearchitem').css('display','none');

			$('.searchlink1').show();
			$('.searchlink1').css('display','inline');

        	 }

        	 $("div#advanced-form").slideToggle("slow",function(){
            });

        });

	$('#propsearchform > *').keypress(function(event) {
	  if (event.keyCode == '13') {
		 event.preventDefault();
		 
		 var loc_data = $('#searchlocation').val();
		 
		 if (loc_data != "")
		 	resetcountrycitydistrict();
		 
		 $('#propsearchform').submit();
	   }
	});
	
        $("a#form-toggle").toggle(function(){
                $(this).text("Back to basic search");
                $('#search-button').animate({                    
                    top: '+=100'
                  }, 'slow', function() {
                    // Animation complete.
                  });
            },function(){
                $(this).text("Search in more detail");
                $('#search-button').animate({
                    top: '-=100'
                  }, 'slow', function() {
                    // Animation complete.
                  });
            });

        $("#searchlocation").focusout(function() {
		populateCountryCityDistrictFromLocation();
        });

	$("#sitesearchtext").Watermark("Text search");
	$("#searchlocation").Watermark("City name");
        
}

function populateCountryCityDistrictFromLocation()
{
    var address = $("#searchlocation").val();
    var location_array = address.split("-");

    var number_of_loc_elements = location_array.length;

    var country_name;
    var city_name;
    var district_name;

   // $("searchcountry").change(function() {});
   // $("searchcity").change(function() {alert('city changed')});
   // $("searchdistrict").change(function() {});

    // Check where we have one, two or three items in the array
    // and read values
    if (number_of_loc_elements == 1)
    {
	country_name = trim(location_array[0]);
	changedropdown("searchcountry", country_name);
    }
    else if (number_of_loc_elements == 2)
    {
	country_name = trim(location_array[0]);
	city_name = trim(location_array[1]);

	if (trim(country_name) != "")
	{
		changedropdown("searchcountry", country_name);                    		

		if (trim(city_name) != "")
			loadcities(city_name, '');
	}

    }
    else if (number_of_loc_elements == 3)
    {
	country_name = trim(location_array[0]);
	city_name = trim(location_array[1]);
	district_name = trim(location_array[2]);

	if (trim(country_name) != "")
	{
		changedropdown("searchcountry", country_name);                    		

		if (trim(city_name) != "")
		{                    			
			if (trim(district_name) != "")
				loadcities(city_name, district_name);
			else
				loadcities(city_name, '');
		}
	}


    }
}


function changedropdown(control_name, control_text)
{
	$("#" + control_name + " option").each(function(i){

		if($(this).text() == control_text)
		{
			$("#" + control_name).val($(this).val());
			$("#" + control_name).attr("enabled","enabled");
			//return false;
		}
	});

}


function translateCurrencyValues()
{
	var to_currency = $('#rates_currency').val();
	var from_currency = $('#property_currency_id').val();

	jQuery.get('/ajax/currencyconversionfactor?from=' + from_currency + '&to=' + to_currency,
		function(return_data) {
			updateRatesTab(return_data, to_currency);
		}
	);

}

function updateRatesTab(conversion_factor, to_currency)
{
	//alert('Conversion returned is ' + conversion_factor);
	var new_symbol = getCurrencySymbol(to_currency);
	
	$("[id^=currencyfigure]").each(function(index,curr_element)
	{

		hidden_equivalent_name = 'hidden' + $(this).attr('id');
		hidden_equivalent_value = $('#' + hidden_equivalent_name).val();

		new_value = hidden_equivalent_value * 1.0 * conversion_factor;

		if (new_value > 999)
			new_value = new_value.toFixed(0);
		else
			new_value = new_value.toFixed(2);

		$(this).html(new_symbol + new_value);
	});
}

function getCurrencySymbol(currency_id)
{
	var currency_symbol_html = new Array();
	
        currency_symbol_html[126] ='&#x00A3;';
        currency_symbol_html[6  ] ='&#x0024;';
        currency_symbol_html[9  ] ='BHD';
        currency_symbol_html[22 ] ='&#x0024;';
        currency_symbol_html[26 ] ='&#x00A5;';
        currency_symbol_html[33 ] ='&#75;&#269;';
        currency_symbol_html[34 ] ='&#107;&#114;';
        currency_symbol_html[37 ] ='&#x00A3;';
        currency_symbol_html[136] ='&#x20AC;';
        currency_symbol_html[137] ='&#x20AC;';
        currency_symbol_html[138] ='&#x20AC;';
        currency_symbol_html[139] ='&#x20AC;';
        currency_symbol_html[140] ='&#x20AC;';
        currency_symbol_html[144] ='&#x20AC;';
        currency_symbol_html[146] ='&#x20AC;';
        currency_symbol_html[148] ='&#x20AC;';
        currency_symbol_html[149] ='&#x20AC;';
        currency_symbol_html[150] ='&#x20AC;';
        currency_symbol_html[155] ='&#x20AC;';
        currency_symbol_html[45 ] ='GEL';
        currency_symbol_html[142] ='&#x00A3;';
        currency_symbol_html[53 ] ='&#70;&#116;';
        currency_symbol_html[55 ] ='&#x20A8;';
        currency_symbol_html[56 ] ='&#82;&#112;';
        currency_symbol_html[60 ] ='&#x00A5;';
        currency_symbol_html[147] ='&#x00A3;';
        currency_symbol_html[75 ] ='&#82;&#77;';
        currency_symbol_html[145] ='&#x00A3;';
        currency_symbol_html[96 ] ='&#x20B1;';
        currency_symbol_html[97 ] ='&#122;&#322;';
        currency_symbol_html[98 ] ='&#xFDFC;';
        currency_symbol_html[100] ='&#x0440;&#x0443;&#x0431;';
        currency_symbol_html[107] ='&#x0024;';
        currency_symbol_html[110] ='&#x0052;';
        currency_symbol_html[111] ='&#x20A9;';
        currency_symbol_html[116] ='&#67;&#72;&#70;';
        currency_symbol_html[119] ='&#x0E3F;';
        currency_symbol_html[151] ='&#84;&#76;';
        currency_symbol_html[124] ='&#x20B4;';
        currency_symbol_html[125] ='AED';
        currency_symbol_html[127] ='&#x0024;';
        currency_symbol_html[132] ='&#x20AB;';
        currency_symbol_html[18 ] ='&#x52;&#x24;';
        currency_symbol_html[  1 ]='ALL';
        currency_symbol_html[  2 ]='DZD';
        currency_symbol_html[  3 ]='ARS';
        currency_symbol_html[  4 ]='AMD';
        currency_symbol_html[  5 ]='AWG';
        currency_symbol_html[  7 ]='AZN';
        currency_symbol_html[  8 ]='BSD';
        currency_symbol_html[ 10 ]='BDT';
        currency_symbol_html[ 11 ]='BBD';
        currency_symbol_html[ 12 ]='BYR';
        currency_symbol_html[ 13 ]='BZD';
        currency_symbol_html[ 14 ]='BMD';
        currency_symbol_html[ 15 ]='BTN';
        currency_symbol_html[ 16 ]='BOB';
        currency_symbol_html[ 17 ]='BWP';
        currency_symbol_html[ 19 ]='BGN';
        currency_symbol_html[ 20 ]='BIF';
        currency_symbol_html[ 21 ]='KHR';
        currency_symbol_html[ 23 ]='CVE';
        currency_symbol_html[ 24 ]='KYD';
        currency_symbol_html[ 25 ]='CLP';
        currency_symbol_html[ 27 ]='COP';
        currency_symbol_html[ 28 ]='KMF';
        currency_symbol_html[ 29 ]='CDF';
        currency_symbol_html[ 30 ]='CRC';
        currency_symbol_html[ 31 ]='HRK';
        currency_symbol_html[ 32 ]='CUP';
        currency_symbol_html[ 35 ]='DJF';
        currency_symbol_html[ 36 ]='DOP';
        currency_symbol_html[ 38 ]='SVC';
        currency_symbol_html[ 39 ]='ERN';
        currency_symbol_html[ 40 ]='EEK';
        currency_symbol_html[ 41 ]='ETB';
        currency_symbol_html[ 42 ]='FKP';
        currency_symbol_html[ 43 ]='FJD';
        currency_symbol_html[ 44 ]='GMD';
        currency_symbol_html[ 46 ]='GHS';
        currency_symbol_html[ 47 ]='GIP';
        currency_symbol_html[ 48 ]='GTQ';
        currency_symbol_html[ 49 ]='GNF';
        currency_symbol_html[ 50 ]='GYD';
        currency_symbol_html[ 51 ]='HTG';
        currency_symbol_html[ 52 ]='HNL';
        currency_symbol_html[ 54 ]='ISK';
        currency_symbol_html[ 57 ]='IQD';
        currency_symbol_html[ 58 ]='ILS';
        currency_symbol_html[ 59 ]='JMD';
        currency_symbol_html[ 61 ]='JOD';
        currency_symbol_html[ 62 ]='KZT';
        currency_symbol_html[ 63 ]='KES';
        currency_symbol_html[ 64 ]='KWD';
        currency_symbol_html[ 65 ]='KGS';
        currency_symbol_html[ 66 ]='LAK';
        currency_symbol_html[ 67 ]='LVL';
        currency_symbol_html[ 68 ]='LBP';
        currency_symbol_html[ 69 ]='LSL';
        currency_symbol_html[ 70 ]='LRD';
        currency_symbol_html[ 71 ]='LYD';
        currency_symbol_html[ 72 ]='LTL';
        currency_symbol_html[ 73 ]='MGF';
        currency_symbol_html[ 74 ]='MWK';
        currency_symbol_html[ 76 ]='MVR';
        currency_symbol_html[ 77 ]='MRO';
        currency_symbol_html[ 78 ]='MUR';
        currency_symbol_html[ 79 ]='MXN';
        currency_symbol_html[ 80 ]='MNT';
        currency_symbol_html[ 81 ]='MAD';
        currency_symbol_html[ 82 ]='MMK';
        currency_symbol_html[ 83 ]='NAD';
        currency_symbol_html[ 84 ]='NPR';
        currency_symbol_html[ 85 ]='ANG';
        currency_symbol_html[ 86 ]='NZD';
        currency_symbol_html[ 87 ]='NIO';
        currency_symbol_html[ 88 ]='&#x20A6;';
        currency_symbol_html[ 89 ]='NOK';
        currency_symbol_html[ 90 ]='OMR';
        currency_symbol_html[ 91 ]='PKR';
        currency_symbol_html[ 92 ]='PAB';
        currency_symbol_html[ 93 ]='PGK';
        currency_symbol_html[ 94 ]='PYG';
        currency_symbol_html[ 95 ]='PEN';
        currency_symbol_html[ 99 ]='RON';
        currency_symbol_html[101 ]='RWF';
        currency_symbol_html[102 ]='SHP';
        currency_symbol_html[103 ]='WST';
        currency_symbol_html[104 ]='SAR';
        currency_symbol_html[105 ]='SCR';
        currency_symbol_html[106 ]='SLL';
        currency_symbol_html[108 ]='SBD';
        currency_symbol_html[109 ]='SOS';
        currency_symbol_html[112 ]='LKR';
        currency_symbol_html[113 ]='SDG';
        currency_symbol_html[114 ]='SZL';
        currency_symbol_html[115 ]='SEK';
        currency_symbol_html[117 ]='SYP';
        currency_symbol_html[118 ]='TJS';
        currency_symbol_html[120 ]='TOP';
        currency_symbol_html[121 ]='TND';
        currency_symbol_html[122 ]='TMM';
        currency_symbol_html[123 ]='UGX';
        currency_symbol_html[128 ]='UYU';
        currency_symbol_html[129 ]='UZS';
        currency_symbol_html[130 ]='VUV';
        currency_symbol_html[131 ]='VEF';
        currency_symbol_html[133 ]='YER';
        currency_symbol_html[134 ]='ZMK';
        currency_symbol_html[135 ]='ZWD';
        currency_symbol_html[162 ]='BAM';
        currency_symbol_html[161 ]='AOA';
        currency_symbol_html[158 ]='XCD';
        currency_symbol_html[157 ]='AFN';
        currency_symbol_html[160 ]='XOF';
        currency_symbol_html[159 ]='XAF';
        currency_symbol_html[163 ]='AON';
        currency_symbol_html[164 ]='BND';
        currency_symbol_html[165 ]='MOP';
        currency_symbol_html[166 ]='XPF';
        currency_symbol_html[167 ]='IRR';
        currency_symbol_html[168 ]='MZN';
        currency_symbol_html[169 ]='MDL';
        currency_symbol_html[170 ]='RSD';
        currency_symbol_html[171 ]='SRD';
        currency_symbol_html[172 ]='MKD';
        currency_symbol_html[173 ]='TTD';
        currency_symbol_html[174 ]='TZS';
        currency_symbol_html[175 ]='ADF';
        currency_symbol_html[176 ]='ADP';
        currency_symbol_html[177 ]='AZM';
        currency_symbol_html[178 ]='CUC';
        currency_symbol_html[179 ]='ECS';
        currency_symbol_html[180 ]='GHC';
        currency_symbol_html[181 ]='HKD';
        currency_symbol_html[182 ]='KPW';
        currency_symbol_html[183 ]='MGA';
        currency_symbol_html[184 ]='MZM';
        currency_symbol_html[185 ]='ROL';
        currency_symbol_html[186 ]='SDD';
        currency_symbol_html[187 ]='SDP';
        currency_symbol_html[188 ]='SKK';
        currency_symbol_html[189 ]='SRG';
        currency_symbol_html[190 ]='STD';
        currency_symbol_html[191 ]='TRL';
        currency_symbol_html[192 ]='TWD';
        currency_symbol_html[193 ]='VEB';
        currency_symbol_html[194 ]='XAG';
        currency_symbol_html[195 ]='XAU';
        currency_symbol_html[196 ]='XEU';
        currency_symbol_html[197 ]='XPD';
        currency_symbol_html[198 ]='XPT';
        currency_symbol_html[199 ]='YUN';

	
	return currency_symbol_html[currency_id];
}

function AddToShortlist(property,id)
{
	jQuery.get('/ajax/shortlistproperty?pid=' + property + '&sessionid=' + id,
		function(return_data) {
			if (return_data == 'true')
                                alert("Shortlist updated");
		}
	);
}

(function($) {
	var map=new Array();
	$.Watermark = {
		ShowAll:function(){
			for (var i=0;i<map.length;i++){
				if(map[i].obj.val()==""){
					map[i].obj.val(map[i].text);					
					map[i].obj.css("color",map[i].WatermarkColor);
				}else{
				    map[i].obj.css("color",map[i].DefaultColor);
				}
			}
		},
		HideAll:function(){
			for (var i=0;i<map.length;i++){
				if(map[i].obj.val()==map[i].text)
					map[i].obj.val("");					
			}
		}
	}
	
	$.fn.Watermark = function(text,color) {
		if(!color)
			color="#aaa";
		return this.each(
			function(){		
				var input=$(this);
				var defaultColor=input.css("color");
				map[map.length]={text:text,obj:input,DefaultColor:defaultColor,WatermarkColor:color};
				function clearMessage(){
					if(input.val()==text)
						input.val("");
					input.css("color",defaultColor);
				}

				function insertMessage(){
					if(input.val().length==0 || input.val()==text){
						input.val(text);
						input.css("color",color);	
					}else
						input.css("color",defaultColor);				
				}

				input.focus(clearMessage);
				input.blur(insertMessage);								
				input.change(insertMessage);
				
				insertMessage();
			}
		);
	};
})(jQuery);

