/*
	Maine Cottage Scripts
	Created by Fastpivot 2011
*/



$(document).ready(function(){ // Main Doc Ready Function

	if($('.productDetail').length > 0){ // Check for Item Page

		///////////////////////////////////////////////////////////////
		////// Runs Swatches
		///////////////////////////////////////////////////////////////
		$('.swatchCtrls .fpFilter').change(function() {
			
			var thisSel = $(this);
			var filter = $(this).val();
			
			if(filter == 'all') {
				$('.swatchArea img').parent('a').show();
				var field = $('.swatchCtrls .fpFilter');
				field.val($('option:first', field).val());
			} else {
				$('.swatchArea img').parent('a').hide();
				$('.swatchArea img.' + filter).parent('a').show();
				
				// Reset Others
				var field = $('.swatchCtrls .fpFilter').not(thisSel);
				field.val($('option:first', field).val());
			}
			
			$('.swatchCtrls span.customStyleSelectBox').remove();
			$('.swatchCtrls select').customStyle();
				
			$('.swatchArea').jScrollPane({showArrows: true}); // Reinitialize the scroller bars	
						
			return false;
		});	
	
		///////////////////////////////////////////////////////////////
		////// Runs Color Swatches
		///////////////////////////////////////////////////////////////
		$('.swatchColorCtrls .fpFilter').change(function() {
			
			var thisSel = $(this);
			var filter = $(this).val();
			
			if(filter == 'all') {
				$('.swatchColorArea img').parent('a').show();
				var field = $('.swatchColorCtrls .fpFilter')
				field.val($('option:first', field).val());
			} else {
				$('.swatchColorArea img').parent('a').hide();
				$('.swatchColorArea img.' + filter).parent('a').show();
				
				// Reset Others
				var field = $('.swatchColorCtrls .fpFilter').not(thisSel);
				field.val($('option:first', field).val());				
			}
				
			$('.swatchColorCtrls span.customStyleSelectBox').remove();
			$('.swatchColorCtrls select').customStyle();
				
			$('.swatchColorArea').jScrollPane({showArrows: true}); // Reinitialize the scroller bars					
				
						
			return false;
		});	


		///////////////////////////////////////////////////////////////
		////// Display's Current Selected Swatch and updates inputs
		///////////////////////////////////////////////////////////////	
		$('.swatchArea a').click(function(){
			$('.swatchArea div.act').remove();
			var act = "<div class=act></div>";
			$(this).append(act);
		
			// Get Grade Option
			var prUg = $(this).find('img').attr('id');
			$('#gradeInput').attr('value', prUg);
		
		
			// Fire Scene 7 Function
			var option = $(this).attr('rel');
			if($('.swatchColorCtrls').length > 0){
				var pass = color2;
			}

			setOptions(option, pass);
			color = option;
		
			// Update Hidden Input
			var swatch = $(this).find('img').attr('alt');
			$('#fabricInput').attr('value', swatch);
		
			// Build Thumbnail
			var tSrc = $(this).find('img').attr('src');
			var utSrc = tSrc.replace("32", "50");
			var ftSrc = "<img src=" + utSrc + " />";
			
			// Update title text
			$('#swatchSelection .mssTitle').text(swatch);
			$('#swatchSelection .mssThumb').html(ftSrc);
		
			return false;
		
		});
		
		///////////////////////////////////////////////////////////////
		////// Display's Current Selected Color and updates inputs
		///////////////////////////////////////////////////////////////	
		$('.swatchColorArea a').click(function(){
			$('.swatchColorArea div.act').remove();
			var act = "<div class=act></div>";
			$(this).append(act);
		
			// Fire Scene 7 Function
			var option = $(this).attr('rel');
			var pass = color;

			setOptionsColor(option, pass);
			color2 = option;
		
			// Update Hidden Input
			var swatch = $(this).find('img').attr('alt');
			$('#colorInput').attr('value', swatch);
		
			// Build Thumbnail
			var tSrc = $(this).find('img').attr('src');
			var utSrc = tSrc.replace("32", "50");
			var ftSrc = "<img src=" + utSrc + " />";
			
			// Update title text
			$('#colorSelection .mssTitle').text(swatch);
			$('#colorSelection .mssThumb').html(ftSrc);
		
			return false;
		
		});		
		
		///////////////////////////////////////////////////////////////
		////// Fires Select & Scrollbar Styles
		///////////////////////////////////////////////////////////////	
		$('.swatchArea').jScrollPane({showArrows: true});
		$('.swatchColorArea').jScrollPane({showArrows: true});
		$('.swatchCtrls select').customStyle();
		$('.swatchColorCtrls select').customStyle();
		$('.itemOption select').customStyle();	

		
		///////////////////////////////////////////////////////////////
		////// Handle Leg Finishes
		///////////////////////////////////////////////////////////////
		$('.legFinishes a').click(function(){
			$('.legFinishes a').removeClass('currLf');
			$('.legFinishes div.act').remove();
			var act = "<div class=act></div>";
			$(this).append(act);
			// Run dat
			var cVal = $(this).attr('rel');
			$('input#legFinish').attr('value', cVal);
			$('#legSelection').text(cVal);
			$(this).addClass('currLf');
			
			return false;
		});

		
		///////////////////////////////////////////////////////////////
		////// Item Page Forced Options
		///////////////////////////////////////////////////////////////		
		$('input#fpATC').click(function(){
			var fabricSwatch = $('#fabricInput').val();
			var legSwatch = $('#legFinish').val();
			var colorSwatch = $('#colorInput').val();
			if(fabricSwatch == 'None') {
				alert('Please select a fabric swatch before ordering');
				return false;
			} else if (colorSwatch == 'None'){
				alert('Please select a color swatch before ordering');
				return false;			
			} else if (legSwatch == 'none') {
				alert('Please select a leg finish before ordering');
				return false;
			} else { return true; }
		}); 
		
		
	} // End Item Page Functions


	///////////////////////////////////////////////////////////////
	////// Top Nav Hover
	///////////////////////////////////////////////////////////////
	$('ul.mainNav li').hover(function(){
		$(this).addClass('hovered');
		$(this).find('ul.subNav').show();
	}, function(){
		$(this).removeClass('hovered');
		$(this).find('ul.subNav').hide();
	});
	
	///////////////////////////////////////////////////////////////
	////// Section / Sub-Section Hover
	/////////////////////////////////////////////////////////////// 
	$('.catItemWrap').hover(function(){
		$(this).addClass('catHovered');
	}, function(){
		$(this).removeClass('catHovered');
	});




	///////////////////////////////////////////////////////////////
	////// Launch Rotator & Featured Area ... Home Page Only...
	///////////////////////////////////////////////////////////////
	if($('.rotatorWrapper').length > 0){ // Check for Home Page
	
		$('#rotator').jshowoff({ speed:8000 });
		
		// According Menu & Switch of Carousels
		initMenu();
	
		// Init Carousels
		
		/*$('.infiniteCarousel').infiniteCarousel();
		$('.infiniteCarousel2').infiniteCarousel();
		$('.infiniteCarousel3').infiniteCarousel();	*/

	}


///////////////////////////////////////////////////////////////
////// End Doc Ready... very impourtant :-)
/////////////////////////////////////////////////////////////// 
});
