/*
 * JS for Holden
 * 
 * @author Sebastiano Armeli
 * @date 16/08/2010
 */

(function(window, document, undefined) {
	
	/* Extension of String */
	String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ""); }  
	
	if (!window.HLD) {window.HLD = {}; }
	
	HLD.contextPath = "";
	
	/* Add js class in case JS is enabled */
	(function() {
		var dd = document.documentElement;
		for (var i = 0; i < navigator.plugins.length; i++) {
			var plugin = navigator.plugins[i];
			if (plugin[0].type === 'application/x-shockwave-flash') {
				dd.className += ((dd.className == "") ? "has-flash" : " has-flash");
			}
		}
		dd.className += ((dd.className == "") ? "js-enabled" : " js-enabled");
	}());
	
	/* Object containing functions related to vehicles */
	HLD.Vehicles = {
		/* Add Deep Link for vehicles page */
		deepLink : function() {
		
			if (window.location.hash.length > 0) {
				$("input[name='showroomOptions']").each(function(index){
					var value = this.value.replace(/ /g,"");
					if (value === window.location.hash.substring(1)) {
						shiftOpacity($(this).context);
						return;
					}
				});
			}
		}
	}
		
	/* package containing Forms functions */
	HLD.Form = {
		
		selectMsg : "Please Select",
		specsMsg : "Select a Drive-Train",
			
		updateModels : function (selectTag, modelID,callback) {
				$('#' + modelID).html("");
				$('#' + modelID).html("<option value=''>" + HLD.Form.selectMsg + "</option>");
				$('#' + modelID).attr('disabled','disabled');
				// display loading image
				$('#modelOptions').attr('class','loading');
				var vehicleId = selectTag.id;
				$('#' + vehicleId).attr('disabled','disabled');
				$.getJSON(HLD.contextPath + "/listmodelsbyvehicle?vid="+  selectTag.value,  
						function(data) {
						log(data);						
						var previousModelId = $('#previousModelId').val();
							for (i = 0; i < data.length; i++) {
								if($('#previousModelId').val() != null && $('#previousModelId').val() != "" && data[i].id==previousModelId){
									var option = "<option  lang='" + data[i].classification + "' value='"+ data[i].id + "' selected=true>" +data[i].name + "</option>";
								}else{
									var option = "<option  lang='" + data[i].classification + "' value='"+ data[i].id+"'>" +data[i].name+ "</option>";
								}
								
								$('#' + modelID).append(option);	
							}
							$('#' + modelID).removeAttr('disabled','false');
							$('#' + vehicleId).removeAttr('disabled','false');
							// hide loading image
							$('#modelOptions').attr('class','');
							if(callback != 'undefined' && callback != null){
								callback(selectTag);
							}
				});
		},

		updateSpecifications : function (selectTag, elId) {
			$('#' + elId).html("<option value=''>" + HLD.Form.specsMsg + "</option>");
			$('#' + elId).attr('disabled','disabled');
			$.getJSON(HLD.contextPath + "/listspecsbymodel?modelId="+  selectTag.value,
				function(data) {
					log(data);
					for (i = 0; i < data.length; i++) {
						var option = "<option value='" + data[i].value + "'>" + data[i].label + "</option>";
						$('#' + elId).append(option);
					}
					$('#' + elId).removeAttr('disabled','false');
				}
			);
		},

		updateDealers : function (selectTag, noServiceDealers, dealerID) {
			log(selectTag.value);
			
			$('#dealername\\.errors').hide();
			$('#' + dealerID).html("<option value=''>" + HLD.Form.selectMsg + "</option>");

			$.getJSON(HLD.contextPath + "/listdealersbylocation?aprid="+  selectTag.value  + "&noServiceDealers=" + noServiceDealers,  
					function(data) {
					log(data);
					if (data.length == 0) {
						$('#nodealers').show();
					} else {
						$('#nodealers').hide();
						for (i = 0; i < data.length; i++) {
							var option = "<option value='"+ data[i].id+"'>" +data[i].name+ "</option>";
							$('#' + dealerID).append(option);	
						}
					}
				});
		}
	}
	
	HLD.Effects  = {
		overlay : function(domElement, position, onload, onclose) {
			domElement.overlay({
				// some mask tweaks suitable for modal dialogs
				mask: {opacity : 0.8,color : '#fff',zIndex : "11000"},
				top: position.top || "",
				left: position.left || "",
				onLoad: onload || "",
				onClose : onclose || ""
			});			
		},
		
		closeOverlay : function () {
			$(".launchOverlay").overlay().close();		  
		},
		
		tooltip : function (content) {
			$("a.toolTipLink").tooltip({
				position : 'bottom right',
				offset : [-20,-20],
				layout : '<div id="easyTooltip"></div>'
			});
		},
		
		select : function (index) {
			if ($("ul.tabs").length > 0) {
				$("ul.tabs").data("tabs").click(index);
			}
		}
	}
	
	HLD.Spotlight  = {
		registerSpotlight : function(type, cat) {
			var axel = Math.random() + "";
			var a = axel * 10000000000000;
			var address = 'http://fls.doubleclick.net/activityi;src=1445143;type='+type+';cat='+cat+';ord=' + a + '?';

			$(".spotlightHolder").remove();
			$("body").append('<div class="spotlightHolder"><iframe src="'+address+'" width="1" height="1" frameborder="0"></iframe></div>')
		}
	}
	
	HLD.TrackPurchaseIntent = {
		
		getDateString : function(date){
			 var month=date.getMonth()+1;
			 var dateString=date.getDate();
			 
			 if(month<10){
				 month="0"+month;
			 }
			 if(dateString<10){
				 dateString="0"+dateString;
			 }
			 
			 var dateString=date.getFullYear()+""+month+""+dateString;
			 
 			 return dateString; 			
		},
		
		getTimeDifferenceString : function(purchaseIntent){
			  today=new Date();
			  endDate=new Date();
			  
			  var todayString=this.getDateString(today);
			  
			  var endDateString="";
			  var customvarval="";
			  
			  if(purchaseIntent==""){
				  customvarval=purchaseIntent+"-s"+todayString+"-e"+todayString;
			  }else if(purchaseIntent=="Select"){
				  customvarval=purchaseIntent+"-s"+todayString+"-e"+todayString;
			  }else if(purchaseIntent=="Do not know"){
				  customvarval=purchaseIntent+"-s"+todayString+"-e"+todayString;
			  }else if(purchaseIntent=="0-3 months"){
				  endDate.setDate(today.getDate()+90);
				  endDateString=this.getDateString(endDate);
				  customvarval=purchaseIntent+"-s"+todayString+"-e"+endDateString;
			  }	else if(purchaseIntent=="3-6 months"){
				  endDate.setDate(today.getDate()+180);
				  endDateString=this.getDateString(endDate);
				  customvarval=purchaseIntent+"-s"+todayString+"-e"+endDateString;
			  }	else if(purchaseIntent=="6-12 months"){
				  endDate.setDate(today.getDate()+360);
				  endDateString=this.getDateString(endDate);
				  customvarval=purchaseIntent+"-s"+todayString+"-e"+endDateString;
			  }	else if(purchaseIntent=="1-2 years"){
				  endDate.setDate(today.getDate()+720);
				  endDateString=this.getDateString(endDate);
				  customvarval=purchaseIntent+"-s"+todayString+"-e"+endDateString;
			  }	else if(purchaseIntent=="2+ years"){
				  endDate.setDate(today.getDate()+1080);
				  endDateString=this.getDateString(endDate);
				  customvarval=purchaseIntent+"-s"+todayString+"-e"+endDateString;
			  }
			  
			  return customvarval;
		},
		
		getTrackVar : function(purchaseIntent){
			return this.getTimeDifferenceString(purchaseIntent);			
		}
		
	
	}
	
	HLD.URLHandler = {
			getParamenter: function(name){
				name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");  
				var regexS = "[\\?&]"+name+"=([^&#]*)";  
				var regex = new RegExp( regexS );  
				var results = regex.exec( window.location.href ); 
				if( results == null ){
					 return ""; 
				}  
				else{
					return results[1];
				}    			
			}		
	}

	
	
	// usage: log('inside coolFunc',this,arguments);
	// paulirish.com/2009/log-a-lightweight-wrapper-for-consolelog/
	window.log = function(){
	  log.history = log.history || [];   // store logs to an array for
											// reference
	  log.history.push(arguments);
	  if(this.console){
	    console.log( Array.prototype.slice.call(arguments) );
	  }
	};
	
	// This is for 'readmore' links when they need a small hover content
	// works for <domElement class="readMoreHover">Read More: <domElememt
	// class="hoverContent>Content Here</domElement></domElement>
})(window, document);
