/*
	Carrigan Farms 2011
	Author : J. Johnson | www.systemsevendesigns.com
*/

$(function(){
	
	/*
	 * 	Handle the header login form for behind the veil and davidson events
	 * 
	 */
	
	$form = $('#protected_login_form');
	
	$('#protected_login li a').each(function(i,item){
		$link = $(item);
		
		$link.bind('click',function(){
			$this = $(this);
			
			$('#protected_login li a').removeClass('selected');
			$('#protected_login input').val('');
			$('#protected_login #message').remove();
			$this.addClass('selected');
			$form.find('.submit').attr('href',$this.attr('href'));
			
			$form.fadeOut(function(){
				$(this).fadeIn();
			});
			
			return false;
		})
		
	});
	
	$('#protected_login .submit').bind('click',function(){
		$('#protected_login #message').remove();
		$('#protected_login .ajax-loader').removeClass('hide');
		
		$.ajax({
			type : 'POST',
			dataType : 'json',
			url : $(this).attr('href'),
			data : {ajaxRequest : true, username : $('#protected_login #username').val(), password : $('#protected_login #password').val()},
			success : function(response){
				$('#protected_login .ajax-loader').addClass('hide');
				if(response.result)
				{
					window.location.href = response.redirect;
				}else{
					$('#protected_login_form .form_definition').before('<div id="message" class="error">'+response.message+'</div>');
				}
			}
		});		

		return false;
	});
	
	$('body').bind('click.hideLogin', function(event){
	   if (!$(event.target).parents('#protected_login_form').length){
	    	$('#protected_login li a').removeClass('selected');
	    	$('#protected_login #message').remove();
	    	$form.fadeOut();
	   }
	});

	
	/*
	 * 	Handle the photo gallery overlay
	 * 
	 */	
if($('a[rel]').length){	
	$("a[rel]").overlay({
		mask: {
			color: '#000',
			loadSpeed: 200,
			opacity: 0.8
		},
		onBeforeLoad: function() {
			var wrap = this.getOverlay().find(".galleryContent");
			wrap.load(this.getTrigger().attr("href"));
		},
		onLoad : function()
		{
			$(".slideTabs").tabs("#slider > img", {
				effect: 'fade',
				fadeOutSpeed: "slow",
				fadeInSpeed : 'slow',
				rotate: true
			}).slideshow({
				autoplay : true
			});
		}

	});

	/*if we want to pop the gallery on page load (wedding page only)*/
	if($('body.wedding.show-gallery').length){
		$("a[rel]:first").trigger('click');
	}
	if($('body.quarry.show-gallery').length){
		$("a[rel]:last").trigger('click');
	}	
}
	
});

