dojo.require("dojo.event.*");
dojo.require("dojo.xml.*");
dojo.require("dojo.widget.*");
dojo.require("dojo.graphics.*");
dojo.require("dojo.graphics.htmlEffects");

function getInnerWidth(win) {
  var winWidth;
  if (win.innerWidth) {
	winWidth = win.innerWidth;
  }
  else if (win.document.documentElement && win.document.documentElement.clientWidth) {
	winWidth = win.document.documentElement.clientWidth;
  }
  else if (document.body) {
	winWidth = win.document.body.clientWidth;
  }
  return Math.max(0,winWidth-30);
}

function getInnerHeight(win) {
  var winHeight;
  if (win.innerHeight) {
	winHeight = win.innerHeight;
  }
  else if (win.document.documentElement && win.document.documentElement.clientHeight) {
	winHeight = win.document.documentElement.clientHeight;
  }
  else if (win.document.body) {
	winHeight = win.document.body.clientHeight;
  }
  return winHeight;
}

function getScrollTop(win) {
    var scrollTop = 0;
    if (win.pageYOffset) {
	    scrollTop = win.pageYOffset;
    }
    else if (win.document.documentElement && win.document.documentElement.scrollTop) {
	    scrollTop = win.document.body.scrollTop;
    }
    else if (win.document.body) {
	    scrollTop = win.document.body.scrollTop;
    }
    return scrollTop;
}

function getScrollLeft(win) {
    var scrollLeft = 0;
    if (win.pageXOffset) {
	    scrollLeft = win.pageXOffset;
    }
    else if (win.document.documentElement && win.document.documentElement.scrollLeft) {
	    scrollLeft = win.document.body.scrollLeft;
    }
    else if (win.document.body) {
	    scrollLeft = win.document.body.scrollLeft;
    }
    return scrollLeft;
}

var gPhotoShow = "";

oPhotoShow = function(foregroundObj, backgroundObj, imageArray, urlsIdx){
	gPhotoShow = this;
	this.imgUrls = imageArray;		// the images we'll go through
	this.urlsIdx = urlsIdx;		// where in the images we are
	this.foreground = foregroundObj; // what's in the fg
	this.background = backgroundObj; // what's in the bg
	this.delay = 6000; 		// give it 6 seconds
	this.transitionInterval = 600; // 0.6 seconds

	this.stopped = false;	// should I stay or should I go?
	this.thumbnailbar = null;
	
	this.init = function(){
		this.foreground.style.opacity = 0.9999;
		this.background.style.opacity = 0.9999;
		
		this.centerImage(this.foreground);
	
		if(this.imgUrls.length>1){
			this.background.src = this.imgUrls[this.urlsIdx++];
			this.endTransition();
		}else{
			this.foreground.src = this.imgUrls[this.urlsIdx++];
			this.centerImage(this.foreground);
			this.background.style.visibility = "hidden";
		}
	}
	
	this.setThumbnailBar = function(aThumbnailBar) {
		this.thumbnailbar = aThumbnailBar;
	}
	
	this.setImageIndex = function(anIndex) {		
		this.urlsIdx = anIndex;
		this.reset = true;
		this.background.src = this.imgUrls[this.urlsIdx++];
		this.centerImage(this.background);
		//this.endTransition();
	}
	
	this.centerImage = function(imgObject) {
			var thumbnailBarWidthOffset = 150;
			var infobarOffset = 0;
			
			var deltaX = Math.round((getInnerWidth(window)-(thumbnailBarWidthOffset + imgObject.width))/2);
			var deltaY = Math.round(((screen.availHeight*.8)-imgObject.height)/2);
			var left = Math.max(0, deltaX);
			var top =  Math.max(0, deltaY);
			imgObject.style.position="absolute";
			imgObject.style.top = top + "px";
			imgObject.style.left = left + "px";
	}

	this.togglePaused = function(){
		if(this.stopped){
			this.stopped = false;
			this.endTransition();
		}else{
			this.stopped = true;
		}
	}

	this.start = function(){
		if(this.stopped){
			this.stopped = false;
			this.endTransition();
		}
	}

	this.stop = function(){
		if(!this.stopped){
			this.stopped = true;
		}
	}

	this.backgroundImageLoaded = function(){
		// start fading out the foreground image
		if(this.stopped){  return; }
		// closure magic for callback
		var _this = this; 
		var callback = function(){ _this.endTransition(); };

		// actually start the fadeOut effect
		// NOTE: if we wanted to use other transition types, we'd set them up
		// 		 here as well
		dojo.graphics.htmlEffects.fadeOut(this.foreground, this.transitionInterval, callback);
	}
	
	this.endTransition = function(){
		// move the foreground image to the background 
		with(this.foreground.style){ zIndex = parseInt(zIndex)-1; }
		with(this.background.style){ zIndex = parseInt(zIndex)+1; }

		// fg/bg book-keeping
		var tmp = this.foreground;
		this.foreground = this.background;
		this.background = tmp;
		
		this.foreground.style.position = "absolute";
		//Global function defined in ...
		setMyImageInfo(this.urlsIdx-1);
		
		this.centerImage(this.foreground);
		this.foreground.style.opacity = "0.0";
		this.foreground.style.visibility = "visible";
		this.background.style.visibility = "hidden";
		dojo.graphics.htmlEffects.fadeIn(this.foreground, this.transitionInterval);
		
		// keep on truckin
		this.loadNextImage();
	}

	this.playAndLoadLoop = function() {
		var _this = this;
		var clozure = function(){ if (_this.background.complete) { _this.backgroundImageLoaded(); } else { _this.playAndLoadLoop(); } }
		setTimeout(clozure, this.delay);
	}
	
	this.loadNextImage = function(){
		// load a new image in that container, and make sure it informs
		// us when it finishes loading
		//dojo.event.kwConnect({
			//srcObj: this.background,
			//srcFunc: "onload",
			//adviceObj: this,
			//adviceFunc: "backgroundImageLoaded",
			//once: true, // make sure we only ever hear about it once
			//delay: this.delay
		//});
		this.playAndLoadLoop();
		dojo.xml.htmlUtil.setOpacity(this.background, 1.0);
		this.background.src = this.imgUrls[this.urlsIdx++];
		if(this.urlsIdx>(this.imgUrls.length-1)) {
			this.urlsIdx = 0;
		}
	}			
}
