﻿/// <reference path="jquery-1.4.2.js" />
/// <reference path="starRating/jquery.rating.js" />

//Developed By SOinteractive 19-10-2010
//Director - Simon Ovens
//email - simon.ovens@sointeractive.co.nz
//website - http://sointeractivemodules.com

var embeddedVideos = {
    getVideosByPageUrl: String,
    getVideosCountUrl: String,
    getVideoUrl: String,
    offset: Number,
    setRatingUrl: String,
    lastItemId: Number,
    itemsPerPage: Number,
    totalItems: Number,
    videosCount: Number,
    orderBy: Number,
    orderByDefault: String,
    sortOrder: Number,
    showRatings: Boolean,
    editUrl: String,
    isEditable: String,
    moduleId: String,
    videoTitlesClientId: Number,
    rating: Number,
    init: function () {
        this.offset = 1;
        this.lastItemId = 0;
        this.totalItems = 0;
        this.orderBy = this.orderByDefault;
        $(".orderBy").val(this.orderByDefault);
        this.sortOrder = $(".sortOrderDesc input[type=radio]").attr("checked") ? 0 : 1;
        this.getAllVideosCount();
        this.getVideos(this.offset);
        $("#getVideo").click(function () {
            if ($("#" + embeddedVideos.videoTitlesClientId).val() != -1) {
                embeddedVideos.getVideo($("#" + embeddedVideos.videoTitlesClientId).val());
            }
            return false;
        });
        $("#videoListBtn").click(function () {
            $("#videosContainer").html("");
            embeddedVideos.totalItems = 0;
            embeddedVideos.offset = 1;
            embeddedVideos.getVideos(1);
        }).hide();
        $(".sortOrderAsc").change(function () {
            $("#videosContainer").html("");
            embeddedVideos.sortOrder = 1;
            embeddedVideos.totalItems = 0;
            embeddedVideos.offset = 1;
            embeddedVideos.getVideos(1);
        });
        $(".sortOrderDesc").change(function () {
            $("#videosContainer").html("");
            embeddedVideos.sortOrder = 0;
            embeddedVideos.totalItems = 0;
            embeddedVideos.offset = 1;
            embeddedVideos.getVideos(1);
        });
        $(".orderBy").change(function () {
            $("#videosContainer").html("");
            embeddedVideos.totalItems = 0;
            embeddedVideos.offset = 1;
            embeddedVideos.orderBy = $(this).val();
            embeddedVideos.getVideos(1);
        });
        $("#moreBtn").click(function () {
            if (embeddedVideos.offset != 1)
                embeddedVideos.getVideos(embeddedVideos.offset);
        }).hide();
    },
    getEditUrl: function (itemId) {
        return this.editUrl + "?itemId=" + itemId;
    },
    decodeHtml: function (encodedStr) {
        var str = $("<div/>").html(encodedStr).text();
        return str;
    },
    isVisible: function () {
        if (this.isEditable == "True")
            return "";
        else {
            return "hide";
        }
    },
    showVideoListButton: function () {
        $("#moreBtn").hide();
        $("#videoListBtn").show();
    },
    hideButtons: function () {
        $("#moreBtn").hide();
        $("#videoListBtn").hide();
    },
    showMoreButton: function () {
        $("#moreBtn").show();
        $("#videoListBtn").hide();
    },
    getAllVideosCount: function () {
        var data = "{moduleId: " + this.moduleId + "}";
        $.ajax({
            type: "POST",
            url: this.getVideosCountUrl,
            data: data,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (results) {
                embeddedVideos.videosCount = results.d;
            },
            error: function (request, error) {
                alert(request.status + request.responseText);
            }
        });
    },
    getVideos: function (offset) {
        this.hideButtons();
        var data = "{moduleId: " + this.moduleId + ", itemsPerPage: " + this.itemsPerPage + ", orderBy: " + this.orderBy + ", sortOrder: " + this.sortOrder + ", offset: " + offset + "}";
        $.ajax({
            type: "POST",
            url: this.getVideosByPageUrl,
            data: data,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (results) {
                if (results.d.length > 0)
                    embeddedVideos.renderVideos(results);
            },
            error: function (request, error) {
                if (console != null) console.log(request.status + request.responseText);
            }
        });
    },
    getVideo: function (itemId) {
        this.hideButtons();
        $("#videosContainer").html("");
        var data = "{moduleId: " + this.moduleId + ", itemId: " + itemId + "}";
        $.ajax({
            type: "POST",
            url: this.getVideoUrl,
            data: data,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (results) {
                embeddedVideos.renderVideo(results);
            },
            error: function (request, error) {
                if (console != null) console.log(request.status + request.responseText);
            }
        });
    },
    renderVideo: function (results) {
        $("#videoTemplate").tmpl(results).appendTo("#videosContainer");

        this.showVideoListButton();
        this.formatVideos();
        this.showRatings == "True" ? this.formatRatings() : $(".ratingsContainer").hide();
    },
    renderVideos: function (results) {
        $("#videosTemplate").tmpl(results).appendTo("#videosContainer");
        this.showMoreButton();
        this.totalItems = this.totalItems + results.d.length;
        if (this.itemsPerPage == 0 ||
                this.totalItems === this.videosCount)
            $("#moreBtn").hide();
        else
            this.offset = this.offset + this.itemsPerPage;

        this.formatVideos();
        this.showRatings == "True" ? this.formatRatings() : $(".ratingsContainer").hide();
    },
    setRating: function (value, itemId) {
        var data = "{moduleId: " + this.moduleId + ", itemId: " + itemId + ", rating: " + value + "}";
        $.ajax({
            type: "POST",
            url: this.setRatingUrl,
            data: data,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (results) {
                $("input[name='" + itemId + "']").rating("select", results.d.toString(), false);
                $("input[name='" + itemId + "']").rating("disable");
            },
            error: function (request, error) {
                alert(request.status + request.responseText);
            }
        });
    },
    formatRatings: function () {
        $(".ratingAdded").hide();
        $("input.rating").rating({
            required: true,
            callback: function (value, link) {
                var itemId = $(this).attr("name");
                embeddedVideos.setRating(value, itemId);
                $(this).parent().children(".ratingAdded").fadeIn(200, function () {
                    $(this).fadeOut("slow");
                });
            }
        });

        $(".videoContainer .ratingsHid").each(function () {
            var itemId = $(this).attr("name");
            var rating = $(this).val();
            $("input[name='" + itemId + "']").rating("select", rating, false);
        });
    },
    formatVideos: function () {
        $(".description").each(function () {
            if ($(this).children(".formatted").length == 0) {
                var formatted = $("<div/>").addClass("formatted");
                $(this).html(embeddedVideos.decodeHtml($(this).html())).append(formatted);
            }
        });
    }
}


