/*
вкратце: для того, чтоб показывать окно по клику на какой-то ссылке, нужно:
1. кинуть на страницу div контейнер с содержимым окна... <div
id="my-window-id">[ html содержимое блока - будущего окна ] </div>
2. на этой же странице должен быть размещен элемент: <a
id="show-window-link-id" href="#my-window-id">click here</a> ...важно!
в качестве параметра у атрибута href должно быть значение айдишки того
окна, к которому привязываем ссылку... в нашем примере:
href="#my-window-id"
3. далее, на странице размещаем сам код привязки ссылки к окну:

<script>
$().ready(function(){
   $('#my-window-id').window_black({ w:500, l:'#show-window-link-id' });
});
</script>

где, w:500 <<< ширина нашего окна... в пикселях
l: '#show-window-link-id' << id-шка тэга <a> к которому onclick будет привязана отображение окна

 Dependencies: 
 	jQuery Lib - http://jquery.com/
*/
(function($) {
	
	$.window_black = function (data) {
		return $.window_black.init(data);
	};
	
	$.fn.window_black = function (options) {
		return $.window_black.init(this, options);
	};
	
	$.window_black = {
		defaults: {
			appendTo: 'body',
			w: 0,
			l: '',
			overlayId: 'window-overlay',
			overlayCss: {},
			zIndex: 1000
		},
		container : {},
		overlay : {},
		//link : {},
		init: function (data, options) {
			var s = this;

			// merge defaults and user options
			s.o = $.extend({}, $.window_black.defaults, options);

			// convert DOM object to a jQuery object
			//s.container = data instanceof jQuery ? data : $(data);
			s.container = data instanceof jQuery ? data : $(data);
			s.container.hide();

			if(s.o.l != '')
			{
				// show window-black
				$(s.o.l).click(function(){
					var l = $(this);
					$(l.attr('href')).show();
					s.overlay.show();
					return false;
				})
			}
				//.css('position','fixed')
			if(s.o.w > 0) s.container.width(s.o.w);
			
			if( ! s.container.hasClass('window-black') )
			{
				s.container
					.addClass('window-black')
					.wrapInner('<div class="wr" />')
					.wrapInner('<div class="content" />');

				$('.content', s.container)
					.before('<div class="top"><div class="l"></div><div class="r"></div></div>')
					.after('<div class="bottom"><div class="l"></div><div class="r"></div></div>')
					.prepend('<a class="close s-icon icon-close-white" href="#'+s.container.attr('id')+'" title="Закрыть"></a>');
				
				// close button
				$('.close', s.container).click(function(){
					var l = $(this);
					$(l.attr('href')).hide();
					$('#window-overlay').hide();
					return false;
				});
			}

			s.setPosition();
			
			if($('#window-overlay').length == 0)
			{
				
				// create the overlay
				s.overlay = $('<div></div>')
					.attr('id', s.o.overlayId)
					.addClass('window-overlay')
					.css($.extend(s.o.overlayCss, {
						display: 'none',
						height: w[0],
						width: w[1],
						position: 'fixed',
						left: 0,
						top: 0,
						zIndex: s.o.zIndex + 1
					}))
					.appendTo(s.o.appendTo);

			}

			s.bindEvents();
			
			return s;
		},
		/* 
			setPosition
		*/
		setPosition: function (data) {
			var s = this;

			w = s.getDimensions();

			var top = (w[0]/2) - (s.container.outerHeight(true)/2),
				left = (w[1]/2) - (s.container.outerWidth(true)/2);

			s.container.css({ left: left, top: top });
		},
		/* getDimensions */
		getDimensions: function () {
			var el = $(window);

			// fix a jQuery/Opera bug with determining the window height
			var h = $.browser.opera && $.browser.version > '9.5' && $.fn.jquery <= '1.2.6' ? document.documentElement['clientHeight'] :
				$.browser.opera && $.browser.version < '9.5' && $.fn.jquery > '1.2.6' ? window.innerHeight :
				el.height();

			return [h, el.width()];
		},
		/*
		 * Bind events
		 */
		bindEvents: function () {
			var s = this;
			
			// update window size
			$(window).bind('resize.simplemodal', function () {
				// redetermine the window width/height
				//w = s.getDimensions();
				// reposition the dialog
				s.setPosition();

				s.overlay.css({height: w[0], width: w[1]});
			});
		},
		/*
		 * Unbind events
		 */
		unbindEvents: function () {
			$(window).unbind('resize.simplemodal');
		},
		open: function () {
			var s = this;

			s.overlay.show();
			s.container.show();

			// bind default events
			s.bindEvents();
		},
		close: function () {
			var s = this;
			
			// remove the default events
			s.unbindEvents();
			
			s.container.hide();
			s.overlay.hide();
		}
	};
	/*
	$.fn.extend({
		window_black : function(){
			// default values
			var s = this;
						
			s.init();
			
		},
		init: function() {
			var s = this;
			if( ! s.hasClass('window-black') )
			{
				s.addClass('window-black')
				c = s.wrapInner('<div class="content" />');
				c.before('<div class="top"><div class="l"></div><div class="r"></div></div>')
				.after('<div class="bottom"><div class="l"></div><div class="r"></div></div>')
				.prepend('<a class="news-close s-icon icon-close-white" href="#" title="Закрыть форму"></a>');

				alert('Here');
			}
		}
	})
	*/
})(jQuery);
