﻿/// <reference name="MicrosoftAjax.js" />

Type.registerNamespace("PopUp");

PopUp.ScrollModelli = function(DivMainID, DivScrollID, MinHeight, MenuItemSel, MenuID) {
//debugger
    this._SecsForOneModelScroll = 0.03;
    this._MaxSecsForOneModelScroll = 0.3;
    this._PixelForSingleElement = 18;
    this._ElementsForMouseScroll = 10;
    this._DivScrollID = DivScrollID;
    this._DivMainID = DivMainID;
    this._MinHeight = MinHeight;
    this._DivScroll = null;
    this._DivMain = $get(DivMainID);
    this._MenuItemSelected = MenuItemSel;
    this._CurrentHeight = MinHeight;
    this._MenuID = MenuID;
    this._CanSetScrollLength = true;
    this.SetAutoHeight();

    this.SetHeightInnerUL();

    onmousewheel(this._DivMain, this, this._PixelForSingleElement * this._ElementsForMouseScroll);
}

PopUp.ScrollModelli.prototype = {
    get_DivScrollID: function() {
        return this._divScrollID;
    },
    get_DivMainID: function() {
        return this._divScrollID;
    },
    get_MinHeight: function() {
        return this._MinHeight;
    },
    get_DivScroll: function() {
        if (this._DivScrolltype == null || typeof (this._DivScroll) == 'undefined')
            this._DivScroll = $get(this._DivScrollID);
        return this._DivScroll;
    },
    get_DivMain: function() {
        if (this._DivMain == null || typeof (this._DivMain) == 'undefined')
            this._DivMain = $get(this._DivMainID);
        return this._DivMain;
    },
    get_DivMemu: function() {
        return $get(this._MenuID);
    },
    get_MenuItemSelected: function() {
        return this._MenuItemSelected;
    },
    get_CanSetScrollLength: function() {
        return this._CanSetScrollLength;
    },
    dispose: function() {
        this._DivScroll = null;
        this._DivMain = null;
    },

    CanDoUp: function() {
        return this.get_DivScroll().offsetTop < 0;
    },

    CanDoDown: function() {
        var h = Sys.UI.DomElement.getBounds(this.get_DivMemu()).height;
        if (isNaN(h)) {
            Sys.Debug.assert(!isNaN(h), "Non riesco a beccare l'altezza del div", true);
            return true;
        }
        h = h + this.get_DivScroll().offsetTop - this._CurrentHeight;
        return h > 0;
    },
    DoScroll: function(delta) {
        var anim = this.SetStepOK(delta);
        if (anim == null) return;
        this._CanSetScrollLength = false;
        if (anim.get_OnClickBehavior().isPlaying) {
            anim.get_OnClickBehavior().stop();
        }
        anim.get_OnClickBehavior().play();
        this._CanSetScrollLength = true;

    },
    get_StepValido: function(stepval) {
        if (stepval < 0) {
            var h = Sys.UI.DomElement.getBounds(this.get_DivMemu()).height;
            if (isNaN(h)) {
                Sys.Debug.assert(!isNaN(h), "Non riesco a beccare l'altezza del div", true);
                return true;
            }
            h = h + this.get_DivScroll().offsetTop - this._CurrentHeight + stepval;
            if (h < 0)
                return stepval - h;
        }
        else if (stepval > 0) {
            var h = this.get_DivScroll().offsetTop;
            h = h + stepval;
            if (h > 0) return stepval - h;
        }
        return stepval;
    },
    SetStepOK: function(stepval) {
        stepval = this.get_StepValido(stepval);
        if (stepval == 0) return null;
        var anim, animbh;
        if (stepval > 0)
            anim = $find("animationModelliUp");
        else anim = $find("animationModelliDown");
        animbh = anim.get_OnClickBehavior().get_animation().get_animations()[1]
        var numsec = (stepval / this._PixelForSingleElement) * this._SecsForOneModelScroll;
        if (numsec < 0) numsec = -numsec;
        if (numsec > this.Max_SecsForOneModelScroll) numsec = this._MaxSecsForOneModelScroll;
        animbh.set_vertical(stepval);
        animbh.set_duration(numsec);

        return anim;

    },
    SetAutoHeight: function() {
        var h = document.body.clientHeight;
        if (isNaN(h))
            return;

        h = h - Sys.UI.DomElement.getBounds(this.get_DivMain()).y;
        h = h - 50;
        if (h <= this.MinHeight) h = this.get_MinHeight;
        this._CurrentHeight = h;
        var v = this.get_DivMain();
        v.style.height = this._CurrentHeight + "px";
        this._CurrentHeight = h;

    },
    SetHeightInnerUL: function() {
        var elem = $get(this._MenuID);
        if (elem == null || typeof (elem) == 'undefined') return;

        var figli1 = elem.getElementsByTagName("LI");
        if (figli1.length <= 0) return;
        var ultimo1 = figli1[figli1.length - 1];

        var figli2 = ultimo1.getElementsByTagName("LI");
        if (figli2.length > 0)
            ultimo1 = figli2[figli2.length - 1];

        var b = Sys.UI.DomElement.getBounds(elem);
        var b2 = Sys.UI.DomElement.getBounds(ultimo1);

        var h = b2.y - b.y;
        elem.style.height = h + "px";

        var elem = $get("ctl00_pandown");
        if (elem!=null)
            if (this.CanDoDown()) 
                elem.style.display="";
            else elem.style.display="none";
        var elem = $get("ctl00_panup");
        if (elem!=null)
            if (this.CanDoUp()) 
                elem.style.display="";
            else elem.style.display="none";

    },
    MakeVisibleSelectedItem: function() {
        var h = this._MenuItemSelected.offsetTop;
        var hAll = Sys.UI.DomElement.getBounds($get(this._MenuID)).height;
        if (h + this._CurrentHeight > hAll)
            h = hAll - this._CurrentHeight;
        this.get_DivScroll().style.top = -h;
    },
    OnSelectMenuItem: function(objname) {
        if (objname == null || typeof (objname) == "undefined") return;
        var obja;
        if (this._MenuItemSelected != null) {
            obja = this.get_MenuItemSelected().getElementsByTagName("A");
            if (obja.length > 0) {
                obja = obja[0];
                Sys.UI.DomElement.removeCssClass(obja, "active");
            }
            obja = this.get_MenuItemSelected().getElementsByTagName("UL");
            if (obja.length > 0) {
                obja = obja[0];
                obja.style.display = "none";
            }
            if (this.get_MenuItemSelected().id == objname) {
                this._MenuItemSelected = null;
                return;
            }
        }

        var objLI = $get(objname);
        if (objLI == null) return;

        obja = objLI.getElementsByTagName("A");
        if (obja.length > 0) {
            obja = obja[0];
            Sys.UI.DomElement.addCssClass(obja, "active");
        }

        obja = objLI.getElementsByTagName("UL");
        if (obja.length > 0) {
            obja[0].style.display = "";
            this._MenuItemSelected = objLI;
            this.SetHeightInnerUL();
            this.MakeVisibleSelectedItem();
        }
        else {
            this.LoadModelli(objLI);
            this._MenuItemSelected = objLI;
        }
    },
    LoadModelli: function(objLI) {
        var pos = objLI.id.indexOf("_L_");

        if (pos <= 0) return false;

        var cmarca = objLI.id.substring(pos + 3, objLI.id.length);

        var imgel = document.createElement("img");
        imgel.src = "/images/ajax-loader.gif";
        objLI.appendChild(imgel);
        PopUp.Malossi.Store.WS.WSAJAX_MalossiStore.GetModelli(cmarca, objLI.id, this._clback_okModelli, this._clback_FailModelli, this);
    },
    resetPosition: function() {
        this.get_DivScroll().style.top = 0;
    },
    SearchBrand: function(what, controlID, context) {
        PopUp.Malossi.Store.WS.WSAJAX_MalossiStore.SearchBrand(what, controlID, context.get_clback_okSearch(), context.get_clback_FailModelli, context);
    },
    get_clback_okSearch: function() {
        return this._clback_okSearch;
    },
    get_clback_FailModelli: function() {
        return this._clback_FailModelli;
    },
    _clback_okSearch: function(result, context) {
        var objLI = context.get_DivScroll();
        if (objLI == null) return;

        objLI.innerHTML = result.HTML;
        context.get_DivScroll().style.top = 0;
        context.SetHeightInnerUL();
    },
    _clback_okModelli: function(result, context) {
        var objLI = $get(result.OtherData);
        if (objLI == null) return;

        var o = objLI.getElementsByTagName("UL");
        for (var i = 0; i < o.length; i++)
            objLI.removeChild(o[i]);
        var o = objLI.getElementsByTagName("IMG");
        for (var i = 0; i < o.length; i++)
            objLI.removeChild(o[i]);
        objLI.innerHTML += result.HTML;
        if (context != null) {
            context.SetHeightInnerUL();
            context.MakeVisibleSelectedItem();
        }

    },
    _clback_FailModelli: function(result, context) {
        debugger
        alert(result.HTML);
    }

}

PopUp.ScrollModelli.registerClass("PopUp.ScrollModelli", null, Sys.IDisposable);


function onmousewheel(element, objScroll, MoltiplierDelta) {
    function __onwheel(event) {
        var delta = 0;
        if (event.wheelDelta) {
            delta = event.wheelDelta / 120;
            if (window.opera)
                delta = -delta;
        }
        else if (event.detail)
            delta = -event.detail / 3;
        if (delta) {
            Sys.Debug.trace("Delta = " + delta.toString());
            objScroll.DoScroll(delta * MoltiplierDelta);
        }
        if (event.preventDefault)
            event.preventDefault();
        event.returnValue = false;
        return false;
    };
    if (element.addEventListener && !window.opera) {
        element.addEventListener("DOMMouseScroll", __onwheel, false);
        element.addEventListener("mousewheel", __onwheel, false);
    }
    else
        element.onmousewheel = (function (base) {
            return function (evt) {
                if (!evt) evt = window.event;
                if (base) base.call(element, evt);
                return __onwheel(evt);
            }
        })(element.onmousewheel);
};



var Const_TimeTimer = 500; 
var sb_Timer = null;

function SearchBrandPressKey() {

    if (sb_Timer != null)
        clearTimeout(sb_Timer);
    sb_Timer = setTimeout(SearchBrand, Const_TimeTimer);
}

function SearchBrand() {
    var s = new String($get("ctl00_txt_sb").value);
    if (s.trim() == "") {
        __doPostBack('ctl00$left_menu', '');
        return;
    }
    popScroll.SearchBrand(s, "left_menu", popScroll);

}

if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
