/* RCTgo
 * general.js
 */
$(function() {
	$.localScroll();
});

function loadImg() {
	$("body").prepend('<div class="loading">Loading... <img src="/assets/images/loading_3.gif" alt="" /></div>');
}

function hideImg() {
	$("div.loading").fadeOut("slow").remove();
}

function editComment(t, c) {
	loadImg();
	if(t == 'd') {
		url = '/includes/addons/downloads/ajax.php';
	}
	if(t == 'v') {
		url = '/includes/addons/videos/ajax.php';
	}
	$.get(url, { action: "edit_comment", cid: c },	
	function(data){
		hideImg();
		$("div#comment_" + c).after(data);
		$("div#comment_" + c).hide();
	});
	return false;
}

function cancelEdit(c) {
	$("div#edit_" + c).hide().remove();
	$("div#comment_" + c).show();
	return false;
}

function saveComment(t, c) {
	loadImg();
	var comment = $("textarea#comment_text_" + c).val();
	if(t == 'd') {
		$.post('/includes/addons/downloads/ajax.php', { action: "save_comment", cid: c, comment: comment });
	}
	if(t == 'v') {
		$.post('/includes/addons/videos/ajax.php', { action: "save_comment", cid: c, comment: comment });
	}
	$("div#edit_" + c).ajaxStop(function(){
		if(t == 'd') {
			url = '/includes/addons/downloads/ajax.php';
		}
		if(t == 'v') {
			url = '/includes/addons/videos/ajax.php';
		}
		$.get(url, { action: "show_comment", cid: c },
		function(data){
			$("div#comment_" + c).replaceWith(data);
			$("div#comment_" + c).show();
			$("div#edit_" + c).remove();
			hideImg();
		});
	});
}

function postComment(t, id, l) {
	loadImg();
	var comment = $("textarea#comment").val();
	if(t == 'd') {
		$.post('/includes/addons/downloads/ajax.php', { action: "post_comment", did: id, comment: comment, last: l },
		function(data) {
			$("div#post_comment").remove();
			$("div#comments").append(data);
		});
	}
	if(t == 'v') {
		$.post('/includes/addons/videos/ajax.php', { action: "post_comment", vid: id, comment: comment, last: l },
		function(data) {
			$("div#post_comment").remove();
			$("div#comments").append(data);
		});
	}
	$("div#comments").ajaxStop(function() {
		if(t == 'd') {
			$.get('/includes/addons/downloads/ajax.php', { action: "get_count", did: id },
			function(data) {
				$(".count").html("<span>"+ data +"</span>");
			});
		}
		if(t == 'v') {
			$.get('/includes/addons/videos/ajax.php', { action: "get_count", vid: id },
			function(data) {
				$(".count").html("<span>"+ data +"</span>");
			});
		}
		
	});
	hideImg();
	return false;
}

function rate(d,r) {
	loadImg();
	$.get("/includes/addons/downloads/ajax.php", { action: "rate", did: d, rating: r },
	function(data){
		$("a.rate-" + d).hide();
		$("li#rate" + d).css("width",data);
		hideImg();
	});
	return false;
}
