jQuery(document).ready(function($){
	
	// Last NAV item
	//------------------------------------------------------------
	jQuery("#mainNav li").each(function(){
		if( jQuery(this).children(".sub-menu").length )
		{
			jQuery(this).addClass("is-parent");
		}
	});
	jQuery("#mainNav li.is-parent:last").addClass("last last-parent");
	//------------------------------------------------------------
	
	jQuery(".idle").each(function(){
		$(this).find("a:first").click(function(){
			return false;
		});
	});
	
	// $ LightBox
	// -----------------------------------------------------------
	$("a[rel=lightbox]").lightBox({
		overlayBgColor: "#000",
		overlayOpacity: 0.8,
		keyToClose: 'esc',
		txtImage: 'Photo'
	});
	// -----------------------------------------------------------
	
	
	// Colorbox
	// -----------------------------------------------------------
	$('a#video-link').colorbox();

	
    
    // Changing homepage photos
	// -----------------------------------------------------------
	$("#homeNav").hide();
	if( $("#shuffle li").length > 1 )
	{
		$("#show_loader").show();
		$("#show_loader").html('<div id="loading">Loading featured products</div>');
	
		$("#shuffle li").hide();
		$("#shuffle li:first").show();
		$("#homeNav li:first").addClass("active");
		$("#homeNav li a").live("click", (function() {
		
			var showItem = $(this).attr("id");
		
			$("#shuffle li").hide();
			$("#shuffle li."+showItem).show();
		
			$("#homeNav li").removeClass("active");
			$(this).parent().addClass("active");
		
		}));
	
	}
	// -----------------------------------------------------------

	
	
	// add class "last" to the last item on the product slider
	//------------------------------------------------------------
	$("#flist li:last").css("margin-right","120px");
	
	// $ Scroll
	// -----------------------------------------------------------
	$('.scroll').jScrollPane({ scrollbarMargin: 0 });
	
	// jCarousel for product list
	// -----------------------------------------------------------
    $('#featured_list_inner').jcarousel({
    	scroll:3,
        horizontal: true,
        buttonNextHTML: '<a href="javascript:void(0);" class="featNavRight">Next</a>',
        buttonPrevHTML: '<a href="javascript:void(0);" class="featNavLeft">Previous</a>',
        buttonNextEvent: "click",
        buttonPrevEvent: "click"
    });
    
    // Changing product photos
	// -----------------------------------------------------------
	$("#thumbs a:first").addClass("sel");
	$("#thumbs a").live("click", (function() {
		
		var loadPhoto = $(this).attr("href");
		var loadPhotoTitle = $(this).attr("title");
		$("#thumbs a").removeClass("sel");
		$(this).addClass("sel");
		
		//$("#bigPhoto").html('<img src="'+loadPhoto+'" alt="'+loadPhotoTitle+'" title="'+loadPhotoTitle+'" />');
		
		$("#bigPhoto").find("a").attr("href",loadPhoto);
		$("#bigPhoto").find("img").attr("src",loadPhoto);
		
		return false;
		
	}));
	// -----------------------------------------------------------
	
	// set values for newsletter input
	//------------------------------------------
	//var defNL = "Subscribe to mailing list";
	//$("#emailInput").val(defNL);
	//$("#emailInput").focus(function() {
	//	
	//	$(this).val("");
	//	
	//});
	
	$('#shuffle').hide();
	$('#shuffle').onImagesLoaded(function(_this){
		if( $("#shuffle li").length > 1 ) $("#homeNav").show();
	  	$(_this).show();
	  	$("#show_loader").hide();
	});
	
	
	// PRODUCT BIG PHOTO SLIDING
	// ---------------------------------------------------------------------
	var bigPhotoWrapWidth = $("#product-big-photo").width();
	var countBigPhotos = $("#product-big-photo ul li").length;
	var totalBigPhotoListWidth = countBigPhotos * bigPhotoWrapWidth;
	$("#product-big-photo ul").css( "width", totalBigPhotoListWidth);
	//alert($("#product-big-photo ul").width());
	// ---------------------------------------------------------------------
	
	// PRODUCT THUMBNAILS / BIG PHOTO PAGINATION - SLIDING
	// ---------------------------------------------------------------------
	$("#product_thumbnails li:first a").addClass("active");
	
	$("#product_thumbnails li a").click(function() {
	
		$("#product_thumbnails li a").removeClass("active");
		$(this).addClass("active");
		
		var show_photo = $(this).attr("href");
		
		$("#product-big-photo img.active").removeClass("active");
		$(show_photo).addClass("active");
		
		$("#product-big-photo").scrollTo( this.hash, 900);
	
		return false;
	
	});
	// ---------------------------------------------------------------------
	
	// FIXS BLOCK HEIGHTS IN CHECKOUT
	// ---------------------------------------------------------------------
	if( $('.leftBlock').length > 0 && $('.rightBlock').length > 0 ) {
	
	
		var leftBlockHeight = $('.leftBlock').height();
		var rightBlockHeight = $('.rightBlock').height();
		
		if( parseInt(leftBlockHeight) > parseInt(rightBlockHeight) ) $('.rightBlock').height(leftBlockHeight);
		if( parseInt(rightBlockHeight) > parseInt(leftBlockHeight) ) $('.leftBlock').height(rightBlockHeight);
	
	}
	// ---------------------------------------------------------------------
	
	// Validate form
	// -----------------------------------------------------------
	$("#tws_my_account_info").validate();
	// -----------------------------------------------------------
	
	// Dealers dropdown
	// -----------------------------------------------------------
	/*
	$("#please-select").mouseover(function() {
		$(this).find("ul").show();
	}).mouseout(function(){
		$(this).find("ul").hide();
	});
	*/
	// -----------------------------------------------------------
	
	// Validate product custom options
	// -----------------------------------------------------------
	$("#tws_add_to_cart").live("click",function(){
		
		var addToCartValid = true;
		var requiredWarningTitle = "Ups!\n\n";
		var requiredWarning = "";
		
		if( $(".tws-attached-product").length > 0 )
		{
			$(".attached_product_checkbox:checked").each(function(){
				
				var requiredOption = $(this).parents(".tws-attached-product").find("select.required");
				var requiredOptionLength = requiredOption.length;

				if( requiredOptionLength > 0 )
				{
					
					var requiredLabel = requiredOption.attr("title");
					
					if( requiredOption.val() == "" )
					{
						
						addToCartValid = false;
						requiredWarning += 'Please, select "'+requiredLabel+'" for additional selected product.\n';
					}
					
				}

			});
		}
		
		// check main product also
		$("#cart-form select.required").each(function(){
			
			//console.log($(this).val());
			
			if( $(this).val() == "" )
			{
				var requiredLabel = $(this).attr("title");
				addToCartValid = false;
				requiredWarning += 'Please, select "'+requiredLabel+'" for selected product.\n';
			}
			
		});
		
		if( !addToCartValid ) alert(requiredWarningTitle+requiredWarning);
		
		return addToCartValid;
		
	});
	// -----------------------------------------------------------
	
	// CHECKOUT - SHIPPING ADDRESS SELECTION
		//----------------------------------------------------------------------

		if( jQuery("input.copyData").is(":checked") )
		{
			jQuery(".checkout-shipping-address-wrap").hide();
		}

		jQuery("input.copyData").change(function() {

			var shform = jQuery(".checkout-shipping-address-wrap");

			( jQuery(this).is(":checked") ) ? shform.hide() : shform.show();

		});

		// ------
	
});

