$(document).ready(function(){

	// Mini Basket Opacity
	$("div.minibasket").fadeTo(0, 0.7);
	$("div.minibasket").hover(
		function(){$("div.minibasket").fadeTo(500, 1);},
		function(){$("div.minibasket").fadeTo(500, 0.7);}
	);

	// Shadow Under Elemebt
	$(".dropshadow").wrap("<div class='wrap0'><div class='wrap1'><div class='wrap2'><div class='wrap3'><div style='background-color:#FFFFFF'></div></div></div></div></div>");

	// Submit the order type change
	$("select#OrderOrderTypeId").change(function(){
		$("form#orderstatus").submit();
	});


	// Animation for FlashMessage
	if($("#flashMessage")){
		$("#flashMessage").fadeTo(4000, 0, function(){
			$(this).remove();
		});
	}

	// Validate the terms and conditions in payments pages
	$("div.checkbox #TermsAccept").click(function(){
		if($(this).attr('checked'))	$("input[@type=submit]").removeAttr("disabled");
		else						$("input[@type=submit]").attr("disabled", "disabled");
	});
	$("div.checkbox #CustomerAcceptedterms").click(function(){
		if($(this).attr('checked'))	$("input[@type=submit]").removeAttr("disabled");
		else						$("input[@type=submit]").attr("disabled", "disabled");
	});

	// Check these check boxes for after validation
	// Bug I picked up on
	if($("div.checkbox #CustomerAcceptedterms").attr('checked'))	$("input[@type=submit]").removeAttr("disabled"); // Signup
	if($("div.checkbox #TermsAccept").attr('checked'))				$("input[@type=submit]").removeAttr("disabled"); // Payments

	// Search bar text for empty key
	$("#SearchK").focus(function(){
		if($(this).val() == 'New Search'){
			$(this).val('');
			$(this).attr('class', 'hard');
		}

		if($(this).val() != '' && $(this).val() != 'New Search'){
			$(this).attr('class', 'hard');
		}
	});

	$("#SearchK").blur(function(){
		if($(this).val() == ''){
			$(this).val('New Search');
			$(this).attr('class', 'soft');
		}
	});

});


/**
 * CONTROL FUNCTION
 * To move a category
 * This will change the boxes
 * @access public
 * @return void
 **/
function loadnextlevel(parent, level){

	if(parent.value == -1)
		return;

	// Vars
	var parentName = parent.innerHTML;
	var newSelectId = "select#MoveParents" + level;
	var newDivId = "DivMoveParents" + level;
	var newPclass = "pMoveParents" + level;
	var newSpanId = "span#spanID" + level;
	var newLevelId = level + 1;

	// Defaults And Text All Over
	$("p#" + newPclass).text(parentName);
	$("input#ProductCategoryParentId").val(parent.value);
	$(newSpanId).text(( (level != 1) ? " » " : " " ) + parentName);
	//$("div#selectedCategory").text("Selected Location: " + parentName);

	// Remove Trailing Divs
	for(var i = newLevelId; i <= 4; i++){
		$("select#MoveParents" + i).empty();
		$("p#pMoveParents" + i).text("");
		$("span#spanID" + i).text("");
	}

	// Loading
   	$(newSelectId).empty();
   	$("<option value=\"null\">Loading...</option>").appendTo(newSelectId);

	// Get & Display the next level down
	$.getJSON(
		'http://www.mxalliance.co.za/product_categories/fetch/' + parent.value,
        function(data){
        	// Build Options
        	if(data){
        		$(newSelectId).empty();
        		$("<option value=\"-1\" class=\"moveHere\">Move Here</option>").appendTo(newSelectId);
				$.each(data, function(i, item){
		            $("<option value=\"" + i + "\">" + item + "</option>").appendTo(newSelectId);
		    	});
		    	$(newSelectId + " option").removeAttr("selected");
		    }
		    else{
		    	$(newSelectId).empty();
		    	$("<option value=\"-1\" class=\"moveHere\">Move Here</option>").appendTo(newSelectId);
		    	$(newSelectId).removeAttr('onchange');
		    	$(newSelectId + " option").removeAttr("selected");
		    }

		    return;
        });
}