/*
Lucidia swf banner ad callback interface, for flash ads that need to resize their div tags vertically

Flash applications must expose:

expandCallback(returncode:int);
closeCallback(returncode:int);

Flash applications may call:

requestExpand();
requestClose();
*/

LucidiaSwfOverlay = {

    flashdivtag_: '',
    divtag_container_: '',
    flashid_: '',
    timer_: null,
    width_: 0,
    height_: 0,
    maximised_height_: '',
    minimised_height_: '',

    onSwfLoadStatus: function (e) {
        //console.log("swf load success: "+e.success);
    },

    /*
    begins loading a swf with path 'swfpath' into div tag 'flash_divtag' and assigns it the id 'id'. 'divtag_container' 
    is the flash div tag's parent, and 'maximised_height' is the size the tag is increased to if the flash object 
    requests the tag to expand.   
    */

    init: function (swfpath, id, swfwidth, swfheight, flashvars, flash_divtag, divtag_container, maximised_height) {

        this.width_ = swfwidth;
        this.height_ = swfheight;
        this.maximised_height_ = maximised_height;

        this.flashid_ = id;
        this.flash_divtag_ = flash_divtag;
        this.divtag_container_ = divtag_container;

        var params = {};
        params.menu = 'false';
        params.wmode = 'transparent';
        params.scale = 'noscale';
        params.salign = 'tl';

        var attributes = {};
        attributes.id = id;
        attributes.name = id;

        if (flashvars == null)
            flashvars = {};

        flashvars.classPath = 'LucidiaSwfOverlay';

        swfobject.embedSWF(swfpath, flash_divtag, swfwidth, swfheight, "9.0.45", "", flashvars, params, attributes, this.onSwfLoadStatus);

        timer_ = setTimeout(this.checkDivConstruction, 100);
    },

    /*
    timer callback function to repeatedly check if the parent div tag has been created, if it has, its
    overflow parameter is set to hidden, it is made visible and its initial height is used as the height to return the tag to
    if the flash object requests the tag to close
    */

    checkDivConstruction: function () {
        var divtag = document.getElementById(LucidiaSwfOverlay.divtag_container_);

        if (divtag != null) {
            clearTimeout(timer_);
            LucidiaSwfOverlay.timer_ = null;
            divtag.style.overflow = 'hidden';
            divtag.style.display = 'block';

            LucidiaSwfOverlay.minimised_height_ = divtag.style.height;
        }
        else {
            timer_ = setTimeout(LucidiaSwfOverlay.checkDivConstruction, 100);
        }
    },

    requestExpand: function () {
        var flash = document.getElementById(this.flashid_),
            container = document.getElementById(this.divtag_container_),
            swf = swfobject.getObjectById(this.flashid_);
        container.style.height = this.maximised_height_;
        swf.height = this.maximised_height_.replace('px', '');
        var ret = flash.expandCallback(1);
    },

    requestClose: function () {
        var flash = document.getElementById(this.flashid_),
            container = document.getElementById(this.divtag_container_),
            swf = swfobject.getObjectById(this.flashid_);
        console.log(this.minimised_height_.replace('px', ''));
        container.style.height = this.minimised_height_;
        swf.height = this.height_;
        var ret = flash.closeCallback(1);
    }
};

