binatone = binatone || {};
binatone.productCompare = {};
binatone.productCompare.category = null;
binatone.productCompare.product = new Array();

binatone.productCompare.history = function() {
	var countP;
	binatone.productCompare.product = new Array();
	var compareProducts = $.cookie("compareProduct");
	if(compareProducts){
		compareProducts = compareProducts.split("&-&")
		for(countP = 0 ; countP < compareProducts.length - 1; countP++) {
			var data = compareProducts[countP].split("&&&");
			binatone.productCompare.product[countP] = {
														"imageUrl" : data[0],
														"productName": data[1],
														"productCategory" : data[2]
													  };
			binatone.productCompare.removeCompareOnclick(data[0], data[1], data[2], countP);
		}
		binatone.productCompare.category = binatone.productCompare.product[0].productCategory;
	}
	binatone.productCompare.blankCompareContent(binatone.productCompare.product.length);
}

binatone.productCompare.removeCompareOnclick = function(imageUrl, productName, productCategory, i) {
	var productCompareContent = document.getElementById("productCompareContent");
	var product = document.createElement("div");
	product.setAttribute("id","product"+(i+1));
	product.className = "pCompCont";
	var span = document.createElement("span");
	var img = document.createElement("img");
	img.src = imageUrl;
	img.width = "20";
	img.height = "30";
	span.appendChild(img);
	var div = document.createElement("div");
	div.className = "pCompDet";
	var label = document.createElement("label")
	label.innerHTML = truncate(productName, '10');
	var href = document.createElement("a");
	href.setAttribute("href", "javascript:void(0)");
	href.onclick = function() {binatone.productCompare.removeFromCompare(imageUrl, productName, productCategory)};
	href.innerHTML = "Remove";
	div.appendChild(label);
	div.appendChild(href);
	product.appendChild(span);
	product.appendChild(div);
	productCompareContent.appendChild(product);
	document.getElementById("productImage"+(i+1)).value = imageUrl;
	document.getElementById("productName"+(i+1)).value = productName;
	document.getElementById("productCategory"+(i+1)).value = productCategory;
}

binatone.productCompare.productOverviewHistoryCheck = function () {
	for(var index = 0; index < binatone.productCompare.product.length; index++) {
		var comparedProduct = document.getElementById(binatone.productCompare.product[index].productName);
		if(comparedProduct) {
			comparedProduct.innerHTML = "";
			var imageUrl = binatone.productCompare.product[index].imageUrl;
			var productName = binatone.productCompare.product[index].productName;
			var productCategory =  binatone.productCompare.product[index].productCategory;
			var remove = binatone.productCompare.productOverviewHistory(imageUrl, productName, productCategory);
			comparedProduct.appendChild(remove);
		}
	}
}

binatone.productCompare.productOverviewHistory = function (imageUrl, productName, productCategory) {
	var remove = document.createElement("a");
	remove.setAttribute("href", "javascript:void(0)");
	remove.onclick = function() {binatone.productCompare.removeFromCompare(imageUrl, productName, productCategory)};
	remove.innerHTML = "Remove";
	return remove;
}


binatone.productCompare.blankCompareContent = function(count) {
	var productCompareContent = document.getElementById("productCompareContent");
	for(var i = count ; i < 5; i++) {
		var div = document.createElement("div");
		div.className = "pCompCont";
		div.setAttribute("id", "product"+(i+1));
		var span = document.createElement("span");
		var img = document.createElement("img");
		img.src = "/images/mobileIcon.gif";
		span.appendChild(img);
		var div2 = document.createElement("div");
		div2.className = "pCompDet";
		var label = document.createElement("label");
		label.innerHTML = "Product";
		var href = document.createElement("a");
		href.setAttribute("href", "javascritp:void(0);");
		href.innerHTML = "";
		div2.appendChild(label);
		div2.appendChild(href);
		div.appendChild(span);
		div.appendChild(div2);
		productCompareContent.appendChild(div);
	}
} 

binatone.productCompare.createCookieData = function() {
	var size = binatone.productCompare.product.length
	if(binatone.productCompare.product.length > 0) {
	var compareProduct = "";
		for(var index = 0; index < binatone.productCompare.product.length; index++) {
			var imageUrl = binatone.productCompare.product[index].imageUrl;
			var productName = binatone.productCompare.product[index].productName;
			var productCategory = binatone.productCompare.product[index].productCategory;
			compareProduct += imageUrl + "&&&" + productName + "&&&" + productCategory;
			compareProduct += "&-&";
		}
		$.cookie("compareProduct", compareProduct, { path: '/' });
	}
}

binatone.productCompare.addToCompare = function(imageUrl, productName, productCategory) {
	if(binatone.productCompare.product.length < 5 && 
			(binatone.productCompare.category == productCategory 
			 || binatone.productCompare.category == null)) {
		if(binatone.productCompare.category == null) {
			binatone.productCompare.category = productCategory;
		}
		var product = document.getElementById("product"+(binatone.productCompare.product.length+1));
		product.innerHTML = "";
		var span = document.createElement("span");
		var img = document.createElement("img");
		img.src = imageUrl;
		img.width = "20";
		img.height = "30";
		span.appendChild(img);
		var div = document.createElement("div");
		div.className = "pCompDet";
		var label = document.createElement("label")
		label.innerHTML = truncate(productName, '10');
		var href = document.createElement("a");
		href.setAttribute("href", "javascript:void(0)");
		href.onclick = function() {binatone.productCompare.removeFromCompare(imageUrl, productName, productCategory)};
		href.innerHTML = "Remove";
		div.appendChild(label);
		div.appendChild(href);
		product.appendChild(span);
		product.appendChild(div);
		document.getElementById("productImage"+(binatone.productCompare.product.length+1)).value = imageUrl;
		document.getElementById("productName"+(binatone.productCompare.product.length+1)).value = productName;
		document.getElementById("productCategory"+(binatone.productCompare.product.length+1)).value = productCategory;
		binatone.productCompare.product[binatone.productCompare.product.length] = {
																					"imageUrl" : imageUrl,
																					"productName": productName,
																					"productCategory" : productCategory
																				   };
		var comparedProduct = document.getElementById(productName);
		comparedProduct.innerHTML = "";
		var remove = document.createElement("a");
		remove.setAttribute("href", "javascript:void(0)");
		remove.onclick = function() {binatone.productCompare.removeFromCompare(imageUrl, productName, productCategory)};
		remove.innerHTML = "Remove";
		comparedProduct.appendChild(remove);
	}else if(binatone.productCompare.category != productCategory) {
		var checkbox = document.getElementById(productName+"_checkbox").checked = false;
		binatone.messageLightBox.display(binatone.message.productCompareLimitations,
				"displayLightbox", "lightBoxContent", "lightBoxBackground");
	}else if(binatone.productCompare.product.length == 5) {
		var checkbox = document.getElementById(productName+"_checkbox").checked = false; 
		binatone.messageLightBox.display(binatone.message.productCompareLimitations2,
				"displayLightbox", "lightBoxContent", "lightBoxBackground");
	}
	binatone.productCompare.createCookieData();
}

binatone.productCompare.removeFromCompare = function(imageUrl, productName, productCategory) {
	var index = binatone.productCompare.product.indexOfProductName(productName);
	if(index != -1) {
		var product = document.getElementById("product"+(index+1));
		product.innerHTML = "";
		var span = document.createElement("span");
		var img = document.createElement("img");
		img.src = "/images/mobileIcon.gif";
		span.appendChild(img);
		var div = document.createElement("div");
		div.className = "pCompDet";
		var label = document.createElement("label")
		label.innerHTML = "Product";
		var href = document.createElement("a");
		href.setAttribute("href", "javascript:void(0)");
		href.innerHTML = "";
		div.appendChild(label);
		div.appendChild(href);
		product.appendChild(span);
		product.appendChild(div);
		document.getElementById("productImage"+(index+1)).value = "";
		document.getElementById("productName"+(index+1)).value = "";
		document.getElementById("productCategory"+(index+1)).value = "";
		
		var comparedProduct = document.getElementById(productName);
		if(comparedProduct) {
			comparedProduct.innerHTML = "";
			var checkBox = document.createElement("input");
			checkBox.setAttribute("id", productName+"_checkbox");
			checkBox.setAttribute("type", "checkbox");
			checkBox.className = "checkStd";
			checkBox.onclick = function() {binatone.productCompare.addToCompare(imageUrl, productName, productCategory);};
			var spanCompare = document.createElement("span");
			spanCompare.innerHTML = "Add to Compare";
			comparedProduct.appendChild(checkBox);
			comparedProduct.appendChild(spanCompare);
		}
		binatone.productCompare.product.splice(index, 1);
		binatone.productCompare.createCookieData();
		if(binatone.productCompare.product.length > index 
			&& binatone.productCompare.product.length != 0) {
			binatone.productCompare.shiftComparedProducts();
		}
		if(binatone.productCompare.product.length == 0) {
			binatone.productCompare.category = null;
			$.cookie("compareProduct", null, { path: '/' });
		}
	}
}

binatone.productCompare.removeAllFromCompare = function() {
	for(var index = 0; index < 5; index++) {
		var product = document.getElementById("product"+(index+1));
		product.innerHTML = "";
		var span = document.createElement("span");
		var img = document.createElement("img");
		img.src = "/images/mobileIcon.gif";
		span.appendChild(img);
		var div = document.createElement("div");
		div.className = "pCompDet";
		var label = document.createElement("label")
		label.innerHTML = "Product";
		var href = document.createElement("a");
		href.setAttribute("href", "javascript:void(0)");
		href.innerHTML = "";
		div.appendChild(label);
		div.appendChild(href);
		product.appendChild(span);
		product.appendChild(div);
		document.getElementById("productImage"+(index+1)).value = "";
		document.getElementById("productName"+(index+1)).value = "";
		document.getElementById("productCategory"+(index+1)).value = "";
	}
	for(var index = 0; index < binatone.productCompare.product.length; index++) {
		var imageUrl = binatone.productCompare.product[index].imageUrl;
		var productName = binatone.productCompare.product[index].productName;
		var productCategory = binatone.productCompare.product[index].productCategory;
		binatone.productCompare.removeAllFromProductsCompare(imageUrl, productName, productCategory);
	}
	binatone.productCompare.product = new Array();
	if(binatone.productCompare.product.length == 0) {
		binatone.productCompare.category = null;
	}
	$.cookie("compareProduct", null, { path: '/' });
}

binatone.productCompare.removeAllFromProductsCompare = function(imageUrl, productName, productCategory) {
	var comparedProduct = document.getElementById(productName);
	if(comparedProduct) {
		comparedProduct.innerHTML = "";
		var checkBox = document.createElement("input");
		checkBox.setAttribute("id", productName+"_checkbox");
		checkBox.setAttribute("type", "checkbox");
		checkBox.className = "checkStd";
		checkBox.onclick = function() {binatone.productCompare.addToCompare(imageUrl, productName, productCategory)};
		var spanCompare = document.createElement("span");
		spanCompare.innerHTML = "Add to Compare";
		comparedProduct.appendChild(checkBox);
		comparedProduct.appendChild(spanCompare);
	}
	return;
}

binatone.productCompare.shiftComparedProducts = function() {
	var index;
	for (index = 0; index < binatone.productCompare.product.length; index++) {
		var imageUrl = binatone.productCompare.product[index].imageUrl;
		var productName = binatone.productCompare.product[index].productName;
		var productCategory = binatone.productCompare.product[index].productCategory;
		var product = document.getElementById("product"+ (index+1) );
		product.innerHTML = "";
		var span = document.createElement("span");
		var img = document.createElement("img");
		img.src = binatone.productCompare.product[index].imageUrl;
		img.width = "20";
		img.height = "30";
		span.appendChild(img);
		var div = document.createElement("div");
		div.className = "pCompDet";
		var label = document.createElement("label")
		label.innerHTML = truncate(binatone.productCompare.product[index].productName, '10');
		var href = document.createElement("a");
		href.setAttribute("href", "javascript:void(0)");
		href = binatone.productCompare.shiftComparedProductsEvents(imageUrl, productName, productCategory, href);
		href.innerHTML = "Remove";
		div.appendChild(label);
		div.appendChild(href);
		product.appendChild(span);
		product.appendChild(div);
		document.getElementById("productImage"+ (index+1)).value = binatone.productCompare.product[index].imageUrl;
		document.getElementById("productName"+ (index+1)).value = binatone.productCompare.product[index].productName;
		document.getElementById("productCategory"+ (index+1)).value = binatone.productCompare.product[index].productCategory;
	}
	var product = document.getElementById("product"+(index+1));
	product.innerHTML = "";
	var span = document.createElement("span");
	var img = document.createElement("img");
	img.src = "/images/mobileIcon.gif";
	span.appendChild(img);
	var div = document.createElement("div");
	div.className = "pCompDet";
	var label = document.createElement("label")
	label.innerHTML = "Product";
	var href = document.createElement("a");
	href.setAttribute("href", "javascript:void(0)");
	href.innerHTML = "";
	div.appendChild(label);
	div.appendChild(href);
	product.appendChild(span);
	product.appendChild(div);
	document.getElementById("productImage"+(index+1)).value = "";
	document.getElementById("productName"+(index+1)).value = "";
	document.getElementById("productCategory"+(index+1)).value = "";
}

binatone.productCompare.shiftComparedProductsEvents = function(imageUrl, productName, productCategory, href) {
	href.onclick = function() {binatone.productCompare.removeFromCompare(imageUrl, productName, productCategory)};
	return href;
}

binatone.productCompare.submit = function() {
	if(binatone.productCompare.product.length > 1) {
		document.forms["compareProducts"].submit();
	} else if(binatone.productCompare.product.length <= 1) {
		binatone.messageLightBox.display(binatone.message.productMinimumComparison,
										"displayLightbox", "lightBoxContent", "lightBoxBackground");
	}
}

binatone.productCompare.productCompareViewWidth = function(size) {
	document.getElementById("ProductFeaturesTable").style.width = 191 * size + "px";
}
