﻿
var cpage = 1;
var tpages = null;
var tcount = null;
var pcount = 15;

var nratings = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
var rfriend = 0;
var subid = 0;

var templ;
var meditor;

var ownr;
var subjecttb;

$().ready(function () {

    subjecttb = $(subjecttbid);

    $("input[name='recommend']").each(function () {
        $(this).change(function () {
            rfriend = Number($(this).attr("value"));
        });
    });

    $('.ovrating').each(function () {

        $(this).qtip(
            {
                content: {
                    url: $(this).attr("rel")
                },
                position: {
                    corner: {
                        target: 'topLeft',
                        tooltip: 'bottomMiddle'
                    },
                    adjust: {
                        x: 180
                    }
                },
                style: {
                    name: 'infostyle'
                }
            });

    });

    //SET STARS
    $('.frating').each(function () {
        var tip = $(".hovert", this);
        var par = $(this).attr("rel");
        $("input", this).rating({
            focus: function (value, link) {
                tip[0].data = tip[0].data || tip.html();
                tip.html(link.title || 'value: ' + value);
            },
            blur: function (value, link) {
                tip.html(tip[0].data || '');
            },
            callback: function (value, link) {
                //alert(value + "/" + par);
                tip[0].data = tip[0].data || tip.html();
                tip.html(link.title);
                nratings[Number(par)] = Number(value);
            }
        });
    });

    templ = jQuery.createTemplateURL("/plingg_api/Templ/ReviewTempl.htm", null, { filter_data: false, runnable_functions: true });
    templ.setParam("getratname", getratname);
    templ.setParam("showrating", showrating);
    templ.setParam("getsubprestring", getsubprestring);
    templ.setParam("Texte", tobj);

    loadcomments(1);
});

function loadcomments(pageid) {
    cpage = pageid;

    $.jmsajax({
        url: "/plingg_api/WS/CommentsService.svc/GetReviews",
        data: { ReviewTypInt: btypeid, ID: selreturnid, PageID: cpage },
        success: function (data) {
            addcomments(data);
            if (cpage == 1) {
                if (data.OwnReview.length == 0) {
                    $("#AddReviewDIV").show();
                } else {
                    ownr = data.OwnReview;
                    $("#CommentOwnDIV").show();
                    var cn = { "Reviews": data.OwnReview };
                    var ht = jQuery.processTemplateToText(templ, cn);
                    $(ht).appendTo("#CommentOwnInnerDIV").fadeIn("slow");

                    var hasrev = false;
                    for (var x = 0; x < data.OwnReview.length; x++) {
                        if (data.OwnReview.ReviewType == btypeid) {
                            hasrev = true;
                        }
                    }
                    if (hasrev == false) {
                        $("#AddReviewDIV").show();
                    }

                }
            }
        }
    });
}

function showoldercomments() {
    loadcomments(cpage + 1);
}

function addcomments(commobj) {
    tpages = Math.ceil(commobj.TotalCount / pcount);
    tcount = commobj.TotalCount;

    if (tpages > cpage) {
        $("#OlderCommentsDIV").show();
    } else {
        $("#OlderCommentsDIV").hide();
    }

    $("#commentcountlabel").html(commobj.TotalCount + " ");


    var ht = jQuery.processTemplateToText(templ, commobj);

    $("#CommentsLoadingDIV").hide();

    $(ht).appendTo("#CommentsInsertDIV").fadeIn("slow");

    doformatting();
}

function showrating(rt,rid) {
    if (rid <= rcount[rt-1]) {
        return true;
    } else {
        return false;
    }
}

function getsubprestring() {
    return tobj.SeenString;
}

function showsubobject(subid) {
    showeventmodal(subid);
}

function getratname(rid) {
    return rnames[rid - 1];
}

function getratnamefortype(rt,rid) {
    return rnames[rt-1][rid - 1];
}

function doformatting() {
    $('#CommentsInsertDIV a.ttlink').each(function () {
        $(this).qtip(
            {
                content: {
                    url: $(this).attr("rel")
                },
                position: {
                    corner: {
                        target: 'topMiddle',
                        tooltip: 'bottomMiddle'
                    },
                    adjust: {
                        x: 4
                    }
                },
                hide: {
                    fixed: true // Make it fixed so it can be hovered over
                },
                style: {
                    name: 'infostyle'
                }
            });
    });
}

function subchanged(el) {
    subid = $(el).val();
}

function savecomment() {
    var substr = "";
    var isvalid = true;
    if (subjecttb.val() == "") {
        alert(tobj.NoSubject);
        isvalid = false;
    }

    if (nratings[0] == 0 && isvalid == true) {
        alert(tobj.NoOverall);
        isvalid = false;
    }

    if (isvalid == true) {

        var meditor = tinyMCE.get(editorid);
        meditor.setProgressState(1);

        $("#savecommentbutton").attr("disabled", "true");

        var bodstr = meditor.getContent();
        if (bodstr == "") {
            bodstr = "empty";
        }

        if ($("div#bandcbdiv").length > 0) {
            var cbs = $("div#bandcbdiv input");

            for (var x = 0; x < cbs.length; x++) {
                var bandstr = $(cbs[x]).attr("id").substring($(cbs[x]).attr("id").indexOf("Band_") + 5, $(cbs[x]).attr("id").length);
                if ($(cbs[x]).attr("checked") == true) {
                    substr += bandstr + ","
                }
            }
        } else {
            substr = subid;
        }


        $.jmsajax({
            url: "/plingg_api/WS/CommentsService.svc/AddReview",
            data: { ReviewTypInt: btypeid, ID: selreturnid, Subject: subjecttb.val(), Body: bodstr, OverallRating: nratings[0], R1: nratings[1], R2: nratings[2], R3: nratings[3], R4: nratings[4], R5: nratings[5], R6: nratings[6], R7: nratings[7], R8: nratings[8], R9: nratings[9], R10: nratings[10], RFriend: rfriend, SubID: substr },
            success: function (data) {

                meditor.setProgressState(0);
                meditor.setContent("");

                $("#AddReviewDIV").hide();

                $("#NewCommentDIV").hide(500);
                $("#savecommentbutton").attr("disabled", "");
                subjecttb.val("");

                tcount += 1;
                data.Reviews[0].Nr = tcount;

                var ht = jQuery.processTemplateToText(templ, data);
                $("#CommentsLoadingDIV").hide();

                $(ht).prependTo("#CommentsInsertDIV").slideDown(1400);

                doformatting();


            }
        });
    }
}

function cancelcomment() {

    $("#AddReviewDIV").show();

    $("#NewCommentDIV").hide(500);
    $("#savecommentbutton").attr("disabled", "");
    subjecttb.val("");
}

function showaddcomment(replid) {
    if (cuser.userid > 0) {
        $("#NewCommentDIV").show("slow");
        $("#AddReviewDIV").hide();
    } else {
        showregistermodal();
    }
}

function deletereview(comid) {

    $.jmsajax({
        url: "/plingg_api/WS/CommentsService.svc/DeleteReview",
        data: { ReviewID: comid },
        success: function (data) {
            if (data == true) {
                $("#CommentsInsertDIV").html("");
                $("#CommentOwnDIV").html("");
                $("#CommentOwnDIV").hide();
                loadcomments(1);
            } else {
                alert("Review was not deleted due to an error");
            }
        }
    });
}

function isadmin() {
    return cuser.isadmin;
}
