$(document).ready(function(){

	$("a.email").each(function(){ //Email address obfuscation
		e = this.rel.replace("/","@");
		this.href = "mailto:"+e;
		$(this).text(e);
	});

	$("a.external").click(function(){ //Open link in new window
		window.open(this.href);
		return false;
	});

	$(".rollover").hover( //Image rollovers
		function(){
			if($(this).attr("src").indexOf("-over")==-1) {
				var newSrc = $(this).attr("src").replace(".gif","-over.gif");
				newSrc = newSrc.replace(".jpg","-over.jpg");
				newSrc = newSrc.replace(".png","-over.png");
				$(this).attr("src",newSrc);
			}
		},
		function(){
			if($(this).attr("src").indexOf("-over")!=-1) {
				var oldSrc = $(this).attr("src").replace("-over.gif",".gif");
				oldSrc = oldSrc.replace("-over.jpg",".jpg");
				oldSrc = oldSrc.replace("-over.png",".png");
				$(this).attr("src",oldSrc);
			}
		}
	);
	
	$(".input-text").each ( //Define default text for each input element
		function() {
			this.rel=this.value;
		}
	);

	$(".input-text").focus(function() {
		if (this.value==this.rel) {
			this.value='';
		}
	});

	$(".input-text").blur(function() {
		if (this.value=='') {
			this.value=this.rel;
		}
	});
	
	/* Tooltip */
	$("#header *, #footer *").tooltip({ 
		track: true,
		delay: 0,
		showURL: false,
		opacity: 1,
		fade: 150,
		top: -15,
		left: 5
	});
	
	$("#portfolio dl dd:eq(0) a, #related dl dd:eq(0) a").tooltip({ 
		track: true,
		delay: 0,
		showURL: false,
		opacity: 1,
		fade: 150,
		top: -25,
		left: 5,
		extraClass: "portfolio"
	});
	
	$("#bridal .imageset .image a").tooltip({ 
		track: true,
		delay: 0,
		showURL: false,
		opacity: 1,
		fade: 150,
		top: -25,
		left: 5,
		extraClass: "bridal"
	});
	
	$('#bridal .imageset .image a').each(function() {
		var $this =$(this),
			title =$this.children('img').attr('title');
		
		$this.fancybox({
			'padding'		: 0,
			'autoScale'		: true,
			'overlayOpacity': 0.8,
			'overlayColor'	: '#000',
			'transitionIn'	: 'fade',
			'transitionOut'	: 'fade',
			'speedIn'		: 600,
			'speedOut'		: 600,
			'width'			: 640,
			'height'		: 480,
			title: $(this).children('img').attr('title'),
			titleShow: true,
			titlePosition: 'outside',
			titleFormat: formatTitle

		});
	});

	
	/* Portfolio rollover */
	$("#portfolio dl, #related dl").hover(
		function(){
			$("span",this).fadeIn("fast");
		},
		function(){
			$("span",this).fadeOut("fast");
		}
	);
	
	/* Bridal rollover */
	$("#bridal .imageset .image").hover(
		function(){
			$("span",this)
						.stop()
						.fadeIn("fast");
		},
		function(){
			$("span",this)
						.stop()
						.fadeOut("fast");
		}
	);
	
	/*Bridal boxes*/
	$('#bridal .panel:eq(1)').height($('#bridal .panel:eq(0)').height());
	
	/*Bridal form placeholders*/
	jQuery.support.placeholder = false;
	test = document.createElement('input');
	if('placeholder' in test) jQuery.support.placeholder = true;
	if(!jQuery.support.placeholder) {
		$('input,textarea').each(function() {
			var $this =$(this),
				placeholder =$this.attr('placeholder');
			if(!$this.val()) $this.val(placeholder);
			$this.focus(function() {
				if($this.val()==placeholder) $this.val('');
			});
			
			$this.blur(function() {
				if(!$this.val()) $this.val(placeholder);
			});
			
		});
		
	}
	
	/*Bridal date picker*/
	$('#bridal form #form-date').datepicker({
		dateFormat: 'dd/mm/yy',
		numberOfMonths: 2,
		changeYear: true,
		yearRange: 'c:c+2'
	});
	
	/* Kopek video */
	$("a.kopek").click(function() {
		$.fancybox({
			'padding'		: 0,
			'autoScale'		: false,
			'overlayOpacity': 0.8,
			'overlayColor'	: '#000',
			'transitionIn'	: 'fade',
			'transitionOut'	: 'fade',
			'speedIn'		: 600,
			'speedOut'		: 600,
			'width'			: 640,
			'height'		: 480,
			'href'			: this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
			'type'			: 'swf'
			
		});

		return false;
	});
	
	/* Contact form validation */
	$("#contact-form").submit(
		function() {
			var error=false;
			var response="There was a problem submitting the form:\n";
			
			var message=$("#message",this).val();
			var name=$("#name",this).val();
			var email=$("#email",this).val();
		
			if (!isString(message) || message=="Type your message") {
				error=true;
				response+="Please enter a message\n";
			}
			
			if (!isString(name) || name=="Your Name") {
				error=true;
				response+="Please enter your name\n";
			}
			
			if (!isEmail(email) || message=="Your Email") {
				error=true;
				response+="Please enter your email address\n";
			}
			
			if (error) {
				alert(response);
				return false;
			}
		}
	);
	
});

/* Data type validation */
function isString(str) {
	if (str.length!="") {
		return true;
	} else {
		return false;
	}
};

function isEmail(str) { //Email address
	var emailRegExp="^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$";
	var regex=new RegExp(emailRegExp);
	return regex.test(str);
};

function formatTitle(title, currentArray, currentIndex, currentOpts) {
    return '<div class="image-title">' + title + '</div>';
}

function bridalForm() {
	
	var data ={
		date: $('#form-date').val(),
		venue: $('#form-location').val(),
		name: $('#form-name').val(),
		email: $('#form-email').val(),
		notes: $('#form-details').val()
	},
		err =new Array();
	
	$('form .error').removeClass('error');
	
	if(!data.date || data.date==$('#form-date').attr('placeholder')) {
		err.push('Please provide a date');
		$('#form-date').addClass('error');
	}
	if(!data.venue || data.venue==$('#form-location').attr('placeholder')) {
		err.push('Please provide a location');
		$('#form-location').addClass('error');
	}
	if(!data.name || data.name==$('#form-name').attr('placeholder')) {
		err.push('Please enter your name');
		$('#form-name').addClass('error');
	}
	if(!isEmail(data.email)) {
		err.push('Please enter a valid email address');
		$('#form-email').addClass('error');
	}
	if(err.length>0) {
		alert(err.join('\n'));
	}else{
		$.post('/bridal/bridal-form.php', data, function(r) {
			if(r==='1') {
				$('#bridal-form').hide('slide', {direction: 'up'}, 500, function() {
					$('#bridal-form').html('<h4>Your Message Has Been Sent</h4><p>Thanks for getting in touch. Ashley will get back to you in the next working day about your request.</p><a href="/bridal" id="bridal-reload">Check another date with Ashley</a>');
					$('#bridal-form').show('slide', {direction: 'up'}, 500);
				});
			} 
		},'text');
	}
	
}

