
/**
 * jQuery lightText plugin
 * @name jquery.lighttext.js
 * @author Kazimierz Sajkowski (ksajkowski@gmail.com)
 * @version 0.2
 * @date 2010.02.10
 */

jQuery.prepareLightText = function() {
	if (jQuery("#cover_box").length==0) {
		jQuery("body").append('<div class="hidden close_ajax" id="cover_all"></div><div class="hidden" id="cover_box"><div class="close_ajax close_button"><div class="img"></div></div><div class="content loading"></div></div>');

		jQuery(".close_ajax").click(function() {
			jQuery("#cover_all").fadeTo("fast",1, function() {
				jQuery("#cover_all").fadeOut("fast");
			});
			jQuery("#cover_box").fadeOut("fast");
			jQuery("body").removeClass("nooverflow");
			jQuery("#cover_box div.content").addClass("loading");
		});
	}
	
	var height = $("body").height();

	if (document.body.clientHeight>height) {
		height = document.body.clientHeight;
	}
	
	if (window.innerHeight>height) {
		height = window.innerHeight;
	}
	
	if (document.documentElement.clientHeight>height) {
		height = document.documentElement.clientHeight;
	}
	
	jQuery("#cover_all").css('height',height+'px');
};

jQuery.showLightText = function() {
	var scrollTop = window.pageYOffset ||  document.documentElement.scrollTop || 0;
	var scrollLeft = window.pageXOffset ||  document.documentElement.scrollLeft || 0;

	scrollTop += 90;
	
	jQuery("#cover_box").css('top',scrollTop+'px');
	
	jQuery("#cover_box div.content").html("");
	jQuery("body").addClass("nooverflow");
	jQuery("#cover_all").fadeIn("fast",function() {
		jQuery("#cover_all").fadeTo("fast",0.8);
	});
};

/*
 * call_prefix - prefix dla wywołania, np. przydatne dla stron i formularzy, które można normalnie wywołać, ale jeśli to robi lightText, to jest to wywołanie ajaxowe (bez korzystania ze zbędnych TPLi)
 * call_type - wymuszanie konkretnej metody zapytania (GET lub POST)
*/

jQuery.fn.lightText = function(options) {
	var settings = jQuery.extend({
		call_prefix: "",
		call_type: "",
		form_not_submitable_class: "incomplete"
	}, options);

	return this.each(function() {
		var this_url = jQuery(this).attr('href');
		
		//link
		if (this_url!=undefined && this_url!="") {
			jQuery(this).click(function() {
				var call_url = jQuery(this).attr('href');
				
				jQuery.prepareLightText();
				jQuery.showLightText();
				
				jQuery("#cover_box").fadeIn("fast", function() {
					jQuery.ajax({
						type: "GET",
						url: settings.call_prefix + call_url,
						success: function (msg) {
							jQuery("#cover_box div.content").removeClass("loading");
							jQuery("#cover_box div.content").html(msg);
							//mapAjaxSubmit();
						}
					});
				});
				return false;
			});
		}
		
		//formularz
		this_url = jQuery(this).attr('action');
		
		if (this_url!=undefined && this_url!="") {
			jQuery(this).submit(function() {
				
			
				if (jQuery(this).hasClass(settings.form_not_submitable_class)) {
					return false;
				}
			
				var call_type = jQuery(this).attr('method');
				var call_data = jQuery(this).serialize();
				var call_url = jQuery(this).attr('action');
				
				if (settings.call_type!="") {
					call_type = settings.call_type;
				}
				else {
					if (call_type=="get" || call_type=="GET") {
						call_type = "GET";
					}
					else {
						call_type = "POST";
					}
				}
			
				jQuery.prepareLightText();
				jQuery.showLightText();
				
				jQuery("#cover_box").fadeIn("fast", function() {
					jQuery.ajax({
						type: call_type,
						url: settings.call_prefix + call_url,
						data: call_data,
						success: function (msg) {
							jQuery("#cover_box div.content").removeClass("loading");
							jQuery("#cover_box div.content").html(msg);
							//mapAjaxSubmit();
						}
					});
				});
				return false;
			});
		}
	});
};
