$j(document).ready(function() {	
	$j('a[rel="shout_vote"]').click(function(e){ e.preventDefault(); });

	$j('a.preview[rel="video"]').append('<img src="/modules/shout/images/play.gif" class="playicon" />');

	$j('div.down_vote a').click(function(e){
		e.preventDefault();
		$j(this).parent().children('ul').slideDown('fast');
	});
	
	$j('div.down_vote ul li.close').click(function(e){
		e.preventDefault();
		$j(this).parent().slideUp('fast');
	});
});

function preview(id){
	var preview_html = $j('div#shout-'+id+'_preview').html();
	$j.facebox(preview_html);
}

function ini_countdown()
{
	var span = $j('#edit_msg span');
	
	var spanHtml = span.html().split(' ');
	var time = parseInt(spanHtml[0]);
	var opacity = time + 15;
	
	var div = $j('#edit_msg');
	div.css('filter', 'alpha(opacity='+opacity+')');
	div.css('-moz-opacity', '.'+opacity);
	div.css('opacity', '.'+opacity);
	
	div.fadeIn('slow');
	
	if(time > 0){
		setTimeout("countdown()", 1000*60);
	}	
}

function countdown()
{
	var span = $j('div#edit_msg span');
	
	var spanHtml = span.html().split(' ');
	var time = parseInt(spanHtml[0])-1;
	var opacity = time + 15;
	
	var div = $j('div#edit_msg');
	div.css('filter', 'alpha(opacity='+opacity+')');
	div.css('-moz-opacity', '.'+opacity);
	div.css('opacity', '.'+opacity);
	
	span.html(time+' Min');
	
	if(time > 0){
		setTimeout("countdown()", 1000*60);
	} else {
		div.fadeOut('slow');
	}
}

function preVote(shout_id, value, reason){
	if(check_login()){
		//var currentVotes = $j('.shout_post span#shout-'+shout_id+'_current').html();
		vote(shout_id, value, reason);
	} else {
		loginDoneTxt = 'Your vote has been submitted.';
		loginDoneExe = "preVote('"+shout_id+"', '"+value+"', '"+reason+"')";
		var html = 'You need to <a href="#login" onClick="login()">log in</a> to vote for a post.';
		$j.facebox(html);
	}
}

function vote(id, value, reason){
	if(value == ''){
		value = 1;
	}
	if(!reason){
		reason = '';
	}
	$j.ajax({
		type: "GET",
		url: base_url+"/public/shoutVote/new",
		dataType: "json",
		data: 'shout_id='+id+'&value='+value+'&reason='+reason,
		timeout: 3000,
		error: function(XMLHttpRequest, textStatus, errorThrown){
			$j.facebox("Sorry, but we are having a few issues right now and your vote didn't get received.  Please try again later. ");
		},
		success: function(msg){
			if(msg['data']['return'] == 'success'){
				if(msg['request']['value'] == '-1'){
					$j('div#shout-'+id).addClass('lame')
				}
				var linkup = jQuery('a#shout-'+id+'_voteup');
				linkup.text('Thanks!');
				linkup.addClass('voted');
				linkup.attr('href','#');
				linkup.removeAttr('onClick');
				
				var linkdown = linkup.parent().parent().find('div.down_vote');
				linkdown.fadeOut('slow');
				linkdown.children('a').unbind('click');
				
				var newVotes = parseInt(jQuery('span#shout-'+id+'_current').html()) + parseInt(msg['request']['value']);
				linkup.parent().children('h5').fadeOut('slow', function(){
					jQuery('span#shout-'+id+'_current').html(newVotes.toString());
					linkup.parent().children('h5').fadeIn('slow');
				});
			} else {
				$j.facebox(msg['data']['return']);
			}
		}
	});
}