//my JavaScript things

var contactObj = { 'name': false, 'email': false, 'message':false }, 
	cName, 
	cEmail, 
	cMsg, 
	cGender, 
	cCheck, 
	cCont;

$(document).ready(function() {
	init();
	interaction();
	ie6()
});

function init(){
	$('#gallery a').lightBox();
	
	if(typeof gallerywide == 'number' ){
		$('#content').css('width', '626px');
	}

	$('.shop-address').find('.hide').hide();
	$('.shop-address:first').find('.hide').show();
	
	jQuery.each($('.input'), function(){
		if($(this).val()){
			$(this).prev('.example').animate({opacity: 0}, 10);
		}
	});
	
	$('.error').animate({opacity: 0}, 10);
	
	$('#return-message-succes').hide();
	$('#return-message-fail').hide();
	$('#return-messages').hide();
}

function interaction(){
	//General
	formSubmit();
	//Shops
	$('.shop-address').click(function(e){
		e.preventDefault();
		$('.shop-address').find('.hide').hide('fast');
		$(this).find('.hide').toggle('fast');
		$('#maps iframe').attr({src: 'http://maps.google.nl/maps?f=q&source=s_q&geocode=&q=' + urls[$(this).attr('id')] + '&output=embed'})
		$('#maps a').attr({href: 'http://maps.google.nl/maps?q=' + urls[$(this).attr('id')]})
	});
	
	//Contact
	$(".input").focus(function () {
		$(this).prev('.example').animate({opacity: 0}, 250);
		$(this).next('.error').animate({opacity: 0}, 250);
	});
	$('#name .input').blur(function(){
		if( $('#name .input').val() ){
			contactObj['name'] = true;
			$('#name .error').animate({opacity: 0}, 250);
			$('#name .example').animate({opacity: 0}, 250);
		}else{
			contactObj['name'] = false;
			$('#name .error').animate({opacity: 1}, 250);
			$('#name .example').animate({opacity: 1}, 250);
		}
	});
	$('#email .input').blur(function(){
		if( $('#email .input').val() ){
			if( /^([\w]+)(.[\w]+)*@([\w-]+\.){1,5}([A-Za-z]){2,4}$/.test( $('#email .input').val() ) ){
				contactObj['email'] = true;
				$('#email .error').animate({opacity: 0}, 250);
			}else{
				contactObj['email'] = false;
				$('#email .error').animate({opacity: 1}, 250);
			}
			$('#email .example').animate({opacity: 0}, 250);
		}else{
			contactObj['email'] = false;
			$('#email .error').animate({opacity: 1}, 250);
			$('#email .example').animate({opacity: 1}, 250);
		}
	});
	$('#message .input').blur(function(){
		if( $('#message .input').val() ){
			contactObj['message'] = true;
			$('#message .error').animate({opacity: 0}, 250);
			$('#message .example').animate({opacity: 0}, 250);
		}else{
			contactObj['message'] = false;
			$('#message .error').animate({opacity: 1}, 250);
			$('#message .example').animate({opacity: 1}, 250);
		}
	});
}

function formSubmit(){
	$('#form').submit(function(event){
		event.preventDefault();
		
		cName = $("#name-input").val();
		cEmail = $("#email-input").val();
		cMsg = $("#message-input").val();
		cGender = $("#gender-input").val();
		cCheck = '[--!@#$%--]';
		cCont = 0;
		
		if(cName !== "" && contactObj['name']){ cCont++ }else{ $('#name .error').animate({opacity: 1}, 250); return};
		if(cEmail !== "" && contactObj['email']){ cCont++ }else{ $('#email .error').animate({opacity: 1}, 250); return};
		if(cMsg !== "" && contactObj['message']){ cCont++ }else{ $('#message .error').animate({opacity: 1}, 250); return};
		if(cGender == ""){ cCont++ }else{ return};

		if(cCont == 4){
			$.post('ajax/ajax.php', { 
				name: cName, 
				email: cEmail,
				msg: cMsg,
				gender: cGender,
				check: cCheck},
				function(data){
					$('#form').hide();
					$('#mailto').hide();
					$('#return-messages').show();
					if(data == 1){
						$('#return-message-succes').show();
					}else{
						$('#return-message-fail').show();						
					}
				});
		}else{
			formSubmit();	
		}
	});
};

function ie6(){
	$('#close').click(function(e){
		e.preventDefault();
		$('#ie6-warning').hide('fast');
	});
}
