﻿/// <reference path="~/CMSTemplates/CES/Scripts/jquery-1.6.2-vsdoc.js" />

var ces = {

    init: function () {
        this.loadTweets();
        this.bindModalLinks();
        this.bindTabs();
    },

    loadTweets: function () {
        if ($(".m-tweetbeat").length > 0) {
            // This handler provides the local proxy for the blog feed.
            var handlerUrl = "/CEE/Handlers/TwitterFeedHandler.ashx";

            // Get the "tweetbeat" element from page.
            var tweetbeat = $(".m-tweetbeat");

            // Get the tweetbeawt-specific data from the "data-" attributes.
            var username = $(tweetbeat).attr("data-twitter-username");

            // Append a timestamp to the local proxy URL to ensure no caching issues (IE).
            var timestamp = new Date().getTime();
            var internalFeedUrl = handlerUrl + "?u=" + username + "&ts=" + timestamp;

            var request = $.ajax({
                url: internalFeedUrl,
                type: "GET",
                dataType: "xml"
            });

            // Handle successful ajax request.
            request.done(function (data) {

                if ($(".m-tweetbeat").hasClass("tweetbeat-home") || $(".m-tweetbeat").hasClass("tweets-module")) {
                    $(tweetbeat).append("<div class=\"tweets\"></div>");
                }
                else {
                    $(tweetbeat).find(".tweets-container").append("<div class=\"tweets\"></div>");
                }

                var list = $(tweetbeat).find(".tweets");

                $(data).find("status").each(function () {
                    // Get the tweet "text".
                    var text = $(this).find("text").text();
                    text = twttr.txt.autoLink(text);
                    var textP = text;

                    var html = "<li class=\"tweet\">" + textP + "</li>";

                    // Add the "item" to the list.
                    $(list).append($(html));
                });

                // Remove the "loading" message.
                $(".m-tweetbeat .loading-message").remove();

                $(list).append('<li><a href="http://www.twitter.com/mncee_myhome" target="_blank">Follow @MNCEE_MYHOME</a></li>');
            });

            // Handle failed ajax request.
            request.fail(function (jqXHR, status) {
                //alert("status: " + status);
            });
        }
    },

    bindModalLinks: function () {
        $(".right a").click(function () {
            $.fancybox({
                'padding': 0,
                'openEffect': 'fade',
                'closeEffect': 'fade',
                'closeBtn': false,
                'href': this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
                'type': 'swf',
                'swf': { 'wmode': 'transparent', 'allowfullscreen': 'true' },
                'width': 740,
                'height': 430,
                'title': '<center><a onclick="$.fancybox.close();" style="cursor: pointer;"><img src="/CMSTemplates/CES/Images/modal-close.png" alt="" /></a></center>'
            });

            return false;
        });

        $("a.modal").click(function () {
            $.fancybox({
                'padding': 0,
                'openEffect': 'fade',
                'closeEffect': 'fade',
                'closeBtn': false,
                'href': this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
                'type': 'iframe',
                'width': 740,
                'height': 430,
                'title': '<center><a onclick="$.fancybox.close();" style="cursor: pointer;"><img src="/CMSTemplates/CES/Images/modal-close.png" alt="" /></a></center>'
            });

            return false;
        });
    },

    bindTabs: function () {
        $(".tabs .tab").click(function () {

            var tab = $(this);

            $(".tabs .tab.active").not(tab).removeClass("active");
            $(".tabs .tab").not(tab).addClass("inactive");
            tab.removeClass("inactive").addClass("active");

            $(".tab-overlays .tab-overlay").hide();
            $(".tab-overlays .tab-overlay").eq(tab.index(".tabs .tab")).show();

            $(".testimonials, .workshop-callout, .partners").addClass("active");
        });
    }
}

$(document).ready(function () {
    ces.init();
});
