﻿
$j(document).ready(function(){
	searchBox.Init();
});

var searchBox = {
	Init:function(){
		// objects
		searchBox.AutoCompleteTextBox = $j('#searchIntellLocation')[0];		
		searchBox.SubmitButton = $j('#searchSubmit')[0];
		searchBox.MessageBox = $j('#searchMessageBox')[0];
		searchBox.isLondonLocation = false; //$j('#ctr_searchBox_isLondonLocation');
		
		// prepare Controls
		this.EnableMinControls();
		
		
		// Watermark Controls
		$j(searchBox.AutoCompleteTextBox).Watermark("Where? Country, Region or Place");
		
		
			
		// events
		$j(this.SubmitButton).click(function(e){searchBox.onSubmit(e);});
		
		// get current controls value
		// this.SelectedCurrencyCode = $j("option:selected",this.CurrencyDropDown).val();
		
		// auto complete
		$j(this.AutoCompleteTextBox).autocomplete('/services/GetLocation.aspx', {
		    delay: 100,
		    width: 400,
		    scroll: false,
		    formatItem: formatItem,
		    formatResult: formatResult,
			selectFirst: true,
			cacheLength: 1,
			max:15,
		    extraParams: {
				country: "",
		        countryID: "0",
				GetLocationsByPrefix:"a"
		    }
	    });
		
		
		
		$j(this.AutoCompleteTextBox).result(function(event,data,formatted){
			//alert(data);
			var level = data[4];
			var IDs;
			var regionID;
                //alert(data);
            IDs = data[3].split('_');
			if(level==1){ regionID = data[2]; }
			else{ regionID = IDs[0]; }
			
					
			// set loation id
			searchBox.SelectedLocationID = data[2];
			
			searchBox.SelectedLocationURL = data[1];
			
			// now check if the selected location is in Greater London?
			// if (IDs[0]==50503&&IDs[1]==0) { - Russ - 06/11/2009 remove IDs[1] check to allow /Barking and Dagenham/ level
			if (IDs[0]==50503) {
				searchBox.isLondonLocation = true;
			}
		});
	},
	onSubmit:function(e){
		e.preventDefault();
		
		
		// now update form control with price bands index
		
		$j('#searchForm').attr("action",searchBox.SubmitURL);
		$j('#searchForm').attr("method",searchBox.SubmitMethod);
		
		// construct URL here
		var _targetDomain = '/property';
		var _defaultURL = '/property/all/';
		var _targetURL = '';
		
		if(searchBox.SelectedLocationURL!= '')
			_targetURL += '/' + searchBox.SelectedLocationURL;
		if(_targetURL=='')
			_targetURL = _defaultURL;					
		else
			_targetURL =   _targetURL + '/';
		
		// window.location.replace(_targetDomain+_targetURL);
		
			
		
        
		window.location = (_targetDomain +_targetURL).toLowerCase();
		 $j('#searchForm').submit();
	},
	FillDropDown:function(data,obj){
		$j(obj).removeOption(/./);
		
		$j(data).each(function(index){
			var cols = data[index].split('|');
			//$j(obj).addOption(cols[1],cols[0]);
			
			var optn = document.createElement("option");
			optn.text = cols[0];
			optn.value = cols[1];
			if (cols[1]!=undefined){
				$j(obj)[0].options.add(optn);}
		});
	},
	AutoCompleteTextBox:null,	
	SubmitButton:null,
	MessageBox:null,
	SelectedLocationID:0,
	SelectedLocationURL:'',
	SelectedCountryLocationID:function(){
		return this.SelectedCountryLocationIDByName(siteRootLocation);
	},
	SelectedCountryLocationIDByName:function(name){
		var i = null;
		var countries = this.getCountries();
		$j(countries).each(function(index){
			var cols = countries[index].split('|');
			//$j(obj).addOption(cols[1],cols[0]);
			if(cols[0]==name)
			{
				i = cols[1];
				index = 1000;
			}
		});
		return i;
	},
	CacheRegions:true,
	UseFreshCountryData:false,
	isLondonLocation:false,
	SubmitURL:"property/search",
	SubmitMethod:"POST",
	UpdateSubmitButton:function(){
		var msg = "";
		
		if(msg!=""){
			msg = "Please select " + msg;
			$j(this.SubmitButton).hide();
			$j(this.MessageBox).text(msg);
		}else{
			$j(this.MessageBox).hide();
			$j(this.SubmitButton).show();
		}
	},
	remoteURL:"/tmc.system/tmc.services.locations.asp?IncludePromoted=true&IsDeep=true&GetLMSChildLocations=",
	remoteCountryDataURL:"/tmc.system/tmc.services.locations.asp?Country=ActiveLMS",
	EnableMinControls:function(){
		this.AutoCompleteTextBox.disabled = false;		
		this.UpdateSubmitButton();
	},
	getCountries:function(){
		if (this.UseFreshCountryData){
			var dummy = 'Please Select Country|0';
			$j.ajax({
				type: "GET",
				url:this.remoteCountryDataURL,
				async:false,
				beforeSend: function(xhr) {
				},
				success:function(s){
					// var ukLocations = 'England|35283\nScotland|50505\nWales|50535\nNorthern Ireland|50558\n';
					// s = ukLocations + s;
					var ary = s.split('\n').join(':');
					dummy = dummy + ':' + ary;
					//alert(dummy);
				}
			});
			
			return dummy.split(":");
		}else{
			return countries;
		}
	},
	getCurrencies:function(){return defaultCurrencies;},
	getPriceRanges:function(){return ("@label|-1:@value|0:@valuek|25:@valuek|50:@valuek|75:@valuek|100:@valuek|150:@valuek|200:@valuek|300:@valuek|500:@valuek|1000").split(":");},
	getBedRanges:function(){return ("@label|@label:Studio|0:1|1:2|2:3|3:4|4:5|5:6|6:7|7:8|8:9|9:10|10").split(":");}
}

function Init(){
	searchBox.Init();
}

function formatItem(row) {
    //var row = row.split("|");
    //return row[0] + " (id: " + row[1] + ")";
    return row[1].replace(/[/]/gi," &#187 ").replace(/[_]/gi," ");
}

function formatResult(row) {
	return row[0];
}

function isInteger(s) {
  return (s.toString().search(/^-?[0-9]+$/) == 0);
}