// IHF Inductee Slider
/* Resig if you're looking get out now, it's a disaster area in here */

var leftId = 1;
var nextId = 0;
var transId = leftId;
var maxId = 0;
var showing = 0;
var imgwidth = 100;
var transition = 1200;
var interval;
var timeout;
var moving = false;
var isHome = false;


$(document).ready(function () {
	if ($("img.arrow")) {
		$("img.arrow").hover(
			function()
			{
				this.src = this.src.replace("_off","_on");
			},
			function()
			{
				this.src = this.src.replace("_on","_off");
			}
		);
	}
	if ($("img.arrow-home")) {
		$("img.arrow-home").hover(
			function()
			{
				this.src = this.src.replace("_off","_on");
			},
			function()
			{
				this.src = this.src.replace("_on","_off");
			}
		);

	}
});

function init(maxInductees, numShown, aHome) {
	maxId = maxInductees;
	showing = numShown;
	nextId = leftId+showing;
	isHome = aHome;
	imgwidth = $('#in1').outerWidth();
	for (var i=0; i<maxId; i++) {
		$('#in'+(i+1)).css('left',i*imgwidth+'px');
	}
	if (isHome) {
		$('#in'+1).fadeIn(transition);
		$('#in'+2).fadeIn(transition);
		$('#in'+3).fadeIn(transition);
		togBind(true);
		timeout = setTimeout('$(".inductee").show()',transition);
		//timeout = setTimeout('loadFadeIn()',500);
	} else {
		transId = nextId;
		loadFadeIn();
	}
	setTimeout('startInterval()',transition/2);
}

function startInterval() {
	interval = setInterval('intFwd()',transition*showing);
}

function loadFadeIn() {
	if (transId < nextId) {
		$('#in'+transId).fadeIn(transition);
		transId++;
		timeout = setTimeout('loadFadeIn()',transition/2);
	} else {
		$('.inductee').show();
		nextId--; transId--;
		clearTimeout(timeout);
		$('#slideFwd').bind('click',fwd);
		$('#slideBwd').bind('click',bwd);
	}
}

/*function checkId() { // check if image strip has reached beginning or end
	if (transId <= 1) {
		transId = 1;
		nextId = transId+showing;
		leftId = transId;
	}
	if (nextId >= maxId) {
		nextId = maxId;
		leftId = maxId-showing;
		transId = leftId;
	}
}*/

function togBind(isBind)
{
	if (isBind) {
		$('#slideFwd').bind('click',fwd);
		$('#slideBwd').bind('click',bwd);
	} else {
		$('#slideFwd').unbind('click',fwd);
		$('#slideBwd').unbind('click',bwd);
	}
}

function bwd() {
	if (interval)
		clearInterval(interval);
	togBind(false);
	leftId--; nextId--; transId--;
	if (leftId <= 0) leftId = maxId-1;
	$('#in'+leftId).css('left',-imgwidth+'px');
	for (var i=0; i<maxId; i++) {
		$('#in'+i).animate({
			left: '+='+(imgwidth)
		}, transition);
	}
	
	//togBind(true);
	clearTimeout(timeout);
	timeout = setTimeout('togBind(true)',transition);
}

/*function bwd() {
	$('.inductee').fadeOut();
	leftId = nextId;
	nextId = leftId-showing;
	transId = nextId-showing;
	checkId();
	timeout = setTimeout('loadFadeIn()',500);
	$('#slideFwd').unbind('click',fwd);
	$('#slideBwd').unbind('click',bwd);
}*/

function intFwd() {
	togBind(false);
	moving = true;
	leftId++;
	if (leftId >= maxId) leftId = 1;
	
	for (var i=0; i<maxId; i++) {
		if (parseInt($('#in'+i).css('left')) < 0) {
			$('#in'+i).css('left',(maxId-2)*imgwidth+'px');
		}
	}
	for (var i=0; i<maxId; i++) {
		$('#in'+i).animate({
			left: '-='+(imgwidth)
		}, transition);
	}
	clearTimeout(timeout);
	setTimeout('togBind(true)',transition);
	moving = false;
}

function fwd() {
	if (interval)
		clearInterval(interval);

	togBind(false);
	if (!moving) {
		leftId++;
		if (leftId >= maxId) leftId = 1;
		
		for (var i=0; i<maxId; i++) {
			if (parseInt($('#in'+i).css('left')) < 0) {
				$('#in'+i).css('left',(maxId-2)*imgwidth+'px');
			}
		}
		for (var i=0; i<maxId; i++) {
			$('#in'+i).animate({
				left: '-='+(imgwidth)
			}, transition);
		}
		clearTimeout(timeout);
		setTimeout('togBind(true)',transition);
	}
}

function jump(numJumps) {
	togBind(false);
	for (var k=1; k<numJumps; k++) {
		leftId++;
		if (leftId >= maxId) leftId = 1;
		
		for (var i=0; i<maxId; i++) {
			if (parseInt($('#in'+i).css('left')) < 0) {
				$('#in'+i).css('left',(maxId-2)*imgwidth+'px');
			}
		}
		var pLeft = 0;
		for (var j=0; j<maxId; j++) { // back it up!!!
			pLeft = parseInt($('#in'+j).css('left'))-imgwidth;
			$('#in'+j).css('left',pLeft+'px');
		}
	}
	//togBind(true);
	clearTimeout(timeout);
	setTimeout('togBind(true)',transition);
}

/*function fwd() {
	$('.inductee').fadeOut();
	leftId = nextId;
	nextId = leftId+showing;
	checkId();
	timeout = setTimeout('loadFadeIn()',500);
	$('#slideFwd').unbind('click',fwd);
	$('#slideBwd').unbind('click',bwd);
	while (leftId < nextId) {
		$('#in'+leftId).fadeOut(1000);
		leftId++;
	}
	$('#strip').animate({
		left: '-='+(imgwidth*3)
	}, 5000);
	leftId = nextId;
	while (nextId < leftId+3) {
		$('#in'+leftId).fadeIn(1000);
		//$('#in'+nextId).fadeIn(1000);
		nextId++;
	}
}*/

/* =================================== Holy Jesus last minute ajax */
function showInductee(str)
{
	//alert('trigger');
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null) {
		alert ("Your browser does not support AJAX.");
		return;
	}
	var url="scripts/ihf_inductee.php";
	url=url+"?inductee="+str;
	xmlhttp.onreadystatechange=stateChanged;

	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
	toggleActive(str);
}

function toggleActive(num)
{
	$('.inductee').removeClass('iactive');
	$('#in'+num).addClass('iactive');
}

function stateChanged()
{
	if (xmlhttp.readyState==4) {
		//alert('statechanged');
		document.getElementById("load-ind").innerHTML=xmlhttp.responseText;
		$('#load-ind').hide().fadeIn(transition);
	}
}

function GetXmlHttpObject()
{
	var xmlhttp = false;
	if (window.XMLHttpRequest) {
		xmlhttp = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		try {
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				xmlhttp = false;
			}
		}
	}
	return xmlhttp;

}
