﻿/// <reference name="MicrosoftAjax.js"/>
var AssetDurationElements = new Array();

Type.registerNamespace('AdStream');

AdStream.AssetDuration =
    function AssetDuration(element) {
        AdStream.AssetDuration.initializeBase(this, [element]);
        this.ControlID = element.id;
        this.litDuration = null;
        this.imgSwitchMode = null;
        this.hfDurationInSeconds = null;
        this.hfDurationInTimeCode = null;
        this.hfSwitchToSecondsMessage = null;
        this.hfSwitchToTimeCodeMessage = null;
        this.hfDisplayMode = null;
        this.elementOrder = null;
    }

    AdStream.AssetDuration.prototype = {

        initialize: function ()
        {
            AdStream.AssetDuration.callBaseMethod(this, "initialize");

            var $this = $("#" + this.ControlID);
            this.litDuration = $this.find('[id$=_litDuration]').get(0);
            this.imgSwitchMode = $this.find('[id$=_imgSwitchMode]').get(0);
            this.hfDurationInSeconds = $this.find('[id$=_hfDurationInSeconds]').get(0);
            this.hfDurationInTimeCode = $this.find('[id$=_hfDurationInTimeCode]').get(0);
            this.hfSwitchToSecondsMessage = $this.find('[id$=_hfSwitchToSecondsMessage]').get(0);
            this.hfSwitchToTimeCodeMessage = $this.find('[id$=_hfSwitchToTimeCodeMessage]').get(0);
            this.hfDisplayMode = $this.find('[id$=_hfDisplayMode]').get(0);

            if (this.imgSwitchMode != null)
            {
                this.setSavedMode();
                this.SwitchModeHandler = Function.createDelegate(this, this.switchMode);
                $addHandler(this.imgSwitchMode, "click", this.SwitchModeHandler);
            }
            AssetDurationElements.push(this);
            this.elementOrder = AssetDurationElements.length - 1;
            this.initControl();
        },

        initControl: function ()
        {
            var toolTip = '';
            if (this.hfDisplayMode.value == "TimeCode")
            {
                this.litDuration.innerHTML = this.hfDurationInTimeCode.value;
                toolTip = this.hfSwitchToSecondsMessage.value;

            } else if (this.hfDisplayMode.value == "Seconds")
            {
                this.litDuration.innerHTML = this.hfDurationInSeconds.value;
                toolTip = this.hfSwitchToTimeCodeMessage.value;
            }
            if (this.imgSwitchMode != null)
            {
                this.imgSwitchMode.alt = toolTip;
                this.imgSwitchMode.title = toolTip;
            }
        },

        switchViewMode: function ()
        {
            if (this.hfDisplayMode.value == "TimeCode")
                this.hfDisplayMode.value = "Seconds";
            else if (this.hfDisplayMode.value == "Seconds")
                this.hfDisplayMode.value = "TimeCode";
            else
                this.hfDisplayMode.value = "Seconds";
            this.setCookie(this.hfDisplayMode.value);
        },

        switchMode: function ()
        {
            for (var i = 0; i < AssetDurationElements.length; ++i)
            {
                var item = AssetDurationElements[i];
                if (item != null)
                {
                    item.switchViewMode();
                    item.initControl();
                }
            }
        },

        getCookie: function ()
        {
            var name = "DurationDisplayMode"
            var cookie = " " + document.cookie;
            var search = " " + name + "=";
            var setStr = null;
            var offset = 0;
            var end = 0;
            if (cookie.length > 0)
            {
                offset = cookie.indexOf(search);
                if (offset != -1)
                {
                    offset += search.length;
                    end = cookie.indexOf(";", offset)
                    if (end == -1)
                    {
                        end = cookie.length;
                    }
                    setStr = unescape(cookie.substring(offset, end));
                }
            }
            return (setStr);
        },
        setCookie: function (value)
        {
            var name = "DurationDisplayMode"
            var expires = "December 31, 2050";
            var path = "/";
            var domain = "";
            var secure = "";
            document.cookie = name + "=" + escape(value) +
                ((expires) ? "; expires=" + expires : "") +
                ((path) ? "; path=" + path : "") +
                ((domain) ? "; domain=" + domain : "") +
                ((secure) ? "; secure" : "");
        },

        setSavedMode: function ()
        {
            var cookVal = this.getCookie();
            if (cookVal != null && cookVal != "")
                this.hfDisplayMode.value = cookVal;
        },

        dispose: function ()
        {
            AssetDurationElements.splice(this.elementOrder, 1);
            if (this.imgSwitchMode)
            {
                $clearHandlers(this.imgSwitchMode);
            }
            this.ControlID = null;
            this.litTitle = null;
            this.imgSwitchMode = null;
            this.hfDurationInSeconds = null;
            this.hfDurationInTimeCode = null;
            this.hfSwitchToSecondsMessage = null;
            this.hfSwitchToTimeCodeMessage = null;
            this.hfDisplayMode = null;
            this.elementOrder = null;
            AdStream.AssetDuration.callBaseMethod(this, "dispose");
        }
    }
    AdStream.AssetDuration.registerClass('AdStream.AssetDuration', Sys.UI.Control, Sys.IDisposable);


function createAssetDuration(control) {
    $create(AdStream.AssetDuration, {}, {}, {}, control);
}

//Added to satisfy new notifyScriptLoaded() requirement
if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();


