$(document).ready(function(){	

	// For every checkbox that has a TICK add the Color class  "colorSelected"  to the <label class="clicker">
	$("input.colorCheck[type=checkbox][checked]").each(function() { 
		$(this).parents(".color").find(".clicker").addClass("colorSelected");
	});

	
 	// IF colourShowAll  is CLICKED then pending on its checkbox status 
	// Add/Remove  class "colorSelected" from the <label class="clicker"> + corresponding <input class="color">  set appropriate check value.	
	$('#colourShowAll').click(function(event){		
		if ($(this).attr("checked")) {
					
			$("input.colorCheck[type=checkbox]").each(function() { 
		      $(this).parents(".color").find(".clicker").addClass("colorSelected");
		      $(this).attr("checked", true);
			});

			document.forms[0].submit();
		} else {
			
			$("input.colorCheck[type=checkbox]").each(function() { 
		      $(this).parents(".color").find(".clicker").removeClass("colorSelected");
		      $(this).attr("checked", false);
			});
			
		}
	});
	
	// IF the <div class="color">  is clicked THEN 
	//		uncheck "colourShowAll"
	//		IF "colorCheck" is TRUE switch it to FALSE and remove colorselected class from <label class="clicker">
	//		
	//		ELSE
	//		
	//		IF "colorCheck" is FALSE switch it to TRUE and add colorselected class from <label class="clicker">
	//	
	$(".color").click(function(event) {
	
		$('#colourShowAll').attr("checked",false);
	
		if ($(this).find(".colorCheck").attr("checked")) {
			
			$(this).find(".clicker").removeClass("colorSelected");
			$(this).find(".colorCheck").attr("checked", false);

		} else {
			
			$(this).find(".clicker").addClass("colorSelected"); 
			$(this).find(".colorCheck").attr("checked", true);
		
		}
		
		document.forms[0].submit();
	});
	
	
	//add selected to chosen design 
	$("ul.design").find("input").click(function(event){
		$(".design li").removeClass("selected");
		$(this).parent("li").addClass("selected");
	});	
	

	editNavIcon();
	
	$(".overlay a").click(function(event){
		event.preventDefault();
	});	
	
	//overlay popup fix
	overlay();
	

		
}); 

//overlay popup fix
function overlay(){
	
	var height = $(document).height();	
	//alert(height);	
	$("#dhtmlwindowholder").css("height",height);
}


//adds cross or tick to edit nav popup item if shown or hidden
function editNavIcon(){ 
	
	$(".editNav tr").find("input").click(function(event){	
		var id = $(this).attr("id").replace("input" , 'icon');		
		if($(this).attr("checked")) {
			$("#" + id).addClass("checked");
		} else {
			$("#" + id).removeClass("checked");
		}	
		//alert(this.id);			
	});
			
	$(".editNav tr").find(":nth-child(3)").children("input").attr("id", function (number) {
		  return "input" + number;
	})

	$(".editNav tr").find(".displayIcon").attr("id", function (number) {
			  return "icon" + number;		  
	});
				
	function checkIfChecked(){		
		//alert("woop");
		$(".editNav tr").find("input[type=checkbox][checked]").each(function() {						
			$(this).parents("td").next("td").find(".displayIcon").addClass("checked");  		
		});
	}
		
	checkIfChecked();
				
}






