//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',
			height: 'auto',
			position: 'bottom',
			closeMeerkat: '.close',
			dontShowMeerkat: '#dont-show',
			showAnimation: 'none',
			hideAnimation: 'none',
			animationSpeed: 'normal',
			cookieExpires: 0
		}
				
		//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);
		
		//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', 'width':'100%', 'height': settings.height}).css(settings.position, "0");
				jQuery('#meerkat-container').css({'background': settings.background, 'height': settings.height});
				
				//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) {
						jQuery('#meerkat-container').css({'margin-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.showAnimation)
				{
					case "slide":
						jQuery('#meerkat-wrap').hide().slideDown(settings.animationSpeed);
						break;
					case "fade":
						jQuery('#meerkat-wrap').hide().fadeIn(settings.animationSpeed);
						break;
					case "none":
						jQuery('#meerkat-wrap').show();
						break;
					default:
						alert('The showAnimation option only accepts "slide", "fade", or "none"');
				}
				
				switch (settings.hideAnimation)
				{
					case "slide":
						jQuery(settings.closeMeerkat).click(function(){
							jQuery('#meerkat-wrap').slideUp(settings.animationSpeed);
						});
						jQuery(settings.dontShowMeerkat).click(function(){
							jQuery('#meerkat-wrap').slideUp(settings.animationSpeed);
							createCookie('meerkat','dontshow', settings.cookieExpires);
						});
						break;
					
					case "fade":
					  jQuery(settings.closeMeerkat).click(function(){
							jQuery('#meerkat-wrap').fadeOut(settings.animationSpeed);
						});
					  jQuery(settings.dontShowMeerkat).click(function(){
							jQuery('#meerkat-wrap').fadeOut(settings.animationSpeed);
							createCookie('meerkat','dontshow', settings.cookieExpires);
						});
						break;
					
					case "none":
					  jQuery(settings.closeMeerkat).click(function(){
							jQuery('#meerkat-wrap').hide();
						});
					  jQuery(settings.dontShowMeerkat).click(function(){
							jQuery('#meerkat-wrap').hide();
							createCookie('meerkat','dontshow', settings.cookieExpires);
						});
						break;
					
					default:
					  alert('The hideAnimation option only accepts "slide", "fade", or "none"');
				}
	
			});
		} else {
			jQuery("#meerkat").hide();	
		}
	}
});
