/* 
 * All Module specific
 */

$.extend(M, { Gallery: {
	initialize: function() {
		this.onUpdateSuccess();

		// bind the sortby
		$('#Gallery #sortby').live('change', function() {
			var $this = $(this);
			V.set('Gallery', 'sort', $this.find('option:selected').val());
			V.doUpdate();
		});
	},

	onUpdate: function() {
		var doUpdate = false;
		if (V.changed('gallery[filter]')) {
			V.unset('Gallery', 'sort');
			V.unset('Gallery', 'page');
			if(!V.changed('gallery[query]')) V.unset('Gallery', 'query');
			doUpdate = true;
		} else if(V.changed('gallery[query]')) {
			V.unset('Gallery', 'sort');
			V.unset('Gallery', 'page');
			doUpdate = true;
		} else if(V.changed('gallery[sort]')) {
			V.unset('Gallery', 'page');
			doUpdate = true;
		} else if(V.changed('gallery[page]')) {
			doUpdate = true;
		}

		(doUpdate) && V.ajaxModuleUpdate('Gallery');
	},

	onUpdateSuccess: function() {
		this.initSlider();

		// bind the search form
		$('#Gallery form').bind('submit', function() {
			var $this = $(this);
			var formData = $('form.uoForm').serializeArray();
			var mn = $this.parents('div.uoModule').attr('id');

			$.each(formData, function(k,v) {
				V.set('Gallery', v.name, v.value)
			});
			V.doUpdate();

			return false;
		});


	},

	onUpdateBeforeSend: function() {
		$('#Gallery .content > *').animate({'opacity': 0}, 200);
		$('#Gallery .content').append($(document.createElement('div')).addClass('loadingIndicator'));
	},

	initSlider: function() {
		if($('div.uoCarousel').length) {
			$('div.uoCarousel').jCarouselLite({
				scroll: 1,
				circular: false,
				visible: 5,
				btnPrev: ".uoCarouselPrev",
				btnNext: ".uoCarouselNext",
				afterEnd: function(a) {
					M.Gallery.sliderAfterEnd();
				}
			});
			M.Gallery.sliderAfterEnd();
		}
	},

	sliderAfterEnd: function() {
		var $div = $('div.uoCarousel');
		var $ul  = $('div.uoCarousel ul');
		var $lis = $('div.uoCarousel ul li');

		var elCount   = $lis.length;
		var liWidth   = $ul.innerWidth() / elCount;
		var elVisible = $div.width() / liWidth;
		var elFirst   = $ul.position().left / -liWidth + 1;
		var elLast    = elFirst + elVisible - 1;

		$('#Gallery p.pageXOf').text('Video ' + elFirst + '-' + elLast + ' of ' + elCount);
	}

}});



$.extend(M, { Watchplayer: {
	
	initialize: function() {
		this.initPlayer();
	},

	onUpdate: function() {
		V.changed('v') && V.ajaxModuleUpdate('Watchplayer');
	},

	onUpdateSuccess: function() {
		this.initialize();
	},

	initPlayer: function  () {
		var flashVideoPlayer = $('#flashVideoPlayer');
			if(flashVideoPlayer && $.browser.msie) {
			// Gather all required information from the DOM
			var swfUrl = flashVideoPlayer.attr('data');
					var width  = flashVideoPlayer.attr('width');
					var height = flashVideoPlayer.attr('height');
					var params = {};

					flashVideoPlayer.children('param').each(function(k, paramElm) {
						// it seems, that the IE greps all param-Tags, not only the children of flashVideoPlayer
						paramElm = $(paramElm);
						// (for further information, ask tst)
						//if(paramElm.parentNode == flashVideoPlayer) {
							params[paramElm.attr('name')] = paramElm.attr('value');
						//}
					});

					swfobject.embedSWF(
						// SWF file
						swfUrl,
						// ID to replace and ID of the Flash object
						'flashVideoPlayer',
						// Width
						width,
						// Height
						height,
						// Required Flash version
						'9.0.0',
						// ExpressInstall SWF file
						'/static/swf/expressInstall.swf',
						// Flashvars
						{},
						// Params
						params,
						// Attributes
						{}
					);
				} else if(flashVideoPlayer) {
					swfobject.registerObject(
						// ID of the Flash object
						flashVideoPlayer.id,
						// Required Flash version
						'9.0.0',
						// ExpressInstall SWF file
						'/static/swf/expressInstall.swf'
					);
				}
		}

}});

$.extend(M, { Playlist: {
	
	onUpdateBeforeSend: function() {
		$('#Playlist ul.List').animate({'opacity': 0}, 200);
		$('#Playlist').append($(document.createElement('div')).addClass('loadingIndicator'));
	},

	add: function($e, p) {
		$.post('/ajaxm/Playlist/Add', {v : p[0]}, function() { V.ajaxModuleUpdate('Playlist') });

		// playlist animation
		var $pl = $('#Playlist');
		var $div = $(document.createElement('div'));
			$div.appendTo($('body'))
			.css({
				position: 'absolute',
				width: '1px',
				height: '1px',
				top: $e.offset().top + 8,
				left: $e.offset().left + 8,
				'background-color': '#009ee0',
				'z-index': 999,
				opacity: 0.8
			})
			.animate({
				top: $pl.offset().top + 57,
				left: $pl.offset().left,
				width: 300,
				height: 30,
				opacity: 0.1
			}, function() { $div.remove(); });
	},

	remove: function($e, p) {
		$.post('/ajaxm/Playlist/Remove', {v : p[0]}, function() { V.ajaxModuleUpdate('Playlist', false) });

		// playlist animation
		$('#Playlist .uoM-remove-' + p[0]).parent().fadeOut();
	},

	clear: function($e, p) {
		$.post('/ajaxm/Playlist/Clear', {}, function() { V.ajaxModuleUpdate('Playlist') });
	},

	forward: function($e, p) {
		M.Overlay.show('/ajaxm/Playlist/Forward', {});
	}


}});




$.extend(M, { Watchinfo: {
	initialize: function() {
		V.initTabs('#Watchinfo');
		this.initRatingHover();
	},
	
	onUpdate: function() {
		V.changed('v') && V.ajaxModuleUpdate('Watchinfo');
	},

	onUpdateBeforeSend: function() {
		$('#Watchinfo div.uoTab > *').fadeOut(200);
		$('#Watchinfo div.uoTab').append($(document.createElement('div')).addClass('loadingIndicator'));
	},

	onUpdateSuccess: function() {
		V.initTabs('#Watchinfo');
		this.initRatingHover();
	},

	initRatingHover: function() {
		$('#Watchinfo .uoRating a.uoA').hover(
			function() {
				$('#Watchinfo span.rating').addClass('ratingh' + $(this).attr('rel'));
			}, function() {
				$('#Watchinfo span.rating').removeClass('ratingh' + $(this).attr('rel'));
			}
		);
	},

	rate: function($e, p) {
		$.post('/ajaxm/Watchinfo/Rating', {v : p[0], value:p[1]}, function() { V.ajaxModuleUpdate('Watchinfo', false) });
	},

	forward: function($e, p) {
		M.Overlay.show('/ajaxm/Watchinfo/Forward', {v : $e.attr('rel')});
	},

	bookmark: function($e, p) {
		var service = $e.attr('rel');
        var w;
		var video_url_escaped = encodeURIComponent($('#item_url').val());
		var video_title_escaped = encodeURIComponent($('#item_title').val());
		switch(service) {
			case 'facebook':
				w = window.open('http://www.facebook.com/sharer.php?u='+video_url_escaped,'_blank','toolbar=0,status=0,resizable=1,width=626,height=436');
				break;
			case 'oneview':
				w = window.open('http://www.oneview.de:80/quickadd/neu/addBookmark.jsf?URL='+video_url_escaped+'&title='+video_title_escaped);
				break;
			case 'del.icio.us':
				w = window.open("http://del.icio.us/post?url="+video_url_escaped+"&title="+video_title_escaped+"");
				break;
			case 'digg':
				w = window.open("http://digg.com/submit?phase=2&url="+video_url_escaped+"&title="+video_title_escaped+"");
				break;
			case 'webnews':
				w = window.open("http://www.webnews.de/einstellen?url="+video_url_escaped+"&title="+video_title_escaped+"");
				break;
			case 'jigg':
				w = window.open("http://yigg.de/neu?exturl="+video_url_escaped+"&exttitle="+video_title_escaped+"");
				break;
			case 'google':
				w = window.open("http://www.google.com/bookmarks/mark?op=edit&output=popup&bkmk="+video_url_escaped+"&title="+video_title_escaped+"");
				break;
            case 'yahoo':
                w = window.open("http://myweb.yahoo.com/myresults/bookmarklet?u="+video_url_escaped+"&t="+video_title_escaped+"&ei=UTF");
                break;
            case 'twitter':
                w = window.open("http://twitter.com/home?status="+video_title_escaped+" "+video_url_escaped);
                break;
        }
		w.focus();

	}


}});


$.extend(M, { Top5: {
	initialize: function() {
		V.initTabs('#Top5');
	}
}});


$.extend(M, { Overlay: {
	initialize: function() {
		$('#Overlay').hide();
	},

	show: function(actionUrl, params) {
		// hide the player
		$('#flashVideoPlayer').css('visibility', 'hidden');
		$('#Watchplayer .uoThumb').css({ position: 'absolute', top: 0, left: 0}).show();

		$('#Overlay').css('top', $(window).scrollTop() + 150 +'px');

		$.ajax({
			url: actionUrl,
			type: 'get',
			data: params,
			beforeSend: function() {
				$('#Overlay').show();
				$('#Overlay div.loading').show();
				$('#Overlay div.content').hide();
			},
			success: function(content) {
				$('#Overlay div.content').html(content);
				$('#Overlay div.content').show();
				$('#Overlay div.loading').hide();
			}
		});	
	},

	close: function() {
		$('#Overlay').fadeOut();

		// show player again
		$('#Watchplayer .uoThumb').hide();
		$('#flashVideoPlayer').css('visibility', 'visible');
	}

}});

