/*$(window).load(function () {
  //if( cObj ) cObj.fadeIn(); /// not sure i have to do this, but it's a redundancy with no bad side-effects
});*/

var isLoaded = true;
$(window).unload(function () {
  isLoaded = false;
});

var isRunning = true;

// document width and height
var doch;
var docw;
var docRatio; // w/h

var iNum; // number of images
var cIndex; // index of cObj in collection slideObjs
var cObj;
var slideObjs;

$(document).ready(function(){
	$(window).trigger("resize");
	cIndex = 0;
	slideObjs = new Array();
	$('.introImg').each( function( i ) {
		slideObjs.push( new Slide( $(this), i ) );
	});
	iNum = slideObjs.length;
	cObj = slideObjs[ cIndex ];
	cObj.fadeIn();
});
function onSlideOut( who ){
	return;	/// NO ACTION WHEN CROSS FADING
}
function onSlideIn( who ){ /// ONLY CALLED BY FIRST IMAGE
	if(isRunning && (cIndex == who.index)) setTimeout( doNext, 3500 );
}
function doNext(){
	if(!isRunning) return; // INTERRUPT
	
	var prevObj = cObj;
	cIndex++;
	if( cIndex >= iNum ){	
		onFadeOutFinal();
		return;
	}
	cObj = slideObjs[ cIndex ];
	cObj.fadeIn();
	prevObj.fadeOut();
}

////////// Slide object
Slide = function( meDiv, indx ){
	this.div = meDiv;
	this.index = indx;
	this.h = 1;
	this.w = 1;
	this.ratio = 0; // until loaded
	
	this.meImg = $(this.div.find("img")[0]);
	this.isImgLoaded;
	
	if( this.index == 0 ){
		this.div.css("visibility", "visible"); // for back button functionality
	} else {
		this.div.css("visibility", "hidden");
	}
	this.isImgHidden = 1; 
	
	var me = this; // LOCAL SCOPING

	this.onImgLoaded = function(){
		this.isImgLoaded = true;
		this.setSizes();
		if( this.index == cIndex ) this.fadeIn();
	}

	this.fadeIn = function(){
		if( !this.isImgLoaded || this.isImgHidden != 1 || !this.meImg.attr('complete')) return; 
		this.div.css("visibility", "visible");
		this.resize();
		this.isImgHidden = 0;
		me.onFadeIn();
	}
	
	this.fadeOut = function( ){
		this.div.css("visibility", "hidden");
		me.onFadeOut();
	}	
	
	this.onFadeIn = function(){
		onSlideIn( this );
	}
	this.onFadeOut = function(){
		if(this.index != 0) onSlideOut( this );
	}
	this.setSizes = function(){
		this.w = this.meImg.attr( "width" );
		this.h = this.meImg.attr( "height" );
		this.ratio = this.w/this.h;
	}
	this.resize = function( ){
		if( this.ratio <= docRatio ){
			if( this.w != docw ){
				this.meImg.attr( "width", docw );
				this.meImg.attr( "height", docw/this.ratio );
				this.setSizes(); /// this is processor intensive; do a little as possible
			}
		} else {
			if( this.h != doch ){
				this.meImg.attr( "height", doch );
				this.meImg.attr( "width", doch * this.ratio);				
				this.setSizes();
			}		
		}
	}
	
	/// CHECK FOR IMAGE BEING LOADED
	if( this.meImg.attr('complete') ){
		this.isImgLoaded = true;
		this.onImgLoaded();
	} else {
		this.isImgLoaded = false;
		/*this.meImg.load(function () {
			me.onImgLoaded();
		});*/
	}	
	this.meImg.load(function () {
		me.onImgLoaded();
	});
	
	this.setSizes();
}

$(window).resize(function() {
	doch = $(document).height();
	docw = $(document).width();
	docRatio = docw/doch; ///not good enough to just define height; need to determine which dimension to use
	debug("window.resize")
	if(cObj) cObj.resize( doch, docw )
});

function onFadeOutFinal(){
	location.href = "about.html";
}
