var base_url = 'http://navkolo.me/'; 

// Thumbnail response
function thumbResponse(data) 
{        
    if (data.error != null) {
        $("#uploaded_thumb").html("Error: " + data.error);
    } else {                        
        $("#uploaded_thumb").html("<img src=\"" + base_url + 'uploads/posts/' + data.file_name + "\">");
        $("#post").val(data.file_name);
    }
}

// Image response
function imgResponse(data) 
{        
    if (data.error != null) {
        alert("Error: " + data.error);
    } else {                
        var bbcode = '[img]' + base_url + 'uploads/posts/' + data.file_name + '[/img]';            
        edInsertImage(document.getElementById('story'), bbcode);
        $("#img_load_block").hide();
		$("#mask").hide();        
    }           
}

// Post comment
function postComment()
{            
    var s = $("#story").val();
    var id = $("#post_id").val();        
    $.post(base_url + "add_comment", {comment: s, post_id: id}, function(data){ showComment(data); }, "html");
    $("#comment_submit").attr("disabled", true);
    $('#story').val("");       
}

// Show comment
function showComment(data)
{    
    $("#comments").append(data);
	$("#comment_submit").attr("disabled", false);    
}

// Vote for comment
// cp - comment positive vote
// cn - comment negative vote
function voteComment(id, vote)
{    
    $("#comm"+id).html("Загрузка");
    $.post(base_url + "vote_comment", {comment_id: id, vote: vote}, function(data){ voteCommentResult(data); }, "xml"); 
}

// Show comment rating result after voting
function voteCommentResult(data)
{
    var id = $("id",data).text();
    var plus = $("plus",data).text();
    var minus = $("minus",data).text();    
    $("#comm"+id).html("<div class=\"comm_pres\">" + plus + "</div><div class=\"comm_nres\">" + minus +"</div>");    
}

// Vote for post
// pp - post positive vote
// pn - post negative vote
function votePost(id, vote)
{    
    $("#pv"+id).html("<img src=\""+base_url+"design/img/orange_indicator.gif\" style=\"padding: 3px; border: 0;\" alt=\"\">");
    $("#nv"+id).html("<img src=\""+base_url+"design/img/blue_indicator.gif\" style=\"padding: 3px; border: 0;\" alt=\"\">");
    $.post(base_url + "vote_post", {post_id: id, vote: vote}, function(data){ votePostResult(data); }, "xml"); 
}

// Show post rating result after voting
function votePostResult(data)
{
    var id = $("id",data).text();
    var plus = $("plus",data).text();
    var minus = $("minus",data).text();
    $("#pv"+id).html("<img src=\""+base_url+"design/img/pro_inactive.gif\" width=\"23\" height=\"23\" border=\"0\" alt=\"+\">");
    $("#nv"+id).html("<img src=\""+base_url+"design/img/cons_inactive.gif\" width=\"23\" height=\"23\" border=\"0\" alt=\"-\">");
    $("#pr"+id).html(plus);
    $("#nr"+id).html(minus);    
}

// show popup
function showPopup(id) {
    $("#"+id).show();      
}

function show_modal(id) {		  	
	//transition effect       
	$('#mask').show();      	    
	
	//Get the window height and width  
	var winH = $(window).height();  
	var winW = $(window).width();  		
	     
	//Set the popup window to center  
	$("#id").css('top',  winH/2-$("#id").height()/2);  
	$("#id").css('left', winW/2-$("#id").width()/2);  
		
	$("#"+id).show();
}

// hide popup
function hidePopup(id) {
	$("#mask").hide();
    $("#"+id).hide();
}

// Submit form
function submit_form() {	
	$("#title").attr('value', $("#title_input").attr('value'));
	$("#link").attr('value', $("#link_input").attr('value'));	
	$("#post").attr('value', $("#post_input").attr('value'));		
	$("#subcategory").attr('value', $("input[name=video_type]:checked").val());
	$("#post_form").submit();			
}

// Collpse story block
function collapse_story() {
	if ($("#story_container").css("display") == "none") {
		$("#story_container").show();
	} else {
		$("#story_container").hide();
	}
}

// Set max length for textarea
function limitChars(textid, limit, infodiv) {
	var text = $('#'+textid).val(); 
	var textlength = text.length;
	if(textlength > limit) {
  		$('#'+textid).val(text.substr(0,limit));  
		$('#' + infodiv).html('0');		
		return false;
	} else {		
  		$('#' + infodiv).html(limit - textlength);
		return true;
  	}
}

$(document).ready(function() {
    
    $('#post_input').keyup(function() {
  		limitChars('post_input', 1000, 'charlimitinfo');
	});
    
    $("#add_link").click(function() {
        edInsertLink(document.getElementById('story'), '0');
    });
    
    $("#add_b").click(function() {
        edInsertTag(document.getElementById('story'), '2');
    });
    
    $("#add_quote").click(function() {
        edInsertTag(document.getElementById('story'), '1');
    });
    
    $("#post_add_link").click(function() {
        edInsertLink(document.getElementById('post_input'), '0');
    });
    
    $("#post_add_b").click(function() {
        edInsertTag(document.getElementById('post_input'), '2');
    });
    
    $("#post_add_quote").click(function() {
        edInsertTag(document.getElementById('post_input'), '1');
    });
    
    $("#add_img").click(function() {
    	show_modal('img_load_block');        
    });
    
    $("#add_youtube").click(function() {
        edInsertYoutube(document.getElementById('story'), '4');
    });
    
    $("#comment_submit").click(function() {        
        postComment();
        return false;
    });
	
	$("#thumb_submit").click(function() {		
		$('#upload_picture').submit();
		return true;		
	});
	
	// Video prompting
	$("input[@name='video_type']").change(function() {		
	    if ($("input[@name='video_type']:checked").val() == 1) {
			$("#video_url_example").html("http://www.youtube.com/watch?v=v_UyVmITiYQ");	
		}        
	    else if ($("input[@name='video_type']:checked").val() == 2) {
			$("#video_url_example").html("http://rutube.ru/tracks/2093768.html?v=cdc3c273473b7a9991721eaa1b863c73");	
		}        
	    else if ($("input[@name='video_type']:checked").val() == 3) {	    	
			$("#video_url_example").html("http://vimeo.com/3811427");
	}        
});
			
});
