//Yo jQuery, attach our plugin to your toolbelt.
jQuery.fn.extend({ 

	//Hi, I'm a plugin. My name is meerkat.
	meerkat: function(options) {
		
		//Create Meerkat's default options
		var defaults = {
			background: 'none',
			opacity: null,
			height: 'auto',
			width: '100%',
			position: 'bottom',
			hide: '.hide',
			dontShow: '#dont-show',
			animationIn: 'none',
			animationOut: null,
			easingIn: 'swing',
			easingOut: 'swing',
			animationSpeed: 'normal',
			cookieExpires: 0,
			delay: 0,
			timer: null
		};
				
		//Extend "defaults" to Meerkat()'s argument "options"
		//To refer to our default settings we can now use settings.defaultsname(eg. settings.background)
		var settings = jQuery.extend(defaults, options);
		
	
		if(jQuery.easing.def){
			settings.easingIn = settings.easingIn;
			settings.easingOut = settings.easingOut;
		}else {
			settings.easingIn = 'swing';
			settings.easingOut = 'swing';
		}

		
		//If there is not a defined animation for animationOut it will default to the same as animationIn
		if(settings.animationOut === null){
			settings.animationOut = settings.animationIn;	
		}
		
		//convert delay and meerkatTimeOut milliseconds to seconds
		settings.delay = settings.delay * 1000;
		if(settings.timer != null){
			settings.timer = settings.timer * 1000;
		}
		
		//Create Cookie
		function createCookie(name,value,days) {
			if (days) {
				var date = new Date();
				date.setTime(date.getTime()+(days*24*60*60*1000));
				var expires = "; expires="+date.toGMTString();
			}
			else { 
				var expires = "";
			}
			document.cookie = name+"="+value+expires+"; path=/";
		}
		
		//Read Cookie
		function readCookie(name) {
			var nameEQ = name + "=";
			var ca = document.cookie.split(';');
			for(var i=0;i < ca.length;i++) {
				var c = ca[i];
				while (c.charAt(0)===' ') c = c.substring(1,c.length);
				if (c.indexOf(nameEQ) === 0) return c.substring(nameEQ.length,c.length);
			}
			return null;
		}
		
		if(readCookie('meerkat') != "dontshow"){
			//Loop through each
			return this.each(function() {
				
				//Creating a variable for "this" (referring to element attached to meerkat function) to avoid confusion later
				var element = jQuery(this);
				
				//Reset body and html margin to zero
				jQuery('html, body').css({'margin':'0', 'height':'100%'});
				
				//Find element attached to meerkat() and wrap the wrapper and container divs around it
				jQuery(element).wrap('<div id="meerkat-wrap"><div id="meerkat-container"></div></div>');
	
				//Add necessary styles to the two divs we just added
				jQuery('#meerkat-wrap').css({'position':'fixed', 'z-index': '10000', 'width': settings.width, 'height': settings.height}).css(settings.position, "0");
				jQuery('#meerkat-container').css({'background': settings.background, 'height': settings.height});
				
				if(settings.position === "left" || settings.position === "right"){ jQuery('#meerkat-wrap').css("top", 0);}
				
				if(settings.opacity != null){
					jQuery("#meerkat-wrap").prepend('<div class="opacity-layer"></div>');
					jQuery('#meerkat-container').css({'background': 'transparent', 'z-index' : '2', 'position': 'relative'});
					jQuery(".opacity-layer").css({
							'position': 'absolute', 
							'top' : '0', 
							'height': '100%', 
							'width': '100%',  
							'background': settings.background, 
							"opacity" : settings.opacity
						});					

				}
				
				//If browser is Internet Explorer AND its version is less than or equal to 6
				if(jQuery.browser.msie && jQuery.browser.version <= 6){
					
					//IE6 needs #meerkat-wrap to have a position of absolute because it doesn't support "fixed"
					jQuery('#meerkat-wrap').css({'position':'absolute', 'bottom':'-1px'});
					
					//Find all content to wrap in ie6-content-container but filter out meerkat-wrap
					jQuery('body').children()
						.filter(function (index) {
							return jQuery(this).attr('id') != 'meerkat-wrap';
						})
					.wrapAll('<div id="ie6-content-container"></div>');
					
					//Add all the conditional (to IE6 or below) CSS
					jQuery('html, body').css({'height':'100%', 'width':'100%', 'overflow':'hidden'});
					jQuery('#ie6-content-container').css({'overflow':'auto', 'width':'100%', 'height':'100%', 'position':'absolute'});
					
					//Check if ie6-content-container has a scrollbar present. If it does we need to move the meerkat container over 17px
					var ie6ContentContainer = document.getElementById('ie6-content-container');
					
					if((ie6ContentContainer.clientHeight < ie6ContentContainer.scrollHeight) && (settings.position != 'left')) {
						jQuery('#meerkat-container').css({'margin-right':'17px'});
						jQuery('.opacity-layer ').css({'right':'17px'});
					}
					
					//Build up a string variable "bgProperties" to get the body's background property values
					var bgProperties = document.body.currentStyle.backgroundColor+ " ";
					bgProperties += document.body.currentStyle.backgroundImage+ " ";
					bgProperties += document.body.currentStyle.backgroundRepeat+ " ";
					bgProperties += document.body.currentStyle.backgroundAttachment+ " ";
					bgProperties += document.body.currentStyle.backgroundPositionX+ " ";
					bgProperties += document.body.currentStyle.backgroundPositionY;
					
					//Remove body background property values from IE6
					jQuery("body").css({'background':'none'});
					
					//Move body background property values to #ie6-content-container
					jQuery("#ie6-content-container").css({'background' : bgProperties});
				}
				
				
				switch (settings.animationIn)
				{
					case "slide":
						if(settings.position === "left" || settings.position === "right"){
							jQuery('#meerkat-wrap, #meerkat-container > *').hide().delay(settings.delay).animate({width: 'show'},settings.animationSpeed, settings.easingIn);
						}else {
							jQuery('#meerkat-wrap, #meerkat-container > *').hide().delay(settings.delay).animate({height: 'show'},settings.animationSpeed, settings.easingIn);
						}
						break;
					case "fade":
						jQuery('#meerkat-wrap, #meerkat-container > *').hide().delay(settings.delay).animate({opacity: "show"},settings.animationSpeed, settings.easingIn);
						break;
					case "none":
						jQuery('#meerkat-wrap, #meerkat-container > *').hide().delay(settings.delay).show(0);
						break;
					default:
						alert('The animationIn option only accepts "slide", "fade", or "none"');
				}
				
				switch (settings.animationOut)
				{
					case "slide":
						if(settings.timer != null){
							if(settings.position == "left" || settings.position == "right"){
								jQuery('#meerkat-wrap, #meerkat-container > *').delay(settings.timer).animate({width: 'hide'},settings.animationSpeed, settings.easingOut);
							}else {
								jQuery('#meerkat-wrap, #meerkat-container > *').delay(settings.timer).animate({height: 'hide'},settings.animationSpeed, settings.easingOut);	
							}
						}
						jQuery(settings.hide).click(function(){
							if(settings.position == "left" || settings.position == "right"){
								jQuery('#meerkat-wrap, #meerkat-container > *').stop().animate({width: 'hide'},settings.animationSpeed, settings.easingOut);
							}else {
								jQuery('#meerkat-wrap, #meerkat-container > *').stop().animate({height: 'hide'},settings.animationSpeed, settings.easingOut);	
							}
						});
						jQuery(settings.dontShow).click(function(){
							if(settings.position == "left" || settings.position == "right"){
								jQuery('#meerkat-wrap, #meerkat-container > *').stop().animate({width: 'hide'},settings.animationSpeed, settings.easingOut);
							}else {
								jQuery('#meerkat-wrap, #meerkat-container > *').stop().animate({height: 'hide'},settings.animationSpeed, settings.easingOut);	
							}
							createCookie('meerkat','dontshow', settings.cookieExpires);
						});
						break;
					
					case "fade":
						if(settings.timer != null){
							jQuery('#meerkat-wrap, #meerkat-container > *').delay(settings.timer).animate({opacity: 'hide'},settings.animationSpeed, settings.easingOut);
						}
						jQuery(settings.hide).click(function(){
							jQuery('#meerkat-wrap, #meerkat-container > *').stop().animate({opacity: 'hide'},settings.animationSpeed, settings.easingOut);
						});
						jQuery(settings.dontShow).click(function(){
							jQuery('#meerkat-wrap, #meerkat-container > *').stop().animate({opacity: 'hide'},settings.animationSpeed, settings.easingOut);
							createCookie('meerkat','dontshow', settings.cookieExpires);
						});
						break;
					
					case "none":
						if(settings.timer != null){
							jQuery('#meerkat-wrap, #meerkat-container > *').delay(settings.timer).hide(0);
						}
						jQuery(settings.hide).click(function(){
							jQuery('#meerkat-wrap, #meerkat-container > *').hide();
						});
						jQuery(settings.dontShow).click(function(){
							jQuery('#meerkat-wrap, #meerkat-container > *').hide();
							createCookie('meerkat','dontshow', settings.cookieExpires);
						});
						break;
					
					default:
					  alert('The animationOut option only accepts "slide", "fade", or "none"');
				}
	
			});
		} else {
			jQuery("#meerkat").hide();	
		}
	}
});