
(function($) {
	$.compare = {
		property : {
			strCompareUseFlag		: "",
			strTypeIaUrlNamePath	: "",
			compareUrlPath  		: "",
			currentModel			: "",
			opModeFlag				: ""
		},
		initialize :  function (options) {

			if(options) {
				this.property.strCompareUseFlag		= options.strCompareUseFlag 	|| "N";
				this.property.strTypeIaUrlNamePath	= options.strTypeIaUrlNamePath	|| "";
				this.property.compareUrlPath		= options.compareUrlPath 		|| "";
				this.property.currentModel			= options.currentModel			|| "";
				this.property.opModeFlag			= options.opModeFlag			|| "N";
			}			

			$("body").delegate("input[id^=txtCheckCompare]", "click", function() {
 				var strCompareItems = $("#compareValue").val();
 				var arrCompareItems = [];
 				
 				if(strCompareItems)
 					arrCompareItems = strCompareItems.split(",");
 				
				var itemLength = arrCompareItems.length;
 				if(itemLength == 4 && $(this).is(":checked")) {
 					$(this).removeAttr("checked").fe_checkbox();
					$(this).fe_compare4Alert("show");
 					return false;
 				}

				var strModelCode = $(this).attr("value");
				
 				if($(this).is(":checked")) {
 	 				if($.inArray(strModelCode, arrCompareItems) == -1) {
 	 					arrCompareItems.push(strModelCode);
 	 					$(this).fe_compare("show");
 	 				}
 				} else {
 					var nIdx = $.inArray(strModelCode, arrCompareItems);
 					if(nIdx > -1)
 						arrCompareItems.splice(nIdx, 1); 

 					$(this).fe_compare("hide");
 				}

 				$("#compareValue").val(arrCompareItems.join(","));
 			});

			$("body").delegate("input.compareButton", "click", function() {
 				var strCompareItems = $("#compareValue").val();
 				var arrCompareItems = [];
 				
 				if(strCompareItems)
 					arrCompareItems = strCompareItems.split(",");				

				var itemLength = arrCompareItems.length;
				
				if(itemLength == 1) {
					$(this).fe_compareFirstAlert("show");
				} else if(itemLength > 1 && itemLength < 5){

					if($.compare.property.opModeFlag == "Y"){
						return false;
					}
					
					var params = "";
					for(var item in arrCompareItems) {
						if (item == 0)
							params += "?prda=" + arrCompareItems[item];
						if (item == 1)
							params += "&prdb=" + arrCompareItems[item];
						if (item == 2)
							params += "&prdc=" + arrCompareItems[item];
						if (item == 3)
							params += "&prdd=" + arrCompareItems[item];
					}					
					
					arrCompareItems = arrCompareItems.sort();
					
					var taggingText = "";
					for(var item in arrCompareItems) {
						if(taggingText.length > 0) {
							taggingText += "/";
						}
						taggingText += arrCompareItems[item];
					}

					if(typeof(s_control_click) == "function") {
						s_control_click('events,eVar48', 'event48', 'event48,'+ taggingText, 'o', 'compare click');	
					}
					$.setCookieData("compareBackUrl",window.location);
	 				location.href = $.compare.property.compareUrlPath + params;
				}

 			});

			$("body").delegate("input#compareCurrent4", "click", function() {
				if($.compare.property.opModeFlag == "Y"){
					return false;
				}
				
 				var strCompareItems = $("#compareValue").val();
 				var arrCompareItems = [];
 				
 				if(strCompareItems)
 					arrCompareItems = strCompareItems.split(",");				

				var params = "";
				for(var item in arrCompareItems) {
					if (item == 0)
						params += "?prda=" + arrCompareItems[item];
					if (item == 1)
						params += "&prdb=" + arrCompareItems[item];
					if (item == 2)
						params += "&prdc=" + arrCompareItems[item];
					if (item == 3)
						params += "&prdd=" + arrCompareItems[item];
				} 				
 				
				arrCompareItems = arrCompareItems.sort();
				
				var taggingText = "";
				for(var item in arrCompareItems) {
					if(taggingText.length > 0) {
						taggingText += "/";
					}
					taggingText += arrCompareItems[item];
				}

				if(typeof(s_control_click) == "function") {
					s_control_click('events,eVar48', 'event48', 'event48,'+ taggingText, 'o', 'compare click');	
				}
				$.setCookieData("compareBackUrl",window.location);
 				location.href = $.compare.property.compareUrlPath + params;

 			});

			$("body").delegate("input#clearAll", "click", function() {
 				$.fe_compareClearAll();
 				$("#compareValue").val($.compare.property.currentModel);

 			});

			if($.compare.property.strCompareUseFlag == "Y") {
				$.compare.setCompareCheckByCookie();
			}
		},
		setCompareCheckByCookie : function() {

			var strCompareItems = $("#compareValue").val();
			var arrCompareItems = [];
			
			if(strCompareItems)
				arrCompareItems = strCompareItems.split(",");				

			$("input[id^=txtCheckCompare]").each(function(idx){
				var modelCode = $(this).attr("value");
/*
				if($.inArray(modelCode, arrCompareItems) > -1){
					$(this).attr("checked", "checked");
					$(this).fe_checkbox();
					$(this).fe_compare("show");
				}
*/
				if($.inArray(modelCode, arrCompareItems) > -1){
					$(this).attr("checked", true);
					$(this).fe_compare("show");
				} else {
					$(this).attr("checked", false);
					$(this).fe_compare("hide");
				}
				$(this).fe_checkbox();
			});
		}
	};
})(jQuery);
	
