// ON LOAD
// ----------------------
$(document).ready(function(){
	resizebg();

	// page load
	// $('#main section').hide();
	// $('#main section').width(0);
	// $('#main section').queue(function(){
	// 	
	// });
/*
		$('#footer').height('0');
		$('.header, .connect, .content, #storeview, .footer, .teaofthemonthimg, .teaofthemonth, .promotion, .howtosteep').hide(); 
		$('#header, #connect, #history, #teaofthemonthimg, #teaofthemonth, #storeimg, #promotion, #howtosteep').width('0');
		
		$('#footer').animate({height: '30px'},{duration: 'slow', easing: 'easeInOutExpo'});
		$('.footer').delay(500).fadeIn(400);
		// Queue Start
		$('#footer').queue(function () {
			$('#header').animate({width: '380px'},{duration: 'fast', easing: 'easeInOutExpo'});
			$('.header').delay(500).fadeIn(400);
			$('#header').queue(function () {
				$('#connect').animate({ width: '80px'},{duration: 'slow', easing: 'easeInOutExpo'});
				$('.connect').delay(400).fadeIn(400);
				$('#history').animate({width: '450px'},{duration: 'slow', easing: 'easeInOutExpo'});
				$('.content').delay(800).fadeIn(400);
				$('#history').queue(function (){
					$('#storeimg').animate({width: '190px'},{duration: 'slow', easing: 'easeInOutExpo'});
					$('#promotion').animate({width: '270px'},{duration: 'slow', easing: 'easeInOutExpo'});
					$('#storeview, .promotion').delay(800).fadeIn(400);
					$('#teaofthemonthimg').animate({width: '240px'},{duration: 'slow', easing: 'easeInOutExpo'});
					$('.teaofthemonthimg').delay(800).fadeIn(400);
					$('#teaofthemonthimg').queue(function (){
						$('#teaofthemonth').animate({width: '440px'},{duration: 'slow', easing: 'easeInOutExpo'});
						$('.teaofthemonth').delay(800).fadeIn(400);
					});
				});	
			});
			$(this).dequeue();
		});
*/




	// resize bg image
	$(window).bind("resize", resizebg );
	function resizebg() {
		var iw = $('.bg').attr('data-width') || 1280;	// hard coded
		var ih = $('.bg').attr('data-height') || 680;
		var ratio = iw / ih;

		var x = $(window).width();
		var y = $(window).height();

		if ( (x / y) > ratio ){
		    $('.bg').width(x);
		    $('.bg').height(x / ratio);
			$('.bg').css('left', 0 );
			$('.bg').css('top', -($('.bg').height() - y) / 2 );
		} else {
		    $('.bg').height(y);
		    $('.bg').width(y * ratio);
			$('.bg').css('left', -($('.bg').width() - x) / 2 );
			$('.bg').css('top', 0 );
		}
	}

	// Contact ajax
	//$('form').submit(function(e){
		//e.preventDefault();

		//var load = $(this).addClass('loading');
		//$('form .ajax').fadeOut();

		//var args = {};
		//var inputs = $(this).serializeArray();
		//$.each(inputs,function(i,input) { args[input['name']]=input['value']; });
		//$.post( $(this).attr('action'), args, function(response){
			//load.removeClass('loading');

			//$('form .ajax').html(response.message);
			//$('form .ajax').fadeIn();

		//});
		//return false; // prevent submit (redundant)
	//});

});


// PLUGINS
// ----------------------
(function( $ ){

	$.fn.carousel = function(o) {
		o = $.extend({
			prev: null,
			next: null,
			jump: null,
			mouseWheel: false,
			speed: 200,
			easing: null,
			start: 0,
			circular: false,
			callback: null,
			beforeStart: null,
			width: false	// manually override width
		},
		o || {});
		return this.each(function() {
			var running = false,
			c = $(this),
			ul = $('> ul', c),
			f = $('> li', ul), 
			next = o.next ? $(o.next) : $('.next', c),
			prev = o.prev ? $(o.prev) : $('.prev', c),
			pages = f.length,
			items = $('li',f).length,
			curr = o.start;
			f.css('float','left');
			ul.css({
				'margin': '0',
				'position': 'relative',
				'z-index': 1
			});
			c.css({
				'visibility': 'visible',
				'overflow': 'hidden',
				'position': 'relative',
				'z-index': 2
			});
			var g = o.width ? o.width : f.outerWidth();	// f.offsetWidth;
			var h = g * pages;

			ul.css('width', h +'px').css('left', -(curr * g));

			if (prev.length > 0) prev.click(function() {
				return go(curr - 1);
			});
			if (next.length > 0) next.click(function() {
				var end = curr + 1 > pages - 1;
				return go(curr + 1);
			});
			if (o.jump) $.each(o.jump, function(i, a) {
				$(a).click(function() {
					return go(i);
				});
			});
			if (o.mouseWheel && c.mousewheel) c.mousewheel(function(e, d) {
				return d > 0 ? go(curr - 1) : go(curr + 1);
			});

			function go(a) {

				if (!running) {

					if(o.beforeStart) {					// hack specific to artAnywhere homepage
						var skip = o.beforeStart.call(this, a, curr); 
						if( skip ) {
							ul.css({ left: -(skip * g) });
							curr = skip;
							return false;
						}
					}

					if(o.circular) {
					    if(a < 0) {						// beg'n
					        ul.css('left', -(h) + "px");
					        curr = pages - 1;
					    } else if(a >= pages) {			// end
					        ul.css('left', 0);
					        curr = 0;
					    } else { 
							curr = a;
						}
					} else {
						if (a < 0 || a > pages - 1) return;
						else curr = a;
					}

					running = true;
					ul.animate({left: -(curr * g)}, o.speed, o.easing, function() {
						if (o.callback) {
							o.callback.call(this, curr, f.slice(curr).slice(0,1) );
						}
						running = false;
					});

	                if(!o.circular) {
						next.removeClass("disabled");
						prev.removeClass("disabled");
						$((curr - 1 < 0 && prev) || (curr + 1 > pages - 1 && next) || []).addClass("disabled");
					}

				}
				return false;
			}
		});
	};

})(jQuery);


// IE FAIL
// ----------------------
if (typeof console === "undefined" || typeof console.log === "undefined") {
	console = {};
	console.log = function() {};
}
