var model;
var item_id;
var tab_ajax;
var currPage = 1;

$(document).ready(function(){  
	// Call Events
	commEvents();
});

function tabAjax_comentarios() {
	commEvents ();
}

function commEvents () {
	if ($("#commModel").length > 0) {
		// Get Vars
		model = $("#commModel").text();
		item_id = $("#commItemId").text();
		tab_ajax = $("#commTabAjax").text();
		
		$("#commAdd").submit(commSend);
		$("#commAddOpen").click(commSendOpen);
		$("#commPaging a").click(commPage);
		$(".comm-delete").click(commDelete);
		$(".comm-reply").click(commReply);
		$("#commReplytoCancel").click(commReplyCancel);
		$(".commEmotionsOpen").click(commEmoticonOpen);
		$("#commEmotions a").click(commEmoticonInsert);
		
		openNewWindow();
		
		showUserInfo();
		
		efetueLoginGo();
	}
}

function commSend (e) {
	if ($("#ComentarioComentario").val() == '') {
		alert(LCS['comm_writecomerror']);
		return false;	
	}
	
	$("#ComentarioComentario").attr('disabled', 'disabled');
	$("#ComentarioSend").attr('disabled', 'disabled');
	$("#commLoader").show();
	
	$.ajax({
		type: 'POST',
		url: BASE_URL+'/comentarios/add',
		cache: false,
		data: 'data[Comentario][item_id]='+$("#ComentarioItemId").val()+'&data[Comentario][parent_id]='+$("#ComentarioParentId").val()+'&data[Comentario][model]='+$("#ComentarioModel").val()+"&data[Comentario][comentario]="+encodeURIComponent($("#ComentarioComentario").val()),
		success: function (body) {
			var msgs = alertParse(body);
			if (msgs['type'] == 'success') {
				if ($("#ComentarioParentId").val() == 0)
					var pag = 1;
				else
					var pag = currPage;
				
				commLoad(pag, 'send', msgs, null);
			} else {
				setAlert(msgs['message'], msgs['type']);
				$("#ComentarioComentario").removeAttr('disabled');
				$("#ComentarioSend").removeAttr('disabled');
				$("#commLoader").hide();
			}
		},
		error: function() {
			$("#ComentarioComentario").removeAttr('disabled');
			$("#ComentarioSend").removeAttr('disabled');
			$("#commLoader").hide();
			setAlert('Unknow error!', 'error');
		}
	});
	
	e.preventDefault();	
}

function commSendOpen(e) {
	$("#commAdd").slideToggle('fast', function() {$("#ComentarioComentario").focus();});
	e.preventDefault();
}

function commPage(e) {
	var page = getUrlParams($(this).attr('href'), '/page:', 0);
	$("#commLoader").show();
	commLoad(page, 'paging', null, null);
	
	e.preventDefault();
}

function commDelete(e) {
	if (!confirm(LCS['delete_confirm'])) {
		return false;
	}
	var id = getUrlParams($(this).attr('href'), '/delete/', 0);
	
	$(this).addClass('loading');
	
	$.ajax({
		type: 'DELETE',
		url: BASE_URL+'comentarios/delete/'+id,
		success: function (body) {
			var msgs = alertParse(body);
			if (msgs['type'] == 'success') {
				if ($("[id^=commId_]").length < 2) {
					commLoad(1, 'delete', msgs, id);
				} else {
					$("#commId_"+id).fadeOut('normal', function(){
						setAlert(msgs['message'], msgs['type']);
						$("#commId_"+id).remove();
					});
				}
				
				if ($("#connTtl").length > 0) $("#connTtl").text(parseInt($("#connTtl").text())-1);	
			} else {
				$(this).remove();
				setAlert(msgs['message'], msgs['type']);	
			}
		},
		error: function() {setAlert('Unknow error!', 'error');}
	});
	
	e.preventDefault();
}

function commReply(e) {
	var parent_id = getUrlParams($(this).attr('href'), '/commr:', 0);
	var login = getUrlParams($(this).attr('href'), '/commrl:', 0);
	
	$("#ComentarioParentId").val(parent_id);
	$("#commReplytoInfoLogin").html(login);
	$("#commReplytoInfo").show();
	
	$("#commAdd").slideDown('fast', function() {$("#ComentarioComentario").focus();});
	if ($(window).scrollTop() > $("#commAddOpen").position().top) $(window).scrollTop($("#commAddOpen").position().top);
	e.preventDefault();
}

function commReplyCancel(e) {
	$("#ComentarioParentId").val(0);
	$("#commReplytoInfo").hide();
	$("#ComentarioComentario").focus();
	e.preventDefault();
}

function commLoad(page, tipo, msgs, idComm) {
	$('#commPaging').html('<div class="smallloader_paging_02"></div>');
	$.ajax({
		type: "GET",
		url: BASE_URL+'/comentarios/index/'+model+'/'+item_id+'/page:'+page+'/tab-ajax:'+tab_ajax,
		data: "isajax=1&cc="+Math.ceil(Math.random()*1000),
		success: function(msg){
			if(msg==''){
				alert(LCS['opererror']);
				return false;
			}
			if ($("#commArea").length > 0) {
				$("#commArea").html(msg);
			} else {
				$("#center").html(msg);
			}
			currPage = page;
			if (tipo == 'send') {
				setAlert(msgs['message'], msgs['type']);
				$("#ComentarioComentario").removeAttr('disabled');
				$("#ComentarioSend").removeAttr('disabled');
				if ($("#connTtl").length > 0) $("#connTtl").text(parseInt($("#connTtl").text())+1);	
			} else if (tipo == 'delete') {
				setAlert(msgs['message'], msgs['type']);
				$("#commId_"+idComm).remove();
			} else {
				if ($("#commArea").length > 0)
					if ($(window).scrollTop() > $("#commArea").position().top)	$(window).scrollTop($("#commArea").position().top);	
			}
			efetueLoginGo();
			commEvents();
			openNewWindow();
			showUserInfo();
		},
		error: function() {$('#commPaging').html(''); $("#commLoader").hide(); setAlert('Unknow error!', 'error');}
	});
}

// Emotion Area Opent
function commEmoticonOpen(e) {
	$("#commEmotions").toggle();
	$("#commEmotions").css('top', $(this).position().top+12);
	$("#commEmotions").css('left', $(this).position().left-1);
	e.preventDefault();
}

function commEmoticonInsert(e) {
	var code = $(this).attr('title');
	$("#commEmotions").hide();
	$("#ComentarioComentario").val($("#ComentarioComentario").val()+''+code+'');
	$("#ComentarioComentario").focus();
	e.preventDefault();
}