var ilist = document.getElementsByTagName('img');

var ilistLot = new Array;

var ibigFrame;
var pictureFile;
var link = null;
var vin = null;
var isMouseover = 0;

for ( var j = 0; j < ilist.length; j++ ) {
	element = ilist[j];

	if ( element.alt == 'Picture of vehicle on lot' ) {
		ibigFrame = element;
		ibigFrame.setAttribute('onmouseover','javascript:isMouseover=1');
		ibigFrame.setAttribute('onmouseout','javascript:isMouseover=0');
		if ( link == null ) {
			link = element.parentNode.href.split("#");
			link = link[0];
			vin = link.split("vin=")
			vin = vin[1];
		}
	}

	if ( element.getAttribute('alt') == 'Lot Picture' ) {
		element.setAttribute('onmouseover','javascript:setMainPic(this.src);isMouseover=1');
		element.setAttribute('onmouseout','isMouseover=0');
		pictureFile = getFileName(element.src);
		element.parentNode.href = link + "#" + pictureFile;
		element.parentNode.setAttribute('target', "_blank");
		element.parentNode.setAttribute('onclick', "win('/gallery/?vin=" + vin + "#" + pictureFile + "','','scrollbars=yes,resizable=yes,width=1015,height=600'); event.returnValue = false; return false");
		ilistLot.unshift( element );
	}
}

var ibigFrame_origSrc;
if ( ibigFrame ) ibigFrame_origSrc = ibigFrame.src;

//deprecated old style method: http://developer.mozilla.org/en/docs/DOM:element.addEventListener
//ibigFrame.onmouseover = function() { ibigFrame.setAttribute( 'src', ibigFrame_origSrc ) };

ilistLot.sort(
	function (a, b) {
		var src1 = a.src, src2 = b.src;
		if (src1 < src2) { return -1 }
		else if (src1 == src2) { return 0 }
		else { return 1 }
	}
);

if ( ibigFrame ) window.setInterval( 'imageSwap()', 6500 );

var curimage = 0;

function imageSwap () {
	if ( isMouseover == 1 ) return;

	if ( curimage > ilistLot.length - 1 ) curimage = 0

	var element = ilistLot[curimage];
	var src = element.src
	
	var pictureFile = getFileName(src)

	ibigFrame.setAttribute('src', src );

	ibigFrame.parentNode.href = link + "#" + pictureFile;
	ibigFrame.parentNode.setAttribute('onclick', "win('/gallery/?vin=" + vin + "#" + pictureFile + "','','scrollbars=yes,resizable=yes,width=1015,height=600'); event.returnValue = false; return false");
	
	curimage++;
}

function setMainPic (src) {
	ibigFrame.setAttribute('src', src);
}

function getFileName (url) {
	url = url.split("/");
	url = url[url.length - 1];
	return url;
}

