/* ---------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------- */

Window = function( options ) {
	Window.superClass.apply( this, arguments );
	this.element = this.createNode( 'div', document.body, { 'class': 'window-wrap' });

	this.getSelects();
	//this.selectsOnPage = document.getElementsByTagName( 'SELECT' );
	//this.objectOnPage = document.getElementsByTagName( 'OBJECT' );
	this.buttons = new Array();
	
	if( options ) {                             
		if( options[ 'block' ] ) {
			this.blocked = true;
		}

		if( options[ 'hidden' ] ) {
			this.hide();
		}
	}

	this.createNode( 'div', this.element, { 'class': 'head-l png' });
	this.createNode( 'div', this.element, { 'class': 'head-r png' });

	this.head = this.createNode( 'div', this.element, { 'class': 'head-c' });
	this.title = this.createNode( 'span', this.head, { 'class': 'win-title' });

	var wrap = this.createNode( 'div', this.element, { 'class': 'body-l png iesizing-scale' });
	this.bodyWrap = this.createNode( 'div', wrap, { 'class': 'body-r' });
	this.body = this.createNode( 'div', this.bodyWrap, { 'class': 'body' });

	this.createNode( 'div', this.element, { 'class': 'bot-l png' });
	this.createNode( 'div', this.element, { 'class': 'bot-r png' });

	var bottomWrap = this.createNode( 'div', this.element, { 'class': 'bot-c png iesizing-scale' });

	this.close = false;
	this.closable = false;
	this.params = {
		'width': 0,
		'height': 0,
		'x': 0,
		'y': 0
	};

	if( options ) {
		if( options[ 'title' ] != undefined ) {
			this.setTitle( options[ 'title' ] );
		}

		if( options[ 'content' ] != undefined ) {
			this.fill( options[ 'content' ] );
		}

		if( options[ 'closable' ] ) {
			this.setClosable( true );
		}

		if(
			options[ 'top' ] != undefined ||
			options[ 'left' ] != undefined 
		) {
			this.setPosition( options[ 'left' ], options[ 'top' ] );
		}

		if(
			options[ 'width' ] != undefined ||
			options[ 'height' ] != undefined 
		) {
			this.setSize( options[ 'width' ], options[ 'height' ] );
		}
		
		if( options[ 'center' ] ) {
			this.centered = true;
			this.addHandler( window, 'resize', this.center );
			this.center();
		}
	}
}
Window.inheritsFrom( Glyph );

/* ---------------------------------------------------------------------------------------------- */

Window.prototype.setTitle = function( title ) {
	if( this.title ) {
		this.title.innerHTML = title || '&nbsp;';
	}
	this.params = this.offset();
	return this;
}

/* ---------------------------------------------------------------------------------------------- */

Window.prototype.show = function() {
	if( this.blocked ) {
		this.block( true );
	}
	this.setStyle({ 'display': 'block' });
	this.params = this.offset();
	
	if( this.centered ) {
		this.center();
	}
	
	if( /MSIE (5\.5|6).+Win/.test( navigator.userAgent ) ) {
		this.setSelects( false );
	}
	//this.setFlashElements( false );

	return this;
}

/* ---------------------------------------------------------------------------------------------- */

Window.prototype.hide = function() {
	this.setStyle({ 'display': 'none' });
	if( this.blocked ) {
		this.block( false );
	}
	
	if( /MSIE (5\.5|6).+Win/.test( navigator.userAgent ) ) {
		this.setSelects( true );
	}

	this.notify( 'Hide' );
	return this;
}

/* ---------------------------------------------------------------------------------------------- */

Window.prototype.fill = function( content ) {
	this.setContent( content, this.body );
	this.params = this.offset();
	if( this.centered ) {
		this.center();
	}
	return this;
}

/* ---------------------------------------------------------------------------------------------- */

Window.prototype.setSize = function( width, height ) {
	if(
		width &&
		!isNaN( Number( width ) ) &&
		Number( width ) > 0
	) {
		if( Number( width ) < 254 ) width = 254;
		this.setStyle({ 'width': Number( width ) + 'px' });
		this.params[ 'width' ] = Number( width );
	}

	if(
		height &&
		!isNaN( Number( height ) ) &&
		Number( height )
	) {
		var topDelta = this.offset()[ 'height' ] - this.offset( this.body )[ 'height' ] + 32;
		this.setStyle({ 'height': Number( height ) - topDelta + 'px' }, this.body );
		this.params[ 'height' ] = Number( height );
	}
	return this;
}

/* ---------------------------------------------------------------------------------------------- */

Window.prototype.getParam = function( key ) {
	if( key ) {
		return this.params[ key ] || false;
	} else {
		return this.params;
	}
}

/* ---------------------------------------------------------------------------------------------- */

Window.prototype.setPosition = function( x, y ) {
	if(
		x &&
		!isNaN( Number( x ) )
	) this.setStyle({ 'left': Number( x ) + 'px' });
	
	if(
		y &&
		!isNaN( Number( y ) )
	) this.setStyle({ 'top': Number( y ) + 'px' });
	return this;
}

/* ---------------------------------------------------------------------------------------------- */

Window.prototype.setClosable = function( flag ) {
	this.closable = Boolean( flag );
	if( flag ) {
		if( this.close ) return;
		this.close = this.createNode( 'div', this.head, { 'class': 'but-close' });
		this.addHandler( this.close, 'click', this.hide );
	} else {
		if( !this.close ) return;
		this.removeHandler( this.close, 'click', this.hide );
		this.head.removeChild( this.close );
		this.close = false;
	}
	this.setClassName( 'closable', flag );
	return this;
}

/* ---------------------------------------------------------------------------------------------- */

Window.prototype.center = function() {
	this.params = this.offset();
	
	var availWidth = ( document.documentElement || document.body ).clientWidth;
	var availHeight = ( document.documentElement || document.body ).clientHeight;
	
	var x = ( availWidth - this.getParam( 'width' ) ) / 2;
	var y = ( availHeight - this.getParam( 'height' ) ) / 2;

	this.setPosition( x, y );
	return this;
}

/* ---------------------------------------------------------------------------------------------- */

Window.prototype.block = function( flag ) {
	if( flag ) {
		if( this.blocker ) return false;
		this.blocker = this.createNode( 'div', document.body, { 'class': 'win-block png iesizing-scale' });
	} else {
		if(
			this.blocker &&
			this.blocker.parentNode
		) {
			this.blocker.parentNode.removeChild( this.blocker );
			this.blocker = false;
		}
	}
	return this;
}

/* ---------------------------------------------------------------------------------------------- */

Window.prototype.setFlashElements = function( flag ) {
	for( var i = this.objectOnPage.length; i; i-- ) {
		var obj = this.objectOnPage[ i - 1 ];
		if(
			obj &&
			obj.style
		) {
			this.setStyle({
				'visibility': flag ?  'visible' : 'hidden'
			}, obj );
		}
	}
}

/* ---------------------------------------------------------------------------------------------- */

Window.prototype.getSelects = function() {
	var selectsOnPage = document.getElementsByTagName( 'SELECT' );
	this.selectsOnPage = [];

	for( var i = selectsOnPage.length; i; i-- ) {
		var select = selectsOnPage[ i - 1 ];
		if( !this.isParentOf(select) )
			this.selectsOnPage[ i - 1 ] = select;
	}
	
}

/* ---------------------------------------------------------------------------------------------- */

Window.prototype.setSelects = function( flag ) {
	this.getSelects();
	for( var i = this.selectsOnPage.length; i; i-- ) {
		var select = this.selectsOnPage[ i - 1 ];
		if(
			select &&
			select.style
		) {
			this.setStyle({
				'visibility': flag ?  'visible' : 'hidden'
			}, select );
		}
	}
}

/* ---------------------------------------------------------------------------------------------- */

Window.prototype.addButton = function( value, id, handler ) {
	if( !this.buttonsWrap ) {
		this.buttonsWrap = this.createNode( 'div', this.bodyWrap, { 'class': 'buttons' });
	}

	if( this.getEl( id ) )
	    return false;

	var button = new Button( id, this.buttonsWrap, value );
	if(
		handler &&
		handler instanceof Function
	) {
		button.attachObserver( 'Clicked', handler, this );
	}

	this.buttons[ button.getId() ] = {
		'button': button,
		'handler': handler
	}

	return this;
}

/* ---------------------------------------------------------------------------------------------- */

Window.prototype.removeButtons = function() {
	for( var c in this.buttons ) {
		this.removeButton( c );
	}
}

/* ---------------------------------------------------------------------------------------------- */

Window.prototype.removeButton = function( id ) {
	var button = this.getButton( id );
	var handler = this.getButtonHandler( id );
	if(
		button &&
		button.remove
	) {
		if( !button.remove() ) {
			return false;
		};

		if(
			handler &&
			handler instanceof Function
		) {
			button.detachObserver( 'Clicked', handler, this );
		}

		delete this.buttons[ id ];
	}
	return false;
}

/* ---------------------------------------------------------------------------------------------- */

Window.prototype.getButton = function( id ) {
	if(
		id &&
		this.buttons &&
		this.buttons[ id ] &&
		this.buttons[ id ][ 'button' ]
	) {
		return this.buttons[ id ][ 'button' ]
	}
	return false;
}

/* ---------------------------------------------------------------------------------------------- */

Window.prototype.getButtonHandler = function( id ) {
	if(
		id &&
		this.buttons &&
		this.buttons[ id ] &&
		this.buttons[ id ][ 'handler' ]
	) {
		return this.buttons[ id ][ 'handler' ]
	}
	return false;
}

/* ---------------------------------------------------------------------------------------------- */

Window.prototype.showButtons = function( flag ) {
	if( this.buttonsWrap ) {
		this.setClassName( 'hidden', !flag, this.buttonsWrap );
	}
}

/* ---------------------------------------------------------------------------------------------- */

Window.prototype.remove = function() {
	if( this.centered ) {
		this.removeHandler( window, 'resize', this.center );
	}

	if( this.closable ) {
		this.setClosable( false );
	}

	this.params = null;

	if( this.blocked ) {
		this.block( false );
	}

	for( var c in this.buttons ) {
		this.removeButton( c );
	}
	
	this.body = null;
	this.bodyWrap = null;
	this.title = null;
	this.head = null;
	
	this.buttons = null;
	this.objectOnPage = null;
	this.selectsOnPage = null;
	
	this.element.parentNode.removeChild( this.element );
	this.element = null;
}


Window.prototype.addButtonBefore = function( before, value, id, handler ) {
    this.addButton( value, id, handler );
    this.buttonsWrap.insertBefore( this.getButton(id).element, before );
}

/* ------------------------------------ WindowFormConfirm ------------------------------------- */

WindowFormConfirm = function( options ) {
	WindowFormConfirm.superClass.apply( this, arguments );
	this.form = new Form( options[ 'form' ] );

	var _self = this;
	if( this.form[ 'data' ][ 'handler' ] ) {
		this.form.setHandler( function() {
			this[ 'data' ][ 'handler' ].call( _self, this );
		});
	}

	this.output = this.createNode( 'div', this.body, { 'class': 'output hidden' });
	this.setSendButton( true, options.button_id );
	
	this.addHandler( window, 'resize', this.reBuild );
}
WindowFormConfirm.inheritsFrom( Window );

/* ---------------------------------------------------------------------------------------------- */

WindowFormConfirm.prototype.confirm = function() {
	if( !this.form ) return false;
	if( this.form.check() ) {
		this.setLoad( true );
		this.form.onSubmit();
	}
}

/* ---------------------------------------------------------------------------------------------- */

WindowFormConfirm.prototype.setOutput = function( value ) {
	this.showButtons( false );

	this.setClassName( 'hidden', false, this.output );
	this.form.setClassName( 'hidden', true );

	this.setContent( value, this.output );
	this.center();
}

/* ---------------------------------------------------------------------------------------------- */

WindowFormConfirm.prototype.setLoad = function( flag ) {
	this.showButtons( !flag );

	if( flag ) {
		this.setOutput( '<img width="16" height="16px" src="/static/img/ajax-loader.gif" />&nbsp;' + _text( 'LOADING' ) );
	} else {
		this.setClassName( 'hidden', true, this.output );
	}
	this.form.setClassName( 'hidden', flag );
	this.center();
}

/* ---------------------------------------------------------------------------------------------- */

WindowFormConfirm.prototype.hide = function() {
	this.setStyle({ 'display': 'none' });
	if( this.blocked ) {
		this.block( false );
	}

	if( /MSIE (5\.5|6).+Win/.test( navigator.userAgent ) ) {
		this.setSelects( true );
	}

	this.clear();
	this.notify( 'Hide' );
	return this;
}

/* ---------------------------------------------------------------------------------------------- */

WindowFormConfirm.prototype.setSendButton = function( flag, button_id ) {
	if( flag ) {
		if( !this.getButton( 'send' ) ) {
			this.addButton( _text( 'WINDOW_SEND_BUTTON' ), ( button_id || 'send' ), this.confirm );
		}
	} else {
		this.removeButton( 'send' );
	}
}

/* ---------------------------------------------------------------------------------------------- */

WindowFormConfirm.prototype.clear = function() {
	if( !this.form ) return false;
	this.form.setClassName( 'hidden', false );
	this.setClassName( 'hidden', true, this.output );
	for( var c in this.form.inputs ) {
		var input = this.form.getInput( c );
		if(
			input &&
			input.removeAlert
		) {
			input.setValue( '' );
			input.removeAlert();
		}
	}

	this.showButtons( true );
	this.center();
}

/* ---------------------------------------------------------------------------------------------- */

WindowFormConfirm.prototype.reBuild = function() {
	if(
		this.form &&
		this.form instanceof Form
	) {
		this.form.reBuild();
	}
}

/* ---------------------------------------------------------------------------------------------- */

WindowFormConfirm.prototype.remove = function() {
	WindowFormConfirm.superClass.prototype.remove.call( this );
	this.removeHandler( window, 'resize', this.reBuild );
}

/* ---------------------------------------------------------------------------------------------- */

WindowFormConfirm.prototype.getValues = function( ) {
	var result = new Object();
	if(
		this.form &&
		this.form instanceof Form
	) {
		result = this.form.getValues();
	}
	return result;
}

/* ---------------------------------------------------------------------------------------------- */

Button = function( id, target, value ) {
	Button.superClass.apply( this, arguments );
	this.id = ( id || 'button-' + parseInt( Math.random() * 10000 ) );
	this.element = this.createNode( 'button', ( target || false ), { 'id': this.id, 'class': 'btn gray' });
	var innerWrap = this.createNode( 'span', this.element, { 'class': 'l' });
	this.inner = this.createNode( 'span', innerWrap, { 'class': 'r' });
	
	if( value ) {
		this.setValue( value );
	}
	this.addHandler( this.element, 'click', this.onClick );
}
Button.inheritsFrom( Glyph );

/* ---------------------------------------------------------------------------------------------- */

Button.prototype.remove = function() {
	this.removeHandler( this.element, 'click', this.onClick );
	this.element.parentNode.removeChild( this.element );
	return true;
}

/* ---------------------------------------------------------------------------------------------- */

Button.prototype.onClick = function() {
	this.notify( 'Clicked', this );
}

/* ---------------------------------------------------------------------------------------------- */

Button.prototype.setValue = function( value ) {
	this.setContent( ( value ? value + '' : '' ), this.inner );
}

/* ---------------------------------------------------------------------------------------------- */

Button.prototype.getValue = function() {
	if( this.inner ) {
		return this.inner.innerHTML;
	}
	return false;
}

/* ---------------------------------------------------------------------------------------------- */

Button.prototype.getId = function() {
	return this.id;
}

/* ---------------------------------------------------------------------------------------------- */

Button.prototype.disable = function( flag ) {
	this.srtClassName( 'disabled', flag );
	this.element.disabled = flag;
}

/* ---------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------- */

$(function(){
	PageStatus.PreloadImg([
		'/static/interface/img/window/head-l.png',
		'/static/interface/img/window/head-r.png',
		'/static/interface/img/window/head-bg.gif',
		'/static/interface/img/window/but-close.gif',
		'/static/interface/img/window/bg-l.png',
		'/static/interface/img/window/bg-r.gif',
		'/static/interface/img/window/bottom-l.png',
		'/static/interface/img/window/bottom-r.png',
		'/static/interface/img/window/bottom-bg.png',
		'/static/interface/img/grey-btn-l.png',
		'/static/interface/img/grey-btn-r.png',
		'/static/interface/img/ajax-loader.gif',
		'/static/interface/img/block.png'
	]);
});

