jQuery(document).ready(function(){

	var items = [];
	var speed = 650;
	$('.oneItem').each(function() {	
		items.push($(this));	
	});
	
	var itemCount = items.length;
	var showItem = 0;
	
	$('.oneItem').remove();
	var nextItem = items[showItem];
	nextItem.css({opacity: 0.8});
	
	nextItem.attr("onClick","playVideo('"+nextItem.attr('id').split(':')[1]+"')");
	
	$('#centerItem').append(nextItem);
	
	$('#leftButton').click(function(){
		goLeft();
	});
	
	$('#rightButton').click(function(){
		goRight();
	});
	
	function goRight()
	{
		$('iframe').remove();
		$('.videoBar').show();		
		$('.playDiv').show();		
		$('#rightButton').unbind('click');
		$('#leftButton').unbind('click');
		
		if(showItem == itemCount-1)
		{
			showItem = 0;
		}
		else
		{
			showItem++;
		}
		
		$('.oneItem').animate({
			left: "-=800",
			opacity: 0.1
		},speed,function(){
		
			$(this).remove();
			$('#rightButton').click(function(){goRight();});
			$('#leftButton').click(function(){goLeft();});
		
		});
		
		nextItem = items[showItem];
		nextItem.css({'left': '+800px','opacity': '0.1'});
		
		nextItem.removeAttr('onClick');
		$('#centerItem').append(nextItem);

		nextItem.animate({
			left: "-=800",
			opacity: '0.8'
		},speed,function(){
		
			nextItem.attr("onClick","playVideo('"+nextItem.attr('id').split(':')[1]+"')");
		
		});
		
	}

	function goLeft()
	{
		$('iframe').remove();
		$('.videoBar').show();
		$('.playDiv').show();
		$('#leftButton').unbind('click');
		$('#rightButton').unbind('click');
		
		
		if(showItem == 0)
		{
			showItem = itemCount-1;
		}
		else
		{
			showItem--;
		}
				
		$('.oneItem').animate({
			left: '+=800',
			opacity: '0.1'
		},speed,function(){
		
			$(this).remove();
			$('#leftButton').click(function(){goLeft();});
			$('#rightButton').click(function(){goRight();});

		});
		
		nextItem = items[showItem];
		nextItem.css({'left': '-800px','opacity': '0.1'});
		nextItem.removeAttr('onClick');
		
		$('#centerItem').prepend(nextItem);
		
		nextItem.animate({
			left: '+=800',
			opacity: '0.8'
		},speed,function(){
		
			nextItem.attr("onClick","playVideo('"+nextItem.attr('id').split(':')[1]+"')");
		
		});
		
	}
	
	getShouts();
	
	$('#postMessage').click(function(){
		var message = $('#message').val();
		
		if(message.length > 2)
		{
			$.post('a.php', {a: 'shout', message: $('#message').val()}, function(data) {
				getShouts();
				$('#message').val('');
				$('#minChar').slideUp(200);
				$('#messageSent').slideDown(200);
				$('#messageSent').fadeOut(2000);
			});
		}
		else
		{
			$('#minChar').slideDown(200);
		}
	});
	
	$('#sendPost').click(function(){
			$.post('a.php', {a: 'postNews', header: $('#newsHeader').val(),  message: $('#newsMessage').val()}, function(data) {

			});
	});
	
	$('#loginButton').click(function(){
			$.post('a.php', {a: 'login', username: $('#loginUsername').val(),  password: $('#loginPassword').val()}, function(data) {
				if(data == "ok")
				window.location.reload();
			});
	});
	
	function getShouts()
	{
		$.post('a.php', {a: 'getShouts'}, function(data) {
			$('#shoutBox').html(data);
		});
	}
	
	$('#newPost').click(function(){
		$('#postNews').toggle();
	});
	
});

function playVideo(url)
{
	location.hash = "!/playVideo?id="+url;
	var sourceUrl = "http://www.youtube.com/embed/"+url+"?autoplay=1";
	$('.oneItem').removeAttr('onClick');
	var source = "<iframe title=\"YouTube video player\" width=\"525\" height=\"325\" src=\""+sourceUrl+"\" frameborder=\"0\" allowfullscreen></iframe>";
	$('.oneItem').append(source);
	$('.videoBar').hide();
	$('.playDiv').hide();
}
