﻿/// <reference path="../Edentity.Global.js" />

Edentity.RegisterNamespace("Cosmo.Controls.TopSection");

(function (TopSection, $) {

    TopSection.OnInit = function(wcfHostHeader) {
    
        $(".OnTVLink").click(function() {
            // Set initial style
            var btnOffset = $(this).offset(),
                btnWidth = $(this).width(),
                schedOffsetLeft = btnOffset.left + btnWidth;
            $("#TVSchedule").css({
                top: btnOffset.top,
                left: schedOffsetLeft,
                height: 0,
                width: 0
            }).show();
            
            // Do animation
            animOpenSchedule(schedOffsetLeft, bindCloseSchedule);
        });
        $(".OnTVLink").hoverIntent({
            sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)    
            interval: 50, // number = milliseconds for onMouseOver polling interval    
            over: //makeTall, // function = onMouseOver callback (REQUIRED)    
                    function(event) {
                        $(".OnTVLink").triggerHandler("click");
                    },
            timeout: 1, // number = milliseconds delay before onMouseOut    
            out: //makeShort // function = onMouseOut callback (REQUIRED)
                    function(event) {
                        //animCloseSchedule();
                    }
        });
        getWhatsOn();
        setTimeout(getWhatsOn, 180000); // refresh "What's On" every 3 minutes
    }
    
    function animOpenSchedule(initialLeft, fnComplete) {
        $("#TVSchedule").animate({
            width: "917px",
            height: "228px"
        }, {
            duration: 1000,
            step: function(now, fx) {
                if (fx.prop == "width")
                {
                    var offsetLeft = initialLeft - now;
                    if ($.browser.msie)
                    {
                        offsetLeft = Math.ceil(offsetLeft);
                    }
                    $("#TVSchedule").css("left", offsetLeft);
                }
            },
            complete: fnComplete
        });
    }
    
    function animCloseSchedule() {
        var $TVSchedule = $("#TVSchedule"),
            initialLeft = parseInt($TVSchedule.css("left"), 10);
        $("#TVSchedule").animate({
            width: "0px",
            height: "0px"
        }, {
            duration: 500,
            step: function(now, fx) {
                if (fx.prop == "width") {
                    var offsetLeft = initialLeft + (fx.start - now);
                    if ($.browser.msie) {
                        offsetLeft = Math.ceil(offsetLeft);
                    }
                    $TVSchedule.css("left", offsetLeft);
                }
            },
            complete: function() { $TVSchedule.hide() }
        });
    }
    
    function bindCloseSchedule() {
        $("#TVSchedule .EmptySpace").one("click", animCloseSchedule);
        $("#TVSchedule").click(function(e) {
            e.stopPropagation();
        });
        $(document).one("click", function(e) {
            animCloseSchedule();
        });
    }
    
    function getWhatsOn() {

        var wsurl = wcfHostHeader + '/WebServices/TVSchedule.svc/TVScheduleWhatsOn?method=Cosmo.Controls.TopSection.getWhatsOn_Callback';
        $.ajax({
            url: wsurl,
            contentType: "application/json; charset=utf-8",
            dataType: "jsonp"
        });
    }
    
    TopSection.getWhatsOn_Callback = function(result) {
    
        if (result.WhatsOnRightNowResult && result.WhatsOnRightNowResult.SIM) {
            var $target = $("#WhatsOnNow"),
                show = result.WhatsOnRightNowResult.SIM,
                airingDate = RenderDateFromJSON(show.Airing_datetime),
                airTimeString = airingDate.format("h:mm tt").toUpperCase() + " EST",
                programName = (show.Title_type_name == "Movie") ? show.Title_name : show.Program_name;
				
			if ((show.Title_type_name != "Movie") && ($.isFunction(getShowURL))) {
				programName = '<a href="' + getShowURL(show.Program_name) + '">' + programName + '</a>';
			}
			
            $target.html(airTimeString + " " + programName);
        }
    }

})(Cosmo.Controls.TopSection, jQuery);