(function($){
	if(!window.evo){
		evo = {}
	};
	
	//hover intent plugin http://cherne.net/brian/resources/jquery.hoverIntent.html
	$.fn.hoverIntent=function(f,g){var cfg={sensitivity:7,interval:100,timeout:0};cfg=$.extend(cfg,g?{over:f,out:g}:f);var cX,cY,pX,pY;var track=function(ev){cX=ev.pageX;cY=ev.pageY;};var compare=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);if((Math.abs(pX-cX)+Math.abs(pY-cY))<cfg.sensitivity){$(ob).unbind("mousemove",track);ob.hoverIntent_s=1;return cfg.over.apply(ob,[ev]);}else{pX=cX;pY=cY;ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}};var delay=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);ob.hoverIntent_s=0;return cfg.out.apply(ob,[ev]);};var handleHover=function(e){var p=(e.type=="mouseover"?e.fromElement:e.toElement)||e.relatedTarget;while(p&&p!=this){try{p=p.parentNode;}catch(e){p=this;}}if(p==this){return false;}var ev=jQuery.extend({},e);var ob=this;if(ob.hoverIntent_t){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);}if(e.type=="mouseover"){pX=ev.pageX;pY=ev.pageY;$(ob).bind("mousemove",track);if(ob.hoverIntent_s!=1){ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}}else{$(ob).unbind("mousemove",track);if(ob.hoverIntent_s==1){ob.hoverIntent_t=setTimeout(function(){delay(ev,ob);},cfg.timeout);}}};return this.mouseover(handleHover).mouseout(handleHover);};
		
	evo.pushdown = function(el, options){
		// To avoid scope issues, use '$self' instead of 'this'
		// to reference this class from internal events and functions.
		var $self = this;
		
		var el = $("body");
		
		// Access to jQuery and DOM versions of element
		$self.$el = $(el);
		$self.el = $self.$el[0];
		
		// Add a reverse reference to the DOM object
		$self.$el.data("evo.pushdown", $self);
		
		$self.init = function(){
			
			evo.opts = $self.options = $.extend({},evo.pushdown.defaultOptions, options);
			
			$self.insertStyleSheets([evo.opts.stylesheet]);
			

			// if background starts with a hash, expect a hex color, if it doesn't, wrap in the url() css
			if ( !evo.opts.background.match("^#") ) {
				evo.opts.background = 'url('+evo.opts.background+')';
			}

			for (i=0;i<evo.opts.multiBg.length;++i){
				if ( !evo.opts.multiBg[i].match("^#") ) {
					evo.opts.multiBg[i] = 'url('+evo.opts.multiBg[i]+')';
				}
			}


				
			if ( evo.opts.swf != 'none' ){
				$self.insertFlash(evo.opts.swf, evo.opts.expandedHeight, evo.opts.width, $self.interactions);
			} else {
				$self.insertAds(
					{ //image
						src: evo.opts.img,
						height: evo.opts.expandedHeight
					},
					evo.opts.url,
					$self.interactions
				);
			}
			
			
			return false;
		};
		
		
		
		$self.insertStyleSheets = function(styles){
		/**
		 *
		 *	This function takes an array of stylesheets as it's parameter, 
		 *  it loops through the array and inserts each stylesheet if it
		 *  doesn't already exist in the <head>
		 *  
		 *  @author Liam Potter <lpotter@designedbyevo.com>
		 *
		 *  @param  syles	An array of stylesheets to insert into the head
		 *
		*/
			for ( var i=0; i<styles.length; ++i ){
				
				if ( $("link[href='"+styles[i]+"']" ).length == 0 )
				{
					
					if (document.createStyleSheet) //for IE 
					{
						document.createStyleSheet(styles[i]);
					}
					else
					{
						$("<link/>", {
							"rel": "stylesheet",
							"type": "text/css",
							"href": styles[i]
						}).appendTo("head");
						
					}
				}
			}

			return false;
		};//$self.insertStyleSheets(styles);
		
		
		

		$self.insertAds = function(img,url,callback){
			
		/**
		 *
		 *	This will insert the markup as the first child of the body element
		 *  
		 *  @author Liam Potter <lpotter@designedbyevo.com>
		 *
		 *  @param img			object. this is the src url & height of the image
		 *  @param url			string. this will be the href of the ad link
		 *	@param callback		function. pass a function to be ran at end ($self.interactions)
		 */
						
			if (typeof img == 'undefined' ) {
				img = {src:'http://dummyimage.com/980x105/000/fff.png&text=No%20Image&20Specified',height:evo.opts.expandedHeight}
				return false;
			}
			if (typeof url == 'undefined' ) {
				url = '#';
				return false;
			}
			
			$self.pushdown = $('<div/>', {  
				id: 'pushdown' 
			})
			.css(
				{
					height:0,
					background:evo.opts.background
				}
			)
			.prependTo($self.$el);
			
			if ( evo.opts.multiBg !== false ){
				$self.leftBg = $('<div/>',{
					id: 'leftBg'		 
				})
				.css({
					 background:evo.opts.multiBg[0],
					 height:evo.opts.expandedHeight
				})
				.prependTo($self.pushdown);
				
				$self.rightBg = $('<div/>',{
					id: 'rightBg'		 
				})
				.css({
					 background:evo.opts.multiBg[1],
					 height:evo.opts.expandedHeight
				})
				.prependTo($self.pushdown);
			}
			
			$self.pushdownLink = $('<a/>',{
				href: url,
				target: '_blank'
			})
			.css("width",evo.opts.width)
			.prependTo($self.pushdown);
						
			$self.pushdownImg = $("<img/>", {
				src: img.src,
				height:img.height,
				alt:evo.opts.alt,
				width:evo.opts.width
			}).appendTo($self.pushdownLink);
			
			
			
			if (typeof callback == 'function'){
				callback();
			}
			
			return false;
		}; //$self.insertAds(img,url);
		
		
		
		
		$self.insertFlash = function(movie,movieHeight,movieWidth,callback){
			
		/**
		 *
		 *	This will insert the markup as the first child of the body element
		 *  If a swf and an img are provided, the swf will take precedence. 
		 *  
		 *  @author Liam Potter <lpotter@designedbyevo.com>
		 *
		 *  @param movie			string. path to swf file (evo.opts.swf)
		 *  @param movieHeight		interger. height of movie (evo.opts.expandedHeight)
		 *	@param movieWidth		interger. width of movie (evo.opts.width)
		 *  @param callback			function. pass a function to be ran at end ($self.interactions)
		 *
		 */
		 
			$.getScript(evo.opts.pathToSwfObject,function(){
				$self.pushdown = $('<div/>', {  
					id: 'pushdown' 
				})
				.css({
					height:0,
					background:evo.opts.background
				})
				.prependTo($self.$el);
				
				if ( evo.opts.multiBg !== false ){
					$self.leftBg = $('<div/>',{
						id: 'leftBg'		 
					})
					.css({
						 background:evo.opts.multiBg[0],
						 height:evo.opts.expandedHeight
					})
					.prependTo($self.pushdown);
					
					$self.rightBg = $('<div/>',{
						id: 'rightBg'		 
					})
					.css({
						 background:evo.opts.multiBg[1],
						 height:evo.opts.expandedHeight
					})
					.prependTo($self.pushdown);
				}
				
				$self.flashContainer = $('<div/>',{
					id:'flashContent',
					text:'Please install the Adobe Flash Player'
				})
				.css("width",evo.opts.width)
				.prependTo($self.pushdown);
				
				var flash = $.flash.create({
					swf: movie,
					params:{
						allowScriptAccess: 'sameDomain',
						allowFullScreen: 'true',
						wmode: 'transparent'
					},
					flashvars:{
						clickTag: evo.opts.url	
					},
					height: movieHeight,
					width: movieWidth
				});
				
				$($self.flashContainer).html(flash);
				
				$self.rollHere = $("<span/>", {
					id: 'rolloverHere',
					text: 'Rollover Here'
				}).appendTo($self.flashContainer);
				
				if (typeof callback == 'function'){
					callback();
				}
			});
			
			return false;
			
		};
		
		$self.interactions = function(){
			
			var hoverConfig = {    
				 over: expand, // function = onMouseOver callback (REQUIRED)    
				 timeout: 250, // number = milliseconds delay before onMouseOut    
				 out: collapse // function = onMouseOut callback (REQUIRED)    
			};

			function expand(){
				$(this).stop().animate(
					{
					height:evo.opts.expandedHeight
					},
					{
						duration:evo.opts.duration,
						easing:evo.opts.easing
					}
				);
				
				$self.rollHere
				.stop().animate(
					{
						opacity:0
					}
				);
			}
			
			function collapse(){
				$(this).stop().animate(
					{
						height:evo.opts.collapsedHeight
					},
					{
						duration:evo.opts.duration,
						easing:evo.opts.easing
					}
				);
				
				$self.rollHere
				.stop().animate(
					{
						opacity:1
					}
				);
			}
			
			$self.pushdown
			.stop().animate(
				{
					height:evo.opts.collapsedHeight
				},
				{
					duration:evo.opts.duration,
					easing:evo.opts.easing
				}
			)
			.hoverIntent(hoverConfig)
			
			
			
			
			return false;
		};//$self.interactions();
		
		// Run initializer
		$self.init();
	};
	
	evo.pushdown.defaultOptions = {
		stylesheet: '/assets/js/pushdown/styles.css?v2',
		
		img: 'http://dummyimage.com/980x105/000/fff.png',
		url: 'http://www.google.com',
		alt: 'Alt Text for Images',
		
		swf: 'none',
		pathToSwfObject: '/assets/js/pushdown/swfobject.js',
		
		collapsedHeight: 50,
		expandedHeight: 105,
		width: 980,
		
		background:'#000',
		multiBg: false,		
		
		easing: "linear", // must include the easing plugin if this is used
		duration: 250
	};
	
	/*$.fn.pushdown = function(options){
		return this.each(function(){
			(new evo.pushdown(this, options));
		});
	};*/
	
	$.extend({
		pushdown: function(options){
			(function(){
				new evo.pushdown(this, options);
			})();
		}
	});
		
})(jQuery);
