	/*
		
		JQuery windows
		
		Copyright (c) 2010 Alex Silaev (http://alex.twimp.ru/) chrome.alex@gmail.com
		Licensed under the GPL (http://www.opensource.org/licenses/gpl-license.php) license.
		
		Created at 8 Dec 2010
		
		=================================
		* Use:
		=================================
			var win = new SWindow({
							'id': STRING,				// your new window identificator
							'title': SELECTOR,			// put here the jquery selector
							'content': SELECTOR			// put here the jquery selector
						});
		
		=================================
		* Javascript control:
		=================================
			win.show( [ Object ] );				// Object like: Array ( selector : data , selector : data , ... )
			win.hide();
			win.draw();
	*/
	var SWindow = function(options) {
		
		var title = $(options['title']).html();
		var content = $(options['content']).html();
		var id = options['id'];
		
		this.id = id;
		if (!this.isCached(id)) {
			this.title = title;
			this.content = content;
			this.draw();
			$(options['title']).html('');
			$(options['content']).html('');
		}
		
	};
	SWindow.hide = function(winid) {
		
		SWindow.hideShadow();
		
		var options = {'mode':'hide', 'direction':'up'};
		$('#'+winid).effect("drop", options, 500);
		
	};
	SWindow.hideShadow = function() {
		
		$('#swindow_shadow').remove();
		
	};
	SWindow.cache = new Array();
	SWindow.prototype = {
		
		id: null,
		title: null,
		content: null,
		prefix: 'swin_',
		
		isCached: function(id) {
			
			for (var i = 0; i < SWindow.cache.length; i++) {
				if (SWindow.cache[i] == id) return true;
			}
			return false;
			
		},
		
		getClientHeight: function() {
			return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientHeight:document.body.clientHeight;
		},
		
		getBodyScrollTop: function() {
			return self.pageYOffset || (document.documentElement && document.documentElement.scrollTop) || (document.body && document.body.scrollTop);
		},
		
		draw: function() {
			
			SWindow.cache.push(this.id);
			$('#'+this.prefix+''+this.id).remove();
			
			var scrolltop = 0;
			
			if (this.title) {
				var winc = '\
					<div class="popup" id="'+this.prefix+''+this.id+'" style="display:none; left:340px; top:'+scrolltop+'px">\
						<a href="javascript:void(0);" onclick="SWindow.hide(\''+this.prefix+''+this.id+'\')" class="close"></a>\
						<table><tr><td class="lt"></td><td class="t"></td><td class="rt"></td></tr><tr><td class="l"></td><td class="c">\
						<div class="form_popup">\
							<div class="hd">'+this.title+'</div>\
							'+this.content+'\
						</div></td><td class="r"></td></tr><tr><td class="lb"></td><td class="b"></td><td class="rb"></td></tr></table>\
					</div>\
					';
			} else {
				var winc = '\
					<div class="popup" id="'+this.prefix+''+this.id+'" style="display:none; left:340px; top:'+scrolltop+'px">\
						<a href="javascript:void(0);" style="right:0px;top:-3px;" onclick="SWindow.hide(\''+this.prefix+''+this.id+'\')" class="close"></a>\
						<table><tr><td class="lt"></td><td class="t"></td><td class="rt"></td></tr><tr><td class="l"></td><td class="c">\
						'+this.content+'\
						</td><td class="r"></td></tr><tr><td class="lb"></td><td class="b"></td><td class="rb"></td></tr></table>\
					</div>\
					';
			}
			
			$(winc).appendTo('body');
			
			return true;
			
		},
		
		showShadow: function() {
			
			$('<div id="swindow_shadow" style="position:absolute;top:0px;left:0px;width:100%;height:100%;z-index:920;"></div>').appendTo('body');
			
		},
		
		hideShadow: function() {
			
			$('#swindow_shadow').remove();
			
		},
		
		show: function(fields) {
			
			var options = {'mode':'show', 'direction':'up'};
			
			this.showShadow();
			
			var val = parseInt(this.getBodyScrollTop()) + parseInt(this.getClientHeight() / 2 - $('#'+this.prefix+''+this.id).height() / 2) + 'px';
			$('#'+this.prefix+''+this.id)[0].style.top = val;
			
			$('#'+this.prefix+''+this.id).show();
			//$('#'+this.prefix+''+this.id).effect("drop", options, 500);
			
			if (typeof(fields) != 'undefined') {
				for (var field in fields) {
					$(field).val(fields[field]);
				}
			}
			
			return true;
			
		},
		
		hide: function() {
			
			this.hideShadow();
			
			var options = {'mode':'hide', 'direction':'up'};
			$('#'+this.prefix+''+this.id).effect("drop", options, 500);
			
			return true;
			
		}
		
	}
	

