﻿/// <reference name="MicrosoftAjax.js"/>

Type.registerNamespace("Json");

Json.PauseScrolling = function(element) {
    Json.PauseScrolling.initializeBase(this, [element]);
    this.content = null;
    this.tickerid = null;
    this.delay = 3000;
    this.mouseoverBol = null;
    this.hiddendivpointer = null;
    this.CssClass = "";
}

Json.PauseScrolling.prototype = {
    initialize: function() {
        Json.PauseScrolling.callBaseMethod(this, 'initialize');
        requestDataHandlerDelegate = Function.createDelegate(this, this.OnRequestDataComplete);
        AtlasArena.Web.Services.Product.GetSupplierPricelistDiscounts(requestDataHandlerDelegate);

    },


    dispose: function() {
        //Add custom dispose actions here
        Json.PauseScrolling.callBaseMethod(this, 'dispose');
    },

    animateup: function() {
        var scrollerinstance = this
        if (parseInt(this.hiddendiv.style.top) > (this.visibledivtop + 5)) {
            this.visiblediv.style.top = parseInt(this.visiblediv.style.top) - 5 + "px"
            this.hiddendiv.style.top = parseInt(this.hiddendiv.style.top) - 5 + "px"
            setTimeout(function() { scrollerinstance.animateup() }, 50)
        }
        else {
            this.getinline(this.hiddendiv, this.visiblediv)
            this.swapdivs()
            setTimeout(function() { scrollerinstance.setmessage() }, this.delay)
        }
    },

    OnRequestDataComplete: function(result) {
        //debugger;
        this.content = new Array();
        for (i = 0; i < result.length; i++) {
            if (result[i]["_isDiscountPercentage"] == true)
                this.content[i] = "<span style='font-weight:bold;'>Order over $" + result[i]["_orderTotalAmount"] + " and receive a " + result[i]["_discountAmount"] + "% discount.</span>";
            else
                this.content[i] = "<span style='font-weight:bold;'>Order over $" + result[i]["_orderTotalAmount"] + " and receive a $" + result[i]["_discountAmount"] + " discount.</span>";
        }

        if (this.content.length > 0) {
            this.dataBind();
        }


    },

    dataBind: function() {

        this.createPauseScrolling();

    },
    createPauseScrolling: function() {
        this.mouseoverBol = 0 //Boolean to indicate whether mouse is currently over scroller (and pause it if it is)
        this.hiddendivpointer = 1 //index of message array for hidden div

        var div = document.createElement("div");
        div.id = this.tickerid;
        div.setAttribute("class", this.CssClass);
        div.style.position = "relative";
        div.style.overflow = "hidden";

        var innerDiv1 = document.createElement("div");
        innerDiv1.id = this.tickerid + "1";
        innerDiv1.setAttribute("class", this.CssClass);
        innerDiv1.style.position = "absolute";
        innerDiv1.style.width = "100%";

        var innerDiv2 = document.createElement("div");
        innerDiv2.id = this.tickerid + "2";
        innerDiv2.setAttribute("class", this.CssClass);
        innerDiv2.style.position = "absolute";
        innerDiv2.style.width = "100%";
        innerDiv2.style.visibility = "hidden";


        innerDiv1.innerHTML = this.content[0];
        if (this.content.length > 1)
            innerDiv2.innerHTML = this.content[1];
        else
            innerDiv2.innerHTML = this.content[0];
        div.appendChild(innerDiv1);
        div.appendChild(innerDiv2);
        var element = this.get_element();
        element.appendChild(div);

        this.tickerdiv = document.getElementById(this.tickerid)
        this.visiblediv = document.getElementById(this.tickerid + "1")
        this.hiddendiv = document.getElementById(this.tickerid + "2")
        this.visibledivtop = parseInt(this.getCSSpadding(this.tickerdiv))
        //set width of inner DIVs to outer DIV's width minus padding (padding assumed to be top padding x 2)
        this.visiblediv.style.width = this.hiddendiv.style.width = this.tickerdiv.offsetWidth - (this.visibledivtop * 2) + "px";
        this.getinline(this.visiblediv, this.hiddendiv)
        this.hiddendiv.style.visibility = "visible"
        var scrollerinstance = this
        document.getElementById(this.tickerid).onmouseover = function() { scrollerinstance.mouseoverBol = 1 }
        document.getElementById(this.tickerid).onmouseout = function() { scrollerinstance.mouseoverBol = 0 }
        if (window.attachEvent) //Clean up loose references in IE
            window.attachEvent("onunload", function() { scrollerinstance.tickerdiv.onmouseover = scrollerinstance.tickerdiv.onmouseout = null })
        setTimeout(function() { scrollerinstance.animateup() }, this.delay)
    },
    swapdivs: function() {
        var tempcontainer = this.visiblediv
        this.visiblediv = this.hiddendiv
        this.hiddendiv = tempcontainer
    },

    getinline: function(div1, div2) {
        //debugger;
        div1.style.top = this.visibledivtop + "px"
        div2.style.top = Math.max(div1.parentNode.offsetHeight, div1.offsetHeight) + "px"
    },

    // -------------------------------------------------------------------
    // setmessage()- Populate the hidden div with the next message before it's visible
    // -------------------------------------------------------------------

    setmessage: function() {
        var scrollerinstance = this
        if (this.mouseoverBol == 1) //if mouse is currently over scoller, do nothing (pause it)
            setTimeout(function() { scrollerinstance.setmessage() }, 100)
        else {
            var i = this.hiddendivpointer
            var ceiling = this.content.length
            this.hiddendivpointer = (i + 1 > ceiling - 1) ? 0 : i + 1
            this.hiddendiv.innerHTML = this.content[this.hiddendivpointer]
            this.animateup()
        }
    },

    // tickerid property
    get_tickerid: function() {
        return this.tickerid;
    },
    set_tickerid: function(value) {
        this.tickerid = value;
    },
    //CssClass property
    get_CssClass: function() {
        return this.CssClass;
    },
    set_CssClass: function(value) {
        this.CssClass = value;
    },

    //delay property
    get_delay: function() {
        return this.delay;
    },
    set_delay: function(value) {
        this.delay = value;
    },
    //delay content
    get_content: function() {
        return this.content;
    },
    set_content: function(value) {
        this.content = value;
    },
    getCSSpadding: function(tickerobj) { //get CSS padding value, if any
        if (tickerobj.currentStyle)
            return tickerobj.currentStyle["paddingTop"]
        else if (window.getComputedStyle) //if DOM2
            return window.getComputedStyle(tickerobj, "").getPropertyValue("padding-top")
        else
            return 0
    }


}
Json.PauseScrolling.registerClass('Json.PauseScrolling', Sys.UI.Control);

if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();

