/**
* hoverIntent r5 // 2007.03.27 // jQuery 1.1.2+
* <http://cherne.net/brian/resources/jquery.hoverIntent.html>
* 
* @param  f  onMouseOver function || An object with configuration options
* @param  g  onMouseOut function  || Nothing (use configuration options object)
* @author    Brian Cherne <brian@cherne.net>
*/
(function($){$.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);};})(jQuery);

// this is the new version

(function($){
    
	if(!window.evo){
        evo = {};
    };
    
    evo.reveal = function(el, options){

        var self = this;
        
        self.$el = $(el);
        self.el = el;
        
        self.$el.data("evo.reveal", self);
        
        self.init = function(){
            self.options = $.extend({},evo.reveal.defaultOptions, options);
			self.align();
			
			self.$el.width(self.options.width);
			
			var desc = self.$el.find(".descWrap");
			
			desc.each(function(){
				$(this).data("oldpos",parseInt($(this).css("top")));
				$(this).data("oldwidth",$(this).css("width"));
				$(this).data("oldheight",$(this).css("height"));
				
				$(this).find("h2").each(
					function(){
						$(this).data("h2width",$(this).width());
					}
				);
			});
			$(".panel:last").data("last", true);
			
			self.bindEvents();
        };

			
		self.align = function(){
			
			var panel = self.$el.find(".panel");
			
			panel.wrapAll('<div class="snake"/>');
			
			$(".snake").width(panel.width()*panel.length);
			
			var newWidths = Math.ceil((self.options.width / self.options.visible)-5);
			
			panel.each(
				function(){
					
					$(this).width(newWidths);
					
					if ( $(this).index() > 0 ) {
						$(this).css({
								position:'absolute',
								left: (5+newWidths)*$(this).index()
						});
					}
					
					var description = $(this).find(".description");
					
					var headingHeight = description.find("h2").height();
									
					description.wrap('<div class="descWrap" />');
					
					description.parents('.descWrap').css({
						overflow:'hidden',
						width: parseInt(description.find("h2").width())+10,
						height: parseInt(description.find("h2").height())+10,
						position:'absolute',
						top: parseInt((self.options.height)) - (headingHeight+11)
					});
										
															
				}
			);
		}; // align function
		
		
		self.bindEvents = function(){
			var panel = self.$el.find(".panel");
			
			
			
			if (self.options.carousel) {
				self.carousel(panel.size());
			}
			
			if ( !self.options.noHover ) {
				panel.hoverIntent(
					self.expandPanel,
					self.retractPanel
				);
			}
			
			
		};
		
		self.carousel = function(panels) {
			
			var panels = panels;
			var currentPanel = 1;
			var panelSize = (self.options.width / self.options.visible) ;
			
			$("<a/>",{
				className:'carouselbtnLeft'
			  }).insertBefore(self.$el);
			
			$("<a/>",{
				className:'carouselbtnRight'
			  }).insertAfter(self.$el);
			
			$(".carouselbtnLeft").click(function(){
				if (currentPanel == 1) return false;
				currentPanel--;
				(currentPanel == 1) ? usePanel = 0 : usePanel = currentPanel;
				$('.snake').animate({left:-panelSize*(usePanel)});
			});
			
			$(".carouselbtnRight").click(function(){
				if ((panels - currentPanel) <= 1) return false;
				$('.snake').animate({left:-panelSize*(currentPanel)});
				currentPanel++;
			});
			
		};
		
		self.expandPanel = function(){
			
			$(this).stop(false,true).data("oldwidth",$(".panel").width());
			
			var expandOptions = {'width' : $(this).find("img").width() };
			
			if ($(this).data("last")) { 
				expandOptions.left = '-=' + ($(this).find("img").width() - ($(this).data("oldwidth")));
			}
			
			$(this).stop(false,true).animate(expandOptions,300);
			
			$(this).css({
				zIndex:2, 'marginTop': 0
			}).siblings().css({
				zIndex:1
			});
			
			$(this).find("img").css({
				'marginTop': -(($(this).height() / 2) - 1)
			});
			
			self.expandDescription( $(this) );
		};
		
		self.retractPanel = function(){
			
			var retractOptions = {'width' : $(this).data("oldwidth") };
			

			
			
			if ($(this).data("last")) { 
				retractOptions.left =  '+=' + ( $(this).find("img").width() - $(this).data("oldwidth") );
				
			}
			
			
			$(this).stop(false,true).animate(retractOptions,self.options.duration);
			
			$(this).find("img").css({'marginTop': 0});
			
			self.retractDescription( $(this) );
		};
		
		self.expandDescription = function(b){
			
			var desc = b.find(".descWrap");
desc.find("h2").css({display:'block'});
			b.find(".descWrap").stop(false,true).animate({
					width:b.find("img").width()+10,
					height:(parseInt(b.find(".description").height()+10))
				},
				self.options.duration);
			
			b.find(".descWrap").children(".description").stop(false,true).animate({
					width:b.find("img").width()-10
				},
				self.options.duration);
			
			desc.find("h2").stop(false,true).animate({
				width:"100%"
			},
				self.options.duration	
			);
			
			desc.stop(false,true).animate({	
					height: 118,
					top: 300
				},
				self.options.duration);
			
		};
		
        self.retractDescription = function(b){
			
        	var desc = b.find(".descWrap");
			
			desc.find("h2").css({display:'block'});
			desc.find("h2").animate({
				width:desc.find("h2").data("h2width")
			},
				self.options.duration	
			);
			
			desc.stop(false,true).animate({
				width:desc.data("oldwidth"),
				top:desc.data("oldpos"),
				height: desc.data("oldheight")},
				self.options.duration);
		};
        
        // Run initializer
        self.init();
    };
    
    evo.reveal.defaultOptions = {
        width: 920,
        height: 401,
		carousel: true,
		noHover: false,
		visible: 3,
		duration: 500
    };
    
    $.fn.reveal = function(options){
        return this.each(function(){
            (new evo.reveal(this, options));
        });
    };
    
})(jQuery);

