/**
 * PromoHandler
 *
 * @author Boz
 * @classDescription Set up to handle promo UI components.
 **/

mdp.app.PromoHandler = function(elementId,options){
    /* ---[ CLASS VARIABLES ]--- */


    /* ---[ CONSTRUCTOR ]--- */
    function init(){

        /* initialization code */
        setupEventListeners();

    }

    /* ---[ PUBLIC METHODS ]--- */


    /* ---[ PRIVATE METHODS ]--- */
    function show(el){
        var el = $(el).getParent();
        el.removeEvents("mouseover");
        var promobody = $E(".promobody",el);
        var s_height = promobody.getStyle("height");
        var height = parseInt(s_height.substring(0,s_height.length-2));
        new Fx.Style(promobody,"height", {duration: 700, onStart:function(){
            promobody.setProperty("style","height:0px");
            promobody.removeClass("hide");
        }}).start(0,height);

        /* hide #topinit link */
        $("topinit").addClass("hide");

        /* call onShow if definition exists */
        if(typeof(options) != "undefined"){
            if((typeof(options.onShow)) != "undefined"){
                options.onShow();
            }
        }

    }

    function hide(el){
        var promobody = findParent($(el),"promobody");
        new Fx.Style(promobody,"height", {duration: 700, onComplete:function(){
            promobody.addClass("hide");
            promobody.removeProperty("style");
            if(promobody.getParent().hasClass("initroll")){
                attachInit(promobody);
            }
        }}).start(promobody.getSize().size.y,0);

        /* show #topinit link */
        $("topinit").removeClass("hide");

        /* call onHide if definition exists */
        if(typeof(options) != "undefined"){
            if((typeof(options.onHide)) != "undefined"){
                options.onHide();
            }
        }
    }

    function actionCallback(remoteResult){
        if (remoteResult.statusCode == 0) {
            /* Success! */
        }else{
            /* Failure */
            alert(remoteResult.statusMessage);
        }
    }

    function findParent(el,_class){
        if(el.hasClass(_class)){
            return el;
        }
        else{
            return findParent(el.getParent(),_class);
        }
    }

    function hideall(el){
        var promocontain = $(el).getParent();
        promocontain.remove();

        /* call onHideAll if definition exists */
        if(typeof(options) != "undefined"){
            if((typeof(options.onHideAll)) != "undefined"){
                options.onHideAll();
            }
        }
    }

    function attachInit(el){
        var event;
        if(el.hasClass("initclick")){
            event = "click";
        }
        if(el.hasClass("initroll")){
            event = "mouseover";
        }

        $ES(".promoinit",el).each(function(element){
            if(event == "click" || event == "mouseover"){
                element.addEvent(event,function(){
                    show(this);
                });
            }else{
                show(element);
                element.addEvent("click",function(){
                    show(this);
                });
            }
        });
    }

    /* ---[ EVENT LISTENERS ]--- */
    function setupEventListeners(){

	    var el = $(elementId);

        attachInit(el);

        $ES(".promoclose",el).each(function(element){
            element.addEvent("click",function(){
                hide(this);
            });
        });

        $ES(".promocloseall",el).each(function(element){
            element.addEvent("click",function(){
                hideall(this);
            });
        });

        if(el.hasClass("closetimeout")){
            setTimeout(function(){
                hide($E(".promoclose",el));
            },3000);
        }

    }

    /* ---[ RUN ]--- */
    init();
};
