var cyclerLoaded = false;
var numberOfPreloaded = 0;

function imageLoaded(){      
	numberOfPreloaded++;
	if (numberOfPreloaded >= 4 && !cyclerLoaded){ 
		if ($.support.opacity)
			$('#figure').cycle({timeout:5000,speed:1000,cleartype:!$.support.opacity,cleartypeNoBg:true});
		else
			$('#figure').cycle({timeout:6000,speed:0,cleartype:!$.support.opacity,cleartypeNoBg:true});
		cyclerLoaded = true;
	}
}


$(document).ready(function(){
	// alter the attribute on all external links
	$('a.external').attr("target","_blank");
	$('a.external:not(:has(*))').addClass("marked");
	
	// clear the value of form fields with class of cleardefault
	$("input.cleardefault").focus(function () {
		$(this).attr("value","");
	});
	
	// apply stripes to tables if class stripMe
	$(".stripeMe tr").mouseover(function() {$(this).addClass("over");}).mouseout(function() {$(this).removeClass("over");});
	//$(".stripeMe tr:even").addClass("alt");
		$(".stripeMe tr:even").addClass('alt');

	//Hide (Collapse) the toggle containers on load
	$(".toggle_container").hide();
	
	//Slide up and down & toogle the Class on click
	$("img.trigger").click(function(){
	$(this).parent().next(".toggle_container").slideToggle("slow");
	});
	
	//color picker
	$("img.col_pick").click(function(){
		$("input#colour").attr("value",$("input#colour").val()+$(this).attr("title"));
	});
	$("img.col_pick_first").click(function(){
		$("input#colour").attr("value",$("input#colour").val()+$(this).attr("title"));
	});
	
	$('a.lightbox').lightBox(); // Select all links with lightbox class
	
	// find single filename
	var singleimg = $('#figure img').attr("src");
	
	$('#figure').load('http://www.tartansauthority.com/assets/snippets/figure/populate_all.php?start='+singleimg,function(){
		//alert($('#figure img:last').attr('src'));
		$('#figure').css('overflow','hidden');
		  $('#figure img').each(function()
		  {
		    // Get image instance.
		    var image = new Image();
		    image.src = $(this).attr('src');
		
		    if (image.complete)        
		      imageLoaded();        
		    else        
		      image.onload = imageLoaded;
		
		  });

	});
	if (($('#membership_type_array option:selected').attr('value')) != '1'){
		$('#ages_of_children').parent().fadeTo("fast", 0.33);
		$('#ages_of_children').parent().next().fadeTo("fast", 0.33);
		$('#ages_of_children').attr("disabled", true); 
		$('#ages_of_children').attr("readonly", true); 
	}

	
	$('#membership_type_array').change(function(){
		//alert($('#membership_type_array option:selected').attr('value'));
		if (($('#membership_type_array option:selected').attr('value')) == '9'){
			// hide the recur option below it
			//$('#recurring_1').parent().hide();
			$('#recurring_1').parent().fadeTo("slow", 0.33);
			$('#recurring_1').attr('checked','');
			$('#recurring_1').attr("disabled", true); 
			$('#recurring_1').attr("readonly", true); 
			
			
		}
		else{
			// make visible the recur option below it
			$('#recurring_1').parent().fadeTo("fast",1);
			$('#recurring_1').attr('disabled', ''); 
			$('#recurring_1').attr("readonly", false); 
		}
		
		if (($('#membership_type_array option:selected').attr('value')) == '1'){
			$('#ages_of_children').parent().fadeTo("fast",1);
			$('#ages_of_children').parent().next().fadeTo("fast",1);
			$('#ages_of_children').attr('disabled', ''); 
			$('#ages_of_children').attr("readonly", false); 
		}
		else{
			$('#ages_of_children').parent().fadeTo("slow", 0.33);
			$('#ages_of_children').parent().next().fadeTo("slow", 0.33);
			$('#ages_of_children').attr("disabled", true); 
			$('#ages_of_children').attr("readonly", true); 		
		}
		
		
	});

	


 
});
