jQuery(function() {
	jQuery(".compareAjax").click(bindCompareEvent)
});

bindCompareEvent = function() {
	el = jQuery(this);

	if(el.attr("href")){
		compUrl = el.attr("href");
	} else {
		compUrl = (typeof compareAjaxUrl != 'undefined') ? compareAjaxUrl : buildCompareAjaxUrl();
	}

	compUrl += (compUrl.indexOf('?')==-1?"?":"&")+"mode=_ajax&_emod[]=Dfe_Modules_CompareBox_Module";

	if(!el.attr("href")){
		vehicleVuid = el.attr("id");
		vehicleVuid = vehicleVuid.replace("comp","");
	
		compUrl += "&vuid=" + vehicleVuid;
	}

	
	jQuery.get(compUrl,function(data) {
		box = jQuery("[class*=mod-compare-box]");
		newbox = jQuery(data).find(".mod-compare-box-inner");
		
		box.html(newbox)
		
		if(newbox.find('.item').length>0) box.show();
		else box.hide();
		
		jQuery("a.compareAjax").click(bindCompareEvent);
	});
	
	return false;
}

buildCompareAjaxUrl = function(){
	var result = window.location.href;
	if(window.location.search != ""){
		result += "?" + window.location.search();
	}
	return result;
}
