/* Revision: $Id: general.js 8416 2010-04-16 09:31:10Z igor $ */

$(document).ready(function() {
	
	// -----------------------------------------------------------
	// Merchandise Page
	
	$('body.merchandising div#standBox img').hover(function(){
		
		var c = $(this).attr('class');
		
		$('body.merchandising div#main div.right img').hide();
		$('body.merchandising div#main div.right img.'+c).show();
		
	});
	
	// -----------------------------------------------------------
	
	/* fancy homepage form */
	$("div#product-selector select").uniform();

	/* menu hover fix */
	$("ul#navlist li ul, ul#reseller-navlist li ul").hover(function () {
		$(this).parents('li:has(a)').children('a').addClass("active");
	}, function () {
		$(this).parents('li:has(a)').children('a').removeClass("active");
	});

	/*if($.browser.msie && ($.browser.version < 7)) {
		$("ul#navlist").each(function(i){
			$(this).find("li").hover(function(){
				$(this).addClass("sfhover");
			},function(){
				$(this).removeClass("sfhover");
			});
		});
	}*/
	
	/* product carousel */
	if( $("div#product-scroll div").width()>'808' ){
		$("div#product-scroll").addClass("scrollingactive");
		$("div#product-scroll div").jCarouselLite({
	        btnNext: ".next",
	        btnPrev: ".prev",
	        visible: 4
	    });
	}

	/* IE z-index bug fix */
	/*if ($.browser.msie){
		var zIndexNumber = 1000;
		$('div').each(function() {
			$(this).css('zIndex', zIndexNumber);
			zIndexNumber -= 10;
		});
	}*/
	
	// -----------------------------------------------------------
	// Products selector	
	
	$(".disableme").attr( "disabled", "disabled" );

	//---
	
	//If the selector's JSON exists...	
	if (typeof selectorJSON !== undefined){
	
		/*
		* Deal with the first dropdown.
		* 	This should also update or reset the other three.
		*/
		$("#select-do").change(function () {
			var val = $("#select-do option:selected").val();
			
			if( val != "headline" ){
				//Update 2nd level...
				var second = selectorJSON[val];
				
				//Clear out existing values...
				$("option.levelTwo").remove();
				$("option.levelthree").remove();
				$("option.levelfour").remove();				
				
				if ( val == 'Wall Chase' || val == 'Mortar Rake' ){
					
					$.each(second, function( i, val ) {
						$('<option class="levelfour" value="'+i+'">'+i+'</option>').insertAfter("#select-quality option:last");
					});					
					
					$('#select-quality').removeAttr('disabled');
					$('#uniform-select-quality').removeClass('disabled');		
					
					//Disable the rest...
					
					$('#select-specify').attr( "disabled", "disabled" );
					$('#uniform-select-specify').addClass('disabled');	
					$('#select-type').attr( "disabled", "disabled" );
					$('#uniform-select-type').addClass('disabled');	
					
					$('#submitDiv').addClass('disabled');
					$('#submitInput').attr('disabled','disabled');	
					
					//Reset defaults...
					$('#uniform-select-type span').text( $("#select-type option:first").text() );
					$('#uniform-select-specify span').text( $("#select-specify option:first").text() );	
					$('#uniform-select-quality span').text( $("#select-quality option:first").text() );											
						
				} else {
										
					$.each(second, function( i, val ) {
						$('<option class="levelTwo" value="'+i+'">'+i+'</option>').insertAfter("#select-type option:last");
					});
					
					$('#select-type').removeAttr('disabled');
					$('#uniform-select-type').removeClass('disabled');
					
					//Disable the rest...
					$('#select-specify').attr( "disabled", "disabled" );
					$('#uniform-select-specify').addClass('disabled');	
					$('#select-quality').attr( "disabled", "disabled" );
					$('#uniform-select-quality').addClass('disabled');	
					
					$('#submitDiv').addClass('disabled');
					$('#submitInput').attr('disabled','disabled');	
					
					//Reset defaults...
					$('#uniform-select-type span').text( $("#select-type option:first").text() );
					$('#uniform-select-specify span').text( $("#select-specify option:first").text() );						
					$('#uniform-select-quality span').text( $("#select-quality option:first").text() );					
					
				}
			
			} // - if
			
		}); // $("#select-do").change
		
		//-------------------------------
		
		/*
		* Deal with the second dropdown.
		* 	This should also update or reset the remaining two.
		*/		
		$("#select-type").change(function () {
			var val1 = $("#select-do option:selected").val();
			var val2 = $("#select-type option:selected").val();
			
			if( val2 != "headline" ){
				
				var enableEarly = false;
				
				var thrid = selectorJSON[val1][val2];
				
				//Clear out existing values...
				$("option.levelthree").remove();	
						
				$.each(thrid, function( i, val ) {
					var test = selectorJSON[val1][val2][i];
					if(!(test instanceof Object)){;
						enableEarly = true;						
					} else {
						//This hack solves the 0 (zero) issues...
						if ( i != "NULL" ){
							$('<option class="levelthree" value="'+i+'">'+i+'</option>').insertAfter("#select-specify option:last");	
						}											
					}
				});					
					
				// Deal with the special case of only requiring two drop-downs...
				if ( enableEarly ){
					enableSubmit();	
				} else {
					
					$('#select-specify').removeAttr('disabled');
					$('#uniform-select-specify').removeClass('disabled');	
					
					//Disable the rest...
					$('#select-quality').attr( "disabled", "disabled" );
					$('#uniform-select-quality').addClass('disabled');						
					
					$('#submitDiv').addClass('disabled');
					$('#submitInput').attr('disabled','disabled');						
				}				
				
				//Reset defaults...	
				$('#uniform-select-specify span').text( $("#select-specify option:first").text() );				
				$('#uniform-select-quality span').text( $("#select-quality option:first").text() );
				
			} // - if
			
		});
		
		//-------------------------------
		
		/*
		* Deal with the thrid dropdown.
		* 	This should also update or reset the remaining one.
		*/				
		$("#select-specify").change(function () {
			var val1 = $("#select-do option:selected").val();
			var val2 = $("#select-type option:selected").val();			
			var val3 = $("#select-specify option:selected").val();
			
			if( val3 != "headline" ){
			
				var fourth = selectorJSON[val1][val2][val3];
				
				//Clear out existing values...
				$("option.levelfour").remove();				
				
				$.each(fourth, function( i, val ) {
					//This hack solves the 0 (zero) issues...
					if ( i != "NULL" ){
						$('<option class="levelfour" value="'+i+'">'+i+'</option>').insertAfter("#select-quality option:last");
					}
				});	
				
				$('#select-quality').removeAttr('disabled');
				$('#uniform-select-quality').removeClass('disabled');		
				
				$('#submitDiv').addClass('disabled');
				$('#submitInput').attr('disabled','disabled');													
				
				$('#uniform-select-quality span').text( $("#select-quality option:first").text() );
				
			} // - if	
						
		});
		
		//-------------------------------
		
		// Enables the submit button
		function enableSubmit(){
			$('#submitDiv').removeClass('disabled');
			$('#submitInput').removeAttr('disabled');			
		}
		
		// Disables the sumbit button
		function disableSubmit(){
			$('#submitDiv').addClass('disabled');
			$('#submitInput').attr('disabled','disabled');				
		}
		
		/*
		* Deal with the forth (and last) dropdown.
		* 	Once set, this should basically enable the submit button.
		*/	
		$("#select-quality").change(function () {
			var val4 = $("#select-quality option:selected").val();
			if( val4 != "headline" ){
				enableSubmit();
			} else {	
				disableSubmit();			
			}
		});
		
		//-------------------------------
		
		/*
		* Deal with the form being submitted.
		*		This is required to determine how many dropdowns have been completed before redirecting.
		*/			
		$('#theProductSelector').submit(function () {
			
			var val1 = $("#select-do option:selected").val();
			var val2 = $("#select-type option:selected").val();			
			var val3 = $("#select-specify option:selected").val();
			var val4 = $("#select-quality option:selected").val();
			
			
			var result;
			if( val4 != "headline" && val3 != "headline" ){
				result = selectorJSON[val1][val2][val3][val4];	
			
			//} else if ( val3 != "headline" ){
			//	result = selectorJSON[val1][val2][val3];	
			
			} else if ( val4 != "headline" ){
				result = selectorJSON[val1][val4];	
			}
			
			window.location = "/products/selector/"+result;
			
			return false;
		});
		
		//-------------------------------
		
	} // - if (selectorJSON !== undefined)
	
	// -----------------------------------------------------------
	// Products page
	
	$('#videoThumbLink').show();
	$('#imageThumbLink').show();	
	
	$('#imageThumbLink').click(function() {
		
		$('#productImage').show();
		$('#video').hide();
		
		$('#videoThumbLink').addClass('blank');
		$('#imageThumbLink').removeClass('blank');
					
		return false;
	});	
	
	$('#videoThumbLink').click(function() {

		$('#video').show();
		$('#productImage').hide();
		
		$('#videoThumbLink').removeClass('blank');
		$('#imageThumbLink').addClass('blank');
						
		return false;
	});		

}); // end document ready





