var curI = 0;
var totalNum;
/*
window.addEvent('domready', function() {
	curI = 0;	
	for (var i=0;i<totalNum;i++) {
		var tempDiv = "theImg"+i;	
		if (i>0) {
			$(tempDiv).setStyle('opacity',0)			
		} else {
			$(tempDiv).setStyle('opacity',1)
		}
	}
	playIt();
	
	setEvents();
	
});*/

function setEvents () {
	$('nextBtn').addEvents({
		mouseover: function() {this.set('src','images/HTMLsite/ctrl_04_2.jpg')},
		mouseout: function() {this.set('src','images/HTMLsite/ctrl_04.jpg')}
	});
	$('prevBtn').addEvents({
		mouseover: function() {this.set('src','images/HTMLsite/ctrl_01_2.jpg')},
		mouseout: function() {this.set('src','images/HTMLsite/ctrl_01.jpg')}
	});
	$('playBtn').addEvents({		
		mouseover: function() {if (!itsPlaying) this.set('src','images/HTMLsite/ctrl_03_2.jpg')},
		mouseout: function() {if (!itsPlaying) this.set('src','images/HTMLsite/ctrl_03.jpg')}
	});
	$('stopBtn').addEvents({
		mouseover: function() {if (itsPlaying) this.set('src','images/HTMLsite/ctrl_02_2.jpg')},
		mouseout: function() {if (itsPlaying) this.set('src','images/HTMLsite/ctrl_02.jpg')}
	});
	
	$('dBtn').getElement('img').addEvents({
		mouseover: function() { this.set('src','images/HTMLsite/downloadBtn_2.jpg')},
		mouseout: function() { this.set('src','images/HTMLsite/downloadBtn.jpg')}
	});
	if ($('vBtn')) {
		$('vBtn').getElement('img').addEvents({
			mouseover: function() { this.set('src','images/HTMLsite/vidBtn2.jpg')},
			mouseout: function() { this.set('src','images/HTMLsite/vidBtn.jpg')}
		});
	}
}


function show(fromWhere) {
	if (fromWhere == "btn") {
		stopIt();	
	}
	vDiv = "theImg"+curI;
	if (theDir == "next") {
		curI++;	
		if (curI == totalNum) {
			curI = 0;	
		}
	} else {
		curI--;
		if (curI < 0) {
			curI = totalNum-1;	
		}
	}

	tDiv = "theImg"+curI;
		
	//alert (vDiv + ":"+$(vDiv).getStyle('opacity') + " :: " + tDiv + ":"+$(tDiv).getStyle('opacity'));
	$(tDiv).tween('opacity', 1);
	$(vDiv).tween('opacity', 0);

}

var thePlayer;
var theDir;
var itsPlaying = false;

function playIt(totalNum) {
	clearInterval(thePlayer);
	if (totalNum > 1) {
		thePlayer = setInterval(show, 4000);
	}	
	itsPlaying = true;
	theDir = "next";
	
	$('playBtn').set('src', "images/HTMLsite/ctrl_03_2.jpg");
	$('stopBtn').set('src', "images/HTMLsite/ctrl_02.jpg");
	
}


function stopIt () {	
	clearInterval(thePlayer);
	itsPlaying = false;
	
	$('playBtn').set('src', "images/HTMLsite/ctrl_03.jpg");
	$('stopBtn').set('src', "images/HTMLsite/ctrl_02_2.jpg");

}


function processProds (theID) { 
	var ajaxRequest = null;  // The variable that makes Ajax possible!
	//var ajaxRequest;
	try {
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e) {
		// Internet Explorer Browsers
		try {
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	
	var pageDisplay = document.getElementById('galleryContent');
	var extraDisplay = document.getElementById('theExtra');
	pageDisplay.innerHTML =	"<div id=\'thePage\'></div>";
	
	ajaxRequest.onreadystatechange = function () {
		if(ajaxRequest.readyState == 4 || ajaxRequest.readyState=="complete"){			
			var theStuff = ajaxRequest.responseText;
			temp = theStuff.split("||||");
			thePageStuff = "<div id=\'thePage\' style=\'background-image: url(../images/HTMLsite/bg_02_white.jpg);	background-repeat: no-repeat;\'>" + temp[0];
			theExtraStuff = temp[1];
			totalNum = temp[2];
			pageDisplay.innerHTML = eval('thePageStuff');	
			extraDisplay.innerHTML = eval('theExtraStuff');	
			curI = 0;			
			for (var i=0;i<totalNum;i++) {
				var tempDiv = "theImg"+i;	
				if (i>0) {
					$(tempDiv).setStyle('opacity',0)			
				} else {
					$(tempDiv).setStyle('opacity',1)
				}
			}
			playIt(totalNum);
			
			setEvents();
		}
	}
	
	var queryString="phpCode/processProds.php";
	queryString=queryString+"?theID="+theID;
	queryString=queryString+"&sid="+Math.random();
	
	ajaxRequest.open("GET", queryString, true);
	ajaxRequest.send(null); 
	
}


function directURL (nnn) {
	totalNum = nnn;
	curI = 0;
	for (var i=0;i<totalNum;i++) {
		var tempDiv = "theImg"+i;	
		if (i>0) {
			$(tempDiv).setStyle('opacity',0)			
		} else {
			$(tempDiv).setStyle('opacity',1)
		}
	}
	playIt(totalNum);
	
	setEvents();
}


