/*

    jQuery Modal
    Built by Devstars, July 2011

*/
(function ($) {
	
	// Begin plugin - $(this) is now avaliable for the single element
	$.fn.modal = function (options) {
	   
	    // Set defaults
		var defaults = {
			width: 800, // How long the frame stays visible for
		};
		
		// Enable options in defaults var
		var options = $.extend(defaults, options);
		
		var element = $(this),
		    body = $("body"),
		    link = element.attr("data-modal-href"),
		    section = element.attr("data-modal-section");
		    
		// Resize sections
		function setSize() {
		    var windowHeight = $(window).height(),
		        windowWidth = $(window).width();
		    $("#modalShade").css({"width": windowWidth + "px","height": windowHeight + "px"});
		    $("#modalContent").stop(true, true).animate({"max-height": (windowHeight - 200) + "px"}, 400);		    
		};
		
		$(window).resize(function(){
	        setSize();
	    });
		    		
		// When hovered  
		element.click(function(){
		    
		    if ($("#modalShade").length == 0) {
		    
    		    // Add all the elements in
    		    body.append('<div id="modalShade" style="display: none;"></div>');
    		    $("#modalShade").after('<div id="modalContainer" style="display: none;"></div>');
    		    $("#modalContainer").append('<div id="modalContent"></div>');
		    
    	    }
		    
		    // Set vars for new elements
		    var shade = $("#modalShade"),
		        container = $("#modalContainer"),
		        content = $("#modalContent");
		    
		    // Set width & height of elememts
		    setSize();
		    
		    
		    // Fade elements in
		    shade.fadeIn();
		    container.delay(500).fadeIn();
		    
		    // Fade out
		    shade.click(function(){
		        shade.fadeOut(function(){
		            $(this).remove();
		        });
		        container.fadeOut(function(){
		            $(this).remove();
		        });
		    });
		    		    
		    // Load in content
		    content.load(link + " #" + section);
		    
		    return false;
		        		    
		});
			    
	}; // End begining of plugin

})(jQuery);
