var on_ajax = false; var fid = -1; var comment_now = 'valorado'; var comment_arr = new Object(); $(document).ready(function() { var sid = $("#sid").val(); var comment_url = 'http://compras.innatia.com/comentarios.php?s=compras&sid='+sid; comment_arr["valorado"] = $('#comments-block').html(); $(".like").each(function() { var tmp = $(this).attr("id").split("_"); var cid = tmp[1]; if ($.cookie('comentarios_rating['+cid+']') != null) { $("#like_"+cid).addClass("commentDone"); $("#dislike_"+cid).addClass("commentDone"); $("#like_"+cid+" span").removeClass("voteOK").addClass("doneOK"); $("#dislike_"+cid+" span").removeClass("voteKO").addClass("doneKO"); } }); $(document).on("click", ".like", function() { var tmp = $(this).attr("id").split("_"); comment_rating(tmp[1], 1); return false; }); $(document).on("click", ".dislike", function() { var tmp = $(this).attr("id").split("_"); comment_rating(tmp[1], -1); return false; }); // Encuestas Contenido var removeAll = false; $(".opcion .voteYES").each(function() { var tmp = $(this).attr("id").split("_"); var rid = tmp[1]; var eid = $(this).attr("href").replace("#encuesta", ''); if ($.cookie('votacion_contenido['+rid+']') != null) { var p = $(this).parent(); if (p.hasClass("unica")) { $("#encuesta"+eid+" > .opcion a").each(function () { $(this).remove(); }); removeAll = true; } else { $("#s_"+rid).remove(); $("#n_"+rid).remove(); } } if (removeAll) return; }); if ($.cookie('votacion_contenido_comentario['+sid+']') != null) $("#commentVote").remove(); $(document).on("click", ".opcion .votes", function() { if (on_ajax) return false; on_ajax = true; var t = $(this); var p = t.parent(); var tmp = t.attr("id").split("_"); var rid = tmp[1]; var eid = t.attr("href").replace("#encuesta", ''); if (t.attr("title") == "SI") var value = 1; else if (t.attr("title") == "NO") var value = -1; else { alert("Que intentas hacer?") return false; } $.ajax({ type: 'POST', url: 'http://compras.innatia.com/scripts/ajax.php', data: 'a=articulos-add_vote&sid='+sid+'&r='+rid+'&f='+mpfid+'&v='+value, success: function(result) { on_ajax = false; res = eval('('+result+')'); if (res["success"] == 1) { if (mpfid == -1 && mpcid != -1) $.ajax({ type: 'POST', url: 'http://compras.innatia.com/scripts/ajax.php', data: 'a=articulos-update_fid&c='+mpcid+'&f='+res["fid"], success: function(result){if (res["success"] != 1) alert("Error: "+res["error"]);} }); mpfid = res["fid"]; if ($("#r_"+rid+" > .results_bar").length > 0) { var y = parseInt($("#pss_"+rid).text()); var n = parseInt($("#psn_"+rid).text()); var t = y + n + 1; if (value == 1) { y += 1; $("#pss_"+rid).text(y); } else $("#psn_"+rid).text(n + 1); $("#wss_"+rid).css("width", (y * 100 / t)+"%"); } else { var y = value == 1 ? 1 : 0; $("#r_"+rid).html(' '+y+'/'+(1 - y)+'').addClass("voted"); } if (p.hasClass("unica")) { $("#encuesta"+eid+" > .opcion a").each(function () { $(this).remove(); }); } else { $("#s_"+rid).remove(); $("#n_"+rid).remove(); } } else alert("Error: "+res["error"]); } }); return false; }); $(document).on("click", ".commentVoteButton", function(e) { e.preventDefault(); if (on_ajax) return false; var p = $(this).parent().parent(), n = jQuery.trim(p.find(".commentVoteName").val()).replace(/&/g, "%26"), e = jQuery.trim(p.find(".commentVoteEmail").val()).replace(/&/g, "%26"), t = jQuery.trim(p.find(".commentVoteComment").val()).replace(/&/g, "%26"), o = p.find(".chk_conditions:checked").length; if (e == 'Introduce tu email') e = ''; if (e == '' || !(/^[A-Za-z][A-Za-z0-9_.]*@[A-Za-z0-9_]+\.([A-Za-z0-9_.]+[A-za-z])+$/.test(e))) { alert("El mail no es uno valido!"); return false; } if (n == 'Introduce tu nombre') n = ''; if (n == '' || t == '') { alert("Debe completar todos los campos!"); return false; } on_ajax = true; p.find(".commentVoteLoading").removeClass("hidden"); $.ajax({ type: 'POST', url: 'http://compras.innatia.com/scripts/ajax.php', data: 'a=articulos-vote_comentario&cid=&sid='+sid+'&s=compras&f='+mpfid+'&n='+n+'&e='+e+'&t='+t+'&o='+o, success: function(result) { on_ajax = false; res = eval('('+result+')'); p.find(".commentVoteLoading").addClass("hidden"); if (res["success"] == 1) { mpcid = res["cid"]; p.parent().html("Gracias por comentar!"); if(typeof comment_add == 'function') comment_add(-1, 1, n, t); } else alert("Error: "+res["error"]); } }); }); // Comentarios $(".ahide").click(function() { var tmp = $(this).attr("id").split("-"); var h = $("#hide-"+tmp[1]); if (h.css("display") == 'none') { h.removeClass('hidden'); $(this).text('Click aqui para ocultarlo.'); } else { h.addClass('hidden'); $(this).text('Click aqui para verlo.'); } return false; }); $(".filter").click(function() { var tmp = $(this).attr("id").split("_"); if (tmp[1] != 'comment') { $(".comment").hide(); $("."+tmp[1]).show(); } else $(".comment").show(); return false; }); $(".reply").click(function() { var tmp = $(this).attr("id").split("_"); fid = tmp[1]; var top = $("#formComentario").offset().top; $('html,body').animate({scrollTop: top}, 1000); return false; }); $(".btn_comentarios").click(function(e) { e.preventDefault(); if (on_ajax) return false; var n = jQuery.trim($("#cn").val()).replace(/&/g, "%26"); var e = jQuery.trim($("#ce").val()).replace(/&/g, "%26"); var t = jQuery.trim($("#ct").val()).replace(/&/g, "%26"); var d = parseInt($("#cd_"+fid).val()) + 1; var o = $("#ca:checked").length; if (e == '' || !(/^[A-Za-z][A-Za-z0-9_.]*@[A-Za-z0-9_]+\.([A-Za-z0-9_.]+[A-za-z])+$/.test(e))) { alert("El mail no es uno valido!"); return false; } if (n == 'Nombre*') n = ''; if (n == '' || t == '') { alert("Debe completar todos los campos!"); return false; } on_ajax = true; $("#loading").removeClass("hidden"); $.ajax({ type: 'POST', url: 'http://compras.innatia.com/scripts/ajax.php', data: 'a=comentarios_add&cid=&sid='+sid+'&s=compras&fid='+fid+'&n='+n+'&e='+e+'&t='+t+'&o='+o, success: function(result) { res = eval('('+result+')'); on_ajax = false; $("#loading").addClass("hidden"); if (res["success"] == 1) comment_add(fid, d, n, t); else alert("Error: "+res["error"]); } }); }); }); function comment_rating(cid, rating) { if (on_ajax || $("#like_"+cid).hasClass("commentDone")) return false; on_ajax = true; $("#loading-rating_"+cid).removeClass("hidden"); $.ajax({ type: 'POST', url: 'http://compras.innatia.com/scripts/ajax.php', data: 'a=comentarios-rating_add&cid='+cid+'&r='+rating, success: function(result) { on_ajax = false; $("#loading-rating_"+cid).addClass("hidden"); if (rating == 1) { var cnt = parseInt($("#like_"+cid).text()) + 1; $("#like_"+cid).html(''+cnt); } else { var cnt = parseInt($("#dislike_"+cid).text()) + 1; $("#dislike_"+cid).html(''+cnt); } $("#like_"+cid).addClass("commentDone"); $("#dislike_"+cid).addClass("commentDone"); $("#like_"+cid+" span").removeClass("voteOK").addClass("doneOK"); $("#dislike_"+cid+" span").removeClass("voteKO").addClass("doneKO"); } }); } function comment_add(fid, d, n, t) { var new_c = '\
\ \
\ '+n+'\

'+t.replace(/[\n]+/g, "\n").replace(/\n/g, '
')+'

\ \
'; var e = $("h1").text(); if (d == 1) { $("#comments-form").remove(); $("#comments-block > div:last").remove(); $("#comments-block > hr").remove(); $("#comments-block").append('
'+new_c+'

'); if ($("#comments-block > p.title").text() == 'No hay comentarios') $("#comments-block > p.title").html('1 Comentario en "'+e+'"'); else { var c = parseInt($("#comments-block > p.title").text()) + 1; $("#comments-block > p.title").html(''+c+' Comentarios. en "'+e+'"'); } } else if (d > 1) { $("#comments-form").remove(); $("#comment_"+fid).parent().append(new_c); //var top = $("#comment_"+fid).offset().top; //$('html,body').animate({scrollTop: top}, 1000); c = parseInt($("#comments-block > p.title").text()) + 1; $("#comments-block > p.title").html(''+c+' Comentarios. en "'+e+'"'); } } function comment_order(id) { if (comment_now != id) { comment_now = id; if (comment_arr[id] == null) { $("#comments-block-loading").show(); $(".comentario").hide(); $('#comments-block').load(comment_url+'&o='+id, function(response, status, xhr) { comment_arr[id] = response; }); } else { $('#comments-block').html(comment_arr[id]); } } }