var photoGalleryWidget = {
	STATES : { READY : 0, LOADING_GALLERY : 1, LOADING_PHOTO : 2 },
	items : new Array(),
	index : 0,
	page : 1,
	hasNextPage : false,
	hasPreviousPage : false,
	state : 0,
	handleChangeGallery : function(event, gallery, gallerytype) {
		logger.debug(gallery);
		
		if (gallery == '---') {
			return;
		}
		this.state = this.STATES.LOADING_GALLERY;
		this.gallery = gallery;
		this.gallerytype = gallerytype;
		this.paint();
		var params = "viewGallery=&gallery="+gallery+"&gallerytype="+gallerytype;
		new Ajax.Updater('photoGallery', '/include/process/process_photo.asp', {parameters:params, evalScripts:true, onComplete:this.processGalleryChange.bind(this)});
		
	},
	processGalleryChange : function() {
		logger.debug("Processed page/gallery change.");
		this.state = this.STATES.READY;
		if (this.gallery.indexOf('favoritePhotos') >= 0) {
			urchinTracker("PhotoLive"+this.gallery.substr(0, 14));
		} else if (this.gallery.indexOf('moviePhotos') >= 0) {
			urchinTracker("PhotoLive"+this.gallery.substr(0, 11));
		} else if (this.gallery.indexOf('actorPhotos') >= 0) {
			urchinTracker("PhotoLive"+this.gallery.substr(0, 11));
		} else {
			urchinTracker("PhotoLive"+this.gallery);
		}
		this.paint();
	},
	handlePreviousPage : function(gallery, gallerytype, page, handler) {
		logger.debug("Clicked previous page.");
		this.gallery = gallery;
		this.gallerytype = gallerytype;
		this.state = this.STATES.LOADING_GALLERY;
		this.paint();
		var params = "viewGallery=&gallery="+gallery+"&gallerytype="+gallerytype+"&pageNav="+page+"&handler="+handler;
		new Ajax.Updater('photoGallery', '/include/process/process_photo.asp', {parameters:params, evalScripts:true, onComplete:this.processGalleryChange.bind(this)});
	},
	handleNextPage : function(gallery, gallerytype, page, handler) {
		logger.debug("Clicked next page.");
		this.gallery = gallery;
		this.gallerytype = gallerytype;
		this.state = this.STATES.LOADING_GALLERY;
		this.paint();
		var params = "viewGallery=&gallery="+gallery+"&gallerytype="+gallerytype+"&pageNav="+page+"&handler="+handler;
		new Ajax.Updater('photoGallery', '/include/process/process_photo.asp', {parameters:params, evalScripts:true, onComplete:this.processGalleryChange.bind(this)});
	},
	handlePreviousPhoto : function() {
		logger.debug("Clicked previous photo.");
		if (this.index > 0 && this.items.length > 0) {
			this.handleClickPhoto(this.items[this.index-1], this.index-1, 'previous_photo');
		} else if (this.index == 0 && this.hasPreviousPage) {
			this.handlePreviousPage(this.gallery, this.gallerytype, this.page-1, 'previous_photo');
		}
	},
	handleNextPhoto : function() {
		logger.debug("Clicked next photo.");
		if (this.index+1 < this.items.length && this.items.length > 0) {
			this.handleClickPhoto(this.items[this.index+1], this.index+1, 'next_photo');
		} else if (this.index+1 >= this.items.length && ( this.hasNextPage || this.gallery == 'recentPhotos')) {
			if (this.gallery == 'recentPhotos') {
				this.handleNextPage(this.gallery, this.gallerytype, this.page, 'next_photo');
			} else {
				this.handleNextPage(this.gallery, this.gallerytype, this.page+1, 'next_photo');
			}
		}
	},
	handleClickPhoto : function(imageId, index, handler) {
		logger.debug("Clicked photo "+imageId);
		this.index = index;
		this.state = this.STATES.LOADING_PHOTO;
		this.paint();
		var params = "viewPhoto=&imageId="+imageId+"&gallery="+this.gallery+"&gallerytype="+this.gallerytype+"&handler="+handler;
		new Ajax.Request('/include/process/process_photo.asp', {parameters:params, evalScripts:true, onComplete:this.processPhotoChange.bind(this)});
		//new Effect.ScrollTo('contents', {offset:-100, duration:0.2});
		//THIS NEXT LINE IS SLOWING IT DOWN I THINK
		addView('2',imageId,'13','1','');		
	},
	processPhotoChange : function(response) {
		logger.debug("Processed photo change.");
		$('photoPlayerView').update( response.responseText );
		this.state = this.STATES.READY;
		this.paint();
		if (this.gallery.indexOf('favoritePhotos') >= 0) {
			urchinTracker("PhotoLivePhoto"+this.gallery.substr(0, 14));
		} else if (this.gallery.indexOf('moviePhotos') >= 0) {
			urchinTracker("PhotoLivePhoto"+this.gallery.substr(0, 11));
		} else if (this.gallery.indexOf('actorPhotos') >= 0) {
			urchinTracker("PhotoLivePhoto"+this.gallery.substr(0, 11));
		} else {
			urchinTracker("PhotoLivePhoto"+this.gallery);
		}
	},
	paint : function() {
		switch (this.state) {
			case this.STATES.READY:
				Element.hide('galleryLoadingView');
				Element.hide('photoLoadingView');
				Element.show('galleryListView');
				Element.show('photoPlayerView');
				break;
			case this.STATES.LOADING_GALLERY:
				Element.hide('photoPlayerView');
				Element.show('photoLoadingView');
				//Element.hide('galleryListView');
				//Element.show('galleryLoadingView');
				break;
			case this.STATES.LOADING_PHOTO:
				//NEXT TWO: ACTIVE IF SERVER FAST
				//Element.hide('photoPlayerView');
				//Element.show('photoLoadingView');
				break;
		}
		for (i=0;i<this.items.length;i++) {
			if (i == this.index) {
				$('photo'+i).style.border = "1px solid #000000";
				$('photo'+i).style.padding = "1px";
				$('photo'+i).style.backgroundColor = "#FFFF99";
			} else {
				$('photo'+i).style.border = "1px solid #999";
				$('photo'+i).style.padding = "1px";
				$('photo'+i).style.backgroundColor = "#fff";

			}
		}
	}
};


