$(document).ready(function(){ 


	//the cookie will expire in 365 days
	var options = {expires: 365};
	
	//array of existing product id to compare
	var existingProductsToCompare;
	
	
	
	/*
	 * instantiate the Compare Products Dialog 
	 * setting up the continue and compare buttons  
	*/
	$("#compare_products_dialog").dialog({
		resizable:false,
		bgiframe: true,
		autoOpen: false,
		minHeight:110,		
		width: 212, //208
		modal: true
	});


	$('#compare-selected').click(function() {
		window.location = SITE_URL +'/index.php?page=compare';
		//alert('goto compare page');
		$("#compare_products_dialog").dialog('close');	
	})
	$('#continue').click(function() {
		$("#compare_products_dialog").dialog('close');		
	})
	$('#compare_products_dialog .popup_close_but').click(function() {
		$("#compare_products_dialog").dialog('close');
		return false;
	})
	

	/*
	 * this function loops through all elements that have the class .compare_products_check
	 * 
	 */
	jQuery.each($('.compare_product_check'), function() {    	
		var product_id = this.name.split(product_compare_module_id)[1]; 		
    	existingProductsToCompare = getArrayFromCookie(COOKIE_COMPARE_IDS); 	
		var idx = getArrIdxByVal(existingProductsToCompare, product_id);
		if (idx > -1){
			//the styles wrap the input checkbox in a href which we need to change
			var prevClass = this.previousSibling.className;			
			this.previousSibling.className = prevClass + ' jqTransformChecked';					
			this.checked = true;
		}		
    });
	

	$('.compare_product_check').change(function() {		
		//the jquery wrapper does not implement the checking of the box		
		var elm_name = this.name;		
		var product_id = elm_name.split(product_compare_module_id)[1]; 				
		existingProductsToCompare = getArrayFromCookie(COOKIE_COMPARE_IDS);		
		//check to see if the produdct id is already being compared
		var idx = getArrIdxByVal(existingProductsToCompare, product_id);
		
		if (this.checked){
			//if checked attempted to put in comaprion array	
			if (idx > -1){
				//product has already been added
			}else{
				existingProductsToCompare.push(product_id);
			}
		}else{
			//attempt to remove product from comparison array
			if (idx > -1){				
				existingProductsToCompare.splice(idx,1);		
				//product has already been added
			}
		}

		if (existingProductsToCompare.length > 4 && this.checked == true){			
			$('#compare_error').text('You have exceeded the maximum number of products you can compare.');			
			//the styles wrap the input checkbox in a href which we need to change
			this.previousSibling.className = ' jqTransformChecked';				
			this.checked = false;
			$('#compare_products_dialog').dialog('open');			
		}else{
			$('#compare_error').text('');
			$.cookie(COOKIE_COMPARE_IDS, existingProductsToCompare.join(","),options);			
			if (existingProductsToCompare.length > 1){
				$('#num_of_compare_products').text(existingProductsToCompare.length);				
				$('#compare_products_dialog').dialog('open');	
			}
		}		
	})
	.hover(
		function(){ 
			$(this).addClass("ui-state-hover"); 
		},
		function(){ 
			$(this).removeClass("ui-state-hover"); 
		}
	).mousedown(function(){
		$(this).addClass("ui-state-active"); 
	})
	.mouseup(function(){
			$(this).removeClass("ui-state-active");
	});
	
	
	$('.remove_from_compared').click(function() {
		var product_id = this.id.split('rc_link')[1]; 
		
		existingProductsToCompare = getArrayFromCookie(COOKIE_COMPARE_IDS); 
		var idx = getArrIdxByVal(existingProductsToCompare, product_id);
		
		//attempt to remove product from the favorite products array
		if (idx > -1){				
			existingProductsToCompare.splice(idx,1);		
			$.cookie(COOKIE_COMPARE_IDS, existingProductsToCompare.join(","),options);
			window.location = SITE_URL +'/index.php?page=compare';
		}				
	});	
	

});
