$(function(){
	$('.green_overlay').hide();
	$('.corner_arrow_link').hide();
	$('.blank').hide();

	

	//CYLCE SLIDESHOW HOMEPAGE
	if($('#homeSlider').length > 0){
		$('#homeSlider').cycle({ 
		    fx:     'scrollVert', 
		    speed:  1000, 
		    timeout: 10000, 
		    pager:	'#homeSliderNav',
		    pauseOnPagerHover:	true,
		    before: function(curr, nex, opts, forFlag){
		    	$('.slideSummary').hide();		
		    },
		    after: function(curr, nex, opts, forFlag){
		    	var pHeight = $(this).children('.slideCopy').height();
		    	var cSlide = opts.currSlide;
		    	$(this).children('.slideCopy').animate({'width':440, 'height': 440}, 1000, function(){
		    		$('.slideSummary').fadeIn(500);		
		    		setTimeout("hideSlideSummary("+cSlide+", "+pHeight+")",6500);
		    	});
		    }
		});
	}
		
	//CHURCH DIRECTORY
	$('a.church_dir_grid_thumb').hover(function(){
		$(this).prev('.green_overlay').show();
		$(this).next('.corner_arrow_link').show();	
	}, function(){
		$(this).prev('.green_overlay').hide();
		$(this).next('.corner_arrow_link').hide();
		return false;
	});
	
	$('.church_dir_grid_h3').hover(function(){
		$(this).prev().prev().prev('.green_overlay').show();
		$(this).prev('.corner_arrow_link').show();
	}, function(){
		$(this).prev().prev().prev('.green_overlay').hide();
		$(this).prev('.corner_arrow_link').hide();
	});

	//QUICKSEARCH
	$('ul#church_directory_list li').quicksearch({
  		position: 'after',
  		attached: '#quick_search',
  		labelText: 'Enter a Church Name'
	});
	//END QUICKSEARCH

	//LOG IN FORM
	$('a[name=modal]').click(function(e) {
		e.preventDefault();
		var id = $(this).attr('href');
		var maskHeight = $(document).height();
		var maskWidth = $(window).width();
		$('#mask').css({'width':maskWidth,'height':maskHeight});		
		$('#mask').fadeIn(1000).fadeTo("slow",0.5);	
		var winH = $(window).height();
		var winW = $(window).width();
		$(id).css('top',  winH/2-$(id).height()/2);
		$(id).css('left', winW/2-$(id).width()/2);
		$(id).fadeIn(2000); 
	});
	$('.window .close').click(function (e) {
		e.preventDefault();
		$('#mask').fadeOut(600);
		$('.window').fadeOut(300);
	});		
	$('#mask').click(function () {
		$(this).fadeOut(600);
		$('.window').fadeOut(300);
	});		
	//LOG IN FORM
	
	//SIGN UP FORM
	$('a[name=modal]').click(function(e) {
		e.preventDefault();
		var id = $(this).attr('href');
		var maskHeight = $(document).height();
		var maskWidth = $(window).width();
		$('#mask').css({'width':maskWidth,'height':maskHeight});		
		$('#mask').fadeIn(1000).fadeTo("slow",0.5);	
		var winH = $(window).height();
		var winW = $(window).width();
		$(id).css('top',  winH/2-$(id).height()/2-100);
		$(id).css('left', winW/2-$(id).width()/2);
		$(id).fadeIn(2000); 
	});
	
	$('#accountType').change(function(){
		var $_this = $(this).val();
		if($_this == "Church"){
			$('#priceBreaks').show();
			$('#otherDescription').hide();
			$()
		} else if($_this == "Other"){
			$('#otherDescription').show();
			$('#priceBreaks').hide();
		} else {
			$('#priceBreaks, #otherDescription').hide();
		}
	});
	
	$('#sign_up_form').validate({
		rules: {
			name: {
				required: true,
				minlength: 3
			},
			address: {
				required: true,
				minlength: 3
			},
			post_code: {
				required: true,
				minlength: 3
			},
			number: {
				required: true,
				minlength: 5,
				number: true
			},
			email: {
				required: true
			},
			applicant: {
				required: true
			},
			terms: {
				required: true
			}
		},
		messages: {
		    name: {
				required: "Please enter your name",
		    	minlength: "Please enter 3 or more characters"
			},
			address: {
				required: "Please enter your address",
				minlength: "Please enter 3 or more characters"
			},
			post_code: {
				required: "Please enter your post code",
				minlength: "Please enter 3 or more characters"
			},
			number: {
				required: "Please enter a contact number",
				minlength: "Please enter 5 or more characters",
				number: "Please enter only digits 0-9"
			},
			email: {
				required: "Please enter your email"
			},
			applicant: {
				required: "Please select either Church or CU"
			},
			terms: {
				required: "Please tick to confirm you have read the Terms and Conditions"
			}
		},
		submitHandler: function() {
			$('#sign_up_form').ajaxSubmit({
				url: 'http://www.dontjustsurvive.co.uk/scripts/sign_up.php',
				success: function(response) {
					$('#formResults').html(response);
        }
     	});
		}
	}); 	
	//SIGN UP FORM
	
	//EDIT PROFILE
	$(".accept").hide();
	
	$(".form_action").hide();
		$('.input_text').click(function(){
		$(this).nextAll('.accept').show();
		$(this).css("background-color", "#acdda7");
	});	
		
	//NEW PROFILE
	 $('#new_profile_form').validate({
		rules: {
			new_title: {
				minlength: 2,
				required: true
			}
		},
		messages: {
		    new_title: {
		    	minlength: "Please enter 2 or more characters",
		    	required: "Please enter your Church Name"
		    }
		},
		submitHandler: function() {
			$('#new_profile_form').ajaxSubmit({
				url: 'http://www.dontjustsurvive.co.uk/scripts/new_profile.php',
				success: function() {
                    $('#title_submit').hide();
                    $('#title').css("background-color", "#d6f5d3");
                    $('#title').nextAll('.form_action').show().text("Saved");
                    $('#title').nextAll('.form_action').fadeOut(4000);
                 	$('#new_profile_form').html('<h3><a href="http://www.dontjustsurvive.co.uk/index.php/edit_profile" title="Create Your Profile">Click here</a> to begin creating your profile</h3>');
                }
           	});
		}
     });
	 // END NEW PROFILE	
	 
	 //PrettyPhoto
	$("a[rel^='prettyPhoto']").prettyPhoto();
	 
	 //OUTBOUND LINKS
	 $("a[href^=http]").each(function(){
        if(this.href.indexOf(location.hostname) == -1) {
        	$(this).attr('target', '_blank');
      	}
    });
});

function hideSlideSummary(n, h){
	$('.homeslide:eq('+n+') .slideSummary').fadeOut(500, function(){
		$('.homeslide .slideSummary').eq(n).hide();
		$('.homeslide').eq(n).children('.slideCopy').animate({'width':350, 'height': h}, 1000);
	});
}
