/**
 * @v0.1 2011-06-12 beta 
 * @v0.9 2011-06-14 alpha
 * @v1.0 2011-08-19 IEBugFix
 * @v1.1 2011-08-25 save: cZero_Windows destroy and exist
 */
aWnd = false ;
/**
 * Windows
 */
var cZero_Windows = function(){
	this.windows = new Array() ; // (0-name,1-cZeroWindow)
	this.prefix = 'cZero_wnd_' ;
	this.zindex = 1024 ;
	this.nowOnTop = '' ;
	this.mouseSel = '' ;
	this.mouseX = 0 ;
	this.mouseY = 0 ;
	this.mouseSaveX = 0 ;
	this.mouseSaveY = 0 ;
}
cZero_Windows.prototype.search = function( $name ){
	for ( $i = 0 ; $i < this.windows.length ; $i++ ){
		if ( typeof (this.windows[$i]) == 'undefined' )
			continue;
		if ( this.windows[$i][1].name == $name )
			return this.windows[$i][1];
	}
	return undefined;
}
cZero_Windows.prototype.destroy = function( $name ){
    var c = this.search( $name );
    if ( undefined === c )
        return false;
    c.destroy();
    delete c;
    return true;
}
cZero_Windows.prototype.exist = function( $name ){
    var c = this.search( $name );
    if ( undefined === c )
        return false;
    return true;
}
cZero_Windows.prototype.create = function( $name ){
	exist = this.search( $name );
	if ( typeof(exist) == 'cZero_Window' )
		return exist;
	L = 1 + this.windows.length ;
	this.windows[L] = new Array() ;
	this.windows[L][0] = $name ; //===// Don`t use in code, ask class about it`s name //===//
	this.windows[L][1] = new cZero_Window( $name , this ) ;
	return this.windows[L][1];
}
cZero_Windows.prototype.getZIndex = function(){
	this.zindex++;
	return this.zindex;
}
cZero_Windows.prototype.reLocate = function(){
	c = 0 ;
	for ( $i = 0 ; $i < this.windows.length ; $i++ ){
		if ( typeof (this.windows[$i]) == 'undefined' )
			continue;
		if ( typeof (this.windows[$i][1]) == 'undefined' )
			continue;
		if ( this.windows[$i][1].name == '' )
			continue ;
		this.windows[$i][1].reLocate();
		c++ ;
	}
	return c;
}
/**
 * Window
 */
var cZero_Window = function( $name , $owner ){
	this.name = $name ;
	this.owner = $owner ;
	this.prefix = $owner.prefix + $name + '__' ;
	this.left = 0 ;
	this.top = 0 ;
	this.width = 320 ;
	this.height = 240 ;
	this.caption = this.name ;
	this.moveable = true ;
	this.closeable = true ;
	this.cfgFrameColor = '#225588' ;
	this.cfgFrameOpacity = '0.9' ;
	this.cfgTopColor = '#336699' ;
	this.cfgTopOpacity = '0.95' ;
	this.cfgTopTextColor = '#FFFFFF' ;
	this.iconID = 12 ;
	this.modal = false ;
	this.zindex = this.owner.getZIndex() ;
	this.html();
	return this;
}
cZero_Window.prototype.destroy = function(){
	this.name = '' ;
	$('#'+this.prefix+'wnd').remove();
	$('#'+this.prefix+'modal').remove();
}
cZero_Window.prototype.html = function(){
	var $txt = 
	'<Div id="'+this.prefix+'wnd" class="cZero_Window__wnd" tag="'+this.name+'" style="display: none; position: fixed; left: '+this.left+'px; top: '+this.top+'px; width: '+this.width+'px; height: '+this.height+'px; margin: 0; padding: 0; z-index: '+this.zindex+'">' +
		'<Div id="'+this.prefix+'bTop" style="position: absolute; left:0;top:0;width:'+this.width+'px;height:3px;background-color:'+this.cfgFrameColor+'"></div>' +
		'<Div id="'+this.prefix+'bLeft" style="position: absolute; left:0;top:3px;width:3px;height:'+(this.height-6)+'px;background-color:'+this.cfgFrameColor+'"></div>' +
		'<Div id="'+this.prefix+'bRight" style="position: absolute; left:'+(this.width-3)+'px;top:3px;width:3px;height:'+(this.height-6)+'px;background-color:'+this.cfgFrameColor+'"></div>' +
		'<Div id="'+this.prefix+'bBottom" style="position: absolute; left:0px;top:'+(this.height-3)+'px;width:'+this.width+'px;height:3px;background-color:'+this.cfgFrameColor+'"></div>' +
		'<Div id="'+this.prefix+'top" class="cZero_Window__top" tag="'+this.name+'" style="position: absolute; overflow: hidden; left: 3px; top: 3px; width: '+(this.width-6)+'px; height: 40px; background-color: '+this.cfgTopColor+'; color: '+this.cfgTopTextColor+'">' +
			'<Div>' + 
				'<Div id="'+this.prefix+'icon" style="float: left; margin: 4px 0px 0px 3px;"></Div>' +
				'<Div id="'+this.prefix+'caption" style="float: left; height: 30px; width: '+(this.width-85)+'px; overflow: hidden; margin: 8px 0px 0px 3px; font-family: Arial; font-size: 16pt; font-weight: bold;">' + this.caption + '</Div>' +
				'<Div id="'+this.prefix+'aClose" tag="'+this.name+'" style="display: none; cursor: pointer; float: right; margin-top: 4px;"><Img alt="close" src="/zero/image/cwnd/close.png"></Div>' +
				'<Div style="clear: both;"></Div>' +
			'</Div>' +
		'</Div>' +
		'<Div id="'+this.prefix+'client" style="position: absolute; overflow: auto; left: 3px; top: 43px; width: '+(this.width-6)+'px; height: '+(this.height-46)+'px; background-color: white;">' +
		'</Div>' +
	'</Div>' +
	'<Div id="'+this.prefix+'modal" style="left: 0; top: 0; width: 100%; height: 100%; display: none; position: fixed; background-color: black; z-index: '+this.zindex+';"></Div>' ;
	$('BODY').append($txt);
	$('#'+this.prefix+'bTop').css({'opacity':this.cfgFrameOpacity});
	$('#'+this.prefix+'bRight').css({'opacity':this.cfgFrameOpacity});
	$('#'+this.prefix+'bBottom').css({'opacity':this.cfgFrameOpacity});
	$('#'+this.prefix+'bLeft').css({'opacity':this.cfgFrameOpacity});
	$('#'+this.prefix+'top').css({'opacity':this.cfgTopOpacity});
	$('#'+this.prefix+'aClose').bind('click',function(){
		aWnd.search($(this).attr('tag')).destroy();
	});
	this.htmlIconUpdate();
	this.htmlCloseUpdate();
	return this;
//	alert ( this.name );
}
cZero_Window.prototype.modalHtmlUpdate = function(){
	if ( $('#'+this.prefix+'wnd').css('display') == 'block' ) {
		if ( this.modal ) {
			$('#'+this.prefix+'modal').css({'display':'block','opacity':'0.6','zIndex':this.owner.getZIndex()});
			this.onTop();
		} else {
			$('#'+this.prefix+'modal').css({'display':'none'});
		}
	} else {
		$('#'+this.prefix+'modal').css({'display':'none'});
	}
	return this;
}
cZero_Window.prototype.setModal = function( $modal ){
	if ( true == $modal )
		this.modal = true ;
	else 
		this.modal = false ;
	this.modalHtmlUpdate();
	return this;
}
cZero_Window.prototype.onClose = function( $action ){
	if ('function' == typeof ($action)) {
		$('#'+this.prefix+'aClose').unbind('click');
		$('#'+this.prefix+'aClose').bind('click',function(){$action();});
		return this;
	}
	if ('close' == $action) {
		$('#'+this.prefix+'aClose').unbind('click');
		$('#'+this.prefix+'aClose').bind('click',function(){
			aWnd.search($(this).attr('tag')).destroy();
		});
		return this;
	}
	if ('hide' == $action) {
		$('#'+this.prefix+'aClose').unbind('click');
		$('#'+this.prefix+'aClose').bind('click',function(){
			aWnd.search($(this).attr('tag')).hide();
		});
		return this;
	}
	return this;
}
cZero_Window.prototype.htmlIconUpdate = function(){
	if ( this.iconID > 0 ) {
		$('#'+this.prefix+'icon').html('<Img alt="" src="/zero/image/cwnd/icon' + this.iconID + '.png">');
	} else {
		$('#'+this.prefix+'icon').empty();
	}
	return this;
}
cZero_Window.prototype.icon = function ( $iconID ){
	this.iconID = parseInt($iconID);
	this.htmlIconUpdate() ;
	return this;
}
cZero_Window.prototype.htmlCloseUpdate = function(){
	if ( this.closeable ) {
		$('#'+this.prefix+'aClose').show();
	} else {
		$('#'+this.prefix+'aClose').hide();
	}
	return this;
}
cZero_Window.prototype.setCloseable = function($true){
	if ( $true )
		this.closeable = true ; else
		this.closeable = false ;
	this.htmlCloseUpdate();
	return this;
}
cZero_Window.prototype.getWidth = function(){
	return this.width ;
}
cZero_Window.prototype.setWidth = function($width){
	this.width = parseInt($width) ;
	$('#'+this.prefix+'wnd').css({'width':$width+'px'});
	$('#'+this.prefix+'bTop').css({'width':$width+'px'});
	$('#'+this.prefix+'bRight').css({'left':($width-3)+'px'});
	$('#'+this.prefix+'bBottom').css({'width':$width+'px'});
	$('#'+this.prefix+'top').css({'width':($width-6)+'px'});
	$('#'+this.prefix+'client').css({'width':($width-6)+'px'});
	$('#'+this.prefix+'caption').css({'width':($width-85)+'px'});
	return this;
}
cZero_Window.prototype.getHeight = function(){
	return this.height ;
}
cZero_Window.prototype.setHeight = function($height){
	this.height = parseInt($height) ;
	$('#'+this.prefix+'wnd').css({'height':$height+'px'});
	$('#'+this.prefix+'bLeft').css({'height':($height-6)+'px'});
	$('#'+this.prefix+'bRight').css({'height':($height-6)+'px'});
	$('#'+this.prefix+'bBottom').css({'top':($height-3)+'px'});
	$('#'+this.prefix+'client').css({'height':($height-46)+'px'});
	return this;
}
cZero_Window.prototype.getCaption = function(){
	return this.caption;
}
cZero_Window.prototype.getLeft = function(){
	return this.left ;
}
cZero_Window.prototype.setLeft = function($left){
	this.left = parseInt($left) ;
	$('#'+this.prefix+'wnd').css({'left':$left+'px'});
	return this;
}
cZero_Window.prototype.moveLeft = function($delta){
	this.setLeft( this.left + parseInt($delta) ) ;
	return this;
}
cZero_Window.prototype.getTop = function(){
	return this.top ;
}
cZero_Window.prototype.setTop = function($top){
	this.top = parseInt($top) ;
	$('#'+this.prefix+'wnd').css({'top':$top+'px'});
	return this;
}
cZero_Window.prototype.moveTop = function($delta){
	this.setTop( this.top + parseInt($delta) ) ;
	return this;
}
cZero_Window.prototype.setXYWH = function($x,$y,$w,$h){
	this.setLeft($x);
	this.setTop($y);
	this.setWidth($w);
	this.setHeight($h);
	return this;
}
cZero_Window.prototype.setCaption = function($caption){
	this.caption = $caption;
	$('#'+this.prefix+'caption').html($caption);
	return this;
}
cZero_Window.prototype.show = function(){
	$('#'+this.prefix+'wnd').css({'display':'block'});
	this.modalHtmlUpdate();
	return this;
}
cZero_Window.prototype.hide = function(){
	$('#'+this.prefix+'wnd').css({'display':'none'});
	this.modalHtmlUpdate();
	return this;
}
cZero_Window.prototype.onTop = function(){
	$('#'+this.prefix+'wnd').css({'zIndex':this.owner.getZIndex()});
	return this;
}
cZero_Window.prototype.fullScreen = function(){
	this.setLeft(0);
	this.setTop(0);
	this.setWidth($(window).width());
	this.setHeight($(window).height());
	return this;
}
cZero_Window.prototype.halfScreen = function(){
	this.setWidth(parseInt($(window).width()/2));
	this.setHeight(parseInt($(window).height()/2));
	this.centerScreen();
	return this;
}
cZero_Window.prototype.centerScreen = function(){
	var cx = parseInt(($(window).width()-this.width)/2);
	var cy = parseInt(($(window).height()-this.height)/2);
	this.setLeft(cx).setTop(cy);
	return this;
}
cZero_Window.prototype.leftScreen = function(){ //прижать окно к левой границе 
	return this.setLeft(1);
}
cZero_Window.prototype.rightScreen = function(){ //прижать окно к правой границе
	return this.setLeft( $(window).width() - this.width - 1 );
}
cZero_Window.prototype.topScreen = function(){ //прижать окно к верхней границе
	return this.setTop(1);
}
cZero_Window.prototype.bottomScreen = function(){ //прижать окно к нижней границе
	return this.setTop( $(window).height() - this.height - 1 );
}
cZero_Window.prototype.content = function( $data ){
	$('#'+this.prefix+'client').html( $data );
	return this;
}
cZero_Window.prototype.reLocate = function(){
	nx = this.left ;
	if ( this.left + this.width > $(window).width() )
		nx = $(window).width() - this.width - 1 ;
	if ( nx < 1 )
		nx = 1 ;
	ny = this.top ;
	if ( this.top + this.height > $(window).height() )
		ny = $(window).height() - this.height - 1 ;
	if ( ny < 1 )
		ny = 1 ;
	if ( nx != this.left )
		this.setLeft(nx);
	if ( ny != this.top )
		this.setTop(ny);
	return this;
}
/**
 * Handlers
 */
$(document).ready(function(){
	$('.cZero_Window__wnd').live('click',function(){
		var wtmp = aWnd.search($(this).attr('tag'));
        if ( wtmp != undefined ) wtmp.onTop();
	});
	$('.cZero_Window__top').live('mousedown',function(e){
		aWnd.mouseSel = $(this).attr('tag');
		aWnd.mouseX = e.clientX ;
		aWnd.mouseY = e.clientY ;
		var $o = aWnd.search(aWnd.mouseSel);
		if ( false != $o ) {
			$o.onTop();
			aWnd.mouseSaveX = $o.getLeft() ;
			aWnd.mouseSaveY = $o.getTop() ;
		} else {
			aWnd.mouseSel = '' ;
		}
	});
	$('BODY').bind('mouseup',function(){
		//alert(aWnd.mouseSel);
		aWnd.mouseSel = '' ;
		aWnd.reLocate() ;
	});
	$('BODY').bind('mousemove',function(e){
		if ( aWnd.mouseSel != '' ) {
			var $o = aWnd.search(aWnd.mouseSel);
			if ( false != $o ) {
				var coox = aWnd.mouseSaveX + e.clientX - aWnd.mouseX ;
				var cooy = aWnd.mouseSaveY + e.clientY - aWnd.mouseY ;
				$o.setLeft(coox).setTop(cooy);				
			}
		}
	});
});
/**
 * Client side
 * json[0] = 'awnd'|'aWnd'
 * 		json[1] = 'create' (or 'update')
 * 			json[2] => name
 * 			json[3] => caption
 * 			json[4] => content (html)
 * 			json[5] => params "(key:value),...,(key:value)"
 * 		json[1] = 'content'
 * 			json[2] => name 
 * 			json[3] => content
 * 			json[4] => [params]
 * 		json[1] = 'cmd'
 * 			json[2] => name
 * 			json[3] => params
 */
function aWndParser ( $json ) {
	function aWndExecute ( $w , cmdA , cmdB ) {
		if ( ( cmdA == 'w' ) || ( cmdA == 'width' ) ) { $w.setWidth( cmdB ) ; return true ; }
		if ( ( cmdA == 'h' ) || ( cmdA == 'height' ) ) { $w.setHeight( cmdB ) ; return true ; }
		if ( ( cmdA == 'l' ) || ( cmdA == 'left' ) ) { $w.setLeft( cmdB ) ; return true ; }
		if ( ( cmdA == 't' ) || ( cmdA == 'top' ) ) { $w.setTop( cmdB ) ; return true ; }
		if ( ( cmdA == 'v' ) || ( cmdA == 'visible' ) ) {
			if ( ( cmdB == '1' ) || ( cmdB == 'true' ) )
				$w.show();
			if ( ( cmdB == '0' ) || ( cmdB == 'false' ) )
				$w.hide();
			return true ;
		}
		if ( ( cmdA == 'm' ) || ( cmdA == 'modal' ) ) {
			if ( ( cmdB == '1' ) || ( cmdB == 'true' ) )
				$w.setModal(true);
			if ( ( cmdB == '0' ) || ( cmdB == 'false' ) )
				$w.setModal(false);
			return true ;
		}
		if ( ( cmdA == 'c' ) || ( cmdA == 'caption' ) ) { $w.setCaption ( cmdB ); return true ; }
		if ( ( cmdA == 'i' ) || ( cmdA == 'icon' ) ) { $w.icon( cmdB ) ; return true ; }
		if ( ( cmdA == 'p' ) || ( cmdA == 'pos' ) || ( cmdA == 'position' ) ) {
			if ( ( cmdB == 'f' ) || ( cmdB == 'full' ) )
				$w.fullScreen(); 
			if ( ( cmdB == 'c' ) || ( cmdB == 'center' ) )
				$w.centerScreen(); 
			if ( ( cmdB == 'h' ) || ( cmdB == 'half' ) )
				$w.halfScreen(); 
			return true ; 
		}
		if ( ( cmdA == 'd' ) || ( cmdA == 'die' ) || ( cmdA == 'destroy' ) || ( cmdA == 'close' ) ) { 
			if ( cmdB == 'close' )
				$w.onClose('close');
			if ( cmdB == 'hide' )
				$w.onClose('hide');
			return true ; 
		}
		return false ;
	}
	function aWndExecuteLn ( $w , ln ) {
		var cmdLine = new String(ln+'|');
		while(true){
			iCmdLine = cmdLine.indexOf('|');
			if ( ( iCmdLine < 0 ) || ( cmdLine.length < 3 ) )
				break ;
			cmd = cmdLine.substring(0,iCmdLine);
			cmdLine = cmdLine.substring(iCmdLine+1);
			if ( cmd.length < 3 )
				continue ;
			iCmd = cmd.indexOf(':');
			if ( iCmd <= 0 )
				continue ;
			cmdA = cmd.substring(0,iCmd);
			cmdB = cmd.substring(iCmd+1);
			aWndExecute( $w , cmdA , cmdB );
		}		
	}
	if ( $json.length < 2 )
		return false ;
	if ( ( $json[0] != 'awnd' ) && ( $json[0] != 'aWnd' ) )
		return false ;
	if ( ( $json[1] == 'create' ) || ( $json[1] == 'update' ) ) {
		var $w = aWnd.create( $json[2] ).setCaption( $json[3] ).content( $json[4] ) ;
		aWndExecuteLn( $w , $json[5] ) ;
	}
	if ( $json[1] == 'content' ) {
		var $w = aWnd.search( $json[2] );
		if ( false != $w ) {
			$w.content( $json[3] );
			if ( $json.length > 4 )
				aWndExecuteLn( $w , $json[4] ) ;
		}
	}
	if ( $json[1] == 'cmd' ) {
		var $w = aWnd.search( $json[2] );
		aWndExecuteLn( $w , $json[3] ) ;
	}
}
/**
 * master object
 */
aWnd = new cZero_Windows('aWnd');
