﻿function pageLoad(sender, args) {
    var comment = $("[id$='txtComments']");
    var record = $("[id$='hdnMediaId']");
    var userId = $("[id$='hdnUserId']");

    $("#dlprogress").hide();
    $("#btnDownload").delay(2000).fadeIn(2000);

    $("#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=Video",
            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 va = $("[id$='hdnVidAccess']");
        if (userId.val() == 0) {
            sayConfirm('download videos');
            return false;
        }
        if (pm.val() == 'False') {
            var randomnumber = Math.floor(Math.random() * 1000001);
            if (!jConfirm('You must upgrade to a DJ Member to download Videos.  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;
        }
        if (va.val() == 'False') {
            var randomnumber = Math.floor(Math.random() * 1000001);
            if (!jConfirm('Sorry, you must upgrade your membership to the new pricing in order to download videos.  You currently have access to all audio files, however you must upgrade and get unlimited downloads to all videos.  Do you want to be redirected to the membership subscription upgrade 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=Video",
            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 video remixes/edit you are required to rate this video or post a comment before you can download', 'Feedback Required');
            return false;
        } else {
            $("#btnDownload").hide();
            $("#dlprogress").show();
            window.location.replace("../Handlers/Download.ashx?section=Video&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=Video",
                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("You rated this " + value + " star(s)");
            }
        },
        cancelShow: false
    });
    $("#hover_ocv2").html("Click a star to rate!");
    $("#flowPlayerAccordion").tabs("#flowPlayerAccordion div.flowPlayerPane", { tabs: 'h2', effect: 'slide', initialIndex: null });
}

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(id, w, h) {
    var pic = "<div style='padding-top:40px;padding-left:30px'>No Screen Shot Available</div>"
    if (w > 300) {
        pic = "<span style='padding:10px;width:" + w + "px;height:" + h + "'><img src='../Uploads/Gallery/vid_" + id + ".jpg'></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 });

$.tools.tabs.addEffect("slide", function (i, done) {
    this.getPanes().slideUp().css({ backgroundColor: "#b8128f" });
    this.getPanes().eq(i).slideDown(function () {
        $(this).css({ backgroundColor: 'transparent' });
        done.call();
    });
});

function formatImage(cellValue, options, rowObject) {
    var imageHtml = "<img src='../App_Themes/Style/Images/video_green_icon.png' originalValue='" + cellValue + "' alt='Preview & Details' style='cursor: pointer;'></img>";
    return imageHtml;
}
function unformatImage(cellValue, options, cellObject) {
    return $(cellObject.html()).attr("originalValue");
}
