var currenWin = false;

function moskWin( winId )
{

       this.winId    = winId;           

       this.win      = false;    
   
       this.heigth   = 300;
       
       this.width    = 300;
       
       this.appendTo = false;
       
       this.cache    = false;
       
       this.url      = false;
       
       this.text     = false;
       
       this.shadow   = true;
};


function openWin( winId, params )
{

     var win = new moskWin( winId );

     for( param in params )
     {
          win[ param ] = params[ param ];
     }
     
     win.create();
     
     return win;      
};

function closeWin()
{
        if( currentWin )
        {
            currentWin.close();
        }
        
        return false;
}

moskWin.prototype.create = function()
{
     var needAppend = false;    
        
     this.win = me( this.winId );
     
     if( !this.win )
     {
          this.win    = document.createElement( 'div' );          
          needAppend  = true;     
     };
     
     this.win.id = this.winId;

     this.win.style.width  = this.width + 'px';
     this.win.style.height = this.height + 'px';
     this.win.style.position = 'absolute';
     
     this.setToPosition();     
     
     if( this.shadow )
     {
         shadow( 'block' );
     }

     if( needAppend )
     {
           document.body.appendChild( this.win );
     }

     this.win.style.display = 'block';
     this.win.style.zIndex = 999;
     
     this.setContent();
     this.win.style.overflow = 'auto';     

     currentWin = this;
     
     var tmpWin = this;
    
     startListenEvent( document, "keydown", function( e ){ tmpWin.onEscape( e ) } );
     startListenEvent( window, "scroll", function( e ){ placeShadow() } );


};

moskWin.prototype.onEscape = function( event )
{
     if( window.event )
     {
         var key = window.event.keyCode;
     }
     else
     {
         var key = event.keyCode;
     }
     
     if( key == 27 )
     {
            this.close();
     } 
      
}

moskWin.prototype.close = function()
{
       this.win.style.display = "none";
       endListenEvent( document, "keydown" );
       endListenEvent( window, "scroll" );
       currentWin = false;
       shadow( 'none' );
}



moskWin.prototype.setToPosition = function()
{
         
      if( this.appendTo )
      {
            var cordinates  = Cordinates( this.appendTo )
            var posX        = cordinates[0];
            var posY        = cordinates[1];
            
            winSize = winSizes();          
            
            if( ( posX + this.width ) > winSize[ 0 ] )
            {
                  posX = posX - this.width;
            }
            
            if( ( posY + this.height ) > winSize[ 1 ] )
            {
                 posY = posY - this.height;
            }
            
      }
      else
      {
            winSize = winSizes();          

            posX = ( winSize[ 0 ]   / 2 ) - ( this.width / 2 );
            posY = ( ( winSize[ 1 ]  / 2 ) + winSize[ 3 ] ) - ( this.height / 2 );       
      }     
      
      this.win.style.top  = posY + "px";
      this.win.style.left = posX + "px";
    
};

moskWin.prototype.setContent = function()
{
           var hasContent = false;
         
           if( this.cache )
           {
                   var casheElement = me( cashe );
                   
                   if( casheElement && casheElement.innerHTML != '' )
                   {
                        hasContent = casheElement.innerHTML;
                        return;
                   }                   
           };
           
           if( !hasContent )
           {
                   if( this.url )
                   {
                   
                        var tmpObj = this;
                        
                        
                        Load( this.url, { done: function()
                                                   {
                                                   
                                                       hasContent = this.ajax.responseText;
                                                       tmpObj.win.innerHTML = hasContent;
                                                   },
                                           loading: tmpObj.loading,
                                           loadKey: tmpObj.loadKey
                                        }
                               );      
                                  
                   }
                   else if( this.text )
                   {
                        hasContent = this.text;
                        return;
                   }                
           };       

};


placeShadow  = function()
{
    if( currentWin )
    {
       shadow( "block" );
    }
}
