/**
 * Create a new instance of VCMModal
 * 
 * @classDescription	This class creates a Modal
 * @return {VCMModal}	Returns a new VCMModal object
 * @constructor
 */
function VCMModal() {
}

VCMModal.prototype = {
	/**
	 * jQuery reference to the container
	 * @type {jQuery}
	 */
	_jContainer: null,
	
	/**
	 * jQuery reference to the content
	 * @type {jQuery}
	 */
	_jContent: null,
	
	/**
	 * jQuery reference to the close area
	 * @type {jQuery}
	 */
	_jCloseArea: null,
	
	/**
	 * jQuery reference to the close area info
	 * @type {jQuery}
	 */
	_jCloseAreaInfo: null,
	
	/**
	 * jQuery reference to the drop shadow
	 * @type {jQuery}
	 */
	_jDropShadow: null,
	
	/**
	 * jQuery reference to the box
	 * @type {jQuery}
	 */
	_jBox: null,
	
	_closeCallback: null,
	
	/**
	 * Initialize the popup
	 */
	init: function() {
		console.log('VCMModal.init()');
		// Make references to jQuery objects
		this._jContainer = $('#popupContainer');
		this._jContent = $('#popupContent');
		this._jCloseArea = $('#popupCloseArea');
		this._jCloseAreaInfo = $('#popupCloseAreaInfo');
		this._jDropShadow = $('#popupDropShadow');
		this._jBox = $('#popupBox');
		
		// Add the event
		var me = this;
		this._jCloseArea.click(function() {
			me.close();
			return false;
		});
		
		this._jCloseAreaInfo.click(function (){
			me.close();
			return false;
		});
	},
	
	/**
	 * Destroy
	 */
	destroy: function() {
		console.log('VCMModal.destroy()');
		// Remove the event
		this._jCloseArea.unbind('click');
		this._jCloseAreaInfo.unbind('click');
		// Unreference jQuery
		this._jContainer = null;
		this._jContent = null;
		this._jCloseArea = null;
		this._jCloseAreaInfo = null;
		this._jDropShadow = null;
		this._jBox = null;
	},
	
	/**
	 * Close the modal
	 */
	close: function() {
		//verifier si le cookie existe. S'il n'existe pas, le créer
		var i,x,y,ARRcookies = document.cookie.split(";");
		var result = "";
		var test = "infolettre";
		for (i = 0;i < ARRcookies.length; i++)
		{
		  x = ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
		  y = ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
		  x = x.replace(/^\s+|\s+$/g,"");
		  if(x == test)
		  {
			  result = unescape(y);
		  }
		}
		
		if(result == null || result == "")
		{
			result = "infolettre";
			var exdate = new Date();
			exdate.setDate(exdate.getDate() + 365);
			var c_value = escape(result) + "; expires=" + exdate.toUTCString();
			document.cookie= "infolettre=" + c_value;
		}
		
		console.log('VCMModal.close()');
		// Hide the popup
		this._jContainer.css('visibility', 'hidden');
		// Empty the content
		this._jContent.empty();
		
		if ($.isFunction(this._closeCallback)) {
			this._closeCallback();
		}
	},

	/**
	 * Show the modal with content
	 * @param {String} content The HTML content
	 * @param {Function} closeCallback
	 */
	show: function(content, closeCallback) {
		console.log('VCMModal.show(', content, closeCallback, ')');
		this._closeCallback = closeCallback;
		// Add the content
		this._jContent.html(content);
		// Show the popup
		this._jContainer.css('visibility', 'visible');

		// Delay the sizing so that IE can catch up
		var me = this;
		setTimeout(function() {
			me.resize();
		}, 13);
	},
	
	/**
	 * Resize and recenter to the content
	 */
	resize: function() {
		console.log('VCMModal.resize()');
		var width	= this._jContent.width() + 72,	// Content width with margins and borders (35 + 35 + 1 + 1)
			height	= this._jContent.height() + 68,	// Content height with margins and borders (31 + 35 + 1 + 1)
			shadow 	= 10;							// Drop shadow size (5 + 5)
			
		// Adjust drop shadow
		this._jDropShadow.width(width + shadow);
		this._jDropShadow.height(height + shadow);
		this._jDropShadow.css({
			marginLeft: '-' + ((width + shadow) / 2) + 'px',
			marginTop: '-' + ((height + shadow) / 2) + 'px'
		});
		
		// Adjust content
		this._jBox.css({
			marginLeft: '-' + (width / 2) + 'px',
			marginTop: '-' + (height / 2) + 'px' 
		});
	}
};


