/**
 * lh.js
 * @author Lewis Howles (lh)
 *
 * Default niceties and other Voodoo.
 */

var lh = {
	init : function() {
		(searchQuery.config.form.length || searchQuery.config.resultsFrame.length) && searchQuery.init();
		lh.externalLinks();
		lh.highlighter();
		lh.styleSwitcher();
		if($("#images").length || $("#images2").length)
		lh.imageFader();		
	},
	
	/*
	 * Set target blank on external links
	 */
	externalLinks : function() {
		$('a[rel~="external"]')
			.attr('target', '_blank')
	},
	
	highlighter : function() {
		$('form.fancy')
			.delegate('input, select, textarea', 'mouseover focusin mouseout focusout', function(e) {
				var isIn = (e.type === 'mouseover' || e.type === 'focusin');
				
				lh.toggleFocus($(this), isIn);
			});
	},
	
	toggleFocus : function(hovered, modifier) {
		hovered.parent('li').not('.li-submit').toggleClass('focus', modifier);
	},
	
	styleSwitcher : function() {
		styleSwitcher = $('#style-switcher').find('a');
		
		styleSwitcher
			.data('sheet','global')
			.bind('click', function(){
				var params = (styleSwitcher.data('sheet') == 'global') ? ['reader','Visual Mode'] : ['global','Reader Mode'];
				$('link[rel=stylesheet]')
					.eq(1) // 0-indexed position of stylesheet. Change this is you had to move global.
					.attr('href', 'css/'+params[0]+'.css');
				styleSwitcher.data('sheet',params[0]);
				styleSwitcher.text(params[1]);
			});
	},
	
	imageFader: function() {	
		$('#images').innerfade({
			speed: 1000,
			timeout: 6000,
			containerheight: '192px'
		});
		$('#images2').innerfade({
			speed: 1000,
			timeout: 6000,
			containerheight: '189px'
		});
	}
}

var searchQuery = {
	config : {
		sale : {
			"50000" : "&pound;50,000",
			"75000" : "&pound;75,000",
			"100000" : "&pound;100,000",
			"125000" : "&pound;125,000",
			"150000" : "&pound;150,000",
			"175000" : "&pound;175,000",
			"200000" : "&pound;200,000",
			"250000" : "&pound;250,000",
			"300000" : "&pound;300,000",
			"400000" : "&pound;400,000",
			"500000" : "&pound;500,000",
			"750000" : "&pound;750,000",
			"1000000" : "&pound;1,000,000",
			"1500000" : "&pound;1,500,000"
		},
		
		rent : {
			"250" : "&pound;250",
			"500" : "&pound;500",
			"750" : "&pound;750",
			"1000" : "&pound;1000",
			"1250" : "&pound;1250",
			"1500" : "&pound;1500",
			"2000" : "&pound;2000"
		},
		
		form : $('#property-search'),
		
		text : $('#txtQuickSearch'),
		
		contactTypeSelect : $('#contract'),
		
		contractTypeRadio : $('input[name=contract]'),
		
		minimumPrice : $('#ddlPayMin'),
		
		maximumPrice : $('#ddlPayMax'),
		
		resultsFrame : $('#results-frame')
	},
	
	init : function(config) {
		(config && typeof(config) == 'object') && $.extend(lh.config, config);
		
		var form = searchQuery.config.form,
			text = searchQuery.config.text,
			minimumPrice = searchQuery.config.minimumPrice,
			maximumPrice = searchQuery.config.maximumPrice,
			contractTypeSelect = searchQuery.config.contactTypeSelect,
			contractTypeRadio = searchQuery.config.contractTypeRadio;
		
		if (form.length) {
			var contracts = [contractTypeSelect, contractTypeRadio];
			$.each(contracts, function(k,v) {
				if (v.length) {
					var type = (k == 0) ? 'select' : 'radios';
					
					searchQuery.updateCombo(v, type);
					
					v.change(function() {
						searchQuery.updateCombo(v, type)
					});
				}
			});
			
			form.submit(function() {
				text.val(function(i, v){
					return (v === text.attr('title')) ? '' : v;
				})
			});
		}
		
		searchQuery.config.resultsFrame.length && searchQuery.getValues();
	},
	
	updateCombo : function(contract, type) {
		var	options = '',
			minimumPrice = searchQuery.config.minimumPrice,
			maximumPrice = searchQuery.config.maximumPrice,
			minParent = searchQuery.config.minimumPrice.parent(),
			maxParent = searchQuery.config.maximumPrice.parent(),
			sale = searchQuery.config.sale,
			rent = searchQuery.config.rent,
			values = (type == 'select') ? (values = (contract.val() == 'buying') ? sale : rent) : (values = (contract.filter('input:checked').val() == 'buying') ? sale : rent);
		
		// To keep values neat & readable, create options here
		$.each(values, function(val, text) {
			options += '<option value="'+val+'">'+text+'</option>';
		});
		
		var prices = [minimumPrice, maximumPrice];
		$.each(prices, function(k,v){
			v.empty().detach();
			
			(k == 0) ? v.append('<option value="0">Min Price</option>') : v.append('<option value="0">Max Price</option>');
			
			v.append(options).val('0');
		});
		
		minParent.append(minimumPrice);
		maxParent.append(maximumPrice);
	},
	
	getValues : function() {
		var allVars = $.getUrlVars(),
			chainID = allVars["chainID"],
			txtQuickSearch = allVars["txtQuickSearch"],
			contract = allVars["contract"],
			ddlBedrooms = allVars["ddlBedrooms"],
			ddlStyle = allVars["style1"],
			ddlPayMin = allVars["ddlPayMin"],
			ddlPayMax = allVars["ddlPayMax"],
			ddlResultsOrder = allVars["ddlResultsOrder"],
			countries = allVars["countries"],
			resultsFrame = searchQuery.config.resultsFrame;
		
		resultsFrame.attr('src', function(i, a) {
			if (a == '')
				return 'http://search.lonsdales-estates.co.uk/resultslite.aspx?chainID='+chainID+'&Contract='+contract+'&ddlBedrooms='+ddlBedrooms+'&ddlPayMin='+ddlPayMin+'&ddlPayMax='+ddlPayMax+'&txtQuicksearch='+txtQuickSearch+'&ddlResultsOrder='+ddlResultsOrder+'&countries='+countries+'&style1='+ddlStyle;
			else
				return a;
		});
	}
}

// Get URL Parameters
$.extend({
  getUrlVars: function(){
	var vars = [], hash;
	var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
	for(var i = 0; i < hashes.length; i++)
	{
	  hash = hashes[i].split('=');
	  vars.push(hash[0]);
	  vars[hash[0]] = hash[1];
	}
	return vars;
  },
  getUrlVar: function(name){
	return $.getUrlVars()[name];
  }
});

$(lh.init());
