



$(document).ready(function() {
	Modal = {
		
		overlayID : 'overlay',
		overlayFadeDuration : 450,
		content : '<div id="mod_wrap"><h2></h2><div id="mod_content"></div> <div id="mod_close"><img src="images/close.png" alt="Close" /></div></div>',
		contentID : '',
		contentCenter : 0,
		closeID : '',
		overlay : '',
		modalFunction : '',
		docHeight : $(document).height(),
		winHeight : $(window).height(),
		winScrollTop : 0,
		overlayHeight : 0,
		winCenter : 0,
		mod_content_w : 10,
		mod_content_h : 10,
		top_offset : 40,
		
		
		
		
		
	
		addModal : function (){ //if modal doesn't exist in the dom, add it
			
			if($('#' + Modal.overlayID).length == 0){ // look for the ID of whatever you've named your overlay
				$('body').prepend(Modal.overlay); // if it doesn't exist already, add it to the dom
				
			}
			
			
			$('body').prepend(Modal.content);
			
			$(document).pngFix(); 
			
			
			// set the defaults to animate from on our modal content
			
			// set the height of our overlay to the docuement height
			$('#' + Modal.overlayID).height(Modal.overlayHeight);
			$('#mod_content').height(Modal.mod_content_h);
			$('#mod_content').width(Modal.mod_content_w);
			$('#mod_wrap').width(Modal.mod_content_w);
			
			$('#' + Modal.overlayID).fadeIn(Modal.overlayFadeDuration, function(){ // fade in the overlay 
				
			
				$('#mod_wrap').show(); //show the modal content by ID and run any functions
			
				
				//center the modal 
				Modal.winCenter=(Modal.winHeight / 2) + Modal.winScrollTop;
				//Modal.contentCenter = $('#mod_wrap').height() / 2 ) -  Modal.top_offset ;
				Modal.contentCenter = (Modal.mod_content_h / 2 ) + Modal.top_offset ;
				
				//offset our content depending on scrollTop
				var contentTop = Modal.winCenter - Modal.contentCenter;
				
				$('#mod_wrap').css('marginLeft', '-' + (Modal.mod_content_w/2) + 'px'); // IF there is a top margin, get rid of it
				$('#mod_wrap').css('top', contentTop);
				
				$('#mod_wrap').css('marginTop', '0px'); // IF there is a top margin, get rid of it
				
				// run any function that was passed
				//alert(Modal.modalFunction)
				
				
				




				eval(Modal.modalFunction); 
				
			
			})
			
			//hide the shadowbox and reset the defaults
			$('#' + Modal.overlayID + ', #mod_close').click(function(e){
				e.preventDefault();
				Modal.hide();
			});
			
		},
		
		show : function(w, h, func){
			
			// reset defaults	
			
			Modal.docHeight=$(document).height();
			Modal.winHeight=$(window).height();
			Modal.winScrollTop=$(document).scrollTop();
			Modal.mod_content_w = w;
			
			Modal.mod_content_h = h;
			Modal.setOverlayHeight();
			Modal.overlay = '<div id="' + Modal.overlayID + '"></div>';
			Modal.modalFunction = func;	
			
			Modal.addModal();
			
			
		},
		
		hide : function()
		{
			
			$('#mod_wrap').remove(); //remove the modal content 
			$('#' + Modal.overlayID).fadeOut(Modal.overlayFadeDuration) // fade out the overlay
			
			// remove hash tags
			
			if ("pushState" in history){ // html5 compatilbe : remove the # too
        		history.pushState("", document.title, window.location.pathname);
			} else { // not html5 : leaves #
        		window.location.hash = "";
			}
			
			// return doc title
			
			$('title').html(def_doc_title);
		
		},
		
		
		
		
		setOverlayHeight : function()
		{
		
			(Modal.docHeight < Modal.winHeight) ? Modal.overlayHeight = Modal.winHeight : Modal.overlayHeight = Modal.docHeight ;
			
		}
		
	};
});
