// hover states
jQuery(function($) {
	if (!jQuery.support.rgba) {
		$('#slides a.info, #controls a, #promo a.info').addClass('norgba');
	} else {
		$('#slides .slide').hover(function() {
			$(this).find('a.info').stop(true).animate({backgroundColor: "rgba(208,231,145,0.99)"}, 150);
		},function() {
			$(this).find('a.info').stop(true).animate({backgroundColor: "rgba(208,231,145,0.8)"}, 150);
		});
		$('#promo').hover(function() {
			$(this).find('a.info').stop(true).animate({backgroundColor: "rgba(233,206,79,0.99)"}, 150);
		},function() {
			$(this).find('a.info').stop(true).animate({backgroundColor: "rgba(233,206,79,0.8)"}, 150);
		});
		$('#controls a').hover(function() {
			$(this).stop(true).animate({ color: '#00456a', backgroundColor: '#9bcfe3'}, 150);
		}, function() {
			$(this).stop(true).animate({ color: '#9bcfe3', backgroundColor: '#00456a'}, 150);
		})
	}
});

// carousel
jQuery(function($) {
	var slides = $('#slides .slide');
	var w = $('#window');
	var curr = 0;
	var max = slides.length-3;
	var slideWidth = 238;
	if (slides.length <= 3) {
		$('#next,#prev').hide();
	}
	
	
	$('#next').click(function() {
		
		gotoSlide(curr + 1);
		return false;
	});
	$('#prev').click(function() {
		gotoSlide(curr - 1);
		return false;
	});

	initialize();
	
	function gotoSlide(i, init) {
		var offset = slideWidth * i * -1
		
		if (w.filter(':animated').length > 0) {
			return;
		}
		
		curr = i
		if (init) {
			w.css({marginLeft:offset});
		} else {
			$.when(w.stop(true).animate({marginLeft:offset}, {duration: 300, easing:'easeOutQuad'})).then(function() {
				if ((curr+slides.length) == 0) {
					gotoSlide(0, true);
				} else if ((curr-slides.length) == 0) {
					gotoSlide(0, true);
				}
				
			});
			
		}
		
	}
	function fill(section, init) {
		if (init) {
			slides.each(function(i, obj) {
				$(obj).css({left:slideWidth*i})
			});
		} else {
			var offset = slides.length * slideWidth * section;
			var clone = slides.clone(true);
		
			clone.each(function(i, obj) {
				$(obj).css({left:(slideWidth*i)+offset})
			});
			w.append(clone)
		}
	}
	function initialize() {
		fill(0, true);
		fill(-1);
		fill(1);
		gotoSlide(0, true);
	}
});

// t: current time, b: begInnIng value, c: change In value, d: duration
jQuery.easing['jswing'] = jQuery.easing['swing'];

jQuery.extend( jQuery.easing,
{
	def: 'easeOutQuad',
	swing: function (x, t, b, c, d) {
		//alert(jQuery.easing.default);
		return jQuery.easing[jQuery.easing.def](x, t, b, c, d);
	},
	easeInQuad: function (x, t, b, c, d) {
		return c*(t/=d)*t + b;
	},
	easeOutQuad: function (x, t, b, c, d) {
		return -c *(t/=d)*(t-2) + b;
	},
	easeInOutQuad: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t + b;
		return -c/2 * ((--t)*(t-2) - 1) + b;
	}
});

