// The order of display : True - Random / False - Sequential
var randomOrder = false;
// Default Width of the image (if not set individually)
var defaultWidth = 160;
// Default Height of the image (if not set individually)
var defaultHeight = 160;
// An array of the images to be rotated (Image Path[, Width of the Image[, Height of the Image]])
// If the width and height is not specified, the Default value specified above will be used
var isRotating = false;
var rotatingID = 0;
var arImg = new Array();
var arLink = new Array();
var adIndex = 0;
var adLength = 0;
var timesRotated = 0;
var rotateLimit = 0;
var rotateInterval = 5000;
var fadeInReady = true;
var fadeOutReady = true;
var timesFlashed = 0;

function buyFlash() {
	if (timesFlashed < 8) {	
		if (document.getElementById('coursesTable').className == "flash") {
			document.getElementById('coursesTable').className = "";
		} else {
			document.getElementById('coursesTable').className = "flash";
		}
		timesFlashed++;
		setTimeout(buyFlash, 170);
	}
}
function openCourse() {
	popupWin = window.open('http://www.integrative-healthcare.org/popcourse.html', '', 'scrollbars,resizable,width=380,height=610');
}
function viewLarger(picName, width, height) {
	popupWin = window.open('http://www.integrative-healthcare.org/googLand/'+picName, '', 'width='+width+',height='+height);
}

function rotateImage(type, direction){
	if (fadeInReady && fadeOutReady) {
		fadeInReady = fadeOutReady = false;
		$('#readyStatus').val("Not ready");
		var tempIndex = adIndex;
		//if (type == "manual" || timesRotated == rotateLimit) {
		if (type == "manual") {
			clearInterval(rotatingID);
		}
		/*
		} else {
			timesRotated++;
		}
		*/
		if (direction == 'next') {
			adToFadeIn = ++adIndex % adLength;
			if (adIndex == adLength) { adIndex = 0; }
		} else {
			if (adIndex == 0) {
				adToFadeIn = adIndex = adLength-1;
			} else {
				adToFadeIn = --adIndex % adLength;
			}
		}
		$('#adImages').children().eq(tempIndex).fadeOut("slow",function () {fadeOutReady = true; $('#readyStatus').val("Ready");});
		$('#adImages').children().eq(adToFadeIn).fadeIn("slow",function () {fadeInReady = true; $('#readyStatus').val("Ready");});
	}
}

$(document).ready(function () {
	adLength = $('#adImages').children().length;
	rotateLimit = adLength - 1;
	if (adLength > 1) {
		rotatingID = setInterval("rotateImage('automatic','next')", rotateInterval);
	} else {
		fadeInReady = fadeOutReady = false;
		$("#adNav").hide();
	}
	$("div#adImages").bind("mouseenter", function(e){
		clearInterval(rotatingID);
	});
	$("div#adImages").bind("mouseleave", function(e){
		rotatingID = setInterval("rotateImage('automatic','next')", rotateInterval);
	});
	$("#q").autocomplete(courses, {matchContains:true});
});