function selectBrand(selectObj) {
	var oForm = document.getElementById('ProductIndexForm');
	document.getElementById('ProductCategoryId').value = "";
	document.getElementById('ProductId').value = "";
	document.getElementById('ProductColorId').value = 0;
	oForm.submit();
}

function selectCategory(categoryId) {
	var oForm = document.getElementById('ProductIndexForm');
	document.getElementById('ProductCategoryId').value = categoryId;
	document.getElementById('ProductId').value = "";
	document.getElementById('ProductColorId').value = 0;
	oForm.submit();
}

function selectProduct(productId) {
	var oForm = document.getElementById('ProductIndexForm');
	document.getElementById('ProductId').value = productId;
	oForm.submit();
}

function selectColor(colorIndex) {
	var oForm = document.getElementById('ProductIndexForm');
	document.getElementById('ProductColorId').value = colorIndex;
	oForm.submit();
}


// Set hover function on products list
$(function () {
	$(".productListItem").hover(
	  function () {
	    $(this).addClass("productListItemSelected");
	    $(this).children(".productListItemName").show();
	  },
	  function () {
	    $(this).removeClass("productListItemSelected");
	    $(this).children(".productListItemName").hide();
	  }
	);
});

// Loading product detail images
$(function () {
	// Loading main image
	var mainImageContainers = $('#productMainImageContainer div');
	mainImageContainers.each(function(index, el) {
		loadImage($('#productMainImage_' + index), (index==0?true:false));
	});

	// Load thumbs
	var thumbContainers = $('#productThumbsContainer div.productThumbImage');
	loadImages(thumbContainers);

	// Associate mouse over event
	$(".productThumbImage").hover(
	  function () {
	    // Hide others
	    $(".productMainImage").hide();
	    var imageId = $(this).attr("id")
	    imageId = imageId.substr(imageId.length-1, 1);
	    // Show selected image
	    $("#productMainImage_" + imageId).show();
	  },
	  function () {
	    // Hide others
	    $(".productMainImage").hide();
	    // Show first image
	    $("#productMainImage_0").show();
	  }
	);

});


// Loading image function
function loadImages(imageContainers) {
 	imageContainers.each(function(index, el) {
		setTimeout(function() {loadImage(el, true);}, 250);
 	});
}

function addToWishlist(productId) {
	$("#wishlistResult").hide();
	$("#wishlistLoading").show();
	$.ajax({type: 'POST',
		data: "data[Wishlist][method]=add&data[Wishlist][productId]=" + productId,
		success: function (data, textStatus) {
			$("#wishlistLoading").hide();
			$("#wishlistResult").html((textStatus=="success")?addToWishlistMessage:errorOnWishlistMessage).show();
		},
		url: rootPathAction  + "products/wishlist"}
	);
}

function removeFromWishlist(productId) {
	$("#wishlistResult").hide();
	$("#wishlistLoading").show();
	$.ajax({type: 'POST',
		data: "data[Wishlist][method]=remove&data[Wishlist][productId]=" + productId,
		success: function (data, textStatus) {
			$("#wishlistLoading").hide();
			$("#wishlistResult").html((textStatus=="success")?removeFromWishlistMessage:errorOnWishlistMessage).show();

			// Remove product from wishlist dom (if present)
			if ($("#wishlistProduct_" + productId).length > 0)
				$("#wishlistProduct_" + productId).remove();
		},
		url: rootPathAction  + "products/wishlist"}
	);
}

function sendWishlist() {

	// Check for products
	if ($('.wishlistProduct').length == 0) {
		alert(invalidProductsMessage);
		return false;
	}

	// Check email format
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	if(reg.test($("#WishlistEmail").val()) == false) {
		alert(invalidEmailMessage);
		return false;
	}

}
