﻿Type.registerNamespace("Framework.Controls");

Framework.Controls.JSContentRotator = function(element) {
    Framework.Controls.JSContentRotator.initializeBase(this, [element]);
    this._Playlist = null;
    this._Width = 0;
    this._Height = 0;
    this._Time = 0;
    this._PlaybackMode = 0;
    this._IntervalID = null;
    this._nextItemDelegate = Function.createDelegate(this, this._showNext);
    this._animationDelegate = Function.createDelegate(this, this._animationComplete);
    this._Overlay1 = null;
    this._Overlay2 = null;
    this._ActiveOverlayIndex = 2;
    this._ActiveOverlay = null;
    this._CurrentItem = 0;
    this._IsInit = true;
    this._AnimationType = 0;
    this._Easing = "linear"; // "swing"
    this._Navigation = false;
    this._NavigationAlways = false;
    this._Css = "";
    this._CssDsc = "";
    this._CssNxt = "";
    this._CssPrv = "";
    this._CssPlay = "";
    this._CssPause = "";
    this._Nav = { Nav: null, Desc: null, Next: null, Prev: null };
    this._mouseMoveHandler = Function.createDelegate(this, this._mouseMove);
    this._itemNextHandler = Function.createDelegate(this, this._itemNext);
    this._itemPrevHandler = Function.createDelegate(this, this._itemPrev);
    this._pauseHandler = Function.createDelegate(this, this._pause);
    this._isInAnimation = false;
    this._event = null;
    this._isPaused = false;
    this._NavigationOpacity = 1;
    this._isHover = false;
    this._multyLinks = new Array();
    this._el = null;
};

Framework.Controls.JSContentRotator.prototype = {
    get_Playlist: function() {
        return this._Playlist;
    },
    set_Playlist: function(value) {
        this._Playlist = eval(value);
    },
    get_Width: function() {
        return this._Width;
    },
    set_Width: function(value) {
        this._Width = value;
    },
    get_Height: function() {
        return this._Height;
    },
    set_Height: function(value) {
        this._Height = value;
    },
    get_Time: function() {
        return this._Time;
    },
    set_Time: function(value) {
        this._Time = value;
    },
    get_PlaybackMode: function() {
        return this._PlaybackMode;
    },
    set_PlaybackMode: function(value) {
        this._PlaybackMode = value;
    },
    get_AnimationType: function() {
        return this._AnimationType;
    },
    set_AnimationType: function(value) {
        this._AnimationType = value;
    },
    get_Easing: function() {
        return this._Easing;
    },
    set_Easing: function(value) {
        this._Easing = value;
    },
    get_Navigation: function() {
        return this._Navigation;
    },
    set_Navigation: function(value) {
        this._Navigation = value;
    },
    get_NavigationAlways: function() {
        return this._NavigationAlways;
    },
    set_NavigationAlways: function(value) {
        this._NavigationAlways = value;
    },
    get_Css: function() {
        return this._Css;
    },
    set_Css: function(value) {
        this._Css = value;
    },
    get_CssDsc: function() {
        return this._CssDsc;
    },
    set_CssDsc: function(value) {
        this._CssDsc = value;
    },
    get_CssNxt: function() {
        return this._CssNxt;
    },
    set_CssNxt: function(value) {
        this._CssNxt = value;
    },
    get_CssPrv: function() {
        return this._CssPrv;
    },
    set_CssPrv: function(value) {
        this._CssPrv = value;
    },
    get_CssPlay: function() {
        return this._CssPlay;
    },
    set_CssPlay: function(value) {
        this._CssPlay = value;
    },
    get_CssPause: function() {
        return this._CssPause;
    },
    set_CssPause: function(value) {
        this._CssPause = value;
    },

    initialize: function() {
        Framework.Controls.JSContentRotator.callBaseMethod(this, "initialize");
        this._el = this.get_element();
        if (this._Playlist && this._Playlist != "" && this._Playlist.length > 0) {
            this._createNavigation();
            this._create();
        }
    },
    dispose: function() {
        if (this._Navigation) {
            if (!this._NavigationAlways) {
                $removeHandler(document, "mousemove", this._mouseMoveHandler);
            }
            $removeHandler(this._Nav.Next, "click", this._itemNextHandler);
            $removeHandler(this._Nav.Prev, "click", this._itemPrevHandler);
            $removeHandler(this._Nav.Play, "click", this._pauseHandler);
        }

        Framework.Controls.JSContentRotator.callBaseMethod(this, "dispose");
        if (this._IntervalID) clearTimeout(this._IntervalID);
        this._IntervalID = null;
    },
    _create: function() {
        // Create overlays
        this._Overlay1 = document.createElement("div");
        this._Overlay1.style.width = this._Width;
        this._Overlay1.style.height = this._Height;
        this._Overlay1.style.overflow = "hidden";
        this._Overlay1.style.zIndex = 1;
        this._Overlay1.style.position = "absolute";
        $(this._Overlay1).fadeTo(0, 0);
        this.get_element().insertBefore(this._Overlay1, this.get_element().firstChild);

        // Create overlays
        this._Overlay2 = document.createElement("div");
        this._Overlay2.style.width = this._Width;
        this._Overlay2.style.height = this._Height;
        this._Overlay2.style.overflow = "hidden";
        this._Overlay2.style.zIndex = 2;
        this._Overlay2.style.position = "absolute";
        $(this._Overlay2).fadeTo(0, 0);
        this.get_element().insertBefore(this._Overlay2, this.get_element().firstChild);

        // Get first item
        var item = this._getItem(0);
        this._isInAnimation = false;
        if (this._IntervalID) clearTimeout(this._IntervalID);

        // Set next item
        if (this._multyLinks.length > 1)
            this._IntervalID = setTimeout(this._nextItemDelegate, item.Time * 1000);

        // Create multy links
        this._createMultyLinks(item);
    },
    _createNavigation: function() {
        if (!this._Navigation || this._Playlist.length < 2) return;

        // Create nav container
        var nav = document.createElement("DIV");
        nav.className = this._Css;
        nav.style.position = "absolute";
        nav.style.zIndex = 20;
        this._Nav.Nav = nav;

        var desc = document.createElement("DIV");
        desc.className = this._CssDsc;
        nav.appendChild(desc);
        this._Nav.Desc = desc;

        var prev = document.createElement("DIV");
        prev.className = this._CssNxt;
        nav.appendChild(prev);
        this._Nav.Prev = prev;
        $addHandler(this._Nav.Prev, "click", this._itemPrevHandler);

        var play = document.createElement("DIV");
        play.className = this._CssPause;
        nav.appendChild(play);
        this._Nav.Play = play;
        $addHandler(this._Nav.Play, "click", this._pauseHandler);

        var next = document.createElement("DIV");
        next.className = this._CssPrv;
        nav.appendChild(next);
        this._Nav.Next = next;
        $addHandler(this._Nav.Next, "click", this._itemNextHandler);

        // Process navigation
        if (!this._NavigationAlways) {
            $(this._Nav.Nav).fadeTo(0, 0);
            $addHandler(document, "mousemove", this._mouseMoveHandler);
        } else {
            $(this._Nav.Nav).fadeTo(0, this._NavigationOpacity);
        }
        this.get_element().appendChild(nav);
        this._processNavigation();
    },
    _processNavigation: function() {
        if (!this._Navigation || this._Playlist.length < 2) return;
        var item = this._getItem(this._CurrentItem);
        this._Nav.Desc.innerHTML = "<span>" + (this._CurrentItem + 1) + " / " + this._Playlist.length + "</span><span> - " + item.Title + "</span>";
    },
    _mouseMove: function(e) {
        if (Utils.isHit(e, this.get_element()) && !this._isHover) {
            $(this._Nav.Nav).fadeTo(400, this._NavigationOpacity);
            this._isHover = true;
        }
        if (!Utils.isHit(e, this.get_element()) && this._isHover) {
            $(this._Nav.Nav).fadeTo(400, 0);
            this._isHover = false;
        }
    },
    _itemNext: function() {
        if (this._isInAnimation) return;
        this._showNext();
    },
    _itemPrev: function() {
        if (this._isInAnimation) return;
        this._showPrev();
    },
    _showNext: function() {
        // Go to next item
        this._CurrentItem++;

        // Loop
        if (this._CurrentItem >= this._Playlist.length) this._CurrentItem = 0;
        this._processNavigation();
        this._showItem();
    },
    _showPrev: function() {
        // Go to next item
        this._CurrentItem--;

        // Loop
        if (this._CurrentItem < 0) this._CurrentItem = this._Playlist.length - 1;
        this._processNavigation();
        this._showItem();
    },
    _pause: function() {
        if (this._isPaused) {
            this._isPaused = false;
            this._showNext();
            this._Nav.Play.className = this._CssPause;
        } else {
            clearInterval(this._IntervalID);
            this._Nav.Play.className = this._CssPlay;
            this._isPaused = true;
        }
    },
    _showItem: function() {
        // Get current item
        var item = this._getItem(this._CurrentItem);

        // Get content for next item
        var content = this._getItemContent(this._CurrentItem);

        // Set active index
        if (this._ActiveOverlayIndex == 2) {
            this._ActiveOverlay = this._Overlay1;
            this._Overlay1.style.zIndex = 2;
            this._Overlay2.style.zIndex = 1;
        } else {
            this._ActiveOverlay = this._Overlay2;
            this._Overlay1.style.zIndex = 1;
            this._Overlay2.style.zIndex = 2;
        }

        // Animate content
        this._isInAnimation = true;
        this._animate(this._ActiveOverlay, content);

        // Set timeout to next item
        if (this._IntervalID) clearTimeout(this._IntervalID);
        if (!this._isPaused) this._IntervalID = setTimeout(this._nextItemDelegate, item.Time * 1000);
    },
    _createMultyLinks: function(item) {
        // Clear all links
        if (this._multyLinks.length > 0) {
            for (var i = 0; i < this._multyLinks.length; i++) {
                this._multyLinks[i].parentNode.removeChild(this._multyLinks[i]);
            }
            this._multyLinks = new Array();
        }

        // Check if current item has any content links
        if (!item.Links) {
            return;
        }

        // Create links
        for (var i = 0; i < item.Links.length; i++) {
            var link = document.createElement("a");
            link.style.display = "block";
            link.style.position = "absolute";
            link.style.zIndex = 10;
            link.style.width = item.Links[i].W + "px";
            link.style.height = item.Links[i].H + "px";
            link.style.left = item.Links[i].X + "px";
            link.style.top = item.Links[i].Y + "px";
            link.setAttribute("title", item.Links[i].Title);
            link.setAttribute("href", item.Links[i].Link);
            link.style.background = "red";
            link.style.opacity = 0;
            link.style.filter = 'alpha(opacity=0)';

            this._el.appendChild(link);
            this._multyLinks.push(link);
        }
    },
    _animate: function(overlay, content) {
        // Set content and start transition
        overlay.appendChild(content);
        overlay.style.zIndex = 3;

        // Get current item
        var item = this._getItem(this._CurrentItem);
        var animation = item.AnimationType;

        // Flash content is special :)
        if (item.Type == 1) {
            // Flash settings
            var params = {};
            params["play"] = "true";
            params["menu"] = "false";
            params["loop"] = "true";
            params["allowfullscreen"] = "false";
            params["wmode"] = "opaque";

            // Create flash
            swfobject.embedSWF(item.URL, item.ClientID, this._Width, this._Height, "7.0.0", false, {}, params, {});
        }

        // Random animation
        if (animation == 0) {
            animation = Math.ceil(5 * Math.random());
        }

        // Animate
        if (animation == 1) {
            $(overlay).fadeTo(1000, 1.0, this._animationDelegate);
        }
        else if (animation == 2) {
            var bounds = Sys.UI.DomElement.getBounds(overlay);
            if (bounds.height == 0) bounds.height = this._Height;
            $(overlay).css({ 'top': '-' + bounds.height + 'px' });
            $(overlay).fadeTo(0, 1);
            $(overlay).animate({ top: "0px" }, 1000, this._Easing, this._animationDelegate);
        }
        else if (animation == 3) {
            var bounds = Sys.UI.DomElement.getBounds(overlay);
            if (bounds.height == 0) bounds.height = this._Height;
            $(overlay).css({ 'top': (bounds.height * 2) + 'px' });
            $(overlay).fadeTo(0, 1);
            $(overlay).animate({ top: '0px' }, 1000, this._Easing, this._animationDelegate);
        }
        else if (animation == 4) {
            var bounds = Sys.UI.DomElement.getBounds(overlay);
            if (bounds.width == 0) bounds.width = this._Width;
            $(overlay).css({ 'left': '-' + bounds.width + 'px' });
            $(overlay).fadeTo(0, 1);
            $(overlay).animate({ left: '0px' }, 1000, this._Easing, this._animationDelegate);
        }
        else if (animation == 5) {
            var bounds = Sys.UI.DomElement.getBounds(overlay);
            if (bounds.width == 0) bounds.width = this._Width;
            $(overlay).css({ 'left': (bounds.width * 2) + 'px' });
            $(overlay).fadeTo(0, 1);
            $(overlay).animate({ left: '0px' }, 1000, this._Easing, this._animationDelegate);
        }
    },
    _animationComplete: function() {
        // Set IN-active index
        if (this._ActiveOverlayIndex == 2) {
            this._ActiveOverlay = this._Overlay2;
            this._ActiveOverlayIndex = 1;
        } else {
            this._ActiveOverlay = this._Overlay1
            this._ActiveOverlayIndex = 2;
        }

        // Get current item
        var item = this._getItem(this._CurrentItem);

        // Create multy links
        this._createMultyLinks(item);

        // Remove firsst time server generated content
        if (this._IsInit) {
            var first = $get(this.get_element().id + "First");
            first.parentNode.removeChild(first);
            this._IsInit = false;
            this._isInAnimation = false;
            return;
        }

        // Hide lower content
        this._ActiveOverlay.innerHTML = "";
        $(this._ActiveOverlay).fadeTo(0, 0);
        this._isInAnimation = false;
    },
    _getItem: function(index) {
        if (this._Playlist && index < this._Playlist.length) {
            return this._Playlist[index];
        }
        return null;
    },
    _getItemContent: function(index) {
        var item = this._getItem(index);
        if (!item) return;

        // Create image
        if (item.Type == 0 && item.LinkType != 2) {
            var img = document.createElement("img");
            img.src = item.URL;

            var link = document.createElement("a");
            link.href = item.Link;
            link.setAttribute("title", item.Title);
            if (item.LinkType == 0) link.setAttribute("target", "_blank");
            link.style.width = this._Width + "px";
            link.style.height = this._Height + "px";
            link.style.display = "block";
            link.appendChild(img);
            return link;
        }

        // Create flash
        if (item.Type == 1 && item.LinkType != 2) {
            var flash = document.createElement("div");
            flash.id = item.ClientID;
            flash.style.width = this._Width + "px";
            flash.style.height = this._Height + "px";

            var flashLink = document.createElement("a");
            flashLink.href = "http://www.macromedia.com/go/getflashplayer";
            flashLink.innerHTML = "Get flash player here";
            flash.appendChild(flashLink);

            return flash;
        }

        // Create html content
        var link = document.createElement("a");
        link.innerHTML = item.Content;
        link.setAttribute("title", item.Title);
        if (item.LinkType == 0) link.setAttribute("target", "_blank");

        if (item.Link != "") {
            link.href = item.Link;
        }

        return link;
    }
};

Framework.Controls.JSContentRotator.registerClass("Framework.Controls.JSContentRotator", Sys.UI.Control);
//if (typeof (Sys) !== "undefined") Sys.Application.notifyScriptLoaded(); 
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();