var lightBox = function()
{
    var that = this;
    this.id = 'lightBox';
    this.wrapper = 'shadowBox';
    this.width = 'auto';
    this.height = 'auto';
    this.process = 0;
    this.offsetY = 0;
    this.offsetX =0;
    this.interval = 0; 
    this.slideStep = 30;
    this.slidePause = 0;
    this.content = '';
    this.events = '';    
    this.lightBoxWindows = new Array();
    this.lightBoxWindowsEl = new Array();
    
    this.createLightBox = function(id)
    {
    	var topD = document.createElement('div');
    	if(that.detectCSS3())
 		{
			topD.setAttribute('id',id);
			topD.setAttribute('class',id);
        	topD.innerHTML = '<div id="'+id+'Close" class="close">&nbsp;</div><div class="clear"></div><div id="'+id+'Con" class="'+id+'Con"></div>';
		}
		else
		{
        	topD.innerHTML = '<table id="'+id+'" class="'+id+'"><tr><td class="tl"></td><td class="top"></td><td class="tr"><span class="modal"><div id="'+id+'Close" class="close">&nbsp;</div></span></td></tr><tr><td class="left"></td><td id="'+id+'Con" class="'+id+'Con"></td><td class="right"></td></tr><tr><td class="bl"></td><td class="bottom"></td><td class="br"></td></tr></table>';
		}
        window.document.body.appendChild(topD);
    };
    
    this.addToWindows = function(el)
    {
        if(!that.lightBoxWindowsEl[el])
        {
            that.lightBoxWindowsEl[el] = 1;
            that.lightBoxWindows[that.lightBoxWindows.length] = el;
        }
    };
    
    this.closeBox = function()
    {
        if(dl$(that.id))
        {
            if(dl$(this.wrapper))dl$(this.wrapper).parentNode.removeChild(dl$(this.wrapper));
            dl$(this.id).style.display = 'none';
        }
    };   
   
    this.loading = function(obj)
    {
        if(typeof(dlLoader) !== undefined ) dlLoader.init(obj);
    };
    
    this.closeLoading = function()
    {         
        if(typeof(dlLoader)!== undefined) dlLoader.close();
    };
   
    this.winOnScroll = function(e)
    {
        that.process = 0;
        that.moveToY(that.offsetY);
    };
   
    this.winOnResize = function()
    {
        var divW = dl$(that.id).offsetWidth;
        var shadowW = that.getSize().w;
        dl$(that.id).style.left = ((shadowW/2)-(divW/2)) + 'px';
        if(dl$(that.wrapper))dl$(that.wrapper).style.height= that.getSize().h+'px';
        that.moveToY(that.offsetY);
    };
   
    this.moveToY = function(from)
    {
        window.clearInterval(that.interval);
        var divH = dl$(that.id).offsetHeight;
        var shadowH = that.getSize().bodyH;
        var to = parseInt(that.getOffset().y); 
        if(to >= that.offsetY)
        {
            if(from < to)
            {
                that.process = 1;
                dl$(that.id).style.top = ((shadowH/2)-(divH/2)) + from + 'px';              
                from = from + that.slideStep; 
                if((that.offsetY + that.slideStep)<to)that.offsetY = that.offsetY +that.slideStep;
                that.interval = window.setInterval(function(){that.moveToY(from)},that.slidePause);
            }
            else
            {
                that.process = 0;
                dl$(that.id).style.top = ((shadowH/2)-(divH/2)) + to + 'px';
                that.offsetY = that.getOffset().y;
                window.clearInterval(that.interval); 
            }            
        }
        else
        {
            if(from > to)
            {
                that.process = 1;
                dl$(that.id).style.top = ((shadowH/2)-(divH/2)) + from + 'px';              
                from = from - that.slideStep; 
                if((that.offsetY - that.slideStep)>to)that.offsetY = that.offsetY - that.slideStep;
                that.interval = window.setInterval(function(){that.moveToY(from)},that.slidePause);
            }
            else
            {
                that.process = 0;
                dl$(that.id).style.top = ((shadowH/2)-(divH/2)) + to + 'px';
                that.offsetY = that.getOffset().y;
                window.clearInterval(that.interval); 
            }            
        } 
    };
    
    this.init = function(params)
    {
        this.loading({'wrapperId':'shadowBox','position':'global','loaderId':'loadingBox'});
        that.width = (typeof(params.width) != 'undefined')?params.width :'auto';
        that.height = (typeof(params.height) != 'undefined')?params.height :'auto';
        that.events = (typeof(params.events) != 'undefined')?params.events:'';
        that.id = (typeof(params.id) != 'undefined')?params.id:'lightBox';  
        that.wrapper = (typeof(params.wrapper) != 'undefined')?params.wrapper:'shadowBox';
        var text = (typeof(params.text) != 'undefined')?params.text:'';
        var boxId = params.content;
        that.addToWindows(boxId);
               
        if(!dl$(that.id))that.createLightBox(that.id);
        if(dl$(that.id) && that.lightBoxWindows.length > 0)
        {
            var i = 0;
            while(i<that.lightBoxWindows.length)
            {
                if(dl$(that.lightBoxWindows[i]+'Added'))dl$(that.lightBoxWindows[i]+'Added').style.display = 'none';
            	i++;
            }
                   
	        if(!dl$(boxId+'Added') && (dl$(boxId) || boxId=='default' ))
	        {
	            var id = document.createElement('div');
	            id.setAttribute('id',boxId+'Added');
	            dl$(that.id+'Con').appendChild(id);
	            dl$(boxId+'Added').innerHTML =  (boxId=='default')?text:dl$(boxId).innerHTML;
	            if(boxId!='default')dl$(boxId).parentNode.removeChild(dl$(boxId));
	        }
	        
	        dl$(that.id).style.width = that.width;
            dl$(that.id).style.height = that.height; 
	        dl$(boxId+'Added').style.display = 'block';

	        this.closeLoading();
	        
	        that.box();

		    if(typeof(that.events) == 'function') that.events();
        }  
    };    
   
    this.box = function()
    {
        if(!dl$(that.wrapper))
        {
            var shadowDiv = document.createElement('div');
            shadowDiv.setAttribute('id',that.wrapper);
            document.body.appendChild(shadowDiv);
            dl$(that.wrapper).style.height= this.getSize().h+'px';
		}
        dl$(that.id).style.display = 'block';
        
        var divW = dl$(that.id).offsetWidth;
        var divH = dl$(that.id).offsetHeight;
        var shadowW = this.getSize().w;
        var shadowH = this.getSize().bodyH;
        dl$(that.id).style.left = ((shadowW/2)-(divW/2)) +  'px';
        if(divH > shadowH)
        {
            dl$(that.id).style.top = this.getOffset().y + 40 + 'px';
        }
        else
        {
            dl$(that.id).style.top = ((shadowH/2)-(divH/2)) + this.getOffset().y + 'px';
        }

        that.offsetY = this.getOffset().y;

        if(dl$(that.id))
        {
            window.onscroll = that.winOnScroll;
            window.onresize = that.winOnResize;
        }     

        if(dl$(that.id+'Close'))dl$(that.id+'Close').onclick = function()
        {
            dl$(that.wrapper).parentNode.removeChild(dl$(that.wrapper));
            if(dl$(that.id))dl$(that.id).style.display = 'none';
        };

        dl$(that.wrapper).onclick = function()
        {
            dl$(that.wrapper).parentNode.removeChild(dl$(that.wrapper));
            if(dl$(that.id))dl$(that.id).style.display = 'none'; 
        };   
    };      
};   

lightBox.prototype = dl;
lightBox.prototype.constructor = lightBox;
var lightbox = new lightBox();
