

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) {
		
		if (gallery == '---') {
			return;
		}
		this.state = this.STATES.LOADING_GALLERY;
		this.gallery = gallery;
		this.paint();
		var params = "viewGallery=&gallery="+gallery;
		temp_url = '/photos/gallery_change';
		new Ajax.Request(temp_url, {parameters:params, evalScripts:true, onSuccess:this.processGalleryChange.bind(this)});
		//photoChatWidget.handleRefresh();
	},
	processGalleryChange : function(response) {
		$('photoGallery').update(response.responseText);
		this.state = this.STATES.READY;
		this.paint();
	},
	handlePreviousPage : function(gallery, page) {
		this.gallery = gallery;
		this.state = this.STATES.LOADING_GALLERY;
		this.paint();
		var params = "viewGallery=&gallery="+gallery+"&page="+page;
		temp_url = '/photos/gallery_change';
		new Ajax.Updater('photoGallery', temp_url, {parameters:params, evalScripts:true, onSuccess:this.processGalleryChange.bind(this)});
	},
	handleNextPage : function(gallery, page) {
		this.gallery = gallery;
		this.state = this.STATES.LOADING_GALLERY;
		this.paint();
		var params = "viewGallery=&gallery="+gallery+"&page="+page;
		temp_url = '/photos/gallery_change';
		new Ajax.Updater('photoGallery', temp_url, {parameters:params, evalScripts:true, onSuccess:this.processGalleryChange.bind(this)});
	},
	handlePreviousPhoto : function() {
		if (this.index > 0 && this.items.length > 0) {
			this.handleClickPhoto(this.items[this.index-1], this.index-1);
		} else if (this.index == 0 && this.hasPreviousPage) {
			this.handlePreviousPage(this.gallery, this.page-1);
		}
	},
	handleNextPhoto : function() {
		if (this.index+1 < this.items.length && this.items.length > 0) {
			this.handleClickPhoto(this.items[this.index+1], this.index+1);
		} else if (this.index+1 >= this.items.length && ( this.hasNextPage || this.gallery == 'randomPhotos')) {
			if (this.gallery == 'randomphotos') {
				this.handleNextPage(this.gallery, this.page);
			} else {
				this.handleNextPage(this.gallery, this.page+1);
			}
		}
	},
	handleClickPhoto : function(imageId, index) {
		this.index = index;
		this.state = this.STATES.LOADING_PHOTO;
		this.paint();
		var params = "viewPhoto=&imageId="+imageId+"&gallery="+this.gallery;
		new Ajax.Request('/photos/photo', {parameters:params, evalScripts:true, onComplete:this.processPhotoChange.bind(this)});
		//new Effect.ScrollTo('contents', {offset:-100, duration:0.2});
		//photoChatWidget.handleRefresh();
	},
	processPhotoChange : function(response) {
		$('photoPlayerView').update( response.responseText );
		this.state = this.STATES.READY;
		this.paint();
	},
	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('galleryListView');
				Element.show('galleryLoadingView');
				break;
			case this.STATES.LOADING_PHOTO:
				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.backgroundColor = "#FFFF99";
			} else {
				//$('photo'+i).style.border = "1px solid #ffffff";
				//$('photo'+i).style.backgroundColor = "#ffffff";
			}
		}
	}
};

var photoWidget = {
	STATES : { READY : 0, SAVING_FAVORITE : 1, SAVED_FAVORITE : 2, REMOVING_FAVORITE : 3, REMOVED_UNFAVORITE : 4 },
	handleFavorite : function(imageId) {
		if (!currentUser.isLoggedIn()) {
   	        document.location = '/login';
			return;
		}
		this.state = this.STATES.SAVING_FAVORITE;
		this.paint();
		var params = "imageid="+imageId;
		new Ajax.Request('/photo/addfavorite', {parameters:params, onComplete:this.processFavorite.bind(this)});
	},
	processFavorite : function() {
		this.state = this.STATES.SAVED_FAVORITE;
		this.paint();
	},
	handleUnfavorite : function(imageId) {
		this.state = this.STATES.REMOVING_FAVORITE;
		this.paint();
		var params = "imageid="+imageId;
		new Ajax.Request('/photo/removefavorite', {parameters:params, asynchronous:true, onComplete:this.processUnfavorite.bind(this)});
	},
	processUnfavorite : function() {
		this.state = this.STATES.REMOVED_FAVORITE;
		this.paint();			
	},
	paint : function() {
		switch (this.state) {
			case this.STATES.SAVING_FAVORITE : 
				Element.hide( $('favoriteLink') );
				Element.hide( $('savedFavorite') );
				Element.show( $('savingFavorite') );
				break;
			case this.STATES.SAVED_FAVORITE : 
				Element.hide( $('favoriteLink') );
				Element.hide( $('savingFavorite') );
				Element.show( $('savedFavorite') );
				break;
			case this.STATES.REMOVING_FAVORITE : 
				Element.hide( $('unfavoriteLink') );
				Element.hide( $('removedFavorite') );
				Element.show( $('removingFavorite') );
				break;
			case this.STATES.REMOVED_FAVORITE : 
				Element.hide( $('unfavoriteLink') );
				Element.hide( $('removingFavorite') );
				Element.show( $('removedFavorite') );
				break;
		}
	}
};


var photoRatingWidget = {
	
	handleRating : function(imageId, score) {
		if (!currentUser.isLoggedIn()) {
			alert("Sorry, you need to register/sign-in to save favorites");
			return;
		}
		var params = "action=ratePhoto&imageId="+imageId+"&score="+score;
		new Ajax.Request('/photo/rate', {parameters:params});
		$( 'ratingTable' ).hide();
		$( 'ratingSummary' ).show();
		setTimeout( 'photoGalleryWidget.handleNextPhoto();', 1000);
	}
	
};

var photoAdminWidget = {
	handleRemove : function(imageId) {
		if (confirm("Are you sure you want to remove this photo?")) {
			var params = "action=removePhoto&imageId="+imageId;
			new Ajax.Request('/public/photo.php', {parameters:params, onComplete:this.processRemove.bind(this)});
		}
	},
	processRemove : function(response) {
		$('adminRemove').update(response.responseText);
	},
	handleRemoveMovie : function(imageId) {
		var params = "action=removeMovie&imageId="+imageId;
		new Ajax.Request('/public/photo.php', {parameters:params, onComplete:this.processRemoveMovie.bind(this)});			
	},
	processRemoveMovie : function(response) {
		$('adminRemoveMovie').update(response.responseText);
	},
	handleRemoveActor : function(imageId) {
		var params = "action=removeActor&imageId="+imageId;
		new Ajax.Request('/public/photo.php', {parameters:params, onComplete:this.processRemoveActor.bind(this)});
	},
	processRemoveActor : function(response) {
		$('adminRemoveActor').update(response.responseText);
	}
};

var photoSortWidget = {

	initialize : function() {
		this.setupList();
	},
	items : new Array(),
	setupList : function() {
		Sortable.create('favorites', {tag:'li',only:'photo',scroll:window,handle:'photoImg',constraint:false});
	},
	handleSubmit : function() {
	
		$('photoSortSubmitBtn1').disabled = true;
		$('photoSortSubmitBtn2').disabled = true;
	
		var query = Sortable.serialize($('favorites')).toQueryParams("&");
		var ids = query["favorites[]"];
		
		if (ids == null) {
			this.items = new Array();
		} else if (ids.indexOf(",") < 0) {
			this.items = new Array(ids);
		} else {
			this.items = ids.toArray();
		}
		
		$('photoIds').value = this.items;
		return true;
	}
};

var ChatLink = {
	visible : true,
	toggle : function() {
		ChatLink.visible = !ChatLink.visible;
		if (ChatLink.visible) {
			$('chatShowHideLink').update("Hide All");
			$('photoChatComments').show();
			//photoChatWidget.disabled = false;
		} else {
			$('chatShowHideLink').update("Show Comments");
			$('photoChatComments').hide();
			//photoChatWidget.disabled = true;
		}
	}
};