/*
* popup.js
* ScreenToaster
* Michael Bagur
* 
* handles popup open / close
*
**********************************************/

jQuery(function() {
	(function($){
			
		$.fn.extend({
			/*
			* Show the popup Window and the black transparent background
			*/
			openPopup: function() {
				try {
					// definition de variable
					var w = $('body').width();
					var mh = $('body').height();
					var ih = $(document).height();
					
					// ajustement de variable
					if ( mh < ih ) mh = ih;
					
					// affiche le masque
					$("#mask")
						.css({width: w + 'px', height: mh +'px', opacity: 0.64, filter:'Alpha(Opacity=64)'})
						.fadeIn(500);
						
					// affiche le popup
					this.showPopup();
					
					// edition panel sous internet explorer
					var bp = $('.blue-panel');
					if(bp.editpanel instanceof Function) {
						bp.editpanel();
					}

				// Catch errors
				} catch (e) {
					logger.error("popup.js > "+e);
				}
								
				return false;
				
			},
			
			/*
			* Show the popup Window
			*/
			showPopup: function() {
				try {
					// definition de variable
					var tPos = ( $(window).height() - this.height() )/2 + $(window).scrollTop();
					
					// affiche le popup
					this
						.css({top: tPos + 'px'})
						.fadeIn(500);
					
					// edition panel sous internet explorer
					var bp = $('.blue-panel');
					if(bp.editpanel instanceof Function) {
						bp.editpanel();
					}

				// Catch errors
				} catch (e) {
					logger.error("popup.js > "+e);
				}
			},
			
			/*
			* Hide the popup Window
			*/
			hidePopup: function() {
				// cache le popup
				this.fadeOut(500);
			},
			
			/*
			* Hide the popup Window and the black transparent background
			*/
			closePopup: function() {
				try {
					// cache le popup
					this.hidePopup();
					
					// cache le masque
					$("#mask").fadeOut(500);
					
				// Catch errors
				} catch (e) {
					logger.error("popup.js > "+e);
				}
				
				return false;
			}
		});
	})(jQuery);
	
	(function($) {
		$('.popup').each(function() {
			var popup = $(this);
			popup.find(".close").each(function() {
				$(this).click(function() {
					popup.closePopup();
				});
			});
			
		});
	})(jQuery);
	
});

/*
* Switch between two popups (without removing the black background)
*/
function switchPopup(oldPopup, newPopup) {
	jQuery('#'+oldPopup).hidePopup();
	jQuery('#'+newPopup).showPopup();
}

/*
* Open a popup
*/
function openPopup(elementId) {
	return jQuery('#'+elementId).openPopup();
}

/*
* Open a popup and check if the cookies are activated
*/
function openPopupCheckCookies(elementId) {
	if (document.cookie != "") {
		return openPopup(elementId);
	} else {
		return openPopup('needCookies');
	}
}

/*
* Close a popup
*/
function closePopup(elementId) {
	return jQuery("#"+elementId).closePopup();
}