/*

	MRM_Modal.js
	jQuery Modal Window Plugin

*/

(function($) {

	$.Modal = function(options) {

		// Make sure that at least one Option is specified
    	if (options.length < 1) { 
    		alert("Please Specify at least 1 Option!");
        	return false; 
    	};

		// Overrideable Settings & Methods
    	var settings = {
    	File           : null,
			Width          : null,
			Height         : null,
			Logging        : true,
			Transition     : true,
			TransitionType : "fade",
			RandomData     : null,
			SerializedData : null,
    	onBefore       : function(){},
			onAfter        : function(){},
			onDuring       : function(){},
			LoadIMG        : './images/loadingAnimation.gif'
    	};

		// Allowable Overrideable Settings/Options
    	if (options) {
        	jQuery.extend(settings, options);
   	 	};
		
		// Close Modal Window
		function closeModal() {
			
			$('#ModalWindow').fadeOut(350);
			$("#Overlay").fadeOut(350);

			$('#tmp').fadeOut(350);
			$("#tmp").fadeOut(350);
      
			// Callback Handler
			settings.onAfter.apply(this);
		
		};
		
		// Swap Modal Windows
		function swapModalWindows(newFile) {
			$.Modal({File:newFile});
		};
		
		if (settings.TransitionType == "fade") {
			// Append Overlay & Modal Window to Body
			$("body").append('<div id="Overlay">&nbsp;</div>');
			$("body").append('<div id="ModalWindow">&nbsp;</div>');
		} else if (settings.TransitionType == "swap") {
			$("body").append('<div id="tmp"></div>');
		};
		
		// Click on Overlay, Close Modal Window
		$("#Overlay").bind("click", function() {
      //hbx tag
      dynamic_fsCommand('MobileAppsShare|Close')
      
			closeModal();
      if(document.getElementById('flashcontent')) {
        showflash();
      }
		});
		
		// Listen for Enter Key
		$(document).keyup(function(event){
    		if (event.keyCode == 27) {
        		closeModal();
            if(document.getElementById('flashcontent')) {
              showflash();
            }
    		};
		});
		
		// Position Overlay & Modal Window
		function dimensionalize() {
			if(document.getElementById('flashcontent')){
        hideflash();
      }
			// Calculate Dimensions
			var h = $(window).height();
			var bh = $('body').height();
			
			
			var mw = $("#ModalWindow").width();
			var mh = $("#ModalWindow").height();
			var w = $(window).width();
			
      //alert('window width: '+w + ' ' + 'body width: '+bw);
      //alert(w);
      
			// Apply Calculated Dimensions	
			$("#ModalWindow").css("left", ((w / 2) - (mw / 2)) + 9)
			$("#ModalWindow").css("top", "100px");
			$("#tmp").css("left", ((w / 2) - (mw / 2)) + 9)
			$("#tmp").css("top", "100px");
			$("#Overlay").css("top", 0);
			$("#Overlay").css("left", 0);		
			if (bh > h) {
				$("#Overlay").css("height", bh+'px');
			} else {
				$("#Overlay").css("height", h+'px');
			}
      
      if (w < 955) {
        $('#Overlay').css("width", '970px');
      } else {
        $('#Overlay').css("width", '100%');
      }
			
		};
		
		$(window).resize(function(){
			dimensionalize();
		});

		if (settings.TransitionType == "fade") {
			// This Could be Externalized ...
			$("#Overlay").css("display", "none");
			$("#Overlay").css("background-color", "#000000");
			$("#Overlay").css("z-index", 299);
			$("#Overlay").css("position", "absolute");
			$("#Overlay").css("width", "100%");
			$("#ModalWindow").css("display", "none");
			$("#ModalWindow").css("z-index", 300);
			$("#ModalWindow").css("position", "absolute");
		};
		
		// onBefore Modal Window Handler
		settings.onBefore.apply(this);

		// AJAX Error Handler
		function AJAXErrorHandler(event, request, settings) {
			// Log All AJAX Error Information Statistics
			if (console.log) {
				console.log(event);
				console.log(request);
				console.log(settings);
			};
		};

		if (settings.TransitionType == "swap") {
			$("#tmp").css("display", "none");
			$("#tmp").css("z-index", 303);
			$("#tmp").css("position", "absolute");
			$("#left_share_holder").html("&nbsp;");
			$("#center_share_holder").html("&nbsp;");
			dimensionalize();
			// AJAX Call to Load External HTML for Modal Window
			$("#tmp").load(settings.File);
			$("#tmp").fadeIn(500);
		}

		if (settings.TransitionType == "fade") {
		
			// AJAX Call to Load External HTML for Modal Window
			$("#ModalWindow").load(settings.File, function(){
			
				// Dimensionalize / Position Overlay & Modal Window
				dimensionalize();
				
				// Display Modal Window & Overlay
				if (settings.TransitionType == "fade") {
					$("#Overlay").css("opacity", 0.75)
					$("#Overlay").fadeIn(350);
					$("#ModalWindow").fadeIn(350);
				}
			});
		};
		
		// onAfter Modal Window Handler
		settings.onAfter.apply(this);

	};

})(jQuery);

/*

EXAMPLE USAGE WITH FULL OPTIONS:

<script>
	$.Modal({
    	File           : "file.php",
		Width          : 600,
		Height         : 300,
		LoadIMG        : "../images/loader.gif",
		Logging        : false,
    	onBefore       : function(){ alert("onBefore Event Handler"); },
		onAfter        : function(){ alert("onAfter Callback Event Handler"); }
		TransitionType : null,
    });
</script>

*/
