



/* episode selector "assets/js/json-response.js" */

var EpisodeSelector = new Class({

	Implements: Options,

	options: {
		episodesPerPage: 3,
		webserviceUrl: '/Service/GetEpisodeListByBrandId',
		noImageThumbPath: '/assets/img/episodes/1.png'
	},

	initialize: function(options) {
		var that = this;

		this.setOptions(options);

		// setup objects
		this.episodes = $('episodes');
		this.displayFrom = $$('.from')[0];
		this.displayTo = $$('.to')[0];
		if($chk(this.episodes))
		this.window = this.episodes.getElement('.window');
		this.window.width = this.window.getSize().x;
		this.window.currentScrollPos = 0;
		this.next = this.episodes.getElement('.next');
		this.prev = this.episodes.getElement('.prev');
		this.list = this.episodes.getElement('ul');
		this.list.items = this.list.getElements('li');
		this.list.itemWidth = this.list.items[0].getSize().x;
		this.list.width = this.list.itemWidth * this.list.items.length;
		this.videoPlayer = $('video-player');
		this.loadedIndex = 0;

		// setup effects
		this.list.setStyle('width', this.list.width);
		this.window.scroll = new Fx.Scroll(this.window, {
			transition: Fx.Transitions.Sine.easeOut
		});

		// initial link hook-up
		this.list.items.each(function(item) {
			if (item.getElement('a')) {
				item.link = item.getElement('a');
				that.hookUpLink(item.link);
			}
		});



		// wire up buttons
		this.next.addEvent('click', function(e) {
			e.preventDefault();
			if (that.loadedIndex + that.options.episodesPerPage <= that.list.items.length - 1) that.loadedIndex = that.loadedIndex + that.options.episodesPerPage;
			var listItem = that.list.items[that.loadedIndex];
			if (listItem && that.window.currentScrollPos + that.window.width < that.list.width) {
				that.window.currentScrollPos = that.window.currentScrollPos + that.window.width;
				that.window.scroll.start(that.window.currentScrollPos, 0);
				//if (!$defined(listItem.link)) that.getNextPage();
			}
			that.updateDisplayCounter();
		});
		this.prev.addEvent('click', function(e) {
			e.preventDefault();
			that.loadedIndex = that.loadedIndex - that.options.episodesPerPage;
			if (that.loadedIndex < 0) that.loadedIndex = 0;
			if (that.window.currentScrollPos - that.window.width >= 0) {
				that.window.currentScrollPos = that.window.currentScrollPos - that.window.width;
				that.window.scroll.start(that.window.currentScrollPos, 0);
			}
			that.updateDisplayCounter();

		});

		// auto-load first episode
		//		this.getNextPage(function() {
		//that.list.items[0].link.fireEvent('click');
		var firstEpisode = $$('#episode-list li')[0];
		this.video = new Swiff('/assets/swf/videoPlayer.swf', {
			id: 'flash',
			width: 737,
			height: 450,
			container: $('video-player'),
			params: {
				wmode: 'transparent',
				allowfullscreen: 'true'
			},
			vars: {
				videoPath: firstEpisode.getElement('a').get('rel'),
				playerPath: 'http://' + window.location.host + '/assets/swf/videoPlayer.swf',
				videoId: firstEpisode.id,
				videoTitle: firstEpisode.getChildren('input[name=title]')[0].value,
				videoDescription: firstEpisode.getChildren('input[name=description]')[0].value,
				xmlSettings: '/assets/settings.xml',
				videoList: '/service/list',
				playlistURL: 'http://www.thegarfieldshow.com/service/list'
			}
		});
		//});
	},

	updateDisplayCounter: function() {
		var toEpisodes = this.loadedIndex + this.options.episodesPerPage;
		var fromEpisodes = this.loadedIndex + 1;
		
		if (toEpisodes > this.list.items.length) {
			fromEpisodes = this.loadedIndex + 1 - (toEpisodes - this.list.items.length);
			toEpisodes = this.list.items.length;
		}

		this.displayFrom.set('text', fromEpisodes);

		this.displayTo.set('text', toEpisodes);
	},

	getNextPage: function(callBack) {
		var that = this;

		var episodes = [];
		for (i = 0; i < this.options.episodesPerPage; i++) {
			if ($defined(this.list.items[this.loadedIndex * this.options.episodesPerPage + i])) {
				episodes.extend([this.list.items[this.loadedIndex * this.options.episodesPerPage + i].get('id')]);
			}
		}

		var jsonRequest = JSON.encode({
			brandID: window.brandID,
			episodeIDs: episodes
		});

		var jsonCall = new Request.JSON({
			url: that.options.webserviceUrl,
			onSuccess: function(response) {
				that.populateEpisodes(response);
				callBack();
			}
		}).send(jsonRequest);
	},

	populateEpisodes: function(episodes) {


		var that = this;
		episodes.each(function(episode, index) {
			that.list.items[that.loadedIndex + index].link = new Element('a', {
				Class: 'png',
				href: '#',
				rel: $defined(episode.MovieFilename) ? episode.MovieFilename : '',
				text: episode.FldTitle,
				styles: {
					'background-image': 'url(/assets/img/episodes/' + ($defined(episode.FldScreenshotFilename) ? episode.FldScreenshotFilename : that.options.noImageThumbPath) + ')'
				}
			}).inject(that.list.items[that.loadedIndex + index]);
			that.hookUpLink(that.list.items[that.loadedIndex + index].link, episode.Id);
		});
	},

	hookUpLink: function(link, video1) {
		var that = this;
		var vid = video1;
		var flv = link.get('rel');

		link.addEvent('click', function(e) {
			if (e) e.preventDefault();
			that.list.items.each(function(item) {
				item.removeClass('selected');
			});
			//console.log(this.getParent('li'));
			var li = this.getParent('li');

			li.addClass('selected');
			//that.videoPlayer.empty();
			$('flash').loadVideo(flv, li.id, li.getChildren('input[name=title]')[0].value, li.getChildren('input[name=description]')[0].value);
			//            that.video = new Swiff('/assets/swf/videoPlayer.swf', {
			//                id: 'flash',
			//                width: 737,
			//                height: 371,
			//                container: $('video-player'),
			//                params: {
			//                    wmode: 'transparent'
			//                },
			//                vars: {
			//                    videoPath: flv,
			//                    playerPath: 'http://instavision.lightmakerorlando.com/assets/swf/videoPlayer.swf',
			//                    videoId: video1
			//                }
			//            });
		});
	}
});

/* ajax search */

var Search = new Class({

	initialize: function(){
		var that = this;
		
		// setup objects
		this.search = $$('#search-bar .search');
		this.submit = this.search.getElement('.submit');
		this.field = this.search.getElement('#search');
		this.label = this.search.getElement('label');
		
		this.field.set({
			events: {
				'focus': function(){
					that.label.addClass('hide');
				},
				'blur': function(){
					if(this.get('value') === '') that.label.removeClass('hide');
				}
			}
		});
	}

});

function sendEmail(email, video1, type) {

	var jsonReq = JSON.encode({
		emailAddress: email,
		movieID: video1
	});

	var jsonCal = new Request.JSON({
		url: '/Service/SendEmail',
		onSuccess: function(response) {

		}
	}).send(jsonReq);

}

function saveVideoWatchedTime(time, videoId, type) {
	new Request({
		url: '/service/TrackPlayerWatchTime'
	}).post({ time: time, videoId: videoId, type: type });
}
function adAbsent(ad_type) {
	new Request({
		url: '/service/TrackAds'
	}).post({ type: ad_type, status: 'Ad Absent' });
}
function adPresent(ad_type) {
	new Request({
		url: '/service/TrackAds'
	}).post({ type: ad_type, status: 'Ad Present' });
}