$(document).ready(function() {
//alert(language);
var dropDownTimeout = -1;
var galleryImageTimeout = -1;
		
	$(window).resize(function(){												//resize sideBrowsers on window resize
		imageHeight = $('div#overlay div.content img').attr('height');
		holderHeight = $('div#overlay div.content').height();
		dy = Math.ceil((holderHeight-imageHeight)/2);		
		$('div#overlay div.content img').css('margin-top',dy+'px');
	});


	//search box
	$('div#search a').click(function(){
		var link = '/'+language+'/properties/sale/'+$('select#search_type').val()+'/'+$('select#search_location').val()+'/'+$('select#search_areaFrom').val()+'-'+$('select#search_areaTo').val()+'/'+$('select#search_priceFrom').val()+'-'+$('select#search_priceTo').val()+'/';
		window.location=link;
		return false;	
	});
	
	// Ebook Form
	$('a#registerForEbook').click(function(){
		var currentForm=$(this).parent().find('form');
		if (validateForm($(currentForm))==true){
			$.post("_commCenter.php", $(currentForm).serialize(),function(data){
				$('div#ebook').replaceWith(data);
			});	
		}
		return false;
	});

	// Files Form
	$('a#registerForFiles').click(function(){
		var currentForm=$(this).parent().find('form');
		if (validateForm($(currentForm))==true){
			$.post("_commCenter.php", $(currentForm).serialize(),function(data){
				$('div#files').replaceWith(data);
			});	
		}
		return false;
	});
	
	//Inquire Form
	$('a#sendInquiry').click(function(){
		var currentForm=$(this).parent().find('form');
		if (validateForm($(currentForm))==true){
			$.post("_commCenter.php", $(currentForm).serialize(),function(data){
				$('div#inquire a, div#inquire form').remove();
				$('div#inquire').append(data);
			});	
		}
		return false;
	});
	
	//contact form
	$('a#sendMessage').click(function(){
		var currentForm=$(this).parent().find('form');
		if (validateForm($(currentForm))==true){
			$.post("_commCenter.php", $(currentForm).serialize(),function(data){
				$('div#contactForm a, div#contactForm form').remove();
				$('div#contactForm').append(data);
			});	
		}
		return false;
	});
	
	
	//newsletterform
	$('a#registerForNewsletter').click(function(){
		var currentForm=$(this).parent().find('form');
		if (validateForm($(currentForm))==true){
			$.post("_commCenter.php", $(currentForm).serialize(),function(data){
				$('div#newsletterForm a, div#newsletterForm form').remove();
				$('div#newsletterForm').append(data);
			});	
		}
		return false;
	});
	

/*	$('form#message a.button').click(function(){
		if (validateForm($('form#message'))==true){
			$.post("_commCenter.php", $('form#message').serialize(),
				 function(data){
					$('form#message').before(data);
					$('form#message').remove();
				 });
		};
		return false;
	});
	
	$('form#newsletter a.button').click(function(){
		if (validateForm($('form#newsletter'))==true){
			$.post("_commCenter.php", $('form#newsletter').serialize(),
				 function(data){
					$('form#newsletter').before(data);
					$('form#newsletter').remove();
				 });
		};
		return false;
	});

*/
	
	
// gallery 

	
	$('div#gallery a.iconNext,div#gallery a.iconPrev').click(function(){
		var currentPanel = $('div#gallery li').not('.hidden');
		var otherPanel = currentPanel;
		if ($(this).hasClass('iconNext')) {
			if (currentPanel.next().is('li')) {
				otherPanel = currentPanel.next();
			}
		} else if ($(this).hasClass('iconPrev')){
			if (currentPanel.prev().is('li')){
				otherPanel = currentPanel.prev();
			}
		}
		currentPanel.addClass('hidden');
		otherPanel.removeClass('hidden');
		return false;
	});
	
	
	$('div#gallery li a').click(function(){
		a = 'Images/Resized/4ydkc2giz6xnfssj983o_445_350_F_60.jpg';
		var desc = $(this).attr('rev');
		var basename = $(this).attr('rel');
		$('div#gallery div#showArea img').attr('src','Images/Resized/'+basename+'_445_350_F_60.jpg');
		$('div#gallery div#showArea p').text(desc);
		return false;
	});
	
	




//overlay

$('body').append('<div id="overlay"><div class="background"></div><div class="content"></div></div>');

$('div#overlay *').live('click',function(){
	hideOverlay();
});
$('img.overlay').css('cursor','pointer');

$('img.overlay').live('click',function(){
	showOverlay(this);
});


});  //----- end of document.ready------


function showOverlay(caller){
	var physicalName = getPhysicalName($(caller).attr('src'));
	$('div#overlay div.content').append('<img src="Images/Resized/'+physicalName+'_900_900_B_60.jpg" />');
	$('div#overlay div.content img').bind('load',function(){
		$(window).resize();
	});
	$('div#overlay').fadeIn('slow');
	$('div#overlay div.content').fadeIn('slow');
	$('div#overlay div.background').fadeTo('normal',0.5);
	
}

function hideOverlay(){
	$('div#overlay div.background').fadeOut('fast');
	$('div#overlay').fadeOut('fast');
	$('div#overlay').fadeOut('fast',function(){
		$('div#overlay div.content *').remove();
	});

}

function getPhysicalName(src){
return src.split('/')[src.split('/').length-1].split('_')[0];
}

function validateForm(form){
	cLog('validateForm called');
	var validForm = true;
	$(form).find('input[type=text], textarea').each(function(){
		if (validField($(this)) == false) {
			validForm = false;
		}
	});
	
	cLog('validateForm returned '+validForm);
	return validForm;
}


function validField(field){
	cLog ('validField called');
	var valid = true; //assume field is ok
	if ($(field).attr('type')=='text' || $(field).is('textarea')) { //Fix for textarea
		var value = $(field).val();
		cLog ('value of field is: '+value);
		if ($(field).hasClass('valEmail')){
			cLog ('checking for valEmail');
			var pattern = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
			if (!pattern.test(value)) {
				valid = false;
				cLog ('check failed');
			}
		}
			
		if ($(field).hasClass('valFullName')){
			cLog ('checking for valFullName');
			var pattern = /^\w+ \w+[ \w]*$/;
			if (!pattern.test(value)) {
				valid = false;
				cLog ('check failed');
			}
		}
			
		if ($(field).hasClass('valPhone')){
			cLog ('checking for valPhone');
			var pattern = /^\+?[\d- ]{5,}?$/;
			if (!pattern.test(value)) {
				valid = false;
				cLog ('check failed');
			}
		}			
			
		if ($(field).hasClass('valMandatory')){
			cLog ('checking for valMandatory');
			if (value == null || value == ''){
				valid = false;
				cLog ('check failed');
			}
		} else {
			if (value == null || value == ''){
				valid = true;
			}
		}								
	}
	if (valid == true){
		cLog ('adding class Hidden');
		$(field).parent().find('span.formError').addClass('hidden');
	} else {
		cLog ('removing class Hidden');
		$(field).parent().find('span.formError').removeClass('hidden');
	}
	cLog ('validField returned '+valid);
	return valid;
}








function cLog(logData){
	if (typeof console !== 'undefined'){
		console.log(logData);
	}
	return false;
}
