$(document).ready( function($) {


	/** SOLD OUT PRODUCT **/
	
	if($('.CurrentlySoldOut').html() != null){
		$('.buyIt').hide(0);
	}

	/** FAKE CART **/
	if($('#SideCartContents').length){
	}else{
		$('#cartHolder').append('<div style="display:; " id="SideCartContents" class="Block Moveable Panel SideCartContents"><h2>Cart</h2><div class="double-groove"></div><div class="BlockContent gbborder"><div class="viewCart"></div><div class="cartCount">You have 0 items</div><div class="clear"></div><ul class="ProductList gtborder"><li class="scartHeader"><div class="scartName sansitalic">Product</div><div class="scartQuantity sansitalic">Qty</div><div class="scartPrice sansitalic">Price</div><div class="clear"></div></li></ul></div><div class="scartTotal">$0.00</div><div class="scartCheckout button"><a  class="ViewCartLink" href="http://store-9f052.mybigcommerce.com/cart.php" target="_parent">Cart</a><p style="padding-top: 10px;"></p></div></div>');
	}

	/** CART EXPAND **/
	$('.viewCart a').click(function(){
		$('.SideCartContents .ProductList').slideToggle();
		$('.viewCart').toggleClass('uparrow');
		if($('.viewCart').hasClass('uparrow')){
			$('.viewCart a').html('Shrink Cart');
		}else{
			$('.viewCart a').html('Expand Cart');
		}
	});
	
	
	/** CART TOTAL **/
	var arr = $('.scartPrice strong');
	var totalPrice = 0;
	arr.each(function(index, item) {
		var price = $(this).html().replace('$', '');
		price = price.replace(',', '');
		totalPrice = totalPrice+Number(price);
	});
	totalPrice = totalPrice.toString();
	if(totalPrice 		>= 1000000){	totalPrice = totalPrice.substr(0,4) + ',' + totalPrice.substr(4) }
	else if(totalPrice	>= 100000){ 	totalPrice = totalPrice.substr(0,3) + ',' + totalPrice.substr(3) }
	else if(totalPrice 	>= 10000){ 		totalPrice = totalPrice.substr(0,2) + ',' + totalPrice.substr(2) }
	else if(totalPrice 	>= 1000){ 		totalPrice = totalPrice.substr(0,1) + ',' + totalPrice.substr(1) }
	if(totalPrice.search("[.]") < 0){totalPrice = totalPrice+".00"}
	$('.scartTotal').html('$'+totalPrice);
	
	/** EMPTY CART **/
	if(!$('.KeepShopping a').html()){
		$('.KeepShopping').html('');
	}
	
	/** PRODUCT DETAILS **/
	if($("#ProductWarranty").html() != null){
		$('#detailsButtonHolder').append('<a id="ProductWarranty_button" href="javascript:;">Warranty</a>');
	}
	if($("#SimilarProductsByTag").html() != null){
		//$('#detailsButtonHolder').append('<a id="SimilarProductsByTag_button" href="javascript:;">Product Tags</a>');
	}
	
	
	
	$('#detailsButtonHolder a').click(function () {

		if(!$(this).hasClass('selected')){
			
			var $theButton = $(this);		
		
			$('.selected').removeClass('selected');
			$(this).toggleClass('selected');
			
			$("#ProductOtherDetails").slideUp(500,'easeInOutQuad');
			$("#ProductWarranty").slideUp(500,'easeInOutQuad');
			$("#ProductReviews").slideUp(500,'easeInOutQuad');
			$("#SimilarProductsByTag").slideUp(500,'easeInOutQuad', function(){});
			
			if($theButton.is("#ProductOtherDetails_button")){
					$("#ProductOtherDetails").delay(500).slideDown(500,'easeInOutQuad');
			}
			if($theButton.is("#ProductReviews_button")){
				$("#ProductReviews").delay(500).slideDown(500,'easeInOutQuad');
			}
			if($theButton.is("#ProductWarranty_button")){
				$("#ProductWarranty").delay(500).slideDown(500,'easeInOutQuad');
			}
			if($theButton.is("#SimilarProductsByTag_button")){
				$("#SimilarProductsByTag").delay(500).slideDown(500,'easeInOutQuad');
			}
		}
    });
    
    
    $('.ProductList li').mouseenter(function(){
    	$(this).find('.ProductDetails').addClass('green');
    	$(this).find('.ProductDetails').removeClass('black');
    });
    $('.ProductList li').mouseleave(function(){
    	$(this).find('.ProductDetails').addClass('black');
    	$(this).find('.ProductDetails').removeClass('green');
    });
    
    $('.ProductTinyImageList ul li').mouseenter(function(){
    	if(CurrentProdThumbImage != $(this).attr("name")){
    		customShowProductImage($(this).attr("name"));
    	}
    });
    $('.ProductTinyImageList ul li').click(function(){
    	if(CurrentProdThumbImage != $(this).attr("name")){
    		customShowProductImage($(this).attr("name"));
    	}
    });


});

/****************************** 
 * Form Function              *
 *                            *
 ******************************
/                            */
function clearText(field){

	if (field.defaultValue == field.value){
		field.style.color = "#333";
		field.style.backgroundColor = "#FFF";
		field.value = '';
	}
}
function resetText(field){
	if (field.value == ''){
		field.style.color = "#7d7d7d";
		field.style.backgroundColor = "#FCF8F2";
		field.value = field.defaultValue;
	} 
}

function setLinkToParent(){
	$('a').attr('target', '_parent');
	$('form').attr('target', '_parent');
}

/****************************** 
 * Product Images             *
 *                            *
 ******************************
/                            */

function customShowProductImage(ThumbIndex) {
	// Add the loading spinner
	$('.ProductThumbImageOverlay').addClass('loading');

	var img = new Image();
	$(img).load(function () {
		$theimage = this;		
		$('.ProductThumbImage2').hide();
		$('.ProductThumbImage2').html(this);
		$('.ProductThumbImageOverlay').removeClass('loading')
		$('.ProductThumbImage2').fadeIn(300,function(){
			$('.ProductThumbImage').html($theimage);
			$('.ProductThumbImage2').fadeOut(0);
		});
		
	}).error(function () {
		$('.ProductThumbImageOverlay').removeClass('loading')
		$('.ProductThumbImage').html('Sorry! This image can not be loaded...');
	}).attr('src', ThumbURLs[ThumbIndex]);

	
	CurrentProdThumbImage = ThumbIndex;
	ShowVariationThumb = false;
}



function loadImage (gallery,image,checkhash) {
		if(checkhash){
			hash = window.location.hash;
			if(hash){
				hash = hash.replace('#!', '');
				image = hash;
			}
		}

		// Change button highlight
		$('#'+gallery+' .image-list .selected').removeClass('selected');
		$('#'+gallery+' .image-list #'+gallery+'_'+image).addClass('selected');
		
		// Hide image and video and add l	oading class
		$('#'+gallery+' .photo-holder').fadeIn();
		//$('#photo1').addClass('loading');
		//$('#photo1').html('');
		//alert(parseFloat(image)+1);
		
		// Replace the project name
		//$("#featured-name").html(photos[image][1]);
		//Cufon.replace('h2', { fontFamily: 'ConduitITCStd Black' });
		
		//Replace the image link
		//$("#featured-name").html(photos[image][2]);
		//$("#photo-button").attr("href", photos[image][2])
		
		// Load the image here
		var img = new Image();
		$(img).load(function () {
			$theimage = this;
			$('#'+gallery+' .photo1').hide();
			$('#'+gallery+' .photo1').html(this);
			$('#'+gallery+' .photo1').fadeIn(0);
			$('#'+gallery+' .photo2').fadeOut('slow',function(){
				$('#'+gallery+' .photo2').html($theimage);
				$('#'+gallery+' .photo2').fadeIn(0);
			});
		}).error(function () {
			$('#'+gallery+' .photo1').removeClass('loading').html('Sorry! This image can not be loaded...');
			$('#'+gallery+' .photo2').fadeOut();
		}).attr('src', photos[gallery][image]);
		
		// set a new photo_current[gallery] value
		photo_current[gallery] = image;
	}







