/* Load Indicator version 0.1
* (c) April 2008 Ferdi Hulleman
*
* Description:
*
* put a transparent iframe containing a loading indicator image in front
* why iframe? -> problems with form elements (notably select boxes)
* 
* Credits:
*
* License:
* 
* Usage - manual:
*
* var li = new LoadIndicator('id','img src');
* li.start();
* < your code here >
* li.finish();
* 
*/

// create the package object
var A1B;
if (!A1B){
    A1B = new base2.Package(this, {
        name:    "A1B",
        version: "1"
    });
}
if (!A1B.Util){
    Util = new base2.Package(this, {
        name:    "Util",
        parent:  base2.A1B,
        exports: "LoadIndicator, ElementVisibility"
    });
}

// evaluate the imported namespace
eval(this.imports); 

// define package contents
var LoadIndicator = Base.extend({
    id: null,
    elem : null,
    iframe : null,
    
    constructor: function(id, img){
        this.id = id;
        this.img = img;
        this.elem = document.getElementById(id);
    },
    
    start : function() {
        // transparent iframe containing loading image with same dimensions and position as elem  
        var frame = document.createElement('iframe');
        //frame.style.left = document.defaultView.getComputedStyle(this.elem, "").getPropertyValue('left');
        //frame.style.top = document.defaultView.getComputedStyle(this.elem, "").getPropertyValue('top');
        frame.style.left = this._findPosX(this.elem);
        frame.style.top = this._findPosY(this.elem);
        
        frame.style.width = this.elem.offsetWidth;
        frame.style.height = this.elem.offsetHeight;
        frame.style.position = 'absolute';
        frame.style.backgroundImage = this.img;
        frame.style.backgroundColor = '#fff;';
        frame.style.backgroundRepeat = 'no-repeat';
        frame.style.backgroundPosition =  'center 200px'; 
        if ( base2.detect("MSIE") ) frame.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(opacity=70)';
        else frame.style.opacity = 0.7;
        this.iframe = frame;
        document.body.appendChild(frame);
        if (typeof console != "undefined") console.log('start ended');
    },
    
    finish : function() {
        // remove the iframe created in start
        if (this.iframe != null) document.body.removeChild(this.iframe);
        if (typeof console != "undefined") console.log('finish ended');
    },
    
    _findPosX : function(obj) {
        var curleft = 0;
        if (obj.offsetParent){
            while (obj.offsetParent) {
                curleft += obj.offsetLeft;
                obj = obj.offsetParent;
            }
        }
        else if (obj.x)
            curleft += obj.x;
        return curleft;
    }, 
    
    _findPosY : function(obj) {
        var curtop = 0;
        if (obj.offsetParent) {
            while (obj.offsetParent) {
                curtop += obj.offsetTop;
                obj = obj.offsetParent;
            }
        }
        else if (obj.y)
            curtop += obj.y;
        return curtop;
    }
    
});

var ElementVisibility = Module.extend({
    visible: function(element, maxParent) {
        var result = true;
        maxParent = maxParent == undefined || maxParent == null ? document.querySelector("body") : maxParent;
        
        if(element.parentNode == maxParent)
            return result;
        else
            return CSSStyleDeclaration.getPropertyValue(element.style, "display") != "none" ? this.visible(element.parentNode, maxParent) : false;
    },
    
    toggle: function(element) {
        this.visible(element) ? this.hide(element) : this.show(element);
        return element;
    },
    
    hide: function(element) {
        //element.style.display = 'none';
        CSSStyleDeclaration.setProperty(element.style, "display", "none", "");
        return element;
    },
    
    show: function(element) {
        //element.style.display = '';
        CSSStyleDeclaration.setProperty(element.style, "display", "", "");
        return element;
    }   
});

// evaluate the exported namespace (this initialises the Package)
eval(this.exports);