/* Homepage Quick Start v0.1 JS by Josh Lizarraga */
/* Copyright 2009 Autofusion.com */

(function(){

/* AF Library */

if(typeof(YAHOO.AUTOFUSION) == "undefined"){
	YAHOO.namespace("AUTOFUSION");
}
if(typeof(YAHOO.AUTOFUSION.items) == "undefined"){
	YAHOO.AUTOFUSION.items = new Object();
}
var AF = YAHOO.AUTOFUSION;

/* Homepage Quick Start */

var MIL;

AF.HomepageQuickStart = function(pID, pConfig){
		
	// Properties:
	
	this.config = (pConfig) ? pConfig: false;
	
	this.slider = document.getElementById(pID);
	this.slidee = this.slider.getElementsByTagName('div')[0];
	this.links = this.slidee.getElementsByTagName('a');
	
	this.image = false;
	
	this.inventory = false;
	
	this.research = false;
	
	this.quote = false;
	
	this.heading = false;
	
	this.xmlLink = false;
	this.xmlPrice = false;
	this.xmlMPG = false;
	this.xmlPricePrefix = 'Starting at ';
	this.xmlPriceSuffix = '';
	this.xmlMPGPrefix = 'As high as ';
	this.xmlMPGSuffix = ' MPG';
	this.xmlPath = '/hmpg_javascript.xml';
	this.xmlObject = {};
	this.xmlDisplay = 'block';
	
	this.incentives = false;
	this.make = false;
	this.rebate = false;
	this.aprLow = false;
	this.rates = false;
	this.disclaimer = false;
	this.details = false;
	
	this.rebatePrefix = '';
	this.rebateSuffix = ' Factory Rebate!';
	this.aprPrefix = 'As low as ';
	this.aprSuffix = '% APR!';
	this.aprBridge = ' up to ';
	this.aprNode = 'p';
	this.ratePrefix = '';
	this.rateSuffix = '';
	this.detailsDisplay = 'block';
	this.disclaimerDisplay = 'block';
	this.noIncentive = '&nbsp;';
	
	this.confid = false;
	
	this.imagePath = '/hqs/';
	this.inventoryPath = '/inventory.php?VehicleType=New&ModelFamily=';
	this.researchPath = '';
	this.quotePath = '/quick_quote.html?model=';
	
	this.sliderWidth = parseInt(this.slider.offsetWidth);
	this.slideeWidth = (parseInt(this.links[0].offsetWidth) + 1) * this.links.length;
	
	this.scrollLeft = false;
	this.scrollRight = false;
	
	this.scrollInterval = false;
	
	this.scrollSpeed = 5;
	this.scrollIncrement = 5;
	
	this.autoplay = false;
	this.autoInterval = 4000;
	this.autoIndex = -1;
	
	if(this.config != false){
		for(var i in this.config){
			this[i] = this.config[i];
		}
	}
	
	var that = this;
	
	// Methods:
	
	this.resultObject = function(pIncentive){
		// Basic:
		this.year = MIL.getValue(pIncentive.getElementsByTagName('year')[0]);
		this.model = MIL.getValue(pIncentive.getElementsByTagName('model_generic')[0]);
		// Rebate:
		this.rebate = false;
		if(pIncentive.getElementsByTagName('amount').length > 0){
			this.rebate = MIL.getValue(pIncentive.getElementsByTagName('amount')[1]);
		}
		// Rates:
		var oRates = pIncentive.getElementsByTagName('rate');
		this.lowestAPR = 100.00;
		this.rates = [];
		for(var i=0; i<oRates.length; i++){
			var oAPR = parseFloat(MIL.getValue(oRates[i]));
			if(oAPR < this.lowestAPR){
				this.lowestAPR = oAPR;
			}
			var oTerm = oRates[i].getAttribute('term');
			oTerm = (oTerm.toLowerCase().indexOf('months') > -1) ? oTerm : oTerm + ' Months';
			this.rates.push({
				term: oTerm,
				apr: oAPR
			});
		}
		// Trims:
		var oTrims = pIncentive.getElementsByTagName('trim');
		if(oTrims.length > 0){
			this.appliesTo = [];
			for(var i=0; i<oTrims.length; i++){
				this.appliesTo.push(MIL.getValue(oTrims[i].getElementsByTagName('name')[0]));
			}
			this.appliesTo = this.appliesTo.join(', ');
		} else {
			this.appliesTo = 'All trims!';
		}
		// URL:
		this.url = MIL.getValue(pIncentive.getElementsByTagName('details_link')[0]);
		// Disclaimer:
		try {
			var oComment = MIL.getValue(pIncentive.getElementsByTagName('comment')[0]);
		}
		catch(err){
			var oComment = '';
		}
		this.disclaimer = '<strong>Applies to: ' + this.appliesTo + '</strong><br />' + oComment + '<br />Expires ' + MIL.getValue(pIncentive.getElementsByTagName('expiration')[0]) + '.<br /><a href="' + this.url + '#trims_incentives">Click here</a> for complete terms and conditions.';
	}; // this.resultObject()
	
	this.doClicks = function(pTarget){
		that.imageClick(pTarget);
		that.headingClick(pTarget);
		that.inventoryClick(pTarget);
		that.researchClick(pTarget);
		that.quoteClick(pTarget);
		that.defaultClick(pTarget);
		that.incentivesClick(pTarget);
		that.xmlClick(pTarget);
	}; // doClicks()
			
	this.carClick = function(e){
		YAHOO.util.Event.stopPropagation(e);
		YAHOO.util.Event.preventDefault(e);
		var oTarget = YAHOO.util.Event.getTarget(e);
		if(oTarget.nodeName.toUpperCase() != 'A'){
			oTarget = YAHOO.util.Dom.getAncestorByTagName(oTarget, 'a');
		}
		if(that.autoplay != false){
			clearInterval(that.autoplay);
		}
		that.doClicks(oTarget);
	}; // carClick()
	
	this.defaultClick = function(pTarget){
		if(that.image == false && that.inventory == false && that.research == false && that.quote == false && that.incentives == false && that.xmlLink == false){
			document.location.href = pTarget.href;
		}
	}; // defaultClick()
	
	this.headingClick = function(pTarget){
		if(that.heading != false){
			that.heading.innerHTML = pTarget.title;
		}
	}; // imageClick()
	
	this.imageClick = function(pTarget){
		if(that.image != false){
			that.fadeImageOut(pTarget);
		}
	}; // imageClick()
	
	this.inventoryClick = function(pTarget){
		if(that.inventory != false){
			that.inventory.href = that.inventoryPath + pTarget.title;
		}
	}; // inventoryClick()
	
	this.researchClick = function(pTarget){
		if(that.research != false){
			that.research.href = that.researchPath + pTarget.title;
		}
	}; // researchClick()
	
	this.quoteClick = function(pTarget){
		if(that.quote != false){
			that.quote.href = that.quotePath + pTarget.title;
		}
	}; // quoteClick()
	
	this.incentivesClick = function(pTarget){
		if(that.incentives != false){
			MIL.getModel(that.make, pTarget.title, function(){
				
				var oResult = MIL.XML.getElementsByTagName('Incentive');
				
				if(oResult.length > 0){
				
					oResult = new that.resultObject(oResult[0]);
					
					TEST = oResult;
					
					that.rebate.innerHTML = (oResult.rebate != false) ? that.rebatePrefix + oResult.rebate + that.rebateSuffix: that.noIncentive;
					
					if(oResult.rates.length > 0){
						that.aprLow.innerHTML = that.aprPrefix + oResult.lowestAPR + that.aprSuffix;
						that.rates.innerHTML = '';
						for(var i=0; i<oResult.rates.length; i++){
							that.rates.innerHTML += '<' + that.aprNode + '>' + that.ratePrefix + oResult.rates[i].apr + '%' + that.aprBridge + oResult.rates[i].term + that.rateSuffix + '</' + that.aprNode + '>';
						}
					} else {
						that.rates.innerHTML = that.noIncentive;
					}
					
					that.disclaimer.innerHTML = oResult.disclaimer;
					
					that.details.href = oResult.url + '#trims_incentives';
					YAHOO.util.Dom.setStyle(that.details, 'display', that.detailsDisplay);
					YAHOO.util.Dom.setStyle(that.disclaimer, 'display', that.disclaimerDisplay);
				
				} else {
					
					that.rebate.innerHTML = that.noIncentive;
					that.aprLow.innerHTML = that.noIncentive;
					that.rates.innerHTML = that.noIncentive;
					that.disclaimer.innerHTML = that.noIncentive;
					YAHOO.util.Dom.setStyle(that.details, 'display', 'none');
					YAHOO.util.Dom.setStyle(that.disclaimer, 'display', 'none');
					
				}
				
			})
		}
	}; // incentivesClick()
	
	this.autoClick = function(){
		if(that.autoIndex < that.links.length - 1){
			that.autoIndex++;
		} else {
			that.autoIndex = 0;
		}
		that.doClicks(that.links[that.autoIndex]);
	}; // autoClick()
	
	this.xmlClick = function(pTarget){
		if(that.xmlLink != false){
			if(typeof(that.xmlObject[pTarget.title]) != 'undefined'){
				if(typeof(that.xmlLink.id) == 'string'){
					that.xmlLink.href = that.xmlObject[pTarget.title].url;
				} else {
					for(var i=0; i<that.xmlLink.length; i++){
						that.xmlLink[i].href = that.xmlObject[pTarget.title].url;
					}
				}
				YAHOO.util.Dom.setStyle(that.xmlLink, 'display', that.xmlDisplay);
			} else {
				YAHOO.util.Dom.setStyle(that.xmlLink, 'display', 'none');
			}
		}
		if(that.xmlPrice != false){
			if(typeof(that.xmlObject[pTarget.title]) != 'undefined'){
				that.xmlPrice.innerHTML = that.xmlPricePrefix + that.xmlObject[pTarget.title].price + that.xmlPriceSuffix;
				YAHOO.util.Dom.setStyle(that.xmlPrice, 'display', that.xmlDisplay);
			} else {
				YAHOO.util.Dom.setStyle(that.xmlPrice, 'display', 'none');
			}
		}
		if(that.xmlMPG != false){
			if(typeof(that.xmlObject[pTarget.title]) != 'undefined'){
				if(parseInt(that.xmlObject[pTarget.title].mpg, 10) > 0){
					that.xmlMPG.innerHTML = that.xmlMPGPrefix + that.xmlObject[pTarget.title].mpg + that.xmlMPGSuffix;
					YAHOO.util.Dom.setStyle(that.xmlMPG, 'display', that.xmlDisplay);
				} else {
					YAHOO.util.Dom.setStyle(that.xmlMPG, 'display', 'none');
				}
			} else {
				YAHOO.util.Dom.setStyle(that.xmlMPG, 'display', 'none');
			}
		}
	}; // xmlClick()
	
	this.fadeImageOut = function(pTarget){
		var oFadeOut = new YAHOO.util.Anim(that.image, {
				opacity: { to: 0 }
			}, 0.25, YAHOO.util.Easing.easeOut);
		oFadeOut.animate();
		oFadeOut.onComplete.subscribe(function(){
			that.image.src = that.imagePath + pTarget.title.toLowerCase().replace(/\s/g, '-') + '.jpg';
			that.image.alt = pTarget.title;
		});
	}; // fadeImageOut()
	
	this.fadeImageIn = function(){
		var oFadeIn = new YAHOO.util.Anim(that.image, {
				opacity: { to: 1 }
			}, 0.25, YAHOO.util.Easing.easeOut);
		oFadeIn.animate();
	}; // fadeImageIn()
	
	this.scrollOver = function(e){
		var oTarget = YAHOO.util.Event.getTarget(e);
		if(oTarget.id != that.scrollLeft.id && oTarget.id != that.scrollRight.id){
			if(YAHOO.util.Dom.isAncestor(that.scrollLeft, oTarget)){
				oTarget = that.scrollLeft;
			} else {
				oTarget = that.scrollRight;
			}
		}
		switch(oTarget.id){
			case that.scrollLeft.id:
				that.scrollInterval = setInterval(that.scrollLeftLoop, that.scrollIncrement);
				break;
			case that.scrollRight.id:
				that.scrollInterval = setInterval(that.scrollRightLoop, that.scrollIncrement);
				break;
			default:
				// Do nothing.
		}
	}; // scrollOver()
	
	this.scrollOut = function(e){
		clearInterval(that.scrollInterval);
	}; // scrollOver()
	
	this.scrollRightLoop = function(){
		var oLeft = parseInt(YAHOO.util.Dom.getStyle(that.slidee, 'left'));
		var oAbsoluteLeft = (oLeft > 0) ? oLeft : -(oLeft);
		var oMax =  that.slideeWidth - that.sliderWidth;
		if(oAbsoluteLeft < oMax){
			YAHOO.util.Dom.setStyle(that.slidee, 'left', (oLeft - that.scrollIncrement) + 'px');
		}
	}; // scrollRightLoop()
	
	this.scrollLeftLoop = function(){
		var oLeft = parseInt(YAHOO.util.Dom.getStyle(that.slidee, 'left'));
		if(oLeft < 0){
			YAHOO.util.Dom.setStyle(that.slidee, 'left', (oLeft + that.scrollIncrement) + 'px');
		}
	}; // scrollLeftLoop()
	
	this.initSlider = function(){
		if(this.scrollLeft != false && this.scrollRight != false){
			YAHOO.util.Dom.setStyle(this.slidee, 'width', this.slideeWidth + 'px');
			this.scrollLeft = document.getElementById(this.scrollLeft);
			this.scrollRight = document.getElementById(this.scrollRight);
			YAHOO.util.Event.addListener([this.scrollLeft, this.scrollRight], 'mouseover', this.scrollOver);
			YAHOO.util.Event.addListener([this.scrollLeft, this.scrollRight], 'mouseout', this.scrollOut);
			YAHOO.util.Event.addListener(this.links, 'click', this.carClick);
		}
	}; // initSlider()
	
	this.initHeading = function(){
		if(this.heading != false){
			this.heading = document.getElementById(this.heading);
		}
	}; // initHeading()
	
	this.initImage = function(){
		if(this.image != false){
			this.image = document.getElementById(this.image);
			YAHOO.util.Event.addListener(this.image, 'load', this.fadeImageIn);
		}
	}; // initImage()
	
	this.initInventory = function(){
		if(this.inventory != false){
			this.inventory = document.getElementById(this.inventory);
		}
	}; // initInventory()
	
	this.initResearch = function(){
		if(this.research != false){
			this.researchPath = '/carresearch/TrimsBuildDealer/make_' + this.make + '/confid_' + this.confid + '/model_';
			this.research = document.getElementById(this.research);
		}
	}; // initResearch()
	
	this.initQuote = function(){
		if(this.quote != false){
			this.quote = document.getElementById(this.quote);
		}
	}; // initQuote()
	
	this.initIncentives = function(){
		if(this.incentives != false){
			this.rebate = document.getElementById(this.rebate);
			this.aprLow = document.getElementById(this.aprLow);
			this.rates = document.getElementById(this.rates);
			this.disclaimer = document.getElementById(this.disclaimer);
			this.details = document.getElementById(this.details);
			MIL = new MILAPI(this.confid);
		}
	}; // initIncentives()
	
	this.initAutoplay = function(){
		if(this.autoplay != false){
			this.autoplay = setInterval(this.autoClick, this.autoInterval);
		}
	}; // initAutoplay()
	
	this.initXML = function(o){
		if(that.xmlLink != false){
			var oResults = o.responseXML.getElementsByTagName('special');
			for(var i=0; i<oResults.length; i++){
				var oTitle = oResults[i].getElementsByTagName('description')[0].childNodes[0].nodeValue;
				var oPrice = oResults[i].getElementsByTagName('price')[0].childNodes[0].nodeValue;
				var oMPG = oResults[i].getElementsByTagName('mpg')[0].childNodes[0].nodeValue;
				if(oResults[i].getElementsByTagName('year').length > 0){
					var oYear = oResults[i].getElementsByTagName('year')[0].childNodes[0].nodeValue;
				} else {
					var oYear = new Date().getFullYear();
				}
				var oURL = oResults[i].getElementsByTagName('url')[0].childNodes[0].nodeValue;
				that.xmlObject[oTitle] = {
					model: oTitle,
					price: oPrice,
					mpg: oMPG,
					year: oYear,
					url: oURL
				};
			}
			if(typeof(that.xmlLink) == 'string'){
				that.xmlLink = document.getElementById(that.xmlLink);
			} else {
				var oTemp = [];
				for(var i=0; i<that.xmlLink.length; i++){
					oTemp.push(document.getElementById(that.xmlLink[i]));
				}
				that.xmlLink = oTemp;
			}
			if(typeof(that.xmlPrice) == 'string'){
				that.xmlPrice = document.getElementById(that.xmlPrice);
			}
			if(typeof(that.xmlMPG) == 'string'){
				that.xmlMPG = document.getElementById(that.xmlMPG);
			}
		}
	}; // initXML()
	
	// Init:
	
	this.init = function(){
		var delay = function(o){
			that.initSlider();
			that.initHeading();
			that.initImage();
			that.initInventory();
			that.initResearch();
			that.initQuote();
			that.initIncentives();
			that.initAutoplay();
			if(typeof(o) != 'undefined'){
				that.initXML(o);
			}
		};
		if(this.xmlLink != false){
			var oRequest = YAHOO.util.Connect.asyncRequest('GET', this.xmlPath, {
				success:  delay,
				failure: function(){}
			}, null);
		} else {
			delay();
		}
	}; // init()
	
};

})();