

//detect if iphone
if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)))
{
location.href='/mobile/default.aspx';
}



////////////////////////// START: general functions ////////////////////////////////
function randomString(intLength) {
	var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
	var string_length = intLength;
	var randomstring = '';
	for (var i=0; i<string_length; i++) {
		var rnum = Math.floor(Math.random() * chars.length);
		randomstring += chars.substring(rnum,rnum+1);
	}
	return randomstring;
}

function getQueryVariable(variable) {  
	var query = window.location.search.substring(1); 
	var vars = query.split("&");
	for (var i=0;i<vars.length;i++) {
		var pair = vars[i].split("="); 
		if (pair[0] == variable) { 
			return pair[1];  
		}
	}
}

function getQueryVariableFromString(variable,strURL) {  
	var query = strURL; 
	var vars = query.split("&");
	for (var i=0;i<vars.length;i++) {
		var pair = vars[i].split("="); 
		if (pair[0] == variable) { 
			return pair[1];  
		}
	}
}


////////////////////////// END: general functions ////////////////////////////////


var strControlPrefix;
strControlPrefix = "_ctl0_";


var mCurrentVIN;
mCurrentVIN = "";

var intIntervalID;
var intIntervalMilliseconds = 5000;


var arySpecialData;
var intCurrentIndex;
var intervalRotator;



function GetInternetSpecials(){

	intCurrentIndex = -1;
	//clearInterval(intervalPhotoRotator);
	arySpecialData = null;
	
	var url;
	url = 'ajax/hp-layout2-getInternetSpecials.aspx?r='+randomString(10);
	
	var http_request = false;

	if (window.XMLHttpRequest) { // Mozilla, Safari,...
			http_request = new XMLHttpRequest();
			if (http_request.overrideMimeType) {
					http_request.overrideMimeType('text/xml');                    
			}
	} else if (window.ActiveXObject) { // IE
			try {
					http_request = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
					try {
							http_request = new ActiveXObject("Microsoft.XMLHTTP");
					} catch (e) {}
			}
	}

	if (!http_request) {
			alert('Giving up :( Cannot create an XMLHTTP instance');
			return false;
	}

	http_request.onreadystatechange = function() { analyzeResponse_GetInternetSpecials(http_request); };
	http_request.open('GET', url, true);
	http_request.send(null);

}




function analyzeResponse_GetInternetSpecials(http_request) {
		//alert('Ready State: '+http_request.readyState+'\nHTTP Status: '+http_request.status);
		if ((http_request.readyState == 4) && (http_request.status == 200)) {
				
			var strResponse;
			strResponse = http_request.responseText;	
			
			arySpecialData = strResponse.split("\n");
			
			RotatePhotos(1);
			intervalRotator = setInterval('RotatePhotos(1)', 5000);
					
		
		}
}	



function RotatePhotos(intDirection){


	

	if (intDirection > 0){
		intCurrentIndex += 1;
	}
	else{
		intCurrentIndex -= 1;
	}
	
	if (intCurrentIndex < 0){
		intCurrentIndex = arySpecialData.length-1;
	}
			
	if (intCurrentIndex >= arySpecialData.length){
		intCurrentIndex = 0
	}
	
	//alert('about to show: '+intCurrentIndex);
	//alert(aryAllPhotoURLs[intCurrentIndex]);
	
	var aryVehicleData;
	aryVehicleData = arySpecialData[intCurrentIndex].split("|");
	
	
	//alert('special data: '+arySpecialData.length);
	//alert('vehicle data: '+aryVehicleData.length);
	//alert('current index: '+intCurrentIndex);
	
	
	
	//load photo
	var strURLToPhoto;
	strURLToPhoto = "/img/getVehiclePhoto.aspx?return=FIRSTFOUND&maxwidth=195&maxHeight=140&vin="+aryVehicleData[0];
	
	var strURLToVehiclePage;
	strURLToVehiclePage = "/inventory/Used-Vehicle-Detail-Page.aspx?vin="+aryVehicleData[0];
	
	
	var imgSpecialPhoto;
	imgSpecialPhoto = document.getElementById('imgSpecialPhoto');
	imgSpecialPhoto.src=strURLToPhoto;
	
	var ancSpecialVehiclePhoto;
	ancSpecialVehiclePhoto = document.getElementById('ancSpecialVehiclePhoto');
	ancSpecialVehiclePhoto.href=strURLToVehiclePage;
	
	var ancSpecialVehicleText;
	ancSpecialVehicleText = document.getElementById('ancSpecialVehicleText');
	ancSpecialVehicleText.href=strURLToVehiclePage;
	
	var divSpecialYearMakeModel;
	divSpecialYearMakeModel = document.getElementById('divSpecialYearMakeModel');
	divSpecialYearMakeModel.innerHTML = aryVehicleData[1];
	
	var divSpecialPrice;
	divSpecialPrice = document.getElementById('divSpecialPrice');
	divSpecialPrice.innerHTML = aryVehicleData[2];
	
					

}


function GoRight(){
	clearInterval(intervalRotator);
	RotatePhotos(1);
}


function GoLeft(){
	clearInterval(intervalRotator);
	RotatePhotos(-1);
}




function CheckForHomepagePopup(){

		var url;
		url = 'ajax/hp-getPopupEnabled.aspx?r='+randomString(10);
		
			
		var http_request = false;

		if (window.XMLHttpRequest) { // Mozilla, Safari,...
				http_request = new XMLHttpRequest();
				if (http_request.overrideMimeType) {
						http_request.overrideMimeType('text/xml');                    
				}
		} else if (window.ActiveXObject) { // IE
				try {
						http_request = new ActiveXObject("Msxml2.XMLHTTP");
				} catch (e) {
						try {
								http_request = new ActiveXObject("Microsoft.XMLHTTP");
						} catch (e) {}
				}
		}

		if (!http_request) {
				alert('Giving up :( Cannot create an XMLHTTP instance');
				return false;
		}

		http_request.onreadystatechange = function() { analyzeResponse_CheckForHomepagePopup(http_request); };
		http_request.open('GET', url, true);
		http_request.send(null);
}

function analyzeResponse_CheckForHomepagePopup(http_request) {
		//alert('Ready State: '+http_request.readyState+'\nHTTP Status: '+http_request.status);
		if ((http_request.readyState == 4) && (http_request.status == 200)) {
				
			var strResponse;
			strResponse = http_request.responseText;				
						
			if (strResponse != ""){				
				eval(strResponse);				
			}		
		
		}
}	



function initHomepage(){	
	//GetInternetSpecials();
	setTimeout("CheckForHomepagePopup()",1000);
}


