var isIE = (document.all ? true : false);

// Check for incompatible browsers
if(	(navigator.userAgent.indexOf('Mac') != -1 && isIE)			//IE on Mac
	|| !document.getElementById || !document.createTextNode		// We need to use these, make sure we can!
	|| (navigator.userAgent.indexOf("MSIE 6.") != -1)) {		// Redirect IE6 for now
	window.location.href='/bad_browser.html';
}

var curIndex = 0;		
var total = 0;
var stop = 0;
var curImg = "";
var mouse = {'x':0, 'y':0};
var arrow;
var rollover_img;
var images_to_preload = ['arrow_left.png', 'arrow_right.png'];

window.onresize = reload;
var images = [];

function scrollRight(lft){
	btn = document.getElementById("thumbin");
	if(parseInt(btn.style.left) > -(total*51 - 760)) {
		//calc and do step
		btn.style.left= (parseInt(btn.style.left)-parseInt(lft))+ "px";
		if(stop==0) {
			window.setTimeout('scrollRight('+lft+')',20);
		} else if(lft>.2) {
			window.setTimeout('scrollRight('+(lft-.2)+')',20);
		}
	}
}
function scrollLft(rght){
	btn = document.getElementById("thumbin");
	if(parseInt(btn.style.left) < 0) {
		//calc and do step
		document.getElementById("thumbin").style.left= (parseInt(document.getElementById("thumbin").style.left)+parseInt(rght))+ "px";
		if(stop==1) {
			window.setTimeout('scrollLft('+rght+')',20);
		} else if(rght>.2) {
			window.setTimeout('scrollLft('+(rght-.2)+')',20);
		}
	}
}		

function init() {
	for(var x=0; x<images_to_preload.length; x++) {
		var img = new Image();
		img.src = images_to_preload[x];
	}
	
	rollover_img = document.getElementById('rollover');
	var thumbs = document.getElementById('thumbin');
	if(thumbs) {
		thumbs.style.width=(total*51)+'px';
		thumbs.style.left=0;
		if( navigator.userAgent.indexOf("MSIE") != -1 ) {
			document.getElementById("thumbnails").style.left = "-1px";
		}
	}
	
	if(document.getElementById('thumbnails')) {
		arrow = document.createElement('div');
		arrow.setAttribute('class', 'left_arrow');
		arrow.style.display = 'none';
		document.body.appendChild(arrow);

		if (!isIE) {
			document.captureEvents(Event.MOUSEMOVE);
			document.captureEvents(Event.MOUSEOUT);
		}
		rollover_img.onmousemove = getMouseXY;
		arrow.onmousemove = getMouseXY;
		arrow.onclick = arrowClick;
	}
}

function arrowClick(e) {
	if(arrow.className == 'left_arrow') {
		previous();
	} else {
		next();
	}
}

function getMouseXY(e) {
	var tempX, tempY;
	if (isIE) { // grab the x-y pos.s if browser is IE
		tempX = event.clientX + document.body.scrollLeft;
		tempY = event.clientY + document.body.scrollTop;
	} else {  // grab the x-y pos.s if browser is NS
		tempX = e.pageX;
		tempY = e.pageY;
	}  
	// catch possible negative values in NS4
	if (tempX < 0) {tempX = 0;}
	if (tempY < 0) {tempY = 0;}
	mouse.x = tempX;
	mouse.y = tempY;
	
	var img_x = findPosX(rollover_img);
	var img_y = findPosY(rollover_img);
	
	// Should we hide?
	if( mouse.x < img_x || mouse.x > img_x + rollover_img.width ||
		mouse.y < img_y || mouse.y > img_y + rollover_img.height) {
		arrow.style.display = 'none';
	} else if(arrow.style.display == 'none') {
		arrow.style.display = '';
	}
	
	// Find out what class to use
	if( mouse.x > (img_x + rollover_img.width / 2)) {
		if(arrow.className != 'right_arrow') {
			arrow.setAttribute('class', 'right_arrow');
		}
	} else if(arrow.className != 'left_arrow') {
		arrow.setAttribute('class', 'left_arrow');
	}
	
	arrow.style.top = (mouse.y - 30) + 'px';
	arrow.style.left = (mouse.x - 18) + 'px';
	return true;
}

function previous() {
	curIndex--;
	if (curIndex < 0) { curIndex=total-1; }
	preload(curIndex);
}
function next() {
	curIndex++;
	if (curIndex > total-1) { curIndex=0; }
	preload(curIndex);
}
function reload() {
	//If the site is disabled this won't exist, so do nothing
	var loader = document.getElementById('loading');
	if(loader != null){
		preload(curIndex);
	}
}		
function preload(ind) {
	 loader = document.getElementById('loading');
	 setOpacity(loader, 100);
	 curIndex=ind;
	 pic = new String;
	 document.rollover.src = "blank.gif";
	 img = new Image();
	 img.src = 'images/' + images[ind][0];
	 curImg = images[ind][0];
	 document.rollover.height=images[ind][1];
	 document.rollover.width=images[ind][2];

	 imageId = 'rollover';
	 image2 = document.getElementById(imageId);
	 image2.style.visibility = 'visible';
		setOpacity(image2, 0);
		plus=0;
	 if(navigator.userAgent.indexOf("Firefox")!=-1){plus=8;}
	 
	 var addHeight = (isIE ? 100 : 0);
	 
	 var leftB = document.getElementById('left');
	 if(leftB) {
		 leftB.style.left=findPosX(image2) + 'px';
		 leftB.style.top=findPosY(image2)-plus + addHeight + 'px';
		 leftB.style.width = (image2.width/2) + 'px';

		 var rightB = document.getElementById('right');
		 rightB.style.top=findPosY(image2)-plus + addHeight + 'px';
		 rightB.style.left=(findPosX(image2)+(image2.width/2)+1) + 'px';
		 rightB.style.width = (image2.width/2) + 'px';
	 }

	 if(!img.complete) {
		 img.onload = function()
		 {
			setOpacity(loader, 0);
			document.rollover.height=images[ind][1];
			document.rollover.width=images[ind][2];
			document.rollover.src= "images/" + images[ind][0];
			fadeIn(imageId,0,curImg);
		}
	} else {
		setOpacity(loader, 0);
		document.rollover.height=images[ind][1];
		document.rollover.width=images[ind][2];
		document.rollover.src= "images/" + images[ind][0];
		fadeIn(imageId,0,curImg);
	}
}
function setOpacity(obj, opacity) {
  opacity = (opacity == 100)?99.999:opacity;

  // IE/Win
  obj.style.filter = "alpha(opacity:"+opacity+")";
  
  // Safari<1.2, Konqueror
  obj.style.KHTMLOpacity = opacity/100;

// Older Mozilla and Firefox
  obj.style.MozOpacity = opacity/100;

  // Safari 1.2, newer Firefox and Mozilla, CSS3
  obj.style.opacity = opacity/100;
}
function fadeIn(objId,opacity,src) {
	if(src==curImg) {
		obj = document.getElementById(objId);
		if (opacity <= 100) {
			setOpacity(obj, opacity);
			opacity += 10;
			window.setTimeout("fadeIn('"+objId+"',"+opacity+",'"+src+"')", 10);
		}
	}
}
function findPosX(obj) {
	plus=0;
	var curleft = 0;
	if(obj.offsetParent)
		while(1) {
			curleft += obj.offsetLeft;
			if(!obj.offsetParent)
				break;
			obj = obj.offsetParent;
		}
	else if(obj.x)
		curleft += obj.x;
	return (curleft+plus);
}

function removeElement(elem) {
	elem.parentNode.removeChild(elem);
}

function findPosY(obj) {
	var curtop = 0;
	if(obj.offsetParent)
		while(1) {
			curtop += obj.offsetTop;
			if(!obj.offsetParent)
				break;
			obj = obj.offsetParent;
		}
	else if(obj.y)
		curtop += obj.y;
	return curtop;
}