/*
// jQuery Toplayer
//
// Version 0.1 beta
//
// Michał Śliwka
// efan@o2.pl
//
//
// Usage: $('#control_id').uiTopLeyer( options, callback )
//
// is copyright 2008 Michał Śliwka 
//	
*/
jQuery(function($) {
	var overlay='';
	var content='';
	
	var jQueryMatchedObj = this;
	$.fn.uiTopLeyer=function(obj, settings) {

		
		settings = jQuery.extend({
			placeHolder: '#TopLayerContent',
			centerOnScreen: true,
			overlayClick: true,
			overlayBgColor: '#000',	
			overlayOpacity: 0.8
		},settings);

		var jQueryMatchedObj = this;
		var timeout;

		
		function _initialize() {
			_set_interface();
		}
	

		function _set_interface() {

			$('body').append('<div id="TopLayerOverlay"></div><div id="TopLayerContent"></div>');	
			var startwidth=$(obj).find('div:first').width();
			overlay=$('#TopLayerOverlay');
			content=$('#TopLayerContent');
			$(content).html($(obj).html());
			$(obj).html('');
			


			$(overlay).css({
				position:			'absolute',
				backgroundColor:	settings.overlayBgColor,
				opacity:			settings.overlayOpacity,
				top:				0,
				left:				0,
				height:				$(document).height(),
				width:				$(window).width(),
				zIndex:				98
			}).fadeIn();
			content.css({
				position:			'absolute',
				margin:				0, 
				top:				$(window).height()/2-$(content).height()/2+$(window).scrollTop(),
				left:				$(window).width()/2-startwidth/2,
				zIndex:				99
			});






			if(settings.overlayClick){
				$(overlay).click(function() {
					_hide();									
				});
			}

	

			$(window).resize(function() {
				$.fn._center();

				_maximizeOverlay()
			});
			$(window).scroll(function() {
				jQueryMatchedObj = $.fn.uiTopLeyer;
				window.clearTimeout(timeout);
				timeout=window.setTimeout("$.fn._center()",20);
			});
			
			$(content).show();
			_bind();
			//$.fn._center();
			_maximizeOverlay();
		}


		function _hide() {
			$(content).fadeOut().removeClass('loading');
			$(overlay).fadeOut("slow").removeClass('loading');
		}
		
		function Show() {
			$(overlay).height($(document).height()).width($(document).width()).css('opacity', 0).fadeTo(settings.overlayOpacity);
			$(content).fadeIn("slow").addClass('loading');
		}
		
		function _maximizeOverlay(){

				$(overlay).css({
					height:			$(document).height(),
					width:			$(window).width()
				});
		}




		function _ajax(href, data, method){
			$.ajax({
					type: method?method:'POST',
					url: href,
					success: _ajaxSuccess,
					error: _ajaxError,
					data: data
				});
			return false;
		}


		function _ajaxSuccess(data) {
			$(settings.placeHolder).html(data);
			_center();
			return false;
		}

		function _ajaxError(data) {
			alert('blad');
			return false;
		}

		function _bind(){
			$('.uiTopLeyerClose').bind('click',function(){
				_hide();
				return false;
			});

			$('.uiTopLeyerLink').bind('click',function(){
				href=$(this).attr('href');
				$(this).attr('href','#');

				_ajax(href, $(this).attr('rel'));

				return false;
			});

			/*$(content).find('form').submit(function(){
				_ajax($(this).attr('action'), $(this).serialize(), $(this).attr('method'));
				return false;
			});*/
		}

		_initialize();
	};

	$.fn._center=function(){
				content.stop(false,true);
				content.animate({
					margin:0, 
					top: ($(window).height()/2-$(content).height()/2+$(window).scrollTop())-80, 
					left: $(window).width()/2-$(content).width()/2
				}, 200, 'linear');
	};

});
