// JavaScript Document

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

// JavaScript Document

function startforms(){
	var forms = $$('form');
	forms.each (function(el){
		var req_fields = $$('.req', el);
		req_fields.each (function(el){
			el.addEvent('blur', function(e){
				Error = verifyField(el.id);
			});
		});
		el.addEvent('submit', function(e){
			if (!validateForm(el.id)){
				e = new Event(e);
				e.stop();
			}
		});
	});	
}


function verifyField(id){
	error = '';
	type = '';
	rel_content  = '';
	rel_content = $(id).getProperty('rel');
	if (rel_content){
		if (rel_content.indexOf('email') != -1){
			type = 'email';
		}
	}
	switch (type){
		case 'email':
			var value = $(id).value;
			apos = value.indexOf("@");
			dotpos = value.lastIndexOf(".");
			if (apos<1 || dotpos-apos<2){
				error += rel_content + '\r\n';
			}
			break;
		default:
			var value = $(id).value;
			if (!value){
				if (rel_content){
					error += rel_content + '\r\n';
				} else {
					error = true;
				}
			}
			break;
	}
	var p = $(id).getParent();
	if (error){
		p.addClass ('invalid');
		p.removeClass('valid');
		/*var div = new Element('div');
		div.appendText('Error! Please complete this field!');
		div.injectAfter($(id));*/
	} else {
		p.addClass('valid');
		p.removeClass('invalid');
	}
	return error;
}

function validateForm(id){
	main_error = '';
	firstError = '';
	var fields = $$('form#' + id + ' .req');
	for (a=0; a<fields.length; a++){
		Return = verifyField(fields[a].id)
		if (Return){
			main_error += Return;
			firstError = firstError ? firstError : fields[a].id;
		}
	}
	if (main_error){
		$(firstError).focus();
		alert (main_error);
		return false;
	}
	return true;
}
function _getmap(){
	var map_exists = $$('#gmap');
	if (map_exists.length > 0){
		window.addEvent('load',function(){
			load('gmap');
		});	
		window.addEvent('unload',function(){
			GUnload();
		});	
	}
}

function warnings(){
	var w=$$('.warning_notice');
	if (w.length>0){
		w.each(function(el){
			var c=el.getProperty('rel');
			alert (c);
		});
	}
}

function db_functions(){
	removes();
}

function removes(){
	var remove = $$('td.remove a');
	remove.each(function(el){
		el.addEvent('click', function(e){
			el.getParent().getParent().addClass('remove');
			var conf = confirm ('Are you sure you wish to remove this item?\r\n Click \'OK\' to proceed...');
			if (!conf){
				el.getParent().getParent().removeClass('remove');
				var e=new Event(e);
				e.stop();
			}
			return true;
		});
	});
}

var image_rotator = function(id){
	var images = $$('#' + id + ' img');
	for (a=0; a<images.length; a++){
		if (images[a].getStyle('opacity') == 100){
			var current_img = a;
		}
	}
	alert (current_img);
}

function hide_properties(id){
	var images = $$('#' + id + ' img');
	for (a=1; a<images.length; a++){
		images[a].setStyle('opacity',0);
	}
}

function init(){
	hide_properties('f_property');
	image_rotator('f_property');
	var init_rotator = image_rotator.create({
		periodical: 5000,
		arguments: 'f_property'
	});
}

function match_row_dims(){
	var rows = $$('.row');
	rows.each (function(el){
		var titles = $$('#' + el.id + ' .prop_top');
		var max_height = 0;
		for(a=0;a<titles.length;a++){
			this_height = titles[a].getSize()['y'];
			max_height = this_height > max_height ? this_height : max_height;
		}
		titles.each (function(el2){
			el2.setStyle('min-height', max_height);
		});
		var listings = $$('#' + el.id + ' .prop_list');
		var max_height = 0;
		for(a=0;a<listings.length;a++){
			this_height = listings[a].getSize()['y'];
			max_height = this_height > max_height ? this_height : max_height;
		}
		listings.each (function(el2){
			el2.setStyle('min-height', max_height);
		});
	});
}

function imageswap(){
	if ($('property_image')){
		var subimages = $$('#sub_images a');
		subimages.each(function(el){
			el.addEvent('click',function(e){
				$('property_image').src = el.href;
				var e = new Event(e);
				e.stop();
			 });
		});
	}
}

function searchbox(){
	if ($('searchbox')){
		var target = $('searchbox');
		$('postcode').focus();
		//target.setStyle('height',0);
	}
}
function showsearchbox(){
	if ($('showhide_searchbox')){
		$('showhide_searchbox').addEvent('click',function(e){
			var target = $('searchbox');
			if (target.getStyle('display') == 'block'){
				target.setStyles({'display': 'none'});
			}else{
				target.setStyles({'display': 'block'});
			}
		});
	}
}

window.addEvent('domready', function(){
	externalLinks();
	_getmap();
	startforms();
	warnings();
	db_functions();
	match_row_dims();
	imageswap();
	searchbox();
	showsearchbox();
	//init('f_property');
});