// JavaScript Core Document

function external(){
	var l = $$('a');
	l.each(function(el){
		if (el.getProperty('rel')=='external'){
			el.setProperty('target', '_blank');
		}
	});
}

function flash(){
	var movies = $$('.flash');
    movies.each(function(el){
		var container = el.id;
		var id = el.id;
		var target = el.get('rel');
		var dims = el.getSize();
		var width = dims.x;
		var height = dims.y;
		var obj = new Swiff('/_lib/flash/' + target + '.swf', {id: id, width: width, height: height, container: container});
	});
}
function forms(){
	var s = $$('form #sort');
	s.each(function(el){
		el.addEvent('change', function(e){
			if (el.value != 'Please select'){
				el.getParent().getParent().submit();
			}
		});
	});
}
function _getmap(){
	var map_exists = $('gmap');
	if (map_exists){
		window.addEvent('load',function(){
			load('gmap');
		});	
		window.addEvent('unload',function(){
			GUnload();
		});	
	}
}
function homePageForm(){
	if ($('searchform')){
		var radios = $$('#searchform_dbt_1, #searchform_dbt_2');
		radios.each(function(el){
			el.addEvent('click', function(e){
				if(el.checked){
					switch (el.id){
						case 'searchform_dbt_2':
							$('searchform_lop').empty();
							var lop = new Array('Minimum price', '0', '50', '100', '150', '200', '250', '300', '350', '400', '450', '500', '700', '900', '1000', '2000', '2500');
							for(a=0; a<lop.length; a++){
								var newLop = new Element('option');
								newValue = a<1 ? lop[a] : '&pound;' + lop[a] + ' p.c.m.';
								newLop.set('value', lop[a]).set('html', newValue);
								$('searchform_lop').adopt(newLop);
							}
							$('searchform_hip').empty();
							var hip = new Array('Maximum price', '0', '50', '100', '150', '200', '250', '300', '350', '400', '450', '500', '700', '900', '1000', '2000', '2500');
							for(a=0; a<hip.length; a++){
								var newHip = new Element('option');
								newValue = a<1 ? hip[a] : '&pound;' + hip[a] + ' p.c.m.';
								newHip.set('value', hip[a]).set('html', newValue);
								if (a==hip.length-1){
									newHip.setProperty('selected', 'selected');
								}
								$('searchform_hip').adopt(newHip);
							}
							break;
						case 'searchform_dbt_1':
							$('searchform_lop').empty();
							var lop = new Array('Minimum price', '0', '100000', '125000', '150000', '175000', '200000', '225000', '250000', '275000', '300000', '325000', '350000', '375000', '400000', '500000', '600000', '700000', '800000', '900000', '1000000');
							for(a=0; a<lop.length; a++){
								var newLop = new Element('option');
								newValue = a<1 ? lop[a] : '&pound;' + lop[a];
								newLop.set('value', lop[a]).set('html', newValue);
								$('searchform_lop').adopt(newLop);
							}
							$('searchform_hip').empty();
							var hip = new Array('Maximum price', '0', '100000', '125000', '150000', '175000', '200000', '225000', '250000', '275000', '300000', '325000', '350000', '375000', '400000', '500000', '600000', '700000', '800000', '900000', '1000000');
							for(a=0; a<hip.length; a++){
								var newHip = new Element('option');
								newValue = a<1 ? hip[a] : '&pound;' + hip[a];
								newHip.set('value', hip[a]).set('html', newValue);
								if (a==hip.length-1){
									newHip.setProperty('selected', 'selected');
								}
								$('searchform_hip').adopt(newHip);
							}
							break;
					}
				}
			});
		});
	}
}
window.addEvent('domready', function(){
	external();
	forms();
	flash();
	_getmap();
	homePageForm();
});
