$(document).ready(function() {
	
	$("a.sad").live("click", function(e){
		$('#shoutMessage').val($('#shoutMessage').val()+' :( '); 
		e.preventDefault();
    });
	
	$("a.cry").live("click", function(e){
		$('#shoutMessage').val($('#shoutMessage').val()+' :cry: '); 
		e.preventDefault();
    });
	
	$("a.evil").live("click", function(e){
		$('#shoutMessage').val($('#shoutMessage').val()+' :evil: '); 
		e.preventDefault();
    });
	
	$("a.neutral").live("click", function(e){
		$('#shoutMessage').val($('#shoutMessage').val()+' :| '); 
		e.preventDefault();
    });
	
	$("a.confused").live("click", function(e){
		$('#shoutMessage').val($('#shoutMessage').val()+' :confused: '); 
		e.preventDefault();
    });
	
	$("a.blush").live("click", function(e){
		$('#shoutMessage').val($('#shoutMessage').val()+' :blush: '); 
		e.preventDefault();
    });
	
	$("a.eek").live("click", function(e){
		$('#shoutMessage').val($('#shoutMessage').val()+' :eek: '); 
		e.preventDefault();
    });
	
	$("a.surprised").live("click", function(e){
		$('#shoutMessage').val($('#shoutMessage').val()+' :O '); 
		e.preventDefault();
    });
	
	$("a.rollseyes").live("click", function(e){
		$('#shoutMessage').val($('#shoutMessage').val()+' :roll: '); 
		e.preventDefault();
    });
	
	$("a.smile").live("click", function(e){
		$('#shoutMessage').val($('#shoutMessage').val()+' :) '); 
		e.preventDefault();
    });
	
	$("a.biggrin").live("click", function(e){
		$('#shoutMessage').val($('#shoutMessage').val()+' :D '); 
		e.preventDefault();
    });
	
	$("a.wink").live("click", function(e){
		$('#shoutMessage').val($('#shoutMessage').val()+' ;) '); 
		e.preventDefault();
    });

	
	
	
	$("a.shout_remove").live("click", function(e){
		shoutID = $(this).attr("href");
		shoutID = shoutID.substr(1);
		Remover = confirm("Remover este SHOUT ?");
		if (Remover == true) {
			$.post("include/shoutBox/remShouts.php",{id:shoutID, op:"remShout"}, function(data) {
				if (data!="sucesso") {
					if($('error', data).text()) {
						$('#shoutStatus').empty().removeClass().addClass('shoutError').html($('error', data).text());
						refreshShouts();
					} 
				}
			});
		}
		e.preventDefault();
    });
	
	
	
	
	$('#shoutMessage').keyup(function() {
		var len = this.value.length;
		if (len >= 175) {
			this.value = this.value.substring(0, 175);
			$('#chars_left').css("color", "red");
		} else {
			$('#chars_left').css("color", "#f2f2f2");
		}
		$('#chars_left').text(175 - len);
	});
	
	$('#chars_left_container').click(function() {
		$('#shoutMessage').focus();
	});
	
	
	$(".emoticonsTrigger").qtip({
		content: '<a class="sad" href="#" title="sad"><img class="emoticon" src="images/emoticons/sad.gif" alt="sad" border="0" /></a><br/><a class="cry" href="#" title="cry"><img class="emoticon" src="images/emoticons/cry.gif" alt="cry" border="0" /></a><br/><a class="evil" href="#" title="evil"><img class="emoticon" src="images/emoticons/evil.gif" alt="evil" border="0" /></a><br/><a class="neutral" href="#" title="neutral"><img class="emoticon" src="images/emoticons/neutral.gif" alt="neutral" border="0" /></a><br/><a class="confused" href="#" title="confused"><img class="emoticon" src="images/emoticons/confused.gif" alt="confused" border="0" /></a><br/><a class="blush" href="#" title="blush"><img class="emoticon" src="images/emoticons/blush.gif" alt="blush" border="0" /></a><br/><a class="eek" href="#" title="eek"><img class="emoticon" src="images/emoticons/eek.gif" alt="eek" border="0" /></a><br/><a class="surprised" href="#" title="surprised"><img class="emoticon" src="images/emoticons/surprised.gif" alt="surprised" border="0" /></a><br/><a class="rollseyes" href="#" title="rollseyes"><img class="emoticon" src="images/emoticons/rollseyes.gif" alt="rollseyes" border="0" /></a><br/><a class="smile" href="#" title="smile"><img class="emoticon" src="images/emoticons/smile.gif" alt="smile" border="0" /></a><br/><a class="biggrin" href="#" title="biggrin"><img class="emoticon" src="images/emoticons/biggrin.gif" alt="biggrin" border="0" /></a><br/><a class="wink" href="#" title="wink"><img class="" src="images/emoticons/wink.gif" alt="wink" border="0" /></a>',
		style: {
			padding: 0,
			width: 60,
			height: 283,
			border: {
				width: 0,
				radius: 0,
				color: '#6699CC',
				background: 'none'
			}

			
		},
		position: {
			corner: {
				target: 'topLeft',
				tooltip: 'topLeft'
			}
		},
		api: {
			beforeShow: function(event) {
				$(this.elements.wrapper).css("left","-10px");
				$(this.elements.wrapper).css("top","-110px");
				$(this.elements.wrapper).css("box-shadow","0 0 0 rgba(0, 0, 0, 0)");
				$(this.elements.wrapper).css("-webkit-box-shadow","0 0 0 rgba(0, 0, 0, 0)");
				$(this.elements.wrapper).css("-moz-box-shadow","0 0 0 rgba(0, 0, 0, 0)");
				$(this.elements.wrapper).css("background","transparent");
				$(this.elements.contentWrapper).css("background","transparent");
				$(this.elements.content).css("background","transparent url(images/emoticonsTriggerBG.png)");
				$(this.elements.content).css("padding","15px 0 0 18px");
				$("#modulo_SHOUT").attr("id",'modulo_SHOUT_active');
				return true;
			},
			onShow: function(event) {
				$(".shoutTitulo").animate({"margin-left": "+=39px"}, "normal");
				return true;
			},
			onHide: function(event) {
				$(".shoutTitulo").animate({"margin-left": "-=39px"}, "slow");
				$("#modulo_SHOUT_active").attr("id",'modulo_SHOUT');
				return true;
			}
		},
		show: { delay: 300 },
		hide: { when: 'mouseout', fixed: true }
	});

	
	
	
	
	
	refreshShouts();
	
	$('#shoutMessage').bind('keypress', function(e) {
		if (e.which == 13 && e.shiftKey == false && $.trim($(this).val()) != '') {
			adicionarShout();
		}
	});
	
	$('#doShout').bind('click', function(){
		adicionarShout();
	});
	
	$('#shoutForm').submit(function() {
		return false;
		adicionarShout();
	});
	
	
	
	$('.hoverInfo').each(function()  {
		var self = $(this);
		$(this).qtip({
			content: {
				url: 'process.php',
				data: { viewMovieInfo: 1, id: $(this).attr('id') },
				method: 'post'
			},
			api: {
				beforeShow: function(event) {
					$(self).css("z-index","17000");
					return true;
				},
				beforeContentLoad: function(event) {
					$(this.elements.content).html('<div style="margin: 60px 0 0 0; text-align: center; color: #666;"><img src="images/loading.gif" /><br/><b>Aguarde... A carregar informação!</div>');
				}
			},
			style: { 
				width: { min: 691 }, 
				height: 170, 
				padding: 0
			},
			position: {
				corner: {
					target: 'topLeft',
					tooltip: 'topLeft'
				}
			},
			show: { delay: 1200 },
			hide: { when: 'mouseout', fixed: true }
			
		});
	});

	
});

function adicionarShout() {
	var nick = $('#shoutNickname').val();
	var msg = $('#shoutMessage').val();
	$.ajax({
			type: "POST",
			dataType: "xml",
			url: "include/shoutBox/getShouts.php",
			data: {nickname: nick, message: msg, op: 'newShout'},
			error: function() { $('#shoutStatus').empty().addClass('shoutError').html('An error occured! Try again.'); },
			success: function(xml){
				$('#shoutNickname').val(nick);
				$('#shoutMessage').val('');
				if($('shout', xml).size() > 0) {
					if($('error', xml).text()) {
						$('#shoutStatus').empty().removeClass().addClass('shoutError').html($('error', xml).text());
					} else {
						$('#shoutStatus').empty().removeClass().addClass('shoutStatus').html($('status', xml).text());
					}
					$('shout', xml).each(function(id){
						var nickname = $('nickname', this).text();
						var message = $('message', this).text();
						var date = $('date', this).text();
						var cssClass = 'shoutRow1';
						if((id%2)/10 == 0)
							cssClass = 'shoutRow';
						var shoutRow = '<div class="'+cssClass+'"><div class="nickEdata"><div class="nickname">'+nickname+':</div><div class="date">'+date+'</div></div><div class="shoutedMessage">'+message+'</div><br clear="all" />';
						if(id == 0)
							$('#shoutsContainer').empty().removeClass().addClass('shouts').html(shoutRow);
						else
							$('#shoutsContainer').removeClass().addClass('shouts').append(shoutRow);
					});
				} else {
					$('#shoutStatus').empty().addClass('shoutError').html($('error', xml).text());
					$('#shoutsContainer').empty().removeClass().addClass('noShouts').html('Shouts will be displayed here!');
				}
			}
	});
}



function refreshShouts() {
	var refreshRate = 5000; // time in milliseconds
	var cacheBuster = (new Date).getTime();
	$.ajax({
	   	type: "POST",
	   	dataType: "xml",
	   	url: "include/shoutBox/getShouts.php",
	   	data: {op: 'tick', time: cacheBuster},
	   	error: function() { $('#shoutStatus').empty().addClass('shoutError').html('An error occured! Try again.'); },
	   	success: function(xml){
	   		if($('shout', xml).size() > 0) {
				$('#shoutStatus').empty().removeClass().addClass('shoutStatus').html('<img src="images/ajax-loader.gif" />');
		   		$('shout', xml).each(function(id){
		   			var nickname = $('nickname', this).text();
		   			var message = $('message', this).text();
		   			var date = $('date', this).text();
					var actions = $('actions', this).text();
					var shoutid = $('shoutid', this).text();
					if (actions=="YeS") { printActions = '<br/><a class="shout_remove" href="#'+shoutid+'" title="Remover este Shout"></a>' } else {printActions = ''};
		   			var cssClass = 'shoutRow1';
		   			if((id%2)/10 == 0)
		   				cssClass = 'shoutRow';
		   			var shoutRow = '<div class="'+cssClass+'"><div class="nickEdata"><div class="nickname">'+nickname+':</div><div class="date">'+date+printActions+'</div></div><div class="shoutedMessage">'+message+'</div><br clear="all" />';
		   			if(id == 0)
		   				$('#shoutsContainer').empty().removeClass().addClass('shouts').html(shoutRow);
		   			else
		   				$('#shoutsContainer').removeClass().addClass('shouts').append(shoutRow);
		   		});
	   		} else {
	   			$('#shoutStatus').empty().addClass('shoutError').html($('error', xml).text());
	   			$('#shoutsContainer').empty().removeClass().addClass('noShouts').html('Aqui deveriam estar os Shouts!');
	   		}
	   	}
	});
	setTimeout('refreshShouts()', refreshRate);
}
