(function($) {
	/**
	 * $ is an alias to jQuery object
	 *
	 */
	$.fn.cBox = function(settings) {
		// Settings to configure the jQuery lightBox plugin how you like
		settings = jQuery.extend({
			// Configuration related to overlay
			overlayBgColor: 		'#000',		// (string) Background color to overlay; inform a hexadecimal value like: #RRGGBB. Where RR, GG, and BB are the hexadecimal values for the red, green, and blue values of the color.
			overlayOpacity:			0.8,		// (integer) Opacity value to overlay; inform: 0.X. Where X are number from 0 to 9
			borderWidth: 0,
			borderColor: '#000',
			width:       400,
			height:      400,
			closeImage: '/images/close.gif',
			showScrollerbar:false,
			callback:null
			
		},settings);
		// Caching the jQuery object with all elements matched
		var jQueryMatchedObj = this; // This, in this context, refer to jQuery object
		/**
		 * Initializing the plugin calling the start function
		 *
		 * @return boolean false
		 */
		function _initialize() {
			_start(this,jQueryMatchedObj); // This, in this context, refer to object (link) which the user have clicked
			return false; // Avoid the browser following the link
		}
		/**
		 * Start the jQuery lightBox plugin
		 *
		 * @param object objClicked The object (link) whick the user have clicked
		 * @param object jQueryMatchedObj The jQuery object with all elements matched
		 */
		function _start(objClicked,jQueryMatchedObj) {
			// Hide some elements to avoid conflict with overlay in IE. These elements appear above the overlay.
			$('embed, object, .select').css({ 'visibility' : 'hidden' });
			//$('embed, object').css({ 'visibility' : 'hidden' });
			// Call the function to create the markup structure; style some elements; assign events in some elements.
			_set_interface();
//			alert('abc');
			_open(objClicked);
			// Assigning click events in elements to close overlay
		}
		
		function _set_interface() {

			var overlay = $('<div id="cbox_overlay"></div>');
			
			var arrPageSizes = ___getPageSize();
			
			overlay.css({width: '100%', height:arrPageSizes[1], position: 'absolute', left: 0, top: 0, zIndex: 99});
			overlay.css({backgroundColor:settings.overlayBgColor, opacity: settings.overlayOpacity});
			$('body').append(overlay);
			overlay.fadeIn();
		}
		
		function _open(href){

			var wrapper = $('<div id="cbox_wrapper"></div>');

			var arrPageSizes = ___getPageSize();
			var arrPageScroll = ___getPageScroll();
			wrapper.css({width: '100%', height:arrPageSizes[1], background:'none', position: 'absolute', left: 0, top: 0, zIndex: 100});
			
			wrapper.html('<img src="/adults/images/loading.gif" />');
			$('img', wrapper).css({position:'absolute', top:arrPageSizes[3]/2-9, left:arrPageSizes[2]/2 - 110});
			$('body').append(wrapper);
			var ndiv = $('<div></div>');
			ndiv.css({top:arrPageScroll[1] + (arrPageSizes[3]/2-settings.height/2), left:arrPageSizes[2]/2 - settings.width/2});
			ndiv.css({width:settings.width, height:settings.height, border: settings.borderWidth+'px solid '+ settings.borderColor, background:'none', position:'absolute', overflow:'hidden'});
			if(settings.showScrollerbar){
				ndiv.css({overflow:'scroll'});	
			}
//			ndiv.html('');
			//alert(href);
			var href = href + '';
			if(href.search(/inline/) == -1){
				var href = href + '';
				var pos = href.indexOf("?");
				if(pos != -1){
					var url = href.substr(0, pos-1);
					var data = href.substr(pos+1);
				}else{
					var url = href;
					var data = '';
				}
	
				$.ajax({
					type:"POST",
					url:url,
					data:data + '&from_cBox=true',
					success:function(html){
						//ndiv.html('<a href="" id="cbox_close_link" title="close"><img src="'+ settings.closeImage +'" style="float:right;" /></a>').append(html);
						//___pause(500);
						ndiv.html(html);
						wrapper.html(ndiv);
						_apply_action();
						if(settings.callback != null){
							settings.callback();
						}
					}
				});
			}
			else
			{
				var id = href.substr(href.indexOf("inline")+6);
				//var html = $(id).html();
				ndiv.html($('#'+id).children(':first').clone());
				wrapper.html(ndiv);
			}
			//_apply_action();
		}
		
		function _apply_action(){
			/*$('#cbox_overlay').click(function() {
				_finish();									
			});
			$('#cbox_wrapper').click(function() {
				_finish();									
			});*/
			// Assign the _finish function to lightbox-loading-link and lightbox-secNav-btnClose objects
			$('#cbox_close_link').click(function() {
				_finish();
				return false;
			});	
		}
		
		/**
		 * Remove jQuery lightBox plugin HTML markup
		 *
		 */
		function _finish() {
			$('#cbox_wrapper').remove();
			$('#cbox_overlay').fadeOut(function() { $('#cbox_overlay').remove(); });
			// Show some elements to avoid conflict with overlay in IE. These elements appear above the overlay.
			$('embed, object, .select').css({ 'visibility' : 'visible' });
		}
		/**
		 / THIRD FUNCTION
		 * getPageSize() by quirksmode.com
		 *
		 * @return Array Return an array with page width, height and window width, height
		 */
/*		function ___getPageSize() {
			
			//var pageWidth = $(document).width();
			//var pageHeight = $(document).height();
			//var windowWidth = $(window).width();
			//var windowHeight = $(window).height();
			//var xScroll, yScroll;
			arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
			return arrayPageSize;
		};*/
		
		/**
		 / THIRD FUNCTION
		 * getPageSize() by quirksmode.com
		 *
		 * @return Array Return an array with page width, height and window width, height
		 */
		function ___getPageSize() {
			var xScroll, yScroll;
			if (window.innerHeight && window.scrollMaxY) {	
				xScroll = window.innerWidth + window.scrollMaxX;
				yScroll = window.innerHeight + window.scrollMaxY;
			} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
				xScroll = document.body.scrollWidth;
				yScroll = document.body.scrollHeight;
			} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
				xScroll = document.body.offsetWidth;
				yScroll = document.body.offsetHeight;
			}
			var windowWidth, windowHeight;
			if (self.innerHeight) {	// all except Explorer
				if(document.documentElement.clientWidth){
					windowWidth = document.documentElement.clientWidth; 
				} else {
					windowWidth = self.innerWidth;
				}
				windowHeight = self.innerHeight;
			} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
				windowWidth = document.documentElement.clientWidth;
				windowHeight = document.documentElement.clientHeight;
			} else if (document.body) { // other Explorers
				windowWidth = document.body.clientWidth;
				windowHeight = document.body.clientHeight;
			}	
			// for small pages with total height less then height of the viewport
			if(yScroll < windowHeight){
				pageHeight = windowHeight;
			} else { 
				pageHeight = yScroll;
			}
			// for small pages with total width less then width of the viewport
			if(xScroll < windowWidth){	
				pageWidth = xScroll;		
			} else {
				pageWidth = windowWidth;
			}
			arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
			return arrayPageSize;
		};
		/**
		 / THIRD FUNCTION
		 * getPageScroll() by quirksmode.com
		 *
		 * @return Array Return an array with x,y page scroll values.
		 */
		function ___getPageScroll() {
			var xScroll, yScroll;
			if (self.pageYOffset) {
				yScroll = self.pageYOffset;
				xScroll = self.pageXOffset;
			} else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
				yScroll = document.documentElement.scrollTop;
				xScroll = document.documentElement.scrollLeft;
			} else if (document.body) {// all other Explorers
				yScroll = document.body.scrollTop;
				xScroll = document.body.scrollLeft;	
			}
			arrayPageScroll = new Array(xScroll,yScroll);
			return arrayPageScroll;
		};
		 /**
		  * Stop the code execution from a escified time in milisecond
		  *
		  */
		 function ___pause(ms) {
			var date = new Date(); 
			curDate = null;
			do { var curDate = new Date(); }
			while ( curDate - date < ms);
		 };
		// Return the jQuery object for chaining. The unbind method is used to avoid click conflict when the plugin is called more than once
		return this.unbind('click').click(_initialize);
	};
})(jQuery); // Call and execute the function immediately passing the jQuery object

function resize_cBox(width, height){
	var pageWidth = $(document).width();
	var pageHeight = $(document).height();
	var windowWidth = $(window).width();
	var windowHeight = $(window).height();
	var left = windowWidth/2 - width/2;
	var top = windowHeight/2 - height/2;
	$('#cbox_wrapper>div').animate({top:top, height:height, left:left, width:width}, 'slow');
}
function remove_cBox(){
	$('#cbox_wrapper').remove();
	$('#cbox_overlay').fadeOut(function() { $('#cbox_overlay').remove(); });
	// Show some elements to avoid conflict with overlay in IE. These elements appear above the overlay.
	$('embed, object, .select').css({ 'visibility' : 'visible' });	
}