/**
Name :       	jNotify - jQuery Plugin
Revison :    	1.0
Date : 			12/2010
Author:     	Surrel Mickaël (www.msconcept.fr)  for Croissance Net (www.croissance-net.com)
Support:    	
**/

(function($){

	$.jNotify = {
		defaults: {
			MinWidth : 200,					// min-width CSS property
			TimeShown : 2500, 				// Box shown during 'TimeShown' ms
			ShowTimeEffect : 400, 			// duration of the Show Effect
			HideTimeEffect : 400, 			// duration of the Hide effect
			LongTrip : 20,					// in pixel, length of the move effect when show and hide
			HorizontalPosition : 'center', 	// left, center, right
			VerticalPosition : 'center',	 	// top, center, bottom
			ShowOverlay : true,			// show overlay behind the notice ?
			ColorOverlay : '#000',			// color of the overlay
			OpacityOverlay : 0.3			// opacity of the overlay
		},
		
		/*****************/
		/** Init Method **/
		/*****************/
		init:function(msg, options, id, callback) {
			opts = $.extend({}, $.jNotify.defaults, options);
			
			/** Box **/
			if($("#"+id).length == 0)
				$Div = $.jNotify._construct(id, msg);
			
			// Width of the Brower
			WidthDoc = parseInt($(window).width());
			HeightDoc = parseInt($(window).scrollTop());
			
			
			// Position of the jNotify Box
			posTop = $.jNotify.vPos(opts.VerticalPosition);
			posLeft = $.jNotify.hPos(opts.HorizontalPosition);
			
			// Show the jNotify Box
			if(opts.ShowOverlay && $("#jOverlay").length == 0)
				$.jNotify._showOverlay();
				
			$.jNotify._show(msg,function(result){
				if( callback ) callback(result);
			});
		},
		
		/*******************/
		/** Construct DOM **/
		/*******************/
		_construct:function(id, msg) {
			$Div = $("<div />");
			
			$Div
			.attr('id', id)
			.css({opacity : 0,minWidth : opts.MinWidth})
			.html(msg)
			.appendTo('body');
			return $Div;
		},
		
		/**********************/
		/** Postions Methods **/
		/**********************/
		vPos:function(pos) {
			switch(pos) {
				case 'top':
					var vPos = HeightDoc + parseInt($Div.outerHeight(true)/2);
					break;
				case 'center':
					var vPos = HeightDoc + ($(window).height()/2) - (parseInt($Div.outerHeight(true))/2);
					break;
				case 'bottom':
					var vPos = HeightDoc + $(window).height() - parseInt($Div.outerHeight(true));
					break;
			}
			return vPos;
		},
		
		hPos:function(pos) {
			switch(pos) {
				case 'left':
					var hPos = 0;
					break;
				case 'center':
					var hPos = parseInt((WidthDoc/2) - (parseInt($Div.outerWidth(true))/2));
					break;
				case 'right':
					var hPos = parseInt(WidthDoc - parseInt($Div.outerWidth(true)));
					
					break;
			}
			return hPos;
		},
		
		/*********************/
		/** Show Div Method **/
		/*********************/
		_show:function(msg,callback) {
			$Div
			.css({
				top: posTop,
				left : posLeft
			});
			
			$.jNotify._endAnimation($Div,callback);
		},
		
		_showOverlay:function(){
			var overlay = $("<div />");
			
			overlay
			.attr('id', 'jOverlay')
			.css({
				backgroundColor : opts.ColorOverlay,
				opacity: opts.OpacityOverlay
			})
			.appendTo('body')
			.show();
		},
		
		_endAnimation:function(el,callback) {
			switch (opts.VerticalPosition) {
				case 'top':
					el.animate({
						top: posTop + opts.LongTrip,
						opacity:1
					},opts.ShowTimeEffect)
					.delay(opts.TimeShown)
					.animate({
						top: posTop-opts.LongTrip,
						opacity:0
					},opts.HideTimeEffect,function(){
						$(this).remove();
						if($("#jOverlay").length > 0)
							$.jNotify._closeOverlay();
						
						if( callback ) callback(true);
					});
					break;
				case 'center':
					el.animate({
						opacity:1
					},opts.ShowTimeEffect)
					.delay(opts.TimeShown)
					.animate({
						opacity:0
					},opts.HideTimeEffect,function(){
						$(this).remove();
						if($("#jOverlay").length > 0)
							$.jNotify._closeOverlay();
						
						if( callback ) callback(true);
					});
					break;
				case 'bottom' :
					el.animate({
						top: posTop - opts.LongTrip,
						opacity:1
					},opts.ShowTimeEffect)
					.delay(opts.TimeShown)
					.animate({
						top: posTop+opts.LongTrip,
						opacity:0
					},opts.HideTimeEffect,function(){
						$(this).remove();
						if($("#jOverlay").length > 0)
							$.jNotify._closeOverlay();
						
						if( callback ) callback(true);
					});
					break;
			}
		},
		
		_closeOverlay:function(){
			$("#jOverlay").remove();
		},
		
		_isReadable:function(id){
			if($('#'+id).length > 0)
				return false;
			else
				return true;
		}
	};
	
	/** Init method **/
	jNotify = function(msg,options,callback) {
		if($.jNotify._isReadable('jNotify'))
			$.jNotify.init(msg,options,'jNotify', callback);
	};
	
	jSuccess = function(msg,options,callback) {
		if($.jNotify._isReadable('jSuccess'))
			$.jNotify.init(msg,options,'jSuccess',callback);
	};
	
	jError = function(msg,options,callback) {
		if($.jNotify._isReadable('jError'))
			$.jNotify.init(msg,options,'jError',callback);
	};
})(jQuery); 
