ark.namespace('widget.Blind');

(function() {
 var Dom = YAHOO.util.Dom,
     Event = YAHOO.util.Event;


ark.widget.Blind = function() {
}

ark.widget.Blind.prototype = {
    container: null,
    loader: null,
    elt: null,

    bHighlighting: false,
    bHideOnClick: false,
    bFollowClick: false,
    oTop: null,
    oLeft: null,
    oRight: null,
    oBottom: null,
    oMask: null
};

ark.widget.Blind.prototype.show = function(elt,options) {
    this.bHighlighting = false;

    if(!this.container) {
       this.container = document.createElement('div'); 
       Dom.setStyle(this.container,'position','absolute');
       Dom.setStyle(this.container,'display','none');
       Dom.setStyle(this.container,'background-color','black');
       Dom.setStyle(this.container,'opacity','0.2');
       document.body.appendChild(this.container);
    }

    if( options && options['zIndex'] ) {
        Dom.setStyle(this.container,'z-index',options['zIndex']);
    }

    Dom.setStyle(this.container,'display','block');
    if( elt ) {
        var x = Dom.getX(elt);
        var y = Dom.getY(elt);

        Dom.setX(this.container,x);
        Dom.setY(this.container,y);
        Dom.setStyle(this.container,'width',elt.offsetWidth+'px');
        Dom.setStyle(this.container,'height',elt.offsetHeight+'px');
        this._embed(elt,true);
    } else {
        Dom.setX(this.container,0);
        Dom.setY(this.container,0);
        Dom.setStyle(this.container,'width',Dom.getDocumentWidth()+'px');
        Dom.setStyle(this.container,'height',Dom.getDocumentHeight()+'px');
        this._embed(document.body,true);
    }
}

ark.widget.Blind.prototype._embed = function(elt,bHide) {
    for(var i = 0; i < elt.childNodes.length; i++ ) {
        var child = elt.childNodes[i];
        if( child.tagName == 'EMBED' ) {
            if( bHide ) {
                Dom.setStyle(child,'visibility','hidden');
            } else {
                Dom.setStyle(child,'visibility','visible');
            }
        } else {
            if( child.childNodes.length > 0 ) {
                this._embed(child,bHide);
            }
        }
    }
}

ark.widget.Blind.prototype.hide = function() {
    if( !this.bHighlighting ) {
        Dom.setStyle(this.container,'display','none');
    } else if( this.oTop ) {
        Dom.setStyle(this.oTop,'display','none');
        Dom.setStyle(this.oBottom,'display','none');
        Dom.setStyle(this.oLeft,'display','none');
        Dom.setStyle(this.oRight,'display','none');
    }
    if( this.oMask ) {
        Dom.setStyle(this.oMask,'display','none');
    }
    this._embed(document.body,false);
}

ark.widget.Blind.prototype.highlight = function(elt,options) {
    this.elt = elt;
    this.bHighlighting = true;

    if( !this.oTop ) {
        this.oTop = document.createElement('div'); 
        this.oTop.setAttribute('id','blind_top');
        Dom.setStyle(this.oTop,'display','none');
        Dom.setStyle(this.oTop,'position','absolute');
        Dom.setStyle(this.oTop,'background-color','black');
        Dom.setStyle(this.oTop,'opacity','0.2');
        document.body.appendChild(this.oTop);
    }

    if( !this.oLeft ) {
        this.oLeft = document.createElement('div'); 
        this.oLeft.setAttribute('id','blind_left');
        Dom.setStyle(this.oLeft,'display','none');
        Dom.setStyle(this.oLeft,'position','absolute');
        Dom.setStyle(this.oLeft,'background-color','black');
        Dom.setStyle(this.oLeft,'opacity','0.2');
        document.body.appendChild(this.oLeft);
    }

    if( !this.oRight ) {
        this.oRight = document.createElement('div'); 
        this.oRight.setAttribute('id','blind_right');
        Dom.setStyle(this.oRight,'display','none');
        Dom.setStyle(this.oRight,'position','absolute');
        Dom.setStyle(this.oRight,'background-color','black');
        Dom.setStyle(this.oRight,'opacity','0.2');
        document.body.appendChild(this.oRight);
    }

    if( !this.oBottom ) {
        this.oBottom = document.createElement('div'); 
        this.oBottom.setAttribute('id','blind_bottom');
        Dom.setStyle(this.oBottom,'display','none');
        Dom.setStyle(this.oBottom,'position','absolute');
        Dom.setStyle(this.oBottom,'background-color','black');
        Dom.setStyle(this.oBottom,'opacity','0.2');
        document.body.appendChild(this.oBottom);
    }

    this._embed(document.body,true);

    if( options && options['mask'] ) {
        if( !this.oMask ) {
            this.oMask = document.createElement('div'); 
            this.oMask.setAttribute('id','blind_mask');
            Dom.setStyle(this.oMask,'display','none');
            Dom.setStyle(this.oMask,'position','absolute');
            Dom.setStyle(this.oMask,'background-color','black');
            Dom.setStyle(this.oMask,'opacity','0.0');
            Dom.setStyle(this.oMask,'z-index','1');
            document.body.appendChild(this.oMask);
        }
    }

    if( options && options['hideOnClick'] ) {
        if( !options['mask'] ) {
            this.bHideOnClick = true;
            this.bFollowClick = options['followClick'];
            Event.removeListener(this.elt,'click',this._click);
            Event.on(this.elt,'click',this._click,this,true);
        }
    }

    Event.onAvailable(this.elt,this._resize,this,true);

    if( options && !options['noResize'] ) {
        Event.on(window,'resize',this._resize,this,true);
    }
}

ark.widget.Blind.prototype._resize = function(e) {
    if( this.elt ) {
    var o = Dom.get(this.elt);
    var x = Dom.getX(o);
    var y = Dom.getY(o);
    var w = o.offsetWidth;
    var h = o.offsetHeight;

    Dom.setStyle(this.oTop,'display','block');
    Dom.setX(this.oTop,0);
    Dom.setY(this.oTop,0);
    Dom.setStyle(this.oTop,'width','100%');
    Dom.setStyle(this.oTop,'height',y+'px');

    Dom.setStyle(this.oLeft,'display','block');
    Dom.setX(this.oLeft,0);
    Dom.setY(this.oLeft,y);
    Dom.setStyle(this.oLeft,'width', x+'px');
    Dom.setStyle(this.oLeft,'height',h+'px');

    Dom.setStyle(this.oRight,'display','block');
    Dom.setX(this.oRight,x+w);
    Dom.setY(this.oRight,y);
    Dom.setStyle(this.oRight,'width', Dom.getViewportWidth() - x - w +'px');
    Dom.setStyle(this.oRight,'height',h+'px');

    Dom.setStyle(this.oBottom,'display','block');
    Dom.setX(this.oBottom,0);
    Dom.setY(this.oBottom,y+h);
    Dom.setStyle(this.oBottom,'width', '100%');
    var tmp = Dom.getViewportHeight() - y - h;
    tmp = tmp > 0 ? tmp : 0;
    Dom.setStyle(this.oBottom,'height', tmp + 'px' );

    if( this.oMask ) {
        Dom.setStyle(this.oMask,'display','block');
        Dom.setX(this.oMask,0);
        Dom.setY(this.oMask,0);

        Dom.setStyle(this.oMask,'width',Dom.getViewportWidth()+'px');
        Dom.setStyle(this.oMask,'height',Dom.getViewportHeight()+'px');
    }
    }
}

ark.widget.Blind.prototype._click = function(e) {
    if( !this.bFollowClick ) {
        Event.preventDefault(e);
    }
    this.hide();
}

})();
