



var videoGalleryWidget = {
	STATES : { READY : 0, LOADING_GALLERY : 1, LOADING_VIDEO : 2 },
	state : 0,
	gallery : null,
	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 =  '/videos/gallery_change';
		new Ajax.Request(temp_url, {parameters:params, evalScripts:true, onComplete:this.processGalleryChange.bind(this)});
	},
	processGalleryChange : function(response) {
		$('videoGallery').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 = '/videos/gallery_change';
		new Ajax.Updater('videoGallery', temp_url, {parameters:params, evalScripts:true, onComplete: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 = '/videos/gallery_change';
		new Ajax.Updater('videoGallery', temp_url, {parameters:params, evalScripts:true, onComplete:this.processGalleryChange.bind(this)});
	},
	handleClickVideo : function(videoId) {
		this.state = this.STATES.LOADING_VIDEO;
		this.paint();
		var params = "viewVideo=&videoId="+videoId+"&gallery="+this.gallery;
		new Ajax.Request('/videos/video', {parameters:params, onComplete:this.processVideoChange.bind(this)});
		//new Effect.ScrollTo('contents', {offset:-100, duration:0.2});
	},
	processVideoChange : function(response) {
		$('videoPlayerView').update ( response.responseText );
		this.state = this.STATES.READY;
		this.paint();
	},
	paint : function() {
		switch (this.state) {
			case this.STATES.READY:
				Element.hide('galleryLoadingView');
				Element.hide('videoLoadingView');
				Element.show('galleryListView');
				Element.show('videoPlayerView');
				break;
			case this.STATES.LOADING_GALLERY:
				Element.hide('galleryListView');
				Element.show('galleryLoadingView');
				break;
			case this.STATES.LOADING_VIDEO:
				Element.hide('videoPlayerView');
				Element.show('videoLoadingView');
				break;
		}
	}
};

var videoWidget = {
	STATES : { READY : 0, SAVING_FAVORITE : 1, SAVED_FAVORITE : 2, REMOVING_FAVORITE : 3, REMOVED_UNFAVORITE : 4 },
	handleFavorite : function(videoId) {
		if (!currentUser.isLoggedIn()) {
			document.location = '/login';
			return;
		}
		this.state = this.STATES.SAVING_FAVORITE;
		this.paint();
		var params = "videoid="+videoId;
		new Ajax.Request('/video/addfavorite', {parameters:params, asynchronous:true, onComplete:this.processFavorite.bind(this)});
	},
	processFavorite : function() {
		this.state = this.STATES.SAVED_FAVORITE;
		this.paint();
	},
	handleUnfavorite : function(videoId) {
		this.state = this.STATES.REMOVING_FAVORITE;
		this.paint();
		var params = "videoid="+videoId;
		new Ajax.Request('/video/removefavorite', {parameters:params, asynchronous:true, onComplete:this.processUnfavorite.bind(this)});
	},
	processUnfavorite : function() {
		this.state = this.STATES.REMOVED_FAVORITE;
		this.paint();			
	},
	handleRemove : function(formName) {
		if (confirm("Are you sure you want to delete this video from moviechai.com ?")) {
			$(formName).submit();
		}
	},
	reportBadYouTube : function(videoId) {
		var params = "videoId="+videoId;
		new Ajax.Request('/video/badyoutube', {parameters:params});
		alert("Thanks -- we'll fix or remove the video soon.");
	},
	reportBadContent : function(videoId) {
		var params = "videoId="+videoId;
		new Ajax.Request('/video/badcontent', {parameters:params});
		alert("Thanks -- we'll fix the video soon.");
	},
	reportBadActor : function(videoId) {
		var params = "videoId="+videoId;
		new Ajax.Request('/video/badactor', {parameters:params});
		alert("Thanks -- we'll fix the video soon.");
	},
	paint : function() {
		switch (this.state) {
			case this.STATES.SAVING_FAVORITE : 
				Element.hide( $('favoriteLink') );
				Element.show( $('savingFavorite') );
				break;
			case this.STATES.SAVED_FAVORITE : 
				Element.hide( $('savingFavorite') );
				Element.show( $('savedFavorite') );
				break;
			case this.STATES.REMOVING_FAVORITE : 
				Element.hide( $('unfavoriteLink') );
				Element.show( $('removingFavorite') );
				break;
			case this.STATES.REMOVED_FAVORITE : 
				Element.hide( $('removingFavorite') );
				Element.show( $('removedFavorite') );
				break;
		}
	}
};

var videoSortWidget = {

	initialize : function() {
		this.setupList();
	},
	items : new Array(),
	setupList : function() {
		Sortable.create('favorites', {tag:'li',only:'video',scroll:window,handle:'videoImg',constraint:false});
	},
	handleSubmit : function() {
	
		$('videoSortSubmitBtn1').disabled = true;
		$('videoSortSubmitBtn2').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();
		}
		
		$('videoIds').value = this.items;
		return true;
	}
};

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