/**
 * DialogManager
 *
 * @package    mivilagunk
 * @subpackage Manager
 * @author     Szijártó Tamás ( szicsu ) <szicsu@jquery.hu>
 * @version    SVN: $Id: $
 */
dialogManager = new function(){
	var self = this;
    this.windowId = 'cafeDialog';
    this.closeClass = 'cafeDialogClose';
	this.overLayId = 'cafeOverlay';
	
	//autoBind
	$(document).bind('keydown.cafeDialog', function( e ){
		(e.keyCode) ? keyCode=e.keyCode : keyCode=e.which;
		if(keyCode == 27){
			self.close();
		}
	});
	
	
	this.open = function( param, callBack ){
		
		self.close();
		param = self.parseParams( param );
		
		if( param.overlay ){
            self.openOverLay();
        }
		
		return $('<div id="'+ this.windowId +'"></div>')
            .appendTo('body')
            .css({ width: param.width, height: param.height, marginTop: parseInt( param.height  / 2 ) * -1 + parseInt( $(window).scrollTop() ) , marginLeft:  parseInt( param.width / 2 ) * -1 })
            .html( self.getTemplate( param, param.template ) )
            .find('.'+ self.closeClass ).bind('click', self.close ).end()
			.each(function(){
				
				if( typeof( callBack ) == 'function'){
					callBack( $(this) );
				}
				
			});
	}
	
	this.close = function(){
		
		$('#' + self.windowId ).remove();
		self.closeOverLay();
	}
	
	this.openOverLay = function(){
		
		if( $.browser.msie == true && parseInt( $.browser.version ) < 7 ){
            $('select').css({visibility: 'hidden'});
        }
        $('body').append('<div id="'+ self.overLayId +'">&nbsp;</div>');
	}
	
	
	this.closeOverLay = function(){
		
		if( $.browser.msie == true && parseInt( $.browser.version ) < 7 ){
            $('select').css({visibility: 'visible'});
        }
        $('#'+ self.overLayId ).remove();
	}
	
	this.parseParams = function( param ){
		
		param = param || {};

        /** default beállítások **/
        param.template= param.template || 'dialog';
        param.title = param.title || '&nbsp;';
        param.close = param.close || '';
        param.width = param.width || 400;
        param.height = param.height || 170;
        param.overlay = param.overlay || true;
		
		return param;
	}
	
	/**
	 * getTemplate function 
	 * visszaadj a templatet
	 *
	 * @param mixed ( null | object ) data a templatebe az adatok
	 * @param string template a template neve
	 * @access protected
	 * @return string
	 */
	this.getTemplate = function( data , template ){
	
		if( typeof data != 'object' ){
			data = {};
		}
		
		switch( template ){
			
			case 'dialog':
				var arr = new Array(
					'<table cellpadding="0" cellspacing="0" style="width: {width}px; height: {height}px;">',
                        '<tr>',
							'<td valign="top">',
								'<table cellpadding="0" cellspacing="0" border="0" style="width: {width}px;">',
									'<tr>',
										'<td class="topLeft png">&nbsp;</td>',
										'<td class="top">{title}<a class="topX '+ dialogManager.closeClass +'">&nbsp;</a></td>',
										'<td class="topRight png">&nbsp;</td>',
									'</tr>',
									'<tr>',
										'<td class="contentLeft">&nbsp;</td>',
										'<td class="content">{text}</td>',
										'<td class="contentRight">&nbsp;</td>',
									'</tr>',
									'<tr>',
										'<td class="bottomLeft png">&nbsp;</td>',
										'<td class="bottom">&nbsp;</td>',
										'<td class="bottomRight png">&nbsp;</td>',
									'</tr>',
								'</table>',
                        	'</td>',
						'</tr>',
                    '</table>'
				);
			break;
		} 
		
		return  $.template( arr.join('') ).apply( data ) ;	
	
	}
	
}
