﻿function pageLoad(sender, args) {
    var comment = $("[id$='txtComments']");
    var record = $("[id$='hdnMediaId']");
    var userId = $("[id$='hdnUserId']");

    $("#btnComment").click(function () {
        //validation
        if (userId.val() == 0) {
            sayConfirm('leave comments');
            return false;
        }
        if (comment.val().length == 0) {
            jAlert('Please type a comment in the Comment text box and click the Post Comment button', 'No Comment Posted');
            return false;
        }

        //execute the ajax call and get a response
        var options = {
            type: "POST",
            url: "../Handlers/Comment.ashx?section=Mix",
            data: "{'Comment':'" + escape(comment.val()) + "','mediaId':" + record.val() + "}",
            contentType: "application/json;charset=utf-8",
            dataType: "json",
            async: false
        };
        var returnText = $.ajax(options).responseText;
        if (returnText != '') {
            jAlert(returnText + '.  Note: You will not see your comment until the page has been refreshed.', 'Comment Posted');
            //jAlert(returnText, 'Alert', function (r) { __doPostBack('updatePanel2', ''); })
        }
    });

    $("#btnDownload").click(function () {
        var pm = $("[id$='hdnIsPM']");
        var rm = $("[id$='hdnIsRM']");
        if (rm.val() == 'True') {
            if (userId.val() == 0) {
                sayConfirm('download remixes');
                return false;
            }
            if (pm.val() == 'False') {
                var randomnumber = Math.floor(Math.random() * 1000001);
                if (!jConfirm('You must upgrade to a DJ Member to download Remixes.  When you upgrade you get unlimited downloads to all videos, remixes, beats, dj tools, and more.  Do you want to be redirected to the membership subscription order page?', 'Membership Upgrade Required', function (r) { if (r) { window.location.href = '../Public/OrderForm.aspx?userId=M' + userId.val() + '-' + randomnumber } else { return false; } })) return false;
            }
        }
        var dlOptions = {
            type: "POST",
            url: "../Handlers/CheckDownload.ashx?section=Mix",
            data: "{'mediaId':" + record.val() + "}",
            contentType: "application/json;charset=utf-8",
            dataType: "json",
            async: false
        };
        var returnDl = $.ajax(dlOptions).responseText;
        if (returnDl == 0) {
            jAlert('To improve the quality of our remixes you are required to rate this remix or post a comment before you can download', 'Feedback Required');
            return false;
        } else {
            window.location.replace("../Handlers/Download.ashx?section=Mix&ID=" + record.val());
        }
    });

    $("#stars-wrapper").stars({
        captionEl: $("#hover_ocv2"),
        callback: function (ui, type, value, event) {
            var result = $("[id$='lblCurrentRating']");
            if (userId.val() == 0) {
                sayConfirm('rate videos');
                return false;
            }
            var rateOptions = {
                type: "POST",
                url: "../Handlers/Rate.ashx?section=Mix",
                data: "{'mediaId':" + record.val() + ",'rating':" + value + "}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                async: false
            };
            var returnRating = $.ajax(rateOptions).responseText;
            if (returnRating != '') {
                jAlert(returnRating, "Video Rated");
                result.html("Thank you for your feedback.   You rated this " + value + " star(s), and can now download this remix.");
            }
        },
        cancelShow: false
    });
    $("#hover_ocv2").html("Click a star to rate!");
}

function sayConfirm(msg) {
    if (!jConfirm('You must be a member, and logged-in to ' + msg + '.  Would you like to be redirected to the Login/Signup page?', 'Logged-In Members Only', function (r) { if (r) { window.location.href = '../Login.aspx' } else { return false; } })) return false;
}

function showMessageBox(id) {
    $("a[href^='../Modal/MemberMessage.aspx']").attr('href', '../Modal/MemberMessage.aspx?tomid=' + id);
    $("#msgLink").trigger("click");
}
function showImage(file, w, h) {
    var pic = "<div style='padding-top:40px;padding-left:60px'>No Cover Available</div>"
    if (w != 217 && h != 99) {
        pic = "<span style='padding:10px;width:" + w + "px;height:" + h + "'><img src='../Uploads/Gallery/" + file + "'></img></span>";
    }
    $.fn.ceebox.overlay();
    $.fn.ceebox.popup(pic, { titles: false, width: w, height: h });
    return false;
}
$('#videoDetailsPane').jScrollPane({ showArrows: true });
$('#videoCommentsPane').jScrollPane({ showArrows: true });