var compareTool = function() {
	var baseUrl = '';
	var baseImgUrl = '';
	var cachedProducts = {};
	var selectedProducts = {};
	
	
	function loadProduct(systemName,column) {
		if (cachedProducts[systemName] != undefined) {
			processData(cachedProducts[systemName],column);
		}
		$.getJSON(baseUrl + 'json/' + systemName,
	        function(data){
			
				//we store the data so we don't have to send a request anymore in the future
				cachedProducts[systemName] = data;
				processData(data,column);
	        }
	    );
		
	}
	
	function checkDifferences(){
		var differencesArray = {};
		var bufferArray = {};
		for (var i in selectedProducts){
			var product = selectedProducts[i];
			for (var j in product.comparisonSpecifs){
				if (bufferArray[j] == undefined){
					bufferArray[j] = product.comparisonSpecifs[j];
				} else {
					if (bufferArray[j] != product.comparisonSpecifs[j]){ // compare with the previous value
						differencesArray[j] = true; //this spec contains differences
					}
					bufferArray[j] = product.comparisonSpecifs[j];
				}
			}
		}
		return differencesArray;
	}
	
	function processData(data,column){
		
		selectedProducts[column] = data;
		//switch the thumbnail image
		if(data.error == undefined) {
			$('#'+column+'_container .productcomp_image').attr('src',baseImgUrl + data.thumbnail+'?size=125x110');
			$('#'+column+'_container .productcomp_image').show();
			$('#'+column+'_container .productcomp_image_container .new_compare').hide();
			$('#'+column+'_container .product_label').show();
			$('#'+column+'_container').css('background-color', '#fff');
			$('#'+column+'_container .productcomp_noproduct').hide();
		
		} else {
			data.price = '';
			data.discountPrice = '';
			$('#'+column+'_container .productcomp_image').hide();
			$('#'+column+'_container .product_label').hide();
			$('#'+column+'_container').css('background-color', '#F7F7F7');
			$('#'+column+'_container .productcomp_noproduct').show();
			$('#'+column+'_container .productcomp_image_container .new_compare').hide();
		}
		if(data.price == undefined || data.price == '' || data.price == 0) {
			$('#'+column+'_container .productcomp_price').hide();
			$('#'+column+'_container .productcomp_image_container .new_compare').hide();
		} else {
			$('#'+column+'_container .productcomp_price').show();	
		}
		$('.'+column+'_spec').html('- - -');
		$('#'+column+'_container .product_link').html(data.name);

		if(data.discountPrice == undefined || data.discountPrice == '' || data.discountPrice == 0){
		$('#'+column+'_container .productcomp_price').html('$'+data.price);
		}
		else{
			$('#'+column+'_container .productcomp_price').html('$'+data.discountPrice);	
		}
		
		if (data.hasLink == true || data.hasLink == 'true'){
			$('#'+column+'_container .product_link').attr('href',baseUrl + 'products/' + data.categorySystemName + '/' + data.subCategorySystemName + '/' + data.systemName);
			$('#'+column+'_container .product_image_link').attr('href',baseUrl + 'products/' + data.categorySystemName + '/' + data.subCategorySystemName + '/' + data.systemName);
		}
		else {
			$('#'+column+'_container .product_link').attr('href',baseUrl + 'products/' + data.categorySystemName + '/' + data.subCategorySystemName);
			$('#'+column+'_container .product_image_link').attr('href',baseUrl + 'products/' + data.categorySystemName + '/' + data.subCategorySystemName);
		}
		$('#compare_table tbody tr').removeClass('highlighted');
		$('#'+column+'_container .productcomp_image_container .new_compare').hide();
		
		if(data.getNew){
			$('#'+column+'_container .productcomp_image_container .new_compare').show();
		}
		for (var i in data.comparisonSpecifs){
			$('#'+i+'_'+column).html(data.comparisonSpecifs[i]);
		}
		var diff = checkDifferences();
		for (var j in diff){
			$('#'+j).addClass('highlighted');
		}
	}
	
	return {
		handleChange : function(elem){
			var column = elem.id;
			var value = elem[elem.selectedIndex].value;
			loadProduct(value,column);
			}, initiate : function(url,imgUrl) {
				baseUrl = url;
				baseImgUrl = imgUrl;
		}
	}
}();


$(document).ready(function() {
	$('.productcomp_image_container .new_compare').hide();
	compareTool.handleChange(document.getElementById("product1"));
	compareTool.handleChange(document.getElementById("product2"));
	compareTool.handleChange(document.getElementById("product3"));
});


