/* Автор: Бурый Дмитрий Николаевич */
/*
<script type="text/javascript">

	var win = Wins.create().show({
		label:'Заголовок другой 2',
		content:'Добро пожаловать на информационный портал «Я-риэлтор». Это не просто доска объявлений о недвижимости, постоянно развивающаяся база данных, своеобразная социальная сеть для людей, заинтересованных в покупке, продаже или аренде жилья. Здесь представлена самая разнообразная недвижимость южной столицы.',
		styles:{width:500}
	});

</script>
*/
window.Wins = {
	
	root: '/resources/wins',
	
	/**
	 * 
	 * @param label
	 * @param html
	 * @param options
	 * @returns handler
	 */
	create:function(label,html,options){
	
		var tr01 = new Element('tr');
		var tr02 = new Element('tr');
		var tr03 = new Element('tr');
		var options = options ? options : {};
		var onShow = options.onShow ? options.onShow : function(){}; 
		
		var btns = [
		   new Element('a',{'href':'#',events:{'click':function(){ 
			   if (options.onClose){
				   options.onClose(this);
			   }
			   else {
				   $(this).getParent('table[class="wWindow"]').close();
			   }
			   return false; 
		   }}})
		   .adopt(new Element('img',{'src':'/resources/wins/img/btn-close.png'}))
		];
		
		(new Element('td',{'class':'lt','html':'&nbsp;'})).inject(tr01);
		((new Element('td',{'class':'l'})).adopt(
			[
			 	new Element('span',{'id':'wLabel','html':'Заголовок окна'}),
			 	(new Element('div',{'class':'btns'})).adopt(btns)
			]
		)).inject(tr01);
		
		(new Element('td',{'class':'rt','html':'&nbsp;'})).inject(tr01);
		(new Element('td',{'class':'lm','html':'&nbsp;'})).inject(tr02);
		(new Element('td',{'class':'cntnt', 'id':'wContent'})).inject(tr02);
		(new Element('td',{'class':'rm','html':'&nbsp;'})).inject(tr02);
		(new Element('td',{'class':'lb','html':'&nbsp;'})).inject(tr03);
		(new Element('td',{'class':'b'})).inject(tr03);
		(new Element('td',{'class':'rb','html':'&nbsp;'})).inject(tr03);
		
		var win = (new Element('table', {'class':'wWindow', 'cellpadding':0, 'cellspacing':0}))
		.adopt([tr01, tr02, tr03]);
		
		if (options.styles) win.setStyles(options.styles);
		
		$extend(win,{
			setLabel:function(label){
				$(this).getElements('span[id="wLabel"]').set('html',label);
				return this;
			},
			setContent:function(html){
				$(this).getElements('td[id="wContent"]').set('html',html);
				return this;
			},
			hide:function(){
				$(this).setStyle('display','none');
				return this;
			},
			close:function(){
				$(this).destroy();
			},
			show:function(options){
				options = options ? options : {};
				
				if (options.label) this.setLabel(options.label);
				if (options.content) this.setContent(options.content);
				if (options.styles) this.setStyles(options.styles);
				
				var fixedPosition = options.fixed ? options.fixed : false;
				
				var w = $(this).getElements('td[id="wContent"]');
								
				$(this).setStyles({ display:'block' });
				
				var scrollTop = self.pageYOffset || (document.documentElement && document.documentElement.scrollTop) || (document.body && document.body.scrollTop);
				
				if (fixedPosition){
					scrollTop = 0;
					$(this).setStyles({ position:'fixed' });
				}
				
				var top = (window.getHeight()-this.offsetHeight)/2+scrollTop;
				var left = (window.getWidth()-this.offsetWidth)/2;
				
				$(this).setStyles({ left:left, top:top });
				onShow(this);
				
				return this;
			}
		});
		
		if (html) win.setContent(html);
		if (label) win.setLabel(label);
		
		$(document.body).adopt(win);
		return win;
	}
};

document.write("<style>");
document.write(".wWindow {z-index:10000; position:absolute; left:0; top:0px; display:none; font:12px 'Tahoma';}");
document.write(".wWindow .lt {height:41px; width:21px; background:url('"+Wins.root+"/img/wins_03.png');}");
document.write(".wWindow .rt {height:41px; width:21px; background:url('"+Wins.root+"/img/wins_06.png');}");
document.write(".wWindow .lm {background:url('"+Wins.root+"/img/wins_10.png');}");
document.write(".wWindow .rm {background:url('"+Wins.root+"/img/wins_12.png');}");
document.write(".wWindow .lb {height:20px; width:21px; background:url('"+Wins.root+"/img/wins_13.png');}");
document.write(".wWindow .rb {height:20px; width:21px; background:url('"+Wins.root+"/img/wins_15.png');}");
document.write(".wWindow .b {background:url('"+Wins.root+"/img/wins_14.png');}");
document.write(".wWindow .l {background:url('"+Wins.root+"/img/wins_04.png')}");
document.write(".wWindow .cntnt {background-color:#dadada;}");
document.write(".wWindow .btns {float:right; position:relative; top:-2px; left:8px;}");
document.write("</style>");
