YAHOO.namespace('zu');

function bubbleEvent(e) {
   
   YAHOO.zu.lightbox.handleAction(e);   
}    

(function(){

    var Event = YAHOO.util.Event,
        Panel = YAHOO.widget.Panel,
        Dom = YAHOO.util.Dom;

    function init() {
    
        YAHOO.zu.lightbox = new Panel('lightbox', {
            fixedcenter:"contained", 
            constraintoviewport:true, 
            close:true, 
            visible:false,
            modal:true,
            underlay:'none'
        });
        
        YAHOO.zu.lightbox.handleAction = function( e ) {

            var target = Event.getTarget(e);
         
            if (Dom.hasClass(target, 'lightbox-close')) {
                this.hide();
            }
        }
        
        YAHOO.zu.lightbox.setIframeSrc = function( url ) {
            this.setBody('<div class="content"><iframe name="lightbox-iframe" id="lightbox-iframe" frameborder="0" src="'+url+'"></iframe></div>');
        }
        
        YAHOO.zu.lightbox.setContent = function( url ) {
            this.setBody('<div class="content"><img src="'+url+'" /></div>');
        }        
        
        YAHOO.zu.lightbox.updateHeight = function() {
            Dom.getElementsByClassName( 'bd' , '' , 'lightbox' , function(o){
                var height = parseInt( YAHOO.zu.lightbox.cfg.getProperty('height') ) - 20;
                Dom.setStyle(o, 'height',height+'px');
            });                 
        }
        
        YAHOO.zu.lightbox.setConfig = function( config ) {
            for ( x in config ) {
                switch ( x ) {
                    case 'width':
                    case 'height':
                        this.cfg.setProperty(x,config[x]);
                        break;
                }
            }
        }
        
        YAHOO.zu.lightbox.parseConfig = function( link_class ) {

            var config = [],
                prop = "",
                arr = link_class.split(";"),
                match = "";
                
            for(var i=0,len=arr.length;i<len;i++) {
                prop = arr[i].split(":");
                match = prop[0].match(/[^\s]*\s*?$/);
                if ( match[0].length > 0 ) {
                    config[match] = prop[1];
                }
            }
            
            return config;
        }
        
        YAHOO.zu.lightbox.setup = function( e ) {
            
            Event.preventDefault(e);
            
            var link,
                ele = Event.getTarget(e);

            
            while( ele.nodeName.toUpperCase() != "BODY" ) {
                if ( ele.nodeName.toUpperCase() == "A" ) {
                    link = ele;
                }
                ele = ele.parentNode;            
            
            }
            
            
            
            var config = this.parseConfig( link.className );
            
            this.setConfig( config );
            
            if (!!config['iframe']) {
                this.setIframeSrc(link);
            } else {
                this.setContent(link.href);
            }
            
            
            
            this.render(document.body);
            this.updateHeight(); 
            this.show();
        }
        
        YAHOO.zu.lightbox.handleClose = function() {
            this.setIframeSrc('');
        }
        
        YAHOO.zu.lightbox.hideEvent.subscribe(YAHOO.zu.lightbox.handleClose,YAHOO.zu.lightbox);
        
        // setup corners
        YAHOO.zu.lightbox.setHeader('<div class="tl corner"></div>');
        YAHOO.zu.lightbox.setFooter('<div class="bl corner"></div><div class="br corner"></div> ');

        var arr = Dom.getElementsByClassName( 'lightbox' );
        Event.on(arr, 'click', YAHOO.zu.lightbox.setup, YAHOO.zu.lightbox, true);
    };
    

    
    Event.on(window, 'load', init);
    

})();