﻿///<reference path="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"
var BH = function () { };
BH.Home = {};
BH.Videos = {};
BH.Shopping = {};
BH.Home.initDynamo = function () {
    window.feature_api = null;
    window.feature_state = "playing";
    var feature = $("#featured_news").scrollable({
        circular: true,
        easing: "swing",
        speed: 500,
        next: "#featured_news .next",
        prev: "#featured_news .prev"
    }).autoscroll({ autoplay: false, autopause: false, interval: 4000 }).hover(
        function (e) {
            feature_api.pause();
        },
        function (e) {
            if (feature_state != "stopped")
                feature_api.play();
        });

    feature_api = feature.data("scrollable");

    $("#featured_news .nav .pause").click(function (e) {
        e.preventDefault();
        var el = $(this);
        if (el.hasClass("paused")) {
            feature_api.play();
            feature_state = "playing";
            el.removeClass("paused");
        }
        else {
            feature_api.stop();
            feature_state = "stopped";
            el.addClass("paused");
        }
    });

    feature_api.play();
};
BH.Videos.initStrider = function (options) {
    options = $.extend({
        maxPreviewWidth: 168,
        maxPreviewHeight: 100,
        maxTitleLength:30,
        maxPerSlide: 3,
        circular: true,
        vertical: false,
        easing: "swing",
        speed: 500,
        showViews:false,
        showDate:false
    }, options);
    window.strider_api = null;
    var strider = $("#channel_videos").scrollable({
        circular: options.circular,
        vertical: options.vertical,
        easing: options.easing,
        speed: options.speed,
        next: "#channel_videos .next",
        prev: "#channel_videos .prev"
    });
    strider_api = strider.data("scrollable");
    var selector = $("#channel").change(function (e) {
        loadVideos($(this).val());
    });
    function loadVideos(a) {
        showProgress(function () {
            $.post("/videos/rpc", { action: "list-videos", album: a, maxwidth: options.maxPreviewWidth, maxheight: options.maxPreviewHeight, maxtitlelength: options.maxTitleLength }, function (data) {
                if (data != null && data != '') {
                    if (data[0].length > 0) {
                        selector.html("");
                        for (var i = 0; i < data[0].length; i++) {
                            selector.append(buildOption(data[0][i]));
                        }
                        if (data[0][0].Id == 0)
                            selector.val(data[0][1].Id);
                    }
                    if (data[1].length > 0) {
                        var oldItems = strider.find(".item");
                        for (var i = 0; i < data[1].length; i++) {
                            var container = $("<div class='item'></div>");
                            for (var z = 0; z < options.maxPerSlide; z++) {
                                container.append(buildElement(data[1][i]));
                                if (z < (options.maxPerSlide - 1))
                                    i++;
                                if (i >= data[1].length)
                                    break;
                            }
                            strider_api.addItem(container);
                        }
                        oldItems.remove();
                        strider_api.begin();
                    }
                    hideProgress();
                }
            });
        });
    }
    function showProgress(callback) {
        selector.attr("disabled", "disabled");
        strider.find(".items").fadeTo(250, 0.5, function (e) {
            var prog = $(document.createElement("div")).addClass("progress").appendTo(strider);
            prog.css({ left: strider.width() / 2 - prog.width() / 2, top: strider.height() / 2 - prog.height() / 2 });

            if (callback)
                callback();
        });
    }
    function hideProgress() {
        selector.removeAttr("disabled");
        strider.find(".progress").fadeOut(function () {
            $(this).remove();
        });
        strider.find(".items").fadeTo(250, 1);
    }
    function buildOption(data) {
        var opt = $(document.createElement("option"));
        opt.attr("value", data.Id);
        opt.html(data.Name);
        return opt;
    }
    function buildElement(data) {
        var figure = $(document.createElement("figure"));
        var a = $(document.createElement("a"));

        a.attr("href", data.RewriteUrl);
        var a2 = a.clone();
        a2.html(data.Name);

        var img = document.createElement("img");
        img.src = data.Preview;
        a.append(img);
        a.addClass("preview");

        var adorner = document.createElement("span");
        adorner.setAttribute("class", "overlay");
        a.append(adorner);

        figure.append(a);
        figure.append(a2);

        if (options.showViews){
            var span = $(document.createElement("span"));
            span.addClass("views");
            span.html("Views: " + data.ViewCount);
            figure.append(span);
        }
        if (options.showDate){
            var span = $(document.createElement("span"));
            span.addClass("date");
            span.html(data.Description);
            figure.append(span);
        }
        return figure;
    }
    loadVideos(selector.val());
};
BH.Shopping.loadSpecials = function (selector, callback) {
    $(selector).load("/shopping/specials.aspx", function (d) {
        if (callback)
            callback();
    });
};
var Rhino = function() { };
Rhino.prototype.Comment = function(selector) {
    var root = this;
    var formPanel = $(selector + " .commentform div.form");
    var progressPanel = $(selector + " .commentform div.progress");
    var commentList = $(selector + " .commentlist");
    var button = $(selector + " .commentform .form input[type='button']");
    var form = $(selector + " .commentform div.form form");

    this.reset = function (e) {
        progressPanel.slideUp();
        formPanel.slideDown();
        Recaptcha.reload();
    };
    this.isValidForm = function() {
        var errors = [];
        form.find(".required", this).each(function() {
            if (this.value.length == 0)
                errors.push("<li>A " + $(this).attr("title") + " is required.");
        });
        if ($("#recaptcha_response_field").val().length == 0)
            errors.push("<li>A Validation Code is required.");

        if (errors.length == 0)
            return true;
        else {
            progressPanel.slideDown("fast", function() {
                progressPanel.html("<ul class=\"errors\">" + errors.join("</li>") + "</li></ul>");
            });
            return false;
        }
    };
    this.postForm = function() {
        formPanel.slideUp("normal", function() {
            progressPanel.html("<h3>Your comment is being processed...</h3>").slideDown("fast", function() {
                //Post comment
                $.post(form.attr("action"), root.serializeForm(form), function(data) {
                    //Display result
                    if (data) {
                        if (data.substring(0, 5) == "error") {
                            progressPanel.html("<h3>" + data + "</h3>");
                        }
                        else {
                            progressPanel.html("<h3>Your comment has been submitted. Thank you!</h3>");
                            if (commentList.find(".comment").length == 0)
                                commentList.html("");
                            commentList.append(data);
                        }
                    } else {
                        progressPanel.html("<h3>Error occurred</h3>");
                    }

                });

            });
        });
    };
    this.serializeForm = function(form) {
        var inputs = [];
        form.find(":input", this).each(function() {
            inputs.push(this.name + '=' + escape(this.value));
        });
        inputs.push("challenge" + "=" + Recaptcha.get_challenge());
        inputs.push("response" + "=" + Recaptcha.get_response());
        return inputs.join("&");
    };
    this.remove = function(element) {
        var anchor = $(element);
        var id = anchor.attr("rel");
        $.post("/comments/post", "action=remove&commentid=" + id, function(data) {
            anchor.parents("div.comment").remove();
        });
    };
    this.approve = function(element) {
        var anchor = $(element);
        var id = anchor.attr("rel");
        var hash = anchor.attr("title");
        $.post("/comments/post", "action=moderate&commentid=" + id + "&hash=" + hash, function(data) {
            anchor.parents("div.comment").replaceWith(data);
        });
    };
    this.init = function() {
        button.click(function(e) {
            e.preventDefault();
            if (root.isValidForm())
                root.postForm();
        });
        commentList.find("a.remove").click(function(e) {
            e.preventDefault();
            root.remove(this);
        });
        commentList.find("a.moderate").click(function(e) {
            e.preventDefault();
            root.approve(this);
        });
    };
};
$(function () {
//    $(window).bind("scroll", function (e) {
//        var scroll = $(this).scrollTop();
//        //console.log(scroll);
//        if (scroll > 115) {
//            $("#navi li.nav_home a").addClass("logo");
//        }
//        else {
//            $("#navi li.nav_home a").removeClass("logo");
//        }
//    });
    $("input[type='text'].hint").each(function (i) { var el=$(this);el.val(el.attr("title")); })
    .focus(function () {
        var word = $(this).attr("title");
        if (this.value == word) {
            this.value = '';
            $(this).addClass("active");
        }
    }).blur(function () {
        var word = $(this).attr("title");
        if (this.value == '') {
            this.value = word;
            $(this).removeClass("active");
        }
    });
});
var rhino = new Rhino();
var bh = new BH();
