/**
* This is the package where all the moofx extensions resides.
* All moofx original effects and third developers' extenssions are defined here.
* For further information, take a look to: http://mad4milk.net
* This code was originaly released under MIT-style LICENSE. (Sunday, February 05, 2006)
* Versión 1.4.2:
* Se eliminan efectos no empleados para incrementar el rendimiento de la librería.
* Those extensions were released under GPL v2.<br/>
* @author Valerio Proietti (1.2)
* @author Marc Bria Ramírez (1.3)
* @version 1.4.5
* Paso de RemeberHeight a Height.
***/

fx.Scroll = Class.create();
fx.Scroll.prototype = Object.extend(new fx.Base(), {
    initialize: function(options) {
        this.setOptions(options);
    },

    scrollTo: function(el){
        var dest = Position.cumulativeOffset($(el))[1];
        var client = window.innerHeight || document.documentElement.clientHeight;
        var full = document.documentElement.scrollHeight;
        var top = window.pageYOffset || document.body.scrollTop || document.documentElement.scrollTop;
        if (dest+client > full) this.custom(top, dest - client + (full-dest));
        else this.custom(top, dest);
    },

    increase: function(){
        window.scrollTo(0, this.now);
    }
});

fx.Alert = Class.create();
fx.Alert.prototype = Object.extend(new fx.Base(), {

    setOptions: function(pOptions) {
        this.options = {
            opacity: true,
            height: false,
            width: false,
            offsetX: 10,
            offsetY: 20,
            boxWidth: "lite",
            alertTitle: "",
            duration: 400,
            waitTime: 9200,
            onClick: true,
            onMouseOver: false,
            onMouseOut: false
        }
        Object.extend(this.options, pOptions || {});
    },

    initialize: function(options) {
        this.setOptions(options);
        el=this.buildAlert("No content yet.");
        var alertFx=this;
        this.options.onComplete = function(){
            switch (el.fxStatus) {
                case 'shown':
                case 'toShow':
                    el.style.visibility = "visible";
                    el.fxStatus = "shown";
                    if (alertFx.options.waitTime != "none") {
                        setTimeout(function(){alertFx.hideAlert();}.bind(this), alertFx.options.waitTime);
                    }
                    break;
                case 'hidden':
                case 'toHide':
                default:
                    el.style.visibility = "hidden";
                    el.fxStatus = "hidden";
                    break;
            }
            if (document.all) { //Only for IE.
                var iframeShim = $("ieComboFixTooltip");
                if (el.style.visibility != "visible") iframeShim.style.display = "none";
                else iframeShim.style.display = "inline";
            }
        }
        el.fx = new fx.Combo(el, this.options);
        el.style.visibility = "hidden";
        el.fxStatus = "hidden";
        el.fx.hide();

        //Doing M$ work. Creating the iframe that will hide combo boxes.
        //Following VB suggestion, only one iframe is created.
       if (document.all) //only IE
        {
            new Insertion.After('alertContainer42', '<IFRAME src="/jsgen/moo/blank.html" id="ieComboFixTooltip" class="ieComboFixTooltip" />')
        }
        //Cambio 01: 07/06/2006: Versión 1.4.4
        preloadImages("/jsgen/imgs/moo/top-a.jpg;/jsgen/imgs/moo/top-b.jpg;/jsgen/imgs/moo/top-c.jpg;/jsgen/imgs/moo/topArrow-a.gif;/jsgen/imgs/moo/topArrow-b.gif;/jsgen/imgs/moo/topArrow-c.gif;/jsgen/imgs/moo/center-a.jpg;/jsgen/imgs/moo/center-b.jpg;/jsgen/imgs/moo/center-c.jpg;/jsgen/imgs/moo/bottom-a.jpg;/jsgen/imgs/moo/bottom-b.jpg;/jsgen/imgs/moo/bottom-c.jpg");
    },

    buildAlert: function(alertString, pPosition) {
        var el=$('alertContainer42');
        var cssStyleId="tooltip-a";
        var cssStyleTop="superior";
        var cssStyleBottom="inferior";
        var tWidth=193;
        switch (pPosition) {
            case "top":
                cssStyleTop="superior";
                cssStyleBottom="inferiorArrow";
                break;
            case "bottom":
                cssStyleTop="superiorArrow";
                cssStyleBottom="inferior";
                break;
            default:
                cssStyleTop="superior";
                cssStyleBottom="inferior";
                break;
        }

        switch (this.options.boxWidth) {
            case "big":
                cssStyleWidth="tooltip-c";
                tWidth=477;
                break;
            case "middle":
                cssStyleWidth="tooltip-b";
                tWidth=293;
                break;
            case "lite":
            default:
                cssStyleWidth="tooltip-a";
                tWidth=193;
                break;
        }
        //el.innerHTML="<div id='frameTopLeft42' class='"+ cssStyleTop+"'><div id='frameBottomRight42' class='"+ cssStyleBottom+"'><div id='alertContent42'>"+alertString+"</div></div></div>";

        tBox = '<div id="alertContent42" class="'+cssStyleWidth+'" style="width: '+ tWidth.toString() +'px;">'+
               ' <div id="'+cssStyleTop+'"></div>'+
               ' <div id="center">'+
               '  <div class="textos">';
        if (this.options.alertTitle) {
              tBox += '   <h4>'+this.options.alertTitle+'</h4>';
        }
        tBox +='   <p>'+alertString+'</p>'+
               '  </div>'+
               '  </div>'+
               ' <div id="'+cssStyleBottom+'"></div>'+
               '</div>';

        if (!el) {
            el=document.createElement("DIV");
            el.id="alertContainer42";
            el.className="ieOverComboTooltip";
            document.body.appendChild(el);
        }
        el.innerHTML=tBox;
        return el;
    },

    getConfirm: function(){
        return $('alertConatiner42').estado;
    },

    showConfirm: function(alertSring, tog, options){
        el=$('alertContainer42');
        confirmHTML='<br/><input INPUT type="button" value="SI" id="confirmYES" />'+
                    '&nbsp;<input INPUT type="button" value="NO" id="confirmNO" />';
        alertSring=alertSring+confirmHTML;
        this.showAlert(alertSring,tog,options);
        Event.observe('confirmYES', 'click', function(event){
            //$('alertContainer42').style.display="none";       //+hide iframe o usar llamada a métodos.
            $('alertContainer42').estado=true;
            confirmCallback(true);
        });
        Event.observe('confirmNO', 'click', function(event){
            //$('alertContainer42').style.display="none";
            $('alertContainer42').estado=false;
            confirmCallback(false);
        });
    },

    showAlert: function(alertSring, tog, options){
        el=$('alertContainer42');

        //Cambio 01: 14/04/2006: Versión 1.2
        //Control de parámetros: Permitimos llamada con (alertString, options).
        if (typeof(tog) == "object") {
            options=tog;
            tog=null;
        }
        if (options) this.setOptions(options);

        //Dealing with cross viewport positioning kaos.
        var posXY = new Array (300,300);
        var viewPortXY=getScrollOffset();
        var innerPageXY=getInnerSize();
        var arrowSize=7;
        var arrowPos="none";

        //Positioning in the right place.
        if ((tog) && (tog !=null)){                         //Close to the toggler.
            tog=$(tog);
            tog.focus();
            posXY = Position.cumulativeOffset(tog);

            //alert (posXY[1]-viewPortXY[1] + " > " + (innerPageXY[1] /2));
            if (posXY[1]-viewPortXY[1] > innerPageXY[1] /2) {
                this.buildAlert(alertSring,"top");
                posXY[0] = posXY[0] + this.options.offsetX;
                posXY[1] = posXY[1] - this.options.offsetY-Element.getHeight('alertContent42');
                arrowPos="bottom";
            }
            else {
                this.buildAlert(alertSring,"bottom");
                posXY[0] = posXY[0] - this.options.offsetX;
                posXY[1] = posXY[1] - this.options.offsetY+tog.offsetHeight;
                arrowPos="top";
            }
        }
        else {                              //Centered to the viewport.
            this.buildAlert(alertSring);
            var innerXY = new Array (
                (innerPageXY[0]/2)-($('alertContent42').offsetWidth/2),
                (innerPageXY[1]/2)-Element.getHeight('alertContent42'));
            posXY = new Array (innerXY[0]+viewPortXY[0],innerXY[1]+viewPortXY[1]);
            arrowPos="none";
        }

        el.fx.clearTimer();
        el.fxStatus = "toShow";
        el.style.left = posXY[0] + "px";
        el.style.top = posXY[1] + "px";

        if (el.style.visibility != "visible") {
            el.style.visibility = "visible";
            el.fxStatus = "toShow";
            //Doing M$ dirty job: Showing iframes to hide IE dropdown bug.
            if (document.all) //Only for IE.
            {
                var iframeShim = $("ieComboFixTooltip");
                iframeShim.style.display = "inline";
                posE=$("alertContent42");

                //Cambio 04: 13/04/2006: Versión 1.1
                switch (arrowPos) {
                    case "top":
                        iframeShim.style.left = (posXY[0]+20) + "px";
                        iframeShim.style.top = (posXY[1]+20+arrowSize) + "px";
                        iframeShim.style.width = (posE.offsetWidth) + "px";
                        iframeShim.style.height = (posE.offsetHeight-arrowSize*2)+ "px";
                        break;
                    case "bottom":
                        iframeShim.style.left = (posXY[0]+20) + "px";
                        iframeShim.style.top = (posXY[1]+20) + "px";
                        iframeShim.style.width = (posE.offsetWidth) + "px";
                        iframeShim.style.height = (posE.offsetHeight-arrowSize)+ "px";
                        break;
                    case "none":
                    default:
                        iframeShim.style.left = (parseFloat(posXY[0])+20) + "px";
                        iframeShim.style.top = (parseFloat(posXY[1])+20) + "px";
                        iframeShim.style.width = (posE.offsetWidth) + "px";
                        iframeShim.style.height = (posE.offsetHeight-arrowSize)+ "px";
                        break;
                }
            }
            //Cambio 02: 14/04/2006: Versión 1.2
            if (this.options.onClick) {
                el.onclick = function(){
                    this.hideAlert();
                }.bind(this);
            }
            el.fx.toggle();
        }
    },

    hideAlert: function(){
        el=$('alertContainer42');
        el.fxStatus = "toHide";
        if (el.style.visibility != "hidden") {
            el.style.visibility = "visible";
            el.fx.toggle();
            //if (document.all) $("ieComboFix").style.display = "none"; //Fixing IE bug.
        }
    },

    toggleAlert: function(alertString, tog, options){
        el=$('alertContainer42');
        if (el.style.visibility != "visible") {
            this.showAlert(alertString,tog);
        }
        else {
            this.hideAlert();
        }
    },

    toggleConfirm: function(alertString, tog, options){
        el=$('alertContainer42');
        if (el.style.visibility != "visible") {
            this.showConfirm(alertString,tog);
        }
        else {
            this.hideAlert();
        }
    }
});

fx.Combo = Class.create();
fx.Combo.prototype = {
    /**
    * Inicializador de las opciones del efecto.<br/>
    * @param {Array} options Permite parametrizar el efecto:<br/>
    * <ul>
    *     <li>"opacity":    True, activa la transición en fadein/fadeout de la opacidad.</li>
    *     <li>"height":     True, activa la transformación en altura.</li>
    *     <li>"width":      True, activa la transformación en anchura.</li>
    * </ul>
    */
    setOptions: function(options) {
        this.options = {
            opacity: true,
            height: true,
            width: false
        }
        Object.extend(this.options, options || {});
    },

    initialize: function(el, options) {
        this.el = $(el);
        this.setOptions(options);
        if (this.options.opacity) {
            this.el.o = new fx.Opacity(el, options);
            options.onComplete = null;
        }
        if (this.options.height) {
            this.el.h = new fx.Height(el, options);
            options.onComplete = null;
        }
        if (this.options.width) this.el.w = new fx.Width(el, options);
    },

    toggle: function() { this.checkExec('toggle'); },

    hide: function(){ this.checkExec('hide'); },

    clearTimer: function(){ this.checkExec('clearTimer'); },

    checkExec: function(func){
        if (this.el.o) this.el.o[func]();
        if (this.el.h) this.el.h[func]();
        if (this.el.w) this.el.w[func]();
    },

    resizeTo: function(hto, wto) {
        if (this.el.h && this.el.w) {
            this.h.custom(this.el.offsetHeight, this.el.offsetHeight + hto);
            this.w.custom(this.el.offsetWidth, this.el.offsetWidth + wto);
        }
    },

    customSize: function(hto, wto) {
        if (this.el.h && this.el.w) {
            this.h.custom(this.el.offsetHeight, hto);
            this.w.custom(this.el.offsetWidth, wto);
        }
    }
}


fx.Showtime = Class.create();
fx.Showtime.prototype = {
    setOptions: function(options) {
        this.options = {
            delay: 100,
            opacity: false
        }
        Object.extend(this.options, options || {});
    },

    initialize: function(togglers, elements, options) {
        this.elements = elements;
        this.setOptions(options);

        elements.each(function(el, i){
            el.fx = new fx.Height(el, options);
            //Bug 05: 06/07/2006: Versión 1.4.5 (los desplegables deben incluir class="hideMe" y el header correspondiente)
            el.style.display = "block";
        });

        togglers.each(function(tog, i){
            tog.onclick = function(){
                this.toggleItem(elements[i]);
            }.bind(this);
        }.bind(this));

    },

    hideAll: function(elements, options) {
        this.elements.each(function(el, i){
            el.fx = new fx.Height(el, options);
            el.fx.setHide();
        });
    },

    showAll: function(elements, options) {
        this.elements.each(function(el, i){
            el.fx = new fx.Height(el, options);
            el.fx.setShow();
        });
    },

    toggleAll: function(elements, options) {
        this.elements.each(function(el, i){
            el.fx = new fx.Height(el, options);
            el.fx.toggle();
        });
    },

    toggleItem: function(el){
        //el.fx.clearTimer();
        el.fx.toggle();
    }
}

var Remember = new Object();
Remember = function(){};
Remember.prototype = {
    initialize: function(el, options){
        this.el = $(el);
        this.days = 365;
        this.options = options;
        this.effect();
        var cookie = this.readCookie();
        if (cookie) {
            this.fx.now = cookie;
            this.fx.increase();
        }
    },

    setCookie: function(value) {
        var date = new Date();
        date.setTime(date.getTime()+(this.days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
        document.cookie = this.el+this.el.id+this.prefix+"="+value+expires+"; path=/";
    },

    readCookie: function() {
        var nameEQ = this.el+this.el.id+this.prefix + "=";
        var ca = document.cookie.split(';');
        for(var i=0;c=ca[i];i++) {
            while (c.charAt(0)==' ') c = c.substring(1,c.length);
            if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
        }
        return false;
    },

    custom: function(from, to){
        if (this.fx.now != to) {
            this.setCookie(to);
            this.fx.custom(from, to);
        }
    }
}

fx.RememberHeight = Class.create();
fx.RememberHeight.prototype = Object.extend(new Remember(), {
    effect: function(){
        this.fx = new fx.Height(this.el, this.options);
        this.prefix = 'height';
    },

    toggle: function(){
        /*if (this.el.offsetHeight == 0) this.setCookie(this.el.scrollHeight);
        else this.setCookie(0);*/
        this.fx.toggle();
    },

    resize: function(to){
        //this.setCookie(this.el.offsetHeight+to);
        this.fx.custom(this.el.offsetHeight,this.el.offsetHeight+to);
    },

    hide: function(){
        if (!this.readCookie()) {
            this.fx.hide();
        }
    },

    setHide: function(){
        if (this.el.offsetHeight != 0) {
            //this.setCookie(0);
            this.fx.toggle();
        }
    },

    setShow: function(){
        if (this.el.offsetHeight == 0) {
            //this.setCookie(this.el.scrollHeight);
            this.fx.toggle();
        }
    }
});



//useful for-replacement
//Método que permite iterar sobre una array.
//Se puede eliminar si se cargan las prototype.js enteras (en vez de la versión lite de Valerio)
//pues ahí el each ya está incluido.
//Array.prototype.each = function(func){
//  for(var i=0;ob=this[i];i++) func(ob, i);
//}


fx.expoIn = function(pos){
    return Math.pow(2, 10 * (pos - 1));
}
fx.expoOut = function(pos){
    return (-Math.pow(2, -10 * pos) + 1);
}
fx.quadIn = function(pos){
    return Math.pow(pos, 2);
}
fx.quadOut = function(pos){
    return -(pos)*(pos-2);
}
fx.circOut = function(pos){
    return Math.sqrt(1 - Math.pow(pos-1,2));
}
fx.circIn = function(pos){
    return -(Math.sqrt(1 - Math.pow(pos, 2)) - 1);
}
fx.backIn = function(pos){
    return (pos)*pos*((2.7)*pos - 1.7);
}
fx.backOut = function(pos){
    return ((pos-1)*(pos-1)*((2.7)*(pos-1) + 1.7) + 1);
}
fx.sineOut = function(pos){
    return Math.sin(pos * (Math.PI/2));
}
fx.sineIn = function(pos){
    return -Math.cos(pos * (Math.PI/2)) + 1;
}
fx.sineInOut = function(pos){
    return -(Math.cos(Math.PI*pos) - 1)/2;
}

function getScrollOffset(){

    var yScroll;
    if (self.pageYOffset) { // all except Explorer
        yScroll = self.pageYOffset;
    } else if (document.documentElement && document.documentElement.scrollTop){  // Explorer 6 Strict
        yScroll = document.documentElement.scrollTop;
    } else if (document.body) {// all other Explorers
        yScroll = document.body.scrollTop;
    }

    arrayPageScroll = new Array('',yScroll)
    return arrayPageScroll;
}

function getInnerSize(){

    var xInner, yInner;
    if (self.innerHeight) { // all except Explorer
        xInner = self.innerWidth;
        yInner = self.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
        xInner = document.documentElement.clientWidth;
        yInner = document.documentElement.clientHeight;
    } else if (document.body) { // other Explorers
        xInner = document.body.clientWidth;
        yInner = document.body.clientHeight;
    }
    arrayPageInner = new Array(xInner,yInner)
    return arrayPageInner;
}

function isArray(obj) {
   if (obj.constructor.toString().indexOf("Array") == -1)
      return false;
   else
      return true;
}

function preloadImages(pAccionImg) {
    if (!pAccionImg) {
        if (document.images) {
            for (var i = 0; i < preloadImages.arguments.length; i++) {
                (new Image()).src = preloadImages.arguments[i];
            }
        }
    }
    else {
        tImages=pAccionImg.split(";");
        for (var i = 0; i < tImages.length; i++) {
            //alert (tImages[i]);
            (new Image()).src = tImages[i];
        }
    }
}

Event.observe(window, 'load', initAlerts, false);
function initAlerts(){
	eAlert = new fx.Alert({offsetX: 10,offsetY: 20,boxWidth: "lite",alertTitle: "",waitTime: 4200,duration: 400, onClick: true});
}
