/* FORM EFFECTS */

$(function() {

	// Expand Items
	$(".read_more").toggle(
		function() {
			var article =  $(this).parent().parent().parent();					
			// Hide Visisble Text
			article.find(".shown").hide();
			// Fade in other object
			article.find(".hidden").fadeIn("slow");
			article.find(".slide_gallery").fadeIn("slow");
			
			$(this).html("Read less");
		},
		function() {
			var article =  $(this).parent().parent().parent();					
			// Hide Visisble Text
			article.find(".shown").fadeIn("slow");
			// Fade in other object
			article.find(".hidden").hide();
			article.find(".slide_gallery").hide();
			
			$(this).html("Read more");
		}
	);
	
	$(".slide_panel a").lightBox({fixedNavigation:true});
	//$("[class*=wp-image]").lightBox({fixedNavigation:true});
	//$(".quote_list").hide();
	$(".testimonials").hide();
	
	// be_hidden show and hide effect
	$(".be_footer a, .be_footer img").click(function() { 
		$(".be_hidden:visible").hide("blind");
		$(this).parent().parent().children(":hidden").show("blind");
	});
	
	// pp_hidden show and hide effect
	$(".pp_footer a").click(function() {
		$(".pp_hidden:visible").hide("blind");
		$(this).parent().parent().parent().children(":hidden").show("blind");
	});
	
	// run the currently selected effect
	function runEffect(id){
		$("#effect.effect_" + id).show("blind",500);
	}
	
	// callback function to bring a hidden box back
	function callback(id){
		$("#effect.effect_" + id + ":visible").removeAttr('style').hide().fadeOut();
	}
	
	function hideEffect(id) {
		var options = {};
		id.hide("blind",500);
	}
	
	// apply click rules for all buttons
	$("#button[class*='button']").click(function() {
		var options = {};
		
		if($(this).text() == "hide") { //hide stuff
			$("[class*='effect']:visible").each(function() { 
				$(this).removeAttr('style').hide("blind",500).fadeOut();
			});
			$("#button[class*='button']").text("read more");
		}
		else {
			$("#button[class*='button']").text("read more");
			$(this).text("hide");
			for(j=1; j <= 4; j++) {
				// if its this button, run effect on j
				if($(this).hasClass("button_" + j)){
					
					
					// hide all
					$("[class*='effect']:visible").each(function() {$(this).removeAttr('style').hide("blind",500).fadeOut();});
					
					// only show if not shown
					runEffect(j);
					
					return false;
				}
			}
		}
	
		// look through until you know what button it is
		
		return false;
	});
	
	// hide all expanding divs
	$("[class*='effect']:visible").each(function(){ 
		$(this).hide(); 
	});
	
	/* --------------------------------------------
	** Slide Panel
	** ------------------------------------------*/
	
	// Change Image Slide widths
	var imageSize = 60;
	var imagePadding = 9;
	var offset = imageSize + imagePadding;
	var inAnimation = 0;
	
	$(".slide_panel").each(function() {
		$(this).width(($(this).children("a").length * imageSize) + (($(this).children("a").length) * imagePadding ));
	});
	
	$(".button_right").click(function() {
		animate_right($(this).parent().find(".slide_panel"), $(this));
	});
	
	$(".button_left").click(function() {
		animate_left($(this).parent().find(".slide_panel"), $(this));
	});
	
	function animate_right(domElem, button) {
		var out = (domElem.position().left / -60) + 5;
		//console.log(out);
		
		
		if((domElem.position().left / -60) + 5 < domElem.find("a").length ) {
			button.unbind('click');
			var position = domElem.position().left - offset;
			domElem.animate({left: position}, 1000, "swing", function() {
				button.bind('click', function() { animate_right(domElem, button);});
			});
		}
	}
	
	function animate_left(domElem, button) {
		if(domElem.position().left < 0 ) {
			button.unbind('click');
			var position = domElem.position().left + offset;
			domElem.animate({left: position}, 1000, "swing", function() {
				button.bind('click', function() { animate_left(domElem, button);});
			});
		}
	}

	
	/* --------------------------------------------
	** Testimonial scroll through
	** ----------------------------------------- */ 
	
	setTimeout(function() {$(".testimonials").eq(0).show("blind", {}, 1000, showNextTestimonial)}, 500);
	
	function showNextTestimonial() {
		var next = "";
		
		if($(".testimonials:last").is(":visible")) {
			var next = $(".testimonials").eq(0);
		}
		else {
			var next = $(".testimonials:visible").next();
		}
		setTimeout(function() {$(".testimonials:visible").hide("blind", {}, 1000, function() {next.show("blind", {}, 1000, showNextTestimonial)})}, 5000);
	}
		
	/* --------------------------------------------
	** Create account POST
	** ----------------------------------------- */

	//Clear text boxes when focused
	$("input.text_box").one("click", function() { $(this).val(""); });
	
	//Send subscribe submission AJAX
	$("#subscribe_button.submit_button").one("click", function() { 
		$("#subscribe_column").html("<img src='http://theyorkmill.com.au/images/loading.gif' alt='Loading' />"); //Remove submit and replace with loading.gif
		$.ajax({
			type: "POST",
			url: "wp-login.php?action=register",
			data: "user_login=" + $("#user_login").val() + "&user_email=" + $("#user_email").val(),
			success: function(msg){
				$("#subscribe_column").html("<img src='http://theyorkmill.com.au/images/tick.png' alt='Done' />");
			}
		});
	});
	
	/* --------------------------------------------
	** FLIR Dynamic Text Replacement
	** ----------------------------------------- */
	
	FLIR.init({ path: 'http://theyorkmill.com.au/wp-content/themes/yorkmill_update/facelift/' });
	$(".one_third .label").each( function() { FLIR.replace(this, new FLIRStyle({ cFont:'rockwell_bold' , mode:'wrap' })); } );
	$("ul.navigation_bar > li").each( function() { FLIR.replace(this, new FLIRStyle({ cFont:'rockwell' , mode:'wrap' })); } ); 
	$("[class*=heading]").each( function() { FLIR.replace(this, new FLIRStyle({ cFont:'rockwell_bold' , mode:'wrap' })); } ); 
	$("[class*=cd_head]").each( function() { FLIR.replace(this, new FLIRStyle({ cFont:'rockwell_bold' , mode:'wrap' })); } );
	$("#featured_artists").each( function() { FLIR.replace(this, new FLIRStyle({ cFont:'rockwell_bold' , mode:'wrap' })); } );
});
