function showImage(image) {
	$("#fullimage").attr('src', image);
};

function addVariation() {
	
	preserveCartHeight(function() {
		var str = $("input:checked").val();
		if (str == null) {
		} else {
			enstore.cart.addItem(str);
		};

	});
};

function backToShop() {
	var url = $.cookies.get("templatePageCookie");
	if (!url)
		url = STORE_URL;
	window.location.href = url;
};



var firstRender = true;


// Switch between the grid and list view
function showGrid() {

	$("#grid").show();
	$("#list").hide();
	$(".grid").addClass('grey');
	$(".list").removeClass('grey');
	$.cookies.set("templateGridCookie", "False");

};

function showList() {
	visiblegrid = $.cookies.get("templateGridCookie");
	if (visiblegrid == "False") {
		$("#list").show();
		$("#grid").hide();
		$(".list").addClass('grey');
		$(".grid").removeClass('grey');
		$.cookies.set("templateGridCookie", "True");
	};
};


function showButton() {
	$("#add-variation").removeClass("inactive");	
	$("#add-variation").addClass("green");
};

/* 
Pass in a function with no argument that might change the height of the cart (removeItem, addItem, etc)
*/
function preserveCartHeight(fn) {
	var wrapperHeight = $("#cart-wrapper").innerHeight();
	fn();
	$("#cart-wrapper").css("height", wrapperHeight);
};



function addItem(uuid) {
	preserveCartHeight(function() {
		enstore.cart.addItem(uuid);
	});
};



enstore.cart.addListener('preFetch', function() {
	$("#empty_cart").hide();
	$("#cart").hide();
	$("#loading-cart").show();
});



enstore.cart.addListener('postRender', function() {


	// Show the cart bar if we have something in the cart
	if (enstore.cart.totalQuantity > 0) {
		
		if (firstRender == true) {
			$("#cart-wrapper").fadeIn(250);
		} else {
			$("#cart-wrapper").fadeIn(250);
		}
	} else {
		$("#cart-wrapper").hide();
	}
	
	firstRender = false;

	
	$("#loading-cart").fadeOut(200);

	$("#cart-wrapper").css("height", "auto");

	
	// create a var filled with a string, explaining in one line what's in the cart.
	var cartBarProducts = '';

	if (enstore.cart.totalQuantity > 0.0 && enstore.cart.totalQuantity > 1.0)
		cartBarProducts = '<span>' + enstore.cart.totalQuantity + ' products in cart</span>';
	else
		cartBarProducts = '<span>' + enstore.cart.totalQuantity + ' product in cart</span>';
		
	// show what's in the cart when cart is collapsed.
	$("#compact").html(cartBarProducts);
		

	//------------- TOGGLE COLLAPSED AND EXPANDED CART VIEW ----------------------------------------------------
	
	// set a cookie to save the state of the cart
	// see if we have a cookie. otherwise set one.
	if ($.cookies.get("bishopCartCookie") === "null"){
		$.cookies.set("bishopCartCookie", 1);
	}

	
	// expand or collapse cart and set cookie acording.
	$("#switch , #compact" ).click( function() {
		
		var isCartOpen = $.cookies.get("bishopCartCookie");
	
		var indicator = $("#arrow");
		
		if (isCartOpen == 1) {

			indicator.addClass("arrow-close");
			indicator.removeClass("arrow-open");
						
			$("#lines-wrapper").slideUp('fast', function() {
				indicator.removeClass("down")
				$("#cart-wrapper").removeClass("open");
			});
			
			$.cookies.set("bishopCartCookie", 0);
			
		} else {
			$("#lines-wrapper").slideDown('fast');
			$("#cart-wrapper").addClass("open");
			$.cookies.set("bishopCartCookie", 1);

			indicator.addClass("down");
			indicator.removeClass("arrow-close");
			indicator.addClass("arrow-open");
		}
	});
	
	//------------- MISC STUFF ----------------------------------------------------
	
	// remove an item from cart
	$(".remove_item").click( function(){
		var text = $(this).parent().children(".productUUID").text();
		
		preserveCartHeight(function() {
			enstore.cart.removeItem(text);
		});
	});

	// Each product name links to it's respective item page.
	$(".item").each(function() {
		var UUID = " " + STORE_URL + "/item/" + $(this).find(".productUUID").text();
		$(this).find(".name").attr("href", UUID);
	});
	
	
	// set a var to see if the cart is open or closed.
	var isCartOpen = $.cookies.get("bishopCartCookie");
	
	if  (isCartOpen == 1) {
		
		var indicator = $("#arrow");
		
		indicator.addClass("down");
		$("#cart-wrapper").addClass("open");
	}
	
	// if the cart is open show it, otherwise colapse it.
	
	// hide the loading cart div
	$("#loading-cart").hide();
	
	if (enstore.cart.items.length !== 0){
		$("#cart").show();
		$("#main").removeClass("devide")
	} else {
		$("#main").addClass("devide");
	};
	
	
	if (isCartOpen == 1) {
		$("#lines-wrapper").show();
	} else {
		$("#lines-wrapper").hide();
	};
	
	
	$(".itemsincart").each(function() {
		
		var totalInCart = 0;
		var productUUIDs = [$(this).parent().attr("class")];
		
		$("#variations-table tr").each(function(i, item) {
			productUUIDs.push($(item).attr("class"))
		})
		
		$.each(enstore.cart.products, function(i, product) {
			$.each(productUUIDs, function(i, productUUID) {
				if (product.productUUID == productUUID)
					totalInCart += parseInt(product.quantity);
			})
		});
		
		if (totalInCart > 0) {
			$(this).html(totalInCart + " in cart");
			$(this).fadeIn(100);
		} else {
			$(this).fadeOut(100);
		}	
	});
	
});


$(document).ready(function() {

	var variationinput = $(".td_input").children("input").attr("checked");
	
	if (variationinput == false){
		$("#add-variation").addClass("inactive");
		$("#add-variation").removeClass("green");
	}

	
	$("#grid .product, #featured_products div").mouseenter(function(){
		$(this).children().children(".title").stop(true,true).slideDown(100);
	});
	
		$("#grid .product, #featured_products div").mouseleave(function(){
		$(this).children().children(".title").stop(true,true).slideUp(100);
	});
	
	
	// set cookie for grid if there isn't any
	if ($.cookies.get("templateGridCookie") == "null"){
		$.cookies.set("templateGridCookie", "False");
	}

	// set a var for grid
	var visiblegrid = $.cookies.get("templateGridCookie");

	// show grid or list depending on grid-cookie-variable
	if (visiblegrid == "False") {
		$("#list").hide();
		$("#grid").show();
		$(".grid").addClass('grey');
		$(".list").removeClass('grey');
	} else {
		$("#list").show();
		$("#grid").hide();
		$(".list").addClass('grey');
		$(".grid").removeClass('grey');
	};
	

	// sort variations if we have em.
	$("#variations-table").tablesorter({sortList: [[1,0]] }); 

	
	// shorten product description.
	$("#list .product").each(function() {
		//see what the height of the title is.
		nameHeight = $(this).children(".productname").innerHeight()
		//and its lineheight
		nameLineHeight = $(this).children().children(".productname").css("line-height").replace("px", "");
		//divide and we'll know how many lines there are.
		nameLines = nameHeight / nameLineHeight
		//if there 2 shorten the product desc. Use rounded numbers because firefox sux.
		if (Math.round(nameLines) == 2) {
			calculatedHeight = $(this).children(".description").css("line-height").replace("px", "")*2 + "px"
			$(this).children(".description").css({'height' : calculatedHeight, 'overflow' : 'hidden'});
		}; 
		//even more: remove product desc.
		if (Math.round(nameLines) > 2) {
			$(this).children(".description").css({'display' : 'none'});
		}; 
	});
	
});


