//Javascript
(function($){  
	$.fn.jModal = function(options){  
	    
		settings = jQuery.extend({
			 url: "",
			 use_error_box : false,
			 use_overlay : true,
			 buttons : "",
			 align: "center",
			 width : "",
			 height : ""
		}, options);
	   
	    var element = this; 

		return $(this).click(function(){	
			
			element.screenH = $(document).height();//get the screen height
			element.screenW = $(document).width();//get the screen width
			element.tagName = this.tagName;
			element.href = $(this).attr("href");
			element.title = $(this).attr("title");
			//create the element in the document
			init_html();
			if(settings['use_overlay'] == true){
				create_overlay();	
			}//end if
			listen_resize();
			return false;
		});

		function init_html(){
			$("body").append('<div id="jModal"></div>');
			$("#jModal").css("width",element.screenW);
			$("#jModal").css("height",element.screenH);	
			//create a sub div with the two elements
			$("#jModal").append('<div class="jM_window"><a href="#" class="jM_closer"><span>X</span></a><div class="jM_content"></div></div>');
			//check if we need to overwrite the css width
			if(settings['width'] != ""){
				$("#jModal .jM_window").css("width",settings['width']);
			}//end if
			//check if we need to overwrite the css height
			if(settings['width'] != ""){
				$("#jModal .jM_window").css("min-height",settings['height']);
				$("#jModal .jM_window").css("height",settings['height']);
				$("#jModal .jM_content").css("min-height",settings['height']);
				$("#jModal .jM_content").css("height",settings['height']);
			}//end if
			//check alignment
			//add a negative margin which is equal to the half of the box size to center the box in the screen
			$("#jModal .jM_window").css("margin-left",-($("#jModal > .jM_window").width()/2));
			//Vertical alignment
			if(settings['align'] == "center"){				
				$("#jModal .jM_window").css("margin-top",-($("#jModal > .jM_window").height()/2));	
			}//end if
			
			if(settings['align'] == "top"){
				$("#jModal .jM_window").css("top","100px");	
			}//end if
			//check if we need to load external content if is set a fixed url
			if(settings['url'] != "" && element.tagName != "A"){
				$("#jModal .jM_content").load(settings['url']);
			}	
			//alert(settings['buttons']['ok']);
			//if nothing is set about urls, and if the element is a link, just open the related link in the modal window
			if(settings['url'] == "" && element.tagName == "A" && settings['buttons'] == ""){
				if(element.href != "" && element.href != "#"){
					$("#jModal .jM_content").load(element.href,function(){
						do_resize();													
					});
				} else {
					//if there is no url specified and no url in the link, get the title attribute
					$("#jModal .jM_content").html(element.title);
				}//end if
			}//end if
			$("#jModal").css({ opacity: 0});		
			$("#jModal").animate({opacity: 1}, 500, 'linear');		
				
			//check if we need to add a button "ok"
			if(settings['url'] == "" && element.tagName == "A" && settings['buttons'] != ""){
				$("#jModal .jM_content").html(element.title);
				$("#jModal .jM_content").append('<div class="jM_toolbar"></div>');
				if(settings['buttons']['ok'] != ""){
					$("#jModal .jM_toolbar").append('<a href="'+element.href+'" class="jM_ok_btn">'+settings['buttons']['ok']+'</a>');
				}
			}//end if
			
			//check if the user press the key "esc"
			$(document).bind("keypress",function(e){
				if(e.keyCode == 27 && $("#jModal").length > 0){
					close_jModal();
				}//end if		 
			});	
			
			//assign the closer but the action to close the window
			$("#jModal .jM_closer").click(function(){
				close_jModal();
				return false;										 
			});			
			
		}
		
		function create_overlay(){	
			$("#jModal").append('<div class="jM_overlay"></div>');
			$(".jM_overlay").css("width",$("#jModal").width());
			$(".jM_overlay").css("height",$("#jModal").height());
		}
		
		//listen the browser if the user resize the window
		function listen_resize(){
			$(window).bind('resize',function(){
				do_resize();
			});
		}
		
		function do_resize(){
			element.screenH = $(document).height();//get the screen height
			element.screenW = $(document).width();//get the screen width	
			$("#jModal").css("width",element.screenW);
			$("#jModal").css("height",element.screenH);
			if(settings['use_overlay'] == true){
				$(".jM_overlay").css("width",$("#jModal").width());
				$(".jM_overlay").css("height",$("#jModal").height());
			}
		}
		//close the window
		function close_jModal(){
			$("#jModal").remove();	
		}			
	}
})(jQuery);  

(function($){  
	$.fn.jModalLoad = function(options){  
	    
		settings = jQuery.extend({
			 url: "",
			 use_error_box : false,
			 use_overlay : true,
			 buttons : "",
			 align: "center",
			 width : "",
			 height : "",
			 message : "",
			 closeDelay : "",
			 animate : false,
			 css_class : ""
		}, options);
	   
	    var element = this; 

		return $(this).each(function(){	
			
			element.screenH = $(document).height();//get the screen height
			element.screenW = $(document).width();//get the screen width		
			element.tagName = this.tagName;			
			element.title = $(this).attr("title");
			//create the element in the document
			init_html();
			if(settings['use_overlay'] == true){
				create_overlay();	
			}//end if
			listen_resize();
			return false;
		});

		function init_html(){
			$("body").append('<div id="jModal"></div>');
			$("#jModal").css("width",element.screenW);
			$("#jModal").css("height",element.screenH);	
			//create a sub div with the two elements
			$("#jModal").append('<div class="jM_window"><a href="#" class="jM_closer"><span>X</span></a><div class="jM_content"></div></div>');
			if(settings['css_class'] != ""){
				$("#jModal .jM_content").addClass(settings['css_class']);
			}//end if
			//check if we need to overwrite the css width
			if(settings['width'] != ""){
				$("#jModal .jM_window").css("width",settings['width']);
			}//end if
			//check if we need to overwrite the css height
			if(settings['width'] != ""){
				$("#jModal .jM_window").css("min-height",settings['height']);
				$("#jModal .jM_window").css("height",settings['height']);
				$("#jModal .jM_content").css("min-height",settings['height']);
				$("#jModal .jM_content").css("height",settings['height']);
			}//end if
			//check alignment
			//add a negative margin which is equal to the half of the box size to center the box in the screen
			$("#jModal .jM_window").css("margin-left",-($("#jModal > .jM_window").width()/2));
			//Vertical alignment
			if(settings['align'] == "center"){				
				$("#jModal .jM_window").css("margin-top",-($("#jModal > .jM_window").height()/2));	
			}//end if
			
			if(settings['align'] == "top"){
				$("#jModal .jM_window").css("top","100px");	
			}//end if
			//check if we need to load external content if is set a fixed url
			if(settings['url'] != ""){
				$("#jModal .jM_content").load(settings['url']);
			}	
			//alert(settings['buttons']['ok']);
			//if nothing is set about urls, and if the element is a link, just open the related link in the modal window
			if(settings['url'] == "" && settings['message'] != ""){
				$("#jModal .jM_content").html(settings['message']);
			} else {
				//if there is no url specified and no url in the link, get the title attribute
				$("#jModal .jM_content").html(element.title);
			}//end if	
			if(settings['animate'] == true){
				$("#jModal").css({ opacity: 0});		
				$("#jModal").animate({opacity: 1}, 500, 'linear');				
			}//end if

			
			//check if the user press the key "esc"
			$(document).bind("keypress",function(e){
				if(e.keyCode == 27 && $("#jModal").length > 0){
					close_jModal();
				}//end if		 
			});	
			
			//assign the closer but the action to close the window
			$("#jModal .jM_closer").click(function(){
				close_jModal();
				return false;										 
			});		
			
			if(settings['closeDelay'] != ""){
				setTimeout(function(){ $('#jModal').fadeOut("slow",function(){$("#jModal").remove();}); }, settings['closeDelay']);	
			}
			
		}
		
		function create_overlay(){	
			$("#jModal").append('<div class="jM_overlay"></div>');
			$(".jM_overlay").css("width",$("#jModal").width());
			$(".jM_overlay").css("height",$("#jModal").height());
		}
		
		//listen the browser if the user resize the window
		function listen_resize(){
			$(window).bind('resize',function(){
				do_resize();
			});
		}
		
		function do_resize(){
			element.screenH = $(document).height();//get the screen height
			element.screenW = $(document).width();//get the screen width	
			$("#jModal").css("width",element.screenW);
			$("#jModal").css("height",element.screenH);
			if(settings['use_overlay'] == true){
				$(".jM_overlay").css("width",$("#jModal").width());
				$(".jM_overlay").css("height",$("#jModal").height());
			}
		}
		//close the window
		function close_jModal(){
			//$("#jModal").remove();	
			$('#jModal').fadeOut("slow",function(){
				$("#jModal").remove();
			});			
		}			
	}
})(jQuery); 
