(function($){
	if(!$.evo){
		$.evo = new Object();
	};
	
	//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]);
			
			$self.insertAds(
				{ //image
					src: $.evo.opts.img,
					height: $.evo.opts.expandedHeight
				},
				$.evo.opts.url
			);
			
			$self.interactions();
			
			return false;
		};
		
		
		/**
		 *
		 *	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@evoffect.com
		 *
		 *  @param {array} syles	An array of stylesheets to insert into the head
		 *
		*/
		$self.insertStyleSheets = function(styles){
			var arLen=styles.length;
			for ( var i=0, len=arLen; i<len; ++i ){
				
				if (document.createStyleSheet) //for IE 
				{
					document.createStyleSheet(styles[i]);
				}
				else
				{

					if ( $("link[href='"+styles[i]+"']" ).length == 0 ){
						$("<link/>", {
							"rel": "stylesheet",
							"type": "text/css",
							"href": styles[i]
						}).appendTo("head");
					} else {
						return false;
					}
				}
			}

			return false;
		};//$self.insertStyleSheets(styles);
		
		
		/**
		 *
		 *	This will insert the markup as the first child of the body element
		 *  If only the large image is provided it is assumed the top of the large
		 *  image is to be displayed in place of a small image
		 *  
		 *  @author Liam Potter			lpotter@evoffect.com
		 *
		 *  @param {object} img			This is the src url & height of the large image
		 *  @param {string} url			This will be the href of the ad link
		 *
		*/

		$self.insertAds = function(img,url){
						
			if (typeof img == 'undefined' ) {
				img = {src:'http://dummyimage.com/980x105/000/fff.png',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);
			
			$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);
			
			$self.rollHere = $("<span/>", {
				id: 'rolloverHere',
				text: 'Rollover Here'
			}).appendTo($self.pushdownLink);
			
			return false;
		}; //$self.insertAds(img,url);
		
		$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',
		img: 'http://dummyimage.com/980x105/000/fff.png',
		url: 'http://www.google.com',
		alt: 'Alt Text for Images',
		collapsedHeight: 50,
		expandedHeight: 105,
		width: 980,
		background:'#000',
		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);