Array.prototype.indexLookUp = function(value){
	for (i=0; i<this.length; i++){
		if (this[i]==value){
			return i;
		}
	}
	return false;
}


// see http://video.yahoo.com/watch/111593 for null versus undefined
var sCurrentCreatorType 	= '';
var sCurrentWork		 	= '';
var arrPaginateAnimator 	= new Array();
var arrPaginateIllustrator 	= new Array();
var swfSpinner;

function previous(){
	if (sCurrentCreatorType == 'animator'){
		//alert('animator');
		if (sCurrentWork == ''){
			sCurrentWork = arrPaginateAnimator[arrPaginateAnimator.length-1];
		} else if (sCurrentWork == arrPaginateAnimator[0]){
			sCurrentWork = arrPaginateAnimator[arrPaginateAnimator.length-1];
		} else {
			var currentIndex = arrPaginateAnimator.indexLookUp(sCurrentWork);
			sCurrentWork = arrPaginateAnimator[currentIndex-1];
		}
		//alert (sCurrentWork);
		selectAnimatorContent(sCurrentWork);
	} else if (sCurrentCreatorType == 'illustrator'){
		//alert('illustrator');
		if (sCurrentWork == ''){
			sCurrentWork = arrPaginateIllustrator[arrPaginateIllustrator.length-1];
		} else if (sCurrentWork == arrPaginateIllustrator[0]){
			sCurrentWork = arrPaginateIllustrator[arrPaginateIllustrator.length-1];
		} else {
			var currentIndex = arrPaginateIllustrator.indexLookUp(sCurrentWork);
			sCurrentWork = arrPaginateIllustrator[currentIndex-1];
		}
		//alert (sCurrentWork);
		selectIllustratorContent(sCurrentWork);
	} else {
		alert('Please select an animator or illustrator first.');
	}
}
function next(){
	if (sCurrentCreatorType == 'animator'){
		//alert('animator');
		if (sCurrentWork == ''){
			sCurrentWork = arrPaginateAnimator[0];
		} else if (sCurrentWork == arrPaginateAnimator[arrPaginateAnimator.length-1]){
			sCurrentWork = arrPaginateAnimator[0];
		} else {
			var currentIndex = arrPaginateAnimator.indexLookUp(sCurrentWork);
			sCurrentWork = arrPaginateAnimator[currentIndex+1];
		}
		//alert (sCurrentWork);
		selectAnimatorContent(sCurrentWork);
	} else if (sCurrentCreatorType == 'illustrator'){
		//alert('illustrator');
		if (sCurrentWork == ''){
			sCurrentWork = arrPaginateIllustrator[0];
		} else if (sCurrentWork == arrPaginateIllustrator[arrPaginateIllustrator.length-1]){
			sCurrentWork = arrPaginateIllustrator[0];
		} else {
			var currentIndex = arrPaginateIllustrator.indexLookUp(sCurrentWork);
			sCurrentWork = arrPaginateIllustrator[currentIndex+1];
		}
		//alert (sCurrentWork);
		selectIllustratorContent(sCurrentWork);
	} else {
		alert('Please select an animator or illustrator first.');
	}
}


function selectAnimatorContent(name){
	// check to see if change of sCurrentCreatorType is needed.. if so sent ajax request to change session variable
	if (sCurrentCreatorType != 'animator'){
		var url = '/public/setCurrentCreatorType/animator';
		//alert(url);
		$.ajax({
		    url: url,
		    type: 'GET',
		    dataType: 'html',
		    timeout: 120000,
			beforeSend: function (XMLHttpRequest) {
				//show spinner
				toggleSpinner (true);
			},
			error: function (XMLHttpRequest, textStatus, errorThrown) {
		        alert('Error setting setCurrentCreatorType');
			},
		    success: function(data){
		    },
			complete: function (XMLHttpRequest, textStatus) {
				toggleSpinner (false);
			}

		});
	}

	// set sCurrentCreatorType
	sCurrentCreatorType = 'animator';
	sCurrentWork = name;
	$('#content').html ('');
	$('#content').flash({
	    src: '/flash/player.swf',
		flashvars: {movie: '/files/'+name},
		wmode: "transparent", // needed for div over flash
		allowFullScreen: 'true',
		width: 425,
	    height: 400
	});
	
}
function selectIllustratorContent(name){
	// check to see if change of sCurrentCreatorType is needed.. if so sent ajax request to change session variable
	if (sCurrentCreatorType != 'illustrator'){
		var url = '/public/setCurrentCreatorType/illustrator';
		//alert(url);
		$.ajax({
		    url: url,
		    type: 'GET',
		    dataType: 'html',
		    timeout: 120000,
			beforeSend: function (XMLHttpRequest) {
				//show spinner
				toggleSpinner (true);
			},
			error: function (XMLHttpRequest, textStatus, errorThrown) {
		        alert('Error setting setCurrentCreatorType');
			},
		    success: function(data){
		    },
			complete: function (XMLHttpRequest, textStatus) {
				toggleSpinner (false);
			}

		});
	}
	// set sCurrentCreatorType
	sCurrentCreatorType = 'illustrator';
	sCurrentWork = name;
	$("#content").html('<img src="/files/'+name+'" alt=""/>');
}


$(document).ready(function(){
	// create tooltip for content on startup
	$('#animatorsInner > ul > li > a').tooltip({
		track: false,
		delay: 0,
		showURL: false,
		opacity: 1.00
	});
	$('#animatorsPreviewInner > a').tooltip({
		track: false,
		delay: 0,
		showURL: false,
		opacity: 1.00
	});
	$('#illustratorsInner > ul > li > a').tooltip({
		track: false,
		delay: 0,
		showURL: false,
		opacity: 1.00
	});
	$('#illustratorsPreviewInner > a').tooltip({
		track: false,
		delay: 0,
		showURL: false,
		opacity: 1.00
	});

	/*
	// jquery.flash doesn't insert object tag so ie crashes on external interface
	$('#divSpinner').html('');
	$('#divSpinner').flash({
	    src: '/flash/spinner.swf',
		id:	'spinner',
		wmode: "transparent", // needed for div over flash
		width: 454,
	    height: 154
	});
	*/
	// create reference to flash spinner
	if(navigator.appName.indexOf("Microsoft") != -1) {
		swfSpinner = window.spinner;
	}else {
		swfSpinner = window.document.spinner;
	}
});

function toggleSpinner(bool){
	if (bool){
		swfSpinner.myStart();
	} else {
		swfSpinner.myStop();
	}
}



function selectAnimator (id){

	var url = '/public/animatorsMasterpieces/'+id;
	//alert(url);
	$.ajax({
	    url: url,
	    type: 'GET',
	    dataType: 'html',
	    timeout: 120000,
		beforeSend: function (XMLHttpRequest) {
			//this; // the options for this ajax request
			//show spinner
			toggleSpinner (true);
			// toggle color
			$("#animators span.color1").removeClass().addClass("color0");
			$("#animator" + id).removeClass().addClass("color1");
			//remove preview
			$("#animatorsPreviewInner").html ('');
		},
		error: function (XMLHttpRequest, textStatus, errorThrown) {
			// typically only one of textStatus or errorThrown 
			// will have info
			//this; // the options for this ajax request
	        alert('Error loading data');
		},
	    success: function(data){
			// create thumbnails
			$("#animatorsPreviewInner").html (data);
			// set array for previous and next buttons
			// empty array
			arrPaginateAnimator.length = 0;
			$("#animatorsPreviewInner a").each (function(i){
				//alert (this.id);
				arrPaginateAnimator[arrPaginateAnimator.length] = this.id;
			});
			//alert (arrPaginateAnimator);
			
			// set sCurrentCreatorType
			sCurrentCreatorType = 'animator';
			sCurrentWork = '';

	    },
		complete: function (XMLHttpRequest, textStatus) {
			//this; // the options for this ajax request
			$('#animatorsPreviewInner > a').tooltip({
				track: false,
				delay: 0,
				showURL: false,
				opacity: 1.00
			});
			//update scrollbar
			CSBfleXcroll('animatorsPreview');
			
			//var myDiv = document.getElementById("animatorsPreview"); 
			//if(myDiv!=null&&myDiv.scrollUpdate) myDiv.scrollUpdate(); 
			// hide spinner
			toggleSpinner (false);
		}

	});
}
function selectIllustrator (id){
	var url = '/public/illustratorsMasterpieces/'+id;
	//alert (id);
	$.ajax({
	    url: url,
	    type: 'GET',
	    dataType: 'html',
	    timeout: 120000,
		beforeSend: function (XMLHttpRequest) {
			//this; // the options for this ajax request
			//show spinner
			toggleSpinner (true);
			// toggle color
			$("#illustrators span.color1").removeClass().addClass("color0");
			$("#illustrator" + id).removeClass().addClass("color1");
			//remove preview
			$("#illustratorsPreviewInner").html ('');
		},
		error: function (XMLHttpRequest, textStatus, errorThrown) {
			// typically only one of textStatus or errorThrown 
			// will have info
			//this; // the options for this ajax request
	        alert('Error loading data');
		},
	    success: function(data){
			// create thumbnails
			$("#illustratorsPreviewInner").html (data);
			// set array for previous and next buttons
			// empty array
			arrPaginateIllustrator.length = 0;
			$("#illustratorsPreviewInner a").each (function(i){
				//alert (this.id);
				arrPaginateIllustrator[arrPaginateIllustrator.length] = this.id;
			});
			//alert (arrPaginateIllustrator);
			// set sCurrentCreatorType
			sCurrentCreatorType = 'illustrator';
			sCurrentWork = '';
	    },
		complete: function (XMLHttpRequest, textStatus) {
			//this; // the options for this ajax request
			$('#illustratorsPreviewInner > a').tooltip({
				track: false,
				delay: 0,
				showURL: false,
				opacity: 1.00
			});
			//update scrollbar
			CSBfleXcroll('illustratorsPreview');
			/*
			var myDiv = document.getElementById("illustratorsPreview");
			if(myDiv!=null&&myDiv.scrollUpdate){
				myDiv.scrollUpdate();
				CSBfleXcroll('mycustomscroll');
				alert('updating scroll');
			} else {
				alert ('hmm');
			}
			*/
			// hide spinner
			toggleSpinner (false);
		}

	});
}