var move = 387;
var move_to = 0;
var direction = true;
var current_left = 0;
var speed = 750;
var animation_type = "swing"; // swing or linear

/**
 * 
 */
$(document).ready(function()
{
	addtag.init();
	video.vote.init();
	
	var blocks	= $('.video-group');
	var move	= $(blocks[0]).width();
	var nextPos	= $(blocks[1]).css('left');
	var prevPos = '-'+move+'px';
	
	var listen = true;
	var showing = 0;
	$('#right-handle').click(function()
	{
		if (listen) {
			listen = false;
			
			// Positioning
			var next = showing + 1;
			if(next > (blocks.length - 1))
			{
				next = 0;
			}
			$(blocks[next]).css('left',nextPos);
			blocks.each(function(){
				var myPos = $(this).css('left');
				var parts = myPos.split('px');
				var newPos = parseInt(parts[0]) - move;
				$(this).animate( {'left':newPos+'px'}, speed, animation_type,function(){
					listen = true;
				});
			});
			showing = next;
		}
	});
	$('#left-handle').click(function()
	{
		if (listen) {
			listen = false;
			
			// Positioning
			var next = showing - 1;
			if(next < 0)
			{
				next = (blocks.length - 1);
			}
			$(blocks[next]).css('left',prevPos);
			blocks.each(function(){
				var myPos = $(this).css('left');
				var parts = myPos.split('px');
				var newPos = parseInt(parts[0]) + move;
				$(this).animate( {'left':newPos+'px'},speed, animation_type,function(){
					listen = true;
				});
			});
			showing = next;
		}
	});
	
	$('#content .users-vidoes h3,#content .related-vidoes h3').click(function()
	{
		$(this).toggleClass('open');
		var $parent = $(this).parent();
		if ($parent.attr('class') != 'related-vidoes') $parent = $parent.parent().parent();
		var $show = $parent.find('.show');
		var $hide = $parent.find('.hide:not(.more)');
		$show.addClass('hide').removeClass('show');
		$hide.addClass('show').removeClass('hide');
		$parent.find('.more').toggleClass('hide');
	});
	
	$('#show-user-description').click(function()
	{
		$(this).toggleClass('open');
		$('#user-description').toggleClass('hide');
	});
	
	wall.prepare();
});


var video = {
	vote: {
		$stars: null,
		inProgress: false,
		init: function()
		{
			video.vote.$stars = $('.stars-interactive .star');
			
			video.vote.$stars
				.mouseover(video.vote.star.mouseover)
				.mouseout(video.vote.star.mouseout)
				.click(video.vote.star.click);
		},
		reset: function()
		{
			stars.update(video.vote.$stars, videoRating);
		},
		star: {
			mouseover: function()
			{
				clearTimeout(video.vote.timeout);
				var vote = parseInt(video.vote.$stars.index(this)) + 1;
				stars.update(video.vote.$stars, vote);
				return;
				
				var $this = $(this);
				$this
					.addClass('star-gold')
					.prevAll()
					.addClass('star-gold');
				$this
					.nextAll()
					.removeClass(['star-gold', 'star-half'])
					.addClass('star-gray');
			},
			mouseout: function()
			{
				clearTimeout(video.vote.timeout);
				if (!video.vote.inProgress)
				{
					video.vote.timeout = setTimeout(video.vote.reset, 200);
				}
			},
			click: function()
			{
				video.vote.inProgress = true;
				var vote = parseInt(video.vote.$stars.index(this)) + 1;
				
				$('#content div.right div.votes').addClass('votes-loading');
				
				var params = {
					videoId: videoId,
					vote: vote
				}
				
				var onComplete = function(response)
				{
					video.vote.inProgress = false;
					$('#content div.right div.votes').removeClass('votes-loading');
					
					if (response.votebar) 
						$('#votebar').html(response.votebar.html);
						
					if (response.video && response.video.voteCount) 
						$('#content div.right div.votes span.voteCount').html(response.video.voteCount);
					
					if (response.error)
					{
						if (response.signin)
						{
							var params = { 
								message: response.message, 
								log_referer: 'vote',
								log_info: 'videoId: ' + videoId
							};
							popup.open('signInSignUp', params);
							return;
						}
						video.vote.reset();
						alert(response.message);
						return;
					}
					
					videoRating = response.video.rating;
					video.vote.reset();
					$('#content div.right div.votes span.voteCount').html(response.video.voteCount);
					
					stars.update($('#profile .borders .stars .star'), response.user.rating);
					$('#profile div.votes span.voteCount').html(response.user.voteCount);
					
					if (response.userVoteCount == 10 || response.userVoteCount == 50 || response.userVoteCount == 150)
						popup.open('text', {voteCount: response.userVoteCount});
				}
				
				$.post(base_url + 'ajax/vote/allocate/', params, onComplete, 'json');
			}
		}
	},
	search: {
		submit: function()
		{
			$('#videoSearch').submit();
		}
	}
}

var wall = {
	inProgress: false,
	$form: null,
	currentPage: 1,
	currentHash: null,
	prepare: function()
	{
		wall.$form = $('#wall form');
		setInterval(wall.hashListener, 25);
	},
	hashListener: function()
	{
		if (wall.currentHash != location.hash)
		{
			var match = /^#wallPage=([0-9]+)$/.exec(location.hash);
			if (match && match.length > 0)
			{
				wall.navigate(match[1]);
			}
			wall.currentHash = location.hash;
		}
	},
	navigate: function(page)
	{
		if (wall.currentPage != page)
		{
			if (wall.currentHash == null)
			{
				$('#wallContents').hide();
			}
				
			$('#wallContents .paging').hide();
			$('#wall .pageLoading').show();
			
			var params = {
				videoId: videoId,
				page: page
			}
			
			var onComplete = function(response)
			{
				if (!response.error)
				{
					$('#wall .noEntries').remove();
					$('#wallContents').html(response.markup).show();
					$('#wall .pageLoading').hide();
				}
			}
			
			$.post(global_url + 'ajax/wall/navigate', params, onComplete, 'json');
		}
		wall.currentPage = page;
	},
	submit: function()
	{
		if (wall.inProgress) return;
		
		wall.inProgress = true;
		wall.$form.find('.progressIndicator').css('display', 'inline-block');
		
		var entry = wall.$form.find('textarea').val();

		var params = {
			videoId: 	videoId,
			entry: 		entry
		}
		
		var onComplete = function(response)
		{
			if (response.error)
			{
				popup.open('signInSignUp', { message: response.message });
			}
			else
			{
				wall.$form.find('textarea').val('');
				$('#wallContents').html(response.markup).show();
				/*
				$('#wallContents')
					.prepend(response.markup)
					.find('.wallEntry:first')
					.hide()
					.slideDown();
					
				$('#wallContents')
					.find('.wallEntry')
					.eq(wallLimit)
					.slideUp();	
				$('#wallContents .noEntries').remove();
				*/
			}
			wall.inProgress = false;
			wall.$form.find('.progressIndicator').css('display', 'none');
		}
		
		$.post(global_url + 'ajax/wall/postEntry', params, onComplete, 'json');
	},
	deleteEntry: function(wallEntryId)
	{	
		var params = {
			wallEntryId: wallEntryId,
			page: wall.currentPage
		}
		
		var onComplete = function(response)
		{
			if (!response.error)
			{
				$('#wallEntry' + wallEntryId).remove();
				$('#wallContents .wallEntry:last').after(response.markup);
					
				if (response.removePaging)
				{
					$('#wallContents .paging').remove();
				}
			}
		}
		
		$.post(global_url + 'ajax/wall/deleteEntry', params, onComplete, 'json');
	}
}

var addtag = {
	button: null,
	popup: null,
	hideTimeout: null,
	init: function()
	{
		addtag.popup = $('#addtagPopup');
		addtag.popup.mouseover(addtag.showPopup);
		addtag.popup.mouseout(addtag.hidePopup);
		
		addtag.button = $('.taxonomy .addtag');
		addtag.button.mouseover(addtag.showPopup);
		addtag.button.mouseout(addtag.hidePopup);
		
		var offset = addtag.button.offset();
		var canvasOffset = $('#canvas').offset();
		addtag.popup.css({ top: (offset.top - canvasOffset.top + 26), left: (offset.left - canvasOffset.left) });
	},
	showPopup: function()
	{
		clearTimeout(addtag.hideTimeout);
		addtag.popup.show();
	},
	hidePopup: function()
	{
		addtag.hideTimeout = setTimeout(addtag.doHidePopup, 500);
	},
	doHidePopup: function()
	{
		addtag.popup.hide();
	},
	submit: function(keywordId)
	{
		if (userId == null)
		{
			var params = { 
				log_referer: 'add_keyword', 
				log_info: location.href,
				actionOnLogin: 'addKeyword',
				actionOnLoginOptions: videoId + ',' + keywordId,
				message: 'Login to add a tag'
			};
			popup.open('signInSignUp', params);
			return;
		}
		
		addtag.button.addClass('loading');
		
		var params = {
			videoId: videoId,
			keywordId: keywordId
		}
		
		var onComplete = function(response)
		{
			addtag.button.removeClass('loading');
			if (!response.error)
			{
				$('#tag_' + keywordId + ' span.no').html('(' + response.count + ')');
			}
			else
			{
				alert(response.message);
			}
		}
		
		$.post(base_url + 'ajax/video/addKeyword', params, onComplete, 'json');
	}
}