// logging function for debugging
// use log(params) instead of alert() or console.log()

if (typeof window.loadFirebugConsole !== "undefined"){
	log = console.log;
}
else {
	log = function() {};
}

//add reverse method to jquery collections
jQuery.fn.reverse = [].reverse;

var contactUsFilters = function () {
	//Clear choices
	Q('select[filter]').val('').change();
	
	//Register the function to run when a select menu
	//with a 'filter' attribute is changed
	//the final .change() runs the filter now
	Q('select[filter]').change(filter).change();
	
	//Start with contacts hidden
	Q('.office_contacts').hide();
	
	//Get details from URL (if any)
	var href = window.location.href.replace('%20', ' ');
	
	var office = href.split('#office=');
	var department = href.split('#department=');
	var name = href.split('#name=');
	
	if (office[1]) {
		log(office[1]);
		Q('#filteroffices').val(office[1]).change();
	}
	if (department[1]) {
		department = department[1];
		log(department);
		department = department.substr(0,1).toUpperCase() + department.substr(1)
		Q('#filterdepartments').val(department).change();
	}
	if (name[1]) {
		log(name[1]);
		var joinedName = name[1].split(' ').join('');
		log(joinedName);
		Q('#filternames').val(joinedName).change();
		
		//log('looking for option[filtride='+ joinedName + ']');

		//Q('#filternames').change();
		//log('selected: ' + Q('#filternames').selectedTexts());
		Q('.contacts').show();
	}
	
	//Register the function to run when a tag with class show is clicked
	Q('a.show').click(function(){
		if(Q(this).siblings('.office_contacts').filter(':hidden').length > 0){
			Q(this).siblings('.office_contacts').show();
		}
		else {
			Q(this).siblings('.office_contacts').hide();
		}
		return false;
	});
};


Q(function(){
	try {
		contactUsFilters();
	} catch (e) { }
	
	setTimeout(function(){
		Q('img[usemap]').maphilight({
			fill: true,
			fillColor: 'ffffff',
			fillOpacity: 0.5,
			stroke: true,
			strokeColor: 'ffffff',
			strokeOpacity: 0.6,
			strokeWidth: 2,
			fade: true,
			alwaysOn: false
		});
	},1000);
	
	Q('map#scotland_imgmap area').click(function(){
		var loc = Q(this).attr('value');
		var value;
		Q('select[name=location] option').each(function(){
			if (Q(this).attr('location') === loc){
				value = Q(this).val();
				return false;
			}
		});
		Q('select[name=location]').val(value);
		return false;
	});
	
	// This is an ugly hack because IE666 doesn't allow the hiding of of option elements within selects
	
	var bl = Q('[name=buy_or_let]');
	if (bl.length) {
	
		blData = {};
		blSelected = {};
		Q('select:not([name=buy_or_let])',Q(bl[0].form)).each(function(index,element){
			// for each select we get all the options
			var name = element.name;
			var options = Q('option', element)
			blSelected[name] = options.filter(':selected').val();
			blData[name] = options.remove();
		});
		
		Q('select[name=buy_or_let]').change(function(){
			var value = Q(this).val();
			if (value === 'buy'){
				Q('select.buy').attr('disabled', null);
			}
			else {
				Q('select.buy').attr('disabled', 'disabled');
			}
			Q('select:not([name=buy_or_let])',Q(this.form)).each(function(index,element){
				var options = blData[element.name].filter(':not([class]), .' + value);
				var firstValue = options.filter(':first').val();
				log(element.name, blSelected[element.name], firstValue);
				Q(element).empty().append(options).val(blSelected[element.name] || firstValue);
				
			});
		}).change();
	}
	
	checkLinks();
	
	checkRedirect();
	
	// this is in order to make the left blue box the right height
	resetHeight();
});

checkRedirect = function(){
	if (window.location.href.indexOf('johnsale.co.uk') !== -1) {
		Q('body').append('<div class="dialog" id="redirection">\
<p align="center"><font color="#000080" size="6" face="Times New Roman"><b>Buccleuch \
John Sale</b></font> <br></p>\
<p align="center"><font size="5" face="Times New Roman">Following the \
acquisition of Buccleuch John Sale by</font> <br></p>\
<p align="center"><font size="6" face="Times New Roman"><b>CKD Galbraith</b></font></p>\
<p align="center"><font size="6" face="Times New Roman"> </font></p>\
<font size="4" face="Times New Roman">\
	<p align="center">You have been redirected to their website.</a> </p>\
	<p align="center"><a onclick=\'dialog.dialog(\"close\")\'>Close this window to continue.</a></p>\
</font>\
</div>\
<iframe src="about:blank"></iframe>');
		var redirection = Q('#redirection');
		window.dialog = redirection.dialog({
			title: 'Close',
			modal:true,
			height:200,
			width:600,
			//reziable: false,
			draggable: false,
			open: function(){
				Q('select').attr('disabled', 'disabled');
				Q('iframe').css({
					'z-index':'221',
					'height': '200px',
					'width': '600px',
					'position': 'absolute',
					'top': Q('.ui-dialog').offset().top,
					'left': Q('.ui-dialog').offset().left,
					'border': '0'
				});
			},
			close: function(){
				Q('select').attr('disabled', null);
				Q('iframe').remove();
			}
		});
	}
};

resetHeight = function(){
	Q('#empty_box')
		.css('height', 0)
		.css('height', 
			Q('#empty_box').height()
			+ Q('#page').height()
			- Q('#left').outerHeight(true)
			+ 'px');
};