/* gp.postdetails.js
 * Copyright (c) 2009 gotpicks.com, All rights reserved.
 */
var gp = window.gp || {};

gp.postdetails = (function($){
	
	function init() {
		initVoting();
		if(!gp.util.isIOS()) {
		    gp.util.tinymce.init('#reply-form textarea', $('#hid-media-url').val());
		}
		initReplyPreview();
	}
	
	function handleVotes(ev) {
		ev.preventDefault();
		var $this = $(this);
		$('body').addClass('busy');
		$.ajax({
            type: "POST",
            url: $this.attr('href'),
            dataType: 'html',
            success: function(html){
				var t = '';
				
				if(html.indexOf('<head') > 0) {
					window.location.href = '/accounts/login/?next=' + $this.attr('href');
				}
				$p = $this.parent().parent().html(html);
				$('a', $p).click(handleVotes);
				$('body').removeClass('busy');
				$('div.votes',$p).animate({ 'backgroundColor':'#ff3300'}, 1000, function(){
					$(this).animate({'backgroundColor':'#f0f0f0'}, 2000)
				});
            },
            error: function(XMLHttpRequest, textStatus, errorThrown){
				$('body').removeClass('busy');
                alert("sorry there was an error, please try again... ");
            }
        });
		
		return false;
	}
	
	function initVoting(){
		$('div.vote a').click(handleVotes);
	}
	
	function initReplyPreview() {
		$('#btn-post-preview').click(function(ev){
			ev.preventDefault();
			$(this).blur();
			$.ajax({
	            type: "POST",
	            url: $(this).attr('data-preview-url'),
	            data: $('#reply-form form').serializeArray(),
	            dataType: 'html',
	            success: function(data){
					$('#post-preview').css('opacity',0).html(data).animate( { 'opacity':1 });
	            },
	            error: function(XMLHttpRequest, textStatus, errorThrown){
	            }
	        });
			
			return false;
		});
	}
	
	return {
		init:init
	}
	
})(jQuery);

jQuery(document).ready(gp.postdetails.init);