/**
 * 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).parent();
        el.unbind("mouseover");
        var promobody = $(el).find(".promobody:first");
        var s_height = promobody.css("height");
        var height = parseInt(s_height.substring(0,s_height.length-2));
        $(promobody).animate({"height":height},700,function(){
            promobody.removeClass("hide");
        });

        /* 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");
        $(promobody).animate({"height":0},700,function(){
            promobody.addClass("hide");
            promobody.removeAttr("style");
            if(promobody.parent().hasClass("initroll")){
                attachInit(promobody);
            }
        });

        /* 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.parent(),_class);
        }
    }

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

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

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

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

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

	    var el = $("#"+elementId);

        attachInit(el);

        $(el).find(".promoclose").click(function(){
            hide(this);
        });


        $(el).find(".promocloseall").click(function(){
            hideall(this);
        });

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

    }

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

