/*--------------------------------------------------------------------------*/
/**
* 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);

/*--------------------------------------------------------------------------*/ 

var SlideShow = function (container, slides, delay, initial) {
    this.container = $(container);
    this.slides = $(slides);
    this.delay = delay;
    this.current = initial;
    this.show = null;
    this.hide = null;
    this.togglers = this.container.find('.togglers li');
    
	//this.prevBtn = this.container.find('#prevBtn');
	//this.nextBtn = this.container.find('#nextBtn');

    this.setup();
};
SlideShow.prototype = {
    setup: function(){
        var self = this;
        this.showOnly($(this.slides[this.current]));
        $(this.togglers[this.current]).addClass('active');
        
        this.togglers.hover(function(ev) {
			ev.preventDefault();
            self.pause();
            self.goTo($(this).index());
        });
		
		/*
		this.prevBtn.click(function (ev) {
			ev.preventDefault();
			self.back();
		});
		this.nextBtn.click(function (ev) {
			ev.preventDefault();
			self.forward();
		});
		*/
        this.start();
    },
    showOnly: function(el){
        this.slides.hide();
        el.show();
    },
    goTo: function(idx){
        var self = this;
        if (idx === this.current) {
            return;
        }
        $(this.togglers[this.current]).removeClass('active');
        $(this.slides[this.current]).fadeOut(400);
        this.current = idx;
        $(this.togglers[this.current]).addClass('active');
        $(this.slides[this.current]).fadeIn(400);
        self.start();
    },
    start: function(){
        if (this.timer) {
            return;
        }
        var self = this;
        this.timer = setTimeout(function(){
            self.next();
        }, this.delay * 1500);
    },
    pause: function(){
        if (this.timer) {
            clearTimeout(this.timer);
            this.timer = false;
        }
        if (this.hide) {
            this.hide.cancel();
        }
        else {
            return;
        }
    },
    back: function() {
		/*
        if (this.show || this.hide) {
            return;
        }
		*/
        this.pause();
        if (this.current === 0) {
            this.goTo(this.slides.length - 1);
        }
        else {
            this.goTo(this.current - 1);
        }
        this.start();
        //ev.stop(); 
    },
    forward: function(ev){
	/*
        if (this.show || this.hide) {
            return;
        }
	*/
        this.pause();
        this.next();
        //ev.stop(); 
    },
    next: function(){
        this.timer = false;
        if (this.current === this.slides.length - 1) {
            this.goTo(0);
        }
        else {
            this.goTo(this.current + 1);
        }
        this.start();
    }
};

/*--------------------------------------------------------------------------*/ 

var Toggler = function (toggler, contents) {
	this.toggler = $(toggler);
	this.contents = $(contents);
    this.setup();
};
Toggler.prototype = {
	setup: function () {
		var self = this;
		this.toggler.toggle(
			function () {
				$(this).addClass('active');
				self.contents.slideDown(200);
			},
			function () {
				$(this).removeClass('active');
				self.contents.slideUp(200);
			}
		);
	}
};

/*---------------------------------------------------------------------------*/

Number.prototype.between = function( first,last) {
    return (first < last ? this >= first && this <= last : this >= last && this <= first);
}

/*---------------------------------------------------------------------------*/

var Interval = function (min, max) {
	this.min = min;
	this.max = max;
};

/*---------------------------------------------------------------------------*/

var Slider = function (container) {
	this.container = $(container);
	this.contents = container.find('.slider_gallery ul');
	this.slides = container.find('.slider_gallery li');
	this.togglers = container.find('.slider_togglers li');
	this.track = container.find('.track');
	this.slideWidth = this.slides.width();
	this.moreTriggers = this.slides.find('h2 a');
	this.mores = this.togglers.find('.more');
	this.intervals = [];
	this.setup();
};
Slider.prototype = {
	setup: function () {
		var self = this;
		this.contents.width(this.contents.find('li').width() * this.contents.find('li').length);
		var itemsWidth = this.contents.innerWidth() - this.container.outerWidth();
		this.setIntervals();
		this.monitor(0);
		//$(this.togglers[0]).addClass('active');
		
		this.track.slider({
			min: 0,
			max: itemsWidth,
			stop: function (event, ui) { 
				self.monitor(ui.value);
				self.goTo(ui.value * -1, 500); 
			},
			slide: function (event, ui) { 
				self.monitor(ui.value);
				self.goTo(ui.value * -1, 0); 
			}
		});
		this.togglers.find('img').click(function () {
			var toggler = $(this).parent('li');
			self.togglers.removeClass('active');
			toggler.addClass('active');
			self.goTo(-($(toggler).index() * self.slideWidth), 500, $(toggler).index());
			
			self.track.find('a').animate(
				{ left: toggler.offset()['left'] - toggler.width() },
				{ duration: 100 }
			);
		});
		
		$(document).bind('ismoving', function () { self.mores.hide(); });
		
		this.mores.each(function () {
			var pop = $(this);
			$(this).find('.close').click(function () { pop.hide(); });
		});	
		
		this.moreTriggers.click(function (ev) {
			ev.preventDefault();
			self.mores.hide();
			$(self.mores[$(this).index()]).show();
		});
	},
	setIntervals: function () {
		var self = this;
		this.slides.each(function (idx) {
			self.intervals[idx] = new Interval(idx * self.slideWidth, (idx * self.slideWidth) + self.slideWidth - 1);
		});
	},
	monitor: function (val) {
		var self = this;
		$.each(this.intervals, function (key, value) {
			if (val.between(value.min, value.max)) {
				self.togglers.removeClass('active');
				$(self.togglers[key]).addClass('active');
			}
		});
	},
	goTo: function (xCoord, dur, idx) {
		$(document).trigger('ismoving');
		this.contents.animate(
			{ 'left': xCoord }, 
			{ duration: dur }
		);
	}
};

/*---------------------------------------------------------------------------*/

$(document).ready(function () {
	$('aside .list').each(function () { new Toggler($(this).find('h4'), $(this).find('.contents')); });

	$('header nav li.trigger').hoverIntent({
		interval: 100,
		over: function () {
			$(this).addClass('active');
			$(this).find('ul').fadeIn(200);
		},
		out: function () {
			$(this).removeClass('active');
			$(this).find('ul').fadeOut(200);
		}
	});
	
	/* homepage */
	if ($('.hero').length) {
		new SlideShow($('.hero'), $('.hero .slide'), 5, 0);
		$('.hero .togglers li').hoverIntent({
			interval: 100,
			over: function () { $(this).find('.title').fadeOut(150); },
			out: function () { $(this).find('.title').fadeIn(150); }
		});
	}	
});

/*---------------------------------------------------------------------------*/

window.onload = function () {
	if ($('#slider').length) { new Slider($('#slider')); }
};
