/* <![CDATA[ */
$(document).ready( function(){
	
	// call the init function
	initBinding();
	
	// now we check to see if an anchor has been requested,
	// if it has we now open the parent category
	if( getAnchor().length )
	{
		// set the category to visible
		$("div#" + getAnchor() + " div.toggleContainer" ).show();
		
		// set the button to the open state
		$("div#" + getAnchor() ).find('h2 img').attr("src", sImageRoot + "btn_hide.png")
		
		// finally, we jump to the anchor
		window.location.hash = window.location.toString().split('#')[1];
	};
	
	$("input#bJsEnabled").val("1");
	
	$(".rollover").hover(
		function() {
			$(this).attr("src", $(this).attr("src").split(".").join("_over."));
		}, function() {
		$(this).attr("src", $(this).attr("src").split("_over.").join("."));
	});
	
	$("a#clearSearch").click( function(e){
		e.preventDefault();
		$('input#sSearchTerm').val("");
		doSearch();
	});

	
	$('input#sSearchTerm').data('timeout', null).keyup(function(e){
		clearTimeout($(this).data('timeout'));
		$(this).data('timeout', setTimeout( function(){
			
			doSearch();
			
		}, 600));
		
	}).click(function(){
		if( $(this).val() == 'PC code, title or keywords' )
		{
			$(this).removeClass('default').val('');
		}
	});
	
	
	function doSearch(){
		setProgress(true);

		// do ajax post
		$.ajax({
			type: "POST",
			url: "getProductSearch.cfm",
			data: "sSearchTerm=" + $('input#sSearchTerm').val() + "&" + token,
			success: function(msg){
				$("div#resultContainer").html( msg );
				initBinding();
				setProgress(false);
			}
		});
	}


	function initBinding() {
		
		// hide the category products
		$("div.toggleContainer[hasProducts='false']").hide();
		
		$("input.productQty").data('timeout',null).keyup(function(){
			
			var obj = $(this);
			var id = obj.attr('id').split("_");
			var qty = (  isNaN( parseInt( obj.val() ) ) ) ? 0 : parseInt( obj.val() );
			var max = parseInt( $( "input#intMax_" + id[1] ).val() );
			
			clearTimeout($(this).data('timeout'));
			$(this).data('timeout', setTimeout( function(){
				
				// check for max limits
				if( ( max > 0 ) && ( qty > max ) ){
					alert( 'Quantity entered exceeds maximum allowed (' + max + ').' );
					obj.val( '' );
					return false;
				}
				
				// check for negative quantity
				if( qty < 0 ){
					alert( 'Please enter a positive quantity.' );
					return false;
				}
				
				// do ajax post
				$.ajax({
					type: "POST",
					url: "updateCart.cfm",
					data: "id=" + id[1] + "&qty=" + qty + "&" + token,
					success: function(msg){
						obj.val( msg );
						if( obj.val().length == 0 ){
							obj.trigger( jQuery.Event("blur") );
						}
					}
				});
				
			}, 1000 ));
		}).focus( function(){
			// check for empty input when focussed
			if( $(this).val() == $(this).siblings('label').text()){
				$(this).removeClass('default').val('');			
			}
		}).blur( function(){
			// check for empty value when blurred
			if( $(this).val().length == 0 || isNaN( $(this).val() ) )
			{
				setChooserDefault( $(this) );
			}
		}).each( function(){
			// check all for empty value
			if( $(this).val().length == 0 )
			{
				setChooserDefault( $(this) );
			}
		});
		
		function setChooserDefault( obj ){	
			obj.val( obj.siblings('label').text() );
			obj.addClass('default')	
		}
		
		
		// clicking the show/hide button toggles both the visibility of the
		// products contained therein and change the state of the button
		$("a.toggle").click( function(e){
			e.preventDefault();
			
			$(this).parents('div.productCatContainer').find( 'div.toggleContainer' ).slideToggle("slow");

		}).toggle(
			function(){
				if( $(this).find('img').attr("src" ) == sImageRoot + "btn_hide.png" )
				{
					$(this).find('img').attr("src", sImageRoot + "btn_show.png");
				} else
				{
					$(this).find('img').attr("src", sImageRoot + "btn_hide.png");				
				}
					
			},
			function(){
				if( $(this).find('img').attr("src" ) == sImageRoot + "btn_show.png" )
				{
					$(this).find('img').attr("src", sImageRoot + "btn_hide.png");
				} else
				{
					$(this).find('img').attr("src", sImageRoot + "btn_show.png");				
				}
			}
		);
	}
	
	function setProgress( bAnimate ){
		var src = ((bAnimate)) ? "progress.gif" : "progress_off.gif";
		$("img#progressImg").attr("src", sImageRoot + src );
	}
	
	function getAnchor()
	{
		var sUrl = window.location.toString();
		var sAnchor = sUrl.split('#')[1];
		var sCat = '';
		
		if( sAnchor )
		{
			sCat = sAnchor.split('.')[0];
		}

		return sCat;
	}

});
/* ]]> */

