var categoryExpand = "categoryExpand";
var categoryClose = "categoryClose";
var categoryExpandObj = "[id^='"+categoryExpand+"']";
var categoryCloseObj = "[id^='"+categoryClose+"']";
var categoryDetail = "categoryDetail";

var itemExpand = "itemExpand";
var itemClose = "itemClose";	
var itemExpandObj = "[id^='"+itemExpand+"']";
var itemCloseObj = "[id^='"+itemClose+"']";
var itemDetail = "itemDetail";
var itemHeader = "itemHeader";
var obj = "[id^='compareModelButton']";

  hld_documentReady("categoryExpanderCompare");	

var categoryRow = "categoryRow";
var categoryRowObj = "[id^='"+categoryRow+"']";

var itemRow = "itemRow";
var itemRowObj = "[id^='"+itemRow+"']";

function categoryExpanderCompare() {
   
 //$("[id^='"+categoryDetail+"']").show();
 $(categoryExpandObj).hide();
 //$("#"+categoryDetail+"-1").show();
 //$("#"+categoryClose+"-1").show();
 //$("."+itemHeader+"-1").show();
 //$("#"+categoryExpand+"-1").hide();
  
 $("[id^='"+itemDetail+"']").hide();
 $(itemCloseObj).hide();
   	
 	 $(categoryRowObj).unbind();
	 $(categoryRowObj).click(function() 
	 {
		 var thisId = splitValue(this.id, "-", [1]);
		 var thisClass = $("#" + categoryRow + "-" + thisId).attr('class');
		 if(thisClass == 'expanded')
		 {
			 $("#" + categoryRow + "-" + thisId).attr('class', 'closed');
			 $("#" + categoryClose + "-" + thisId).trigger('click');
		 }else if(thisClass == 'closed')
		 {
			 $("#" + categoryRow + "-" + thisId).attr('class', 'expanded');
			 $("#" + categoryExpand + "-" + thisId).trigger('click');
		 } 
	     return false;
	}); 
	  
	 $(categoryExpandObj).unbind();
	 $(categoryExpandObj).click(function() 
	 {
		 var thisId = splitValue(this.id, "-", [1]);
	
		 $("#" + categoryExpand + "-" + thisId).hide();
		 $("#" + categoryClose + "-" + thisId).show();
	     $("#" + categoryDetail + "-" + thisId).fadeIn('slow',
		 	function(){
		 		 var items = "itemHeader-"+thisId;
				 $("."+items).show();
			}
		  );
		  
		  
	     return false;
	  });
	 
	 $(categoryCloseObj).unbind();
	 $(categoryCloseObj).click(function() 
	 {
		 var thisId = splitValue(this.id, "-", [1]);
		 var items = "itemHeader-"+thisId;
		 $("."+items).hide();
		 
	     $("#" + categoryDetail + "-" + thisId).fadeOut('slow');
		 $("#" + categoryExpand + "-" + thisId).show();
		 $("#" + categoryClose + "-" + thisId).hide();
	     return false;
	 });
	 
	 $(itemRowObj).unbind();
	 $(itemRowObj).click(function() 
	 {
		 var thisId = splitValue(this.id, "-", [1]) + "-" + splitValue(this.id, "-", [2]) + "-" + splitValue(this.id, "-", [3]);
		
		 var thisClass = $("#" + itemRow + "-" + thisId).attr('class');
		 if(thisClass.indexOf('expanded') > -1)
		 {
			 $("#" + itemRow + "-" + thisId).removeClass('expanded');
			 $("#" + itemRow + "-" + thisId).addClass('closed');
			 $("#" + itemClose + "-" + thisId).trigger('click');
		 }else if(thisClass.indexOf('closed') > -1)
		 {
			 $("#" + itemRow + "-" + thisId).removeClass('closed');
			 $("#" + itemRow + "-" + thisId).addClass('expanded');
			 $("#" + itemExpand + "-" + thisId).trigger('click');
		 } 
	     return false;
	  });
	 
	  $(itemExpandObj).unbind();
	  $(itemExpandObj).click(function() 
	  {
		 var thisCategory = splitValue(this.id, "-", [1]);
		 var thisId = splitValue(this.id, "-", [2]);
		 var vehicleDataId = splitValue(this.id, "-", [3]);
		  
		 var contents = $("#itemRow-"+thisCategory+"-"+thisId+"-"+ vehicleDataId +" > div.infoTitle").html();
		 
		 if($("input[name='area']").length > 0)
		 {
			 var area = $("input[name='area']").val();
			 hld_trackEventClick(area +" View", whiteSpace(contents), "Compare Models");
		 }
		 
	     $("#" + itemDetail + "-" + thisCategory+ "-" + thisId).fadeIn('slow');
		 
	     var data = {vehicleDataId: vehicleDataId};
		 
	     if (!$("#" + itemRow + "-" + thisCategory+ "-" + thisId + "-" + vehicleDataId).hasClass("loaded")){
	    	 $("#itemInfo" + "-" + thisCategory+ "-" + thisId).load(vehicleDataModuleURL, data, 
	 				function(){}// end load callback
	 			);// end load item
	    	 
	    	 $("#" + itemRow + "-" + thisCategory+ "-" + thisId + "-" + vehicleDataId).addClass('loaded');
	     }
		 
		 
	     //need to load the vehicle data
		 $("#" + itemExpand + "-" + thisCategory+ "-" + thisId + "-" + vehicleDataId).hide();
		 $("#" + itemClose + "-" + thisCategory+ "-" + thisId + "-" + vehicleDataId).show();
		
		 $("#" + itemRow + "-" + thisCategory+ "-" + thisId + "-" + vehicleDataId).removeClass('closed');
		 $("#" + itemRow + "-" + thisCategory+ "-" + thisId + "-" + vehicleDataId).addClass('expanded');
		
		 
	     return false;
	  });
	 
     $(itemCloseObj).unbind();
	 $(itemCloseObj).click(function() 
	 {
		 var thisCategory = splitValue(this.id, "-", [1]);
		 var thisId = splitValue(this.id, "-", [2]);
		 var vehicleDataId = splitValue(this.id, "-", [3]);
		 
	     $("#" + itemDetail + "-" + thisCategory+ "-" + thisId).fadeOut('slow');		
		 $("#" + itemExpand + "-" + thisCategory+ "-" + thisId + "-" + vehicleDataId).show();
		 $("#" + itemClose + "-" + thisCategory+ "-" + thisId + "-" + vehicleDataId).hide();
		 
		 $("#" + itemRow + "-" + thisCategory+ "-" + thisId + "-" + vehicleDataId).removeClass('expanded');
		 $("#" + itemRow + "-" + thisCategory+ "-" + thisId + "-" + vehicleDataId).addClass('closed');
			 
	     return false;
	 });
	 
	 $(".hoverable").hover(
			function() {
				$(this).addClass("hovered");
			}, function() {
				$(this).removeClass("hovered");
			}
		);
	 
	 $(obj).unbind();
	 $(obj).click(function() {
			var thisId = splitValue(this.id, "-", [1]);
			this.innerHTML="";
			$("#modelName-" + thisId).show();
			$("#modelSelect-" + thisId).show();
			$("#modelEngine-" + thisId).show();
			$("#theDIV").attr("class","compareModelSelected fltLeft");
	 });
		
	 $("[id^='itemExpand-1-1']").click();
}
	
function whiteSpace(value)
{
     value = value.replace(/^\s*|\s*$/g,'');
	 return value;
}

function splitValue(value, token, index) {
	var arr = value.split(token);
 	return arr[index];
}

