﻿// Global leModal methods and objects
// There is only EVER one leModal
// Need Dimensions and BgIframe plugins
jQuery.leModal = {
	
	// jQuery object references
	jOverlay: null,
	jOverlayClicker: null,
	jModalWin: null,
	
	// "Private" properties
	_currentSettings: null,
	_hasSetWindowEvents: false,
	_isVisible: false,
	
	
	//
	// Show the modal
	// 3 main divs, Overlay (dimmed out background), Clicker (handles on click of overlay event - required for IE6), ModalWin
	//
	show: function(settings){
		
		var cc = this;	
		
		cc._currentSettings = jQuery.extend({
			// Properties
			modalTopAdjustment: 10,
			overlayOpacity: 0.5,
			overlayBackground: 'black',
			modalWinBackground: 'white',
			zIndex: 5000,
			showAnimationType: 'fadeIn',
			showAnimationSpeed: 'normal',
			clickBackgroundToHide: true,
			// Callback methods
			setContentCallBack: null,
			beforeShowCallBack: null,
			beforeHideCallBack: null
		}, settings);
		
				
		if (cc.jOverlay == null){
		
			// Create the Overlay layer
			cc.jOverlay = jQuery('<div id="LeModalOverlay" style="position: absolute; top: 0; left: 0; width: 100%; height: 100%; z-index: ' + cc._currentSettings.zIndex + '; background: ' + cc._currentSettings.overlayBackground + ';"></div>');
			
			if (cc._currentSettings.clickBackgroundToHide){
				// Create the Clicker layer
				cc.jOverlayClicker = jQuery('<div id="LeModalOverlayClicker" style="position: absolute; top: 0; left: 0; width: 100%; height: 100%;"></div>');
				cc.jOverlayClicker
					.appendTo(cc.jOverlay)
					.click(function(){
						cc.hide();
					});
			}
		
			cc.jOverlay
				.bgiframe()
				.hide()
				.css('opacity', cc._currentSettings.overlayOpacity)
				.appendTo('body');
		}
		
		if (cc.jModalWin == null){
		
			// Create the ModalWin
			cc.jModalWin = jQuery('<div id="LeModalWin" style="position: absolute; top: 0; left: 0; width: auto; height: auto; z-index: ' + cc._currentSettings.zIndex + '; background: ' + cc._currentSettings.modalWinBackground + ';"></div>');
			cc.jModalWin
				.hide()
				.appendTo('body');
		}
		cc.jModalWin.css({ width: 'auto', height: 'auto' });
		
		
		if (typeof cc._currentSettings.setContentCallBack == 'function'){
			
			// Call setContentCallBack if set
			cc._currentSettings.setContentCallBack(cc.jModalWin, cc.jOverlay);
			jQuery('.close',cc.jModalWin).click(function(){
				cc.hide();
				return false;
			});
		}
		
		// Set dimensions and positions
		cc.setDimensions();
		
		if (typeof cc._currentSettings.beforeShowCallBack == 'function'){
			cc._currentSettings.beforeShowCallBack(cc.jModalWin, cc.jOverlay);
		}
		
		// Show layers
		cc.jOverlay.show();
		cc._isVisible = true;
		
		if (cc._currentSettings.showAnimationType == 'slideDown'){
			cc.jModalWin.slideDown(cc._currentSettings.showAnimationSpeed, function(){ checkWindowEvents(); });
		}
		else if (cc._currentSettings.showAnimationType == 'show'){
			cc.jModalWin.show(cc._currentSettings.showAnimationSpeed, function(){ checkWindowEvents(); });
		}
		else {
			cc.jModalWin.fadeIn(cc._currentSettings.showAnimationSpeed, function(){ checkWindowEvents(); });
		}
		
		function checkWindowEvents(){
			if (!cc._hasSetWindowEvents){
		
				// Add an on window resixe event to reset dimensions and positions
				cc._hasSetWindowEvents = true;
				jQuery(window).resize(function(){
					if (cc._isVisible) cc.setDimensions();
				});
			}
		}
		
		
	},
	
	
	//
	// Hide modal
	//
	hide: function(){
	
		var cc = this;
		
		cc.jModalWin.stop();
		
		if (typeof cc._currentSettings.beforeHideCallBack == 'function'){
			cc._currentSettings.beforeHideCallBack(cc.jModalWin, cc.jOverlay);
		}
		
		if (cc.jOverlay != null){
			cc.jOverlay.hide();
		}
		
		if (cc.jModalWin != null){
			cc.jModalWin.hide();
		}
		
		cc._isVisible = false;
	
	},
	
	
	//
	// Set dimensons and positions
	//
	setDimensions: function(){
	
		var cc = this;
		cc._setModalWinDimensions();
		cc._setOverlayDimensions();
		
	},
	
	
	_setModalWinDimensions: function(){
		
		var cc = this;
		var top = jQuery(window).scrollTop() + cc._currentSettings.modalTopAdjustment;
		if (cc.jModalWin.height() > jQuery(window).height()){
			top = jQuery(window).scrollTop();
		}
		var left = parseInt((jQuery('body').width() - cc.jModalWin.width()) / 2);
		var width = cc.jModalWin.width();
		var height = cc.jModalWin.height();
		cc.jModalWin.css({
			top: top + 'px',
			left: left + 'px',
			width: width + 'px',
			height: height + 'px'
		});
		
	},
		
	_setOverlayDimensions: function(){
		
		var cc = this;
		var bodyHeight = jQuery(document).height();
		var checkHeight = parseInt(cc.jModalWin.css('top')) + cc.jModalWin.height();
		var height = bodyHeight;
		var modalTopAdjustment = cc._currentSettings.modalTopAdjustment;
		if (cc.jModalWin.height() > jQuery(window).height()){
			modalTopAdjustment = jQuery(window).scrollTop();
		}
		if (checkHeight > bodyHeight){
			height = checkHeight + modalTopAdjustment;
		}
		if (cc.jOverlay != null){
			cc.jOverlay
				.height(height)
				.width(jQuery('body').width());
		}
		
	}
	
};

