/***********************************
*** Slider for Internet Specials *** 
***********************************/

/********************
*** Configuration ***
********************/

//how many units to show at a time on the page:
var unitsShowing = 1;

// if we don't even have at least enough units to show the above number, what is the least number of units to show, before showing alternate content:\
var minUnitsShowing = 1;

// alternate Content to show if not enough units
var alternateContent = '<p>Please Check Back...</p>';

// maximum number of units to scroll through, in total; if your page runs slow, make this number around 10-ish, and test further:
var maxUnits = 10;

// pixels between units (margin):
var unitMargin = 0;

// padding on sides of units:
var unitPadding = 10;

// border-size:
var unitBorder = 0;

// ~milliseconds between unit scrolls:
var scrollDelay = 10000;

// speed of the scroll; higher numbers are slower. (milliseconds between each 1px movement):
var scrollSpeed = 10;

//image sizes:
var imgWidth = 50; // this also needs to be set in the CSS!!!
var imgHeight = 38;

/************************
*** End Configuration ***
************************/

/*
Notes on Variable Naming Conventions from Ray's original prototype:
'bottlesofBeer' is a counter to help the slider's 'pullOneDown' function know whether to keep moving the panel over a pixel at a time;
'pullonedown' moves the slider a pixel at a time, and reduces the 'bottlesofBeer' counter by one for each pixel;
'passonearound' moves the unit at the tail end of the row (the tail is determined by the direction we're scrolling) to the front, so it can scroll into place next.
'magicNumber' is the total width of each unit, including the margin-space between units.
*/

var magicNumber, dur, dur2, bottlesofBeer;
var units = [];
var wait = 0;

// Parses XML data and builds all of the unit divs
function initSpecials(domain,companyID) {
	var parent = document.getElementById("unitSlider");
	var html = parent.innerHTML;
	//alert(html.indexOf("No vehicles match search criteria"));
	var mask = document.getElementById("mask");
	units = document.getElementById("mask").getElementsByTagName("DIV");

	//error handling when determining how many units are available, vs how many are shown:
	if ( (maxUnits > units.length) || (maxUnits == 0) ) {
		maxUnits = units.length;
	}
	if (unitsShowing > (maxUnits-2)) {
		unitsShowing = maxUnits-2;
	}
	
	//if there aren't enough units to show, show alternate content
	if ((unitsShowing < minUnitsShowing) || (html.indexOf("No vehicles match search criteria") > -1)) {
		//alert("show alternate content, please");
		html = '<div id="mask">';
		html += '<div class="unit" style="width:100%">';
		html += alternateContent;
		html += '</div>';
		html += '</div>';
		parent.innerHTML = html;
	} else {
		// Makes sure that the mask-size is divisible by the number of units showing and sets the magicNumber
		var extraSpace = (mask.clientWidth-unitMargin) % unitsShowing;
		var maskWidth = (mask.clientWidth - extraSpace)
		mask.style.width = maskWidth+"px";
		
		magicNumber = ((maskWidth-unitMargin) / unitsShowing);
		
		// displaced Width for determinig sizes
		var displacedWidth = (2*unitBorder) + (2*unitPadding) + (unitMargin);
		
	// Sets the units width and position, and then the last one goes to the left of the starting one, so its ready for the first scrolling
		for(var i=0;i<units.length;i++){
			units[i].style.width = (magicNumber-displacedWidth)+"px";
			units[i].style.left = (i*magicNumber)+"px";
			units[i].style.marginRight = unitMargin+"px";
			units[i].style.marginLeft = unitMargin+"px";
			units[i].style.paddingRight = unitPadding+"px";
			units[i].style.paddingLeft = unitPadding+"px";
			units[i].style.borderWidth = unitBorder+"px";
			if(i==units.length-1){
				units[i].style.left = -1*magicNumber+"px";
			}
		}
		parent.style.width = mask.offsetWidth+"px";
		bottlesofBeer=magicNumber;
	}
}

// function to navigate to the left (-1) or right (1)
function navigate(dir) {
	clearTimeout(dur);
	clearTimeout(dur2);
	if(wait==0){
		reposition(dir);
	}
	
	// Delete this line if you don't want continuous movement after the user has initiated things:
	dur2 = setTimeout("autoRotate()",scrollDelay);
}

// Switch function
function reposition(dir) {
	wait=1;
	if(bottlesofBeer>0){
		setTimeout("pullonedown("+dir+")",scrollSpeed);
	}else{
		passonearound();
		bottlesofBeer=magicNumber;
		wait=0;
	}
}

// Moves all units one pixel left or right
function pullonedown(dir) {
	for(var i=0;i<units.length;i++){
		var left = parseFloat(units[i].style.left);
		left += dir;
		units[i].style.left = left+"px";
	}
	bottlesofBeer--;
	reposition(dir)
}

// Passes trailing units around to the other end, in either direction
function passonearound() {
	for(var i=0;i<units.length;i++){
		var left = parseFloat(units[i].style.left);
		if(left<=-(magicNumber+1)){
			units[i].style.left = (units.length-2)*(magicNumber)+"px";
		}
		if(left>=(units.length-1)*(magicNumber)){
			units[i].style.left = "-"+(magicNumber)+"px";
		}
	}
}

// Kicks things off, and keeps things moving
function autoRotate() {
	reposition(1);
	dur = setTimeout("autoRotate()",scrollDelay);
}

// Adds function to window event
function addEventFunc(event,func) {
	var oldEvent = eval("window.on"+event);
	if(typeof oldEvent != 'function'){
		if(event=="load"){window.onload = func;}
		if(event=="unload"){window.onunload = func;}
		if(event=="resize"){window.onresize = func;}
	}else{
		function newEvent() {  
			if(oldEvent){
				oldEvent();  
			}
			func();
		}
		if(event=="load"){window.onload = newEvent();}
		if(event=="unload"){window.onunload = newEvent();}
		if(event=="resize"){window.onresize = newEvent();}
	}
}