// Begin the madness

$(document).ready(function(){

	/* Globals
	---------------------------------------------*/

	// Vars
	var header = $('.nav-main');
	var mainNav = $('.footer a, .header a');
	var content = $('.content-main');
	var footer = $('.footer');
	var preloader = $('.preloader');
	var testTip = $('.testimonial .tooltip');
	var testClient = $('.testimonial .client');

	// Dropdown Menu
	qm_create(0,false,0,500,false,true,true,false,false);

	// Preloader & Initial Properties
	preloader.delay(600).animate({'top':'48%', 'opacity' : '1'}, 0);

	// Window loaded
	$(window).load(function(){

		if ($.browser.msie) {
			// You suck IE
			preloader.remove();
			content.show();
			footer.show();
			header.css('overflow','visible');
		}else{
			preloader.delay(1100).animate({'top' : '46%', 'opacity' : '0'}, 0, function(){
				// FadeIn content
				content.delay(500).fadeIn(1200);
				footer.delay(500).fadeIn(1200, function(){
					// Reset scrolling
					// $('html').css('overflow-y', 'auto');
				});
			});

		// Fix dropdown load issue
		$('#qm0 .children').css('top','20px');

		}

		// Add top to dropdown
		$('#qm0 .children').each(function(){
			$(this).append('<span class="drop-top"/>');
		});

		// Home ingredient slider
		var iCon = $('.ingredients-container');
		var iNext = $('.ingredients .next');
		var iPrev = $('.ingredients .prev');
		var iItem = iCon.children('li');
		var iItemWidth = 123; // 80px + 43px margin
		var iItemCount = iItem.length; // Number of items
		var iItemMin = 7;
		var iConWidth = (iItemCount * iItemWidth) - 871; // Subract 7 items being shown -12px margin
		var iConLimit = -(iConWidth);

		// - Set default position
		iCon.css('left', '10px');

		if(iItemCount <= iItemMin){
			iNext.hide();
			iPrev.hide();
		}else{
			iPrev.hide();
		}

		var ingredNext = function(){
			if($(iCon).position().left > iConLimit && !$(iCon).is(':animated')){
				$(iCon).animate({'left' : '-=' + iItemWidth + ''}, 300, 'easeInOutExpo', function(){
					if($(iCon).position().left == iConLimit){
						iNext.fadeOut(300);
						iPrev.fadeIn(300);
					}else{
						iPrev.fadeIn(300);
					}
				});
			}
		}

		var ingredPrev = function(){
			if($(iCon).position().left < -100 && !$(iCon).is(':animated')){
				$(iCon).animate({'left' : '+=' + iItemWidth + ''}, 300, 'easeInOutExpo', function(){
					if($(iCon).position().left == 10){
						iPrev.fadeOut(300);
						iNext.fadeIn(300);
					}else{
						iNext.fadeIn(300);
					}
				});
			}
		}

		// - Slap on those click functions
		iNext.click(ingredNext);
		iPrev.click(ingredPrev);

		iItem.hover(
			function(){
				$(this).children('.bubble-ingredient').fadeIn(300);
			},
			function(){
				$(this).children('.bubble-ingredient').fadeOut(300);
			}
		);
	}); // End window load

	/* About Us
	---------------------------------------------*/

	// Gallery
	$('a.gallery').colorbox({rel:'gal'});

	var qBtn = $('.faq li .question');

	var faqToggle = function(){
		if($(this).parent().hasClass('current')){
			// stupid :not filter >:$	
			}else{
			$('.faq .answer.current').slideToggle(300).removeClass('current');
			$('.faq li.current').removeClass('current')
			$(this).next('.answer').slideToggle(300, 'easeOutExpo').addClass('current');
			$(this).parent().addClass('current');
		}
	}

	qBtn.click(faqToggle);
	
	/* Soups individual page
	---------------------------------------------*/

	// Replace titles - sigh..
	$('.postid-391 .soup-individual h1').html('Carrot Ginger <span class="small">with Coconut</span>');
	$('.postid-388 .soup-individual h1').html('White Bean <span class="small">with Tomato</span>');
	$('.postid-303 .soup-individual h1').html('Green Pea <span class="small">with Dill</span>');

	var soupItBtn = $('.soup-it-up');
	var soupItClose = $('.soup-it-up-close');
	var fullFacts = $('#soup-full-facts');
	var fullFactsClose = $('.btn-close.full-facts');
	var fullNutritionBtn = $('.show-full-nutrition');
	var fullIngredientsBtn = $('.show-full-ingredients');

	// Soup form
	$("a[rel='.soup-form']").overlay({
		top: '20%',
		mask: {
			color: '#000',
			loadSpeed: 200,
			opacity: 0.80
		},
		closeOnClick: true
	});

	// - DOM z-index issues
	$('.soup-form').appendTo('body');

	// Hide soup it up
	$('#soup-it-up-container').hide();

	// Close Button
	fullFactsClose.click(function(){
		$(this).parent().fadeOut(300);
	});

	function showFacts(){
		fullFacts.fadeIn(300);
		return false;
	};

	fullNutritionBtn.click(showFacts);
	fullIngredientsBtn.click(showFacts);

	soupItClose.click(function(){
		$('#soup-it-up-container').slideToggle(600, 'easeOutExpo');
		return false;
	})

	soupItBtn.click(function(){
		$('#soup-it-up-container').slideToggle(600, 'easeOutExpo');
		return false;
	});

	soupItBtn.hover(
	function(){
			$(this).text('Click for great recipe ideas!');
		},
		function(){
			$(this).text('Soup it up!');			
		}
	)

	$(".soup-it-up-slides").slides({
		crossfade: false,
		effect: 'fade',
		fadeSpeed: 400
	});

	// Click functions
	mainNav.click(function() {
		// IE can't handle
		if(!$.browser.msie){
      var url = $(this).attr('href');
      content.fadeOut(900 ,
      function() {
        document.location.href = url;
      });
      footer.fadeOut(900);
      return false;
    }
	});

	// CSS helpers!

	// - Regular links (not awesome buttons)
	$('.content-main a:not(.btn)').addClass('reg');
	// - Format Soup Friends
	$('.soup-friends li:odd').addClass('odd');
	// - Button class for contact form
	$('.wpcf7-submit').addClass('btn red');
	// - Button class for soup form
	$('.soup-form .wpcf7-submit').addClass('btn green');
	// - Find Soup li class helper
	$('.map-retailers li:nth-child(7n)').addClass('last');
	// - Our Soups li class helper
	$('.soup-tags li:last-child').addClass('last');
	// - Dairy Free needs to be last - guhh..
	if($('.soup-slides .soup-tags:has(.dairy-free)')){
		var df = $('.soup-slides .soup-tags:has(.dairy-free) .dairy-free').parent();
		$('.soup-slides .soup-tags:has(.dairy-free) .last').removeClass('last');
		df.appendTo($('.soup-slides .soup-tags:has(.dairy-free)'))
		df.addClass('last');
	};
	// - Even/Odd li styles
	$('.even-odd li:even').addClass('even');
	$('.even-odd li:odd').addClass('odd');
	$('.even-odd li:last').addClass('last');
	// - Transparent images for IE
	if($.browser.msie){
		$('img[src*=".png"]').addClass('trans');
	}
	// - Focus class for inputs IE - seriously..
	$('input[type="text"], textarea').bind('blur', function(){ $(this).toggleClass('focus'); });
	$('input[type="text"], textarea').bind('focus', function(){ $(this).toggleClass('focus'); });
	// - Firefox fix for soup-it-up
	if($.browser.mozilla){
		$('.soup-it-up-slides .slides_container').css('top','48px');
		$('.soup-it-up-slides .pagination').css({ 'position' : 'relative', 'top' : '45px' });
	}

	/* Home
	---------------------------------------------*/

	// Variables
	var sHome = function(){
		slide = $('.active .slide'),
		slideProduct = $('.active .slide:not(.four) .slide-product'),
		slideProductOne = $('.active .slide-product.one'),
		slideProductTwo = $('.active .slide-product.two'),
		slideProductThree = $('.active .slide-product.three'),
		slideShadow = $('.active .slide-shadow')
	};


	// Home Slider
	if($.browser.msie){
		// Awful IE
		$(".slides").slides({
		preload: true,
		crossfade: false,
		effect: 'slide',
		slideSpeed: 0,
		play: 6000, 
		animationComplete: function(){
			$('.slide-wrapper.active').removeClass('active');
			$('.slide-wrapper:visible').addClass('active');
			if($('.pagination li:last').hasClass('current')){
				$('.pagination li:last a').trigger('click');
			}
		},
		preload: true,
		preloadImage: '/wp-content/themes/bsw/lib/img/shared/loader.gif'
		});
	}else{
		// Awesome everything else..
		$(".slides").slides({
		preload: true,
		crossfade: false,
		effect: 'fade',
		fadeEasing: "easeOutQuad",
		fadeSpeed: 500,
		play: 6000, 
		animationComplete: function(){
			$('.slide-wrapper.active').removeClass('active');
			$('.slide-wrapper:visible').addClass('active');
			introSlides();
			if($('.pagination li:last').hasClass('current')){
				$('.pagination li:last a').trigger('click');
			}
		},
		preload: true,
		preloadImage: '/wp-content/themes/bsw/lib/img/shared/loader.gif'
		});
	}

	function introSlides(){
		// Re-evaluate active slide
		sHome();
		// Single Slides
		slide.css({ 'top' : '20px', 'opacity' : '0'});
		slide.animate({'top' : '0', 'opacity' : '1'}, 800, 'easeOutExpo');
		slideProduct.css({'bottom' : '420px'});
		slideProduct.delay(300).animate({'bottom' : '0'}, 800, 'easeOutBounce');
		slideShadow.css('opacity','0');
		slideShadow.delay(200).animate({'opacity' : '1'}, 800, 'easeOutBounce');
		// Last Slide
		slideProductOne.css({'bottom' : '420px'});
		slideProductTwo.css({'bottom' : '420px'});
		slideProductThree.css({'bottom' : '420px'});
		slideProductOne.delay(300).animate({'bottom' : '0'}, 800, 'easeOutBounce');
		slideProductTwo.delay(400).animate({'bottom' : '30px'}, 900, 'easeOutBounce');
		slideProductThree.delay(500).animate({'bottom' : '25px'}, 900, 'easeOutBounce');
	};

	if($.browser.msie){
		// Once again - you suck IE..
		$(".testimonials-slides").slides({
			effect: 'slide',
			slideSpeed: 0,
			randomize: true,
			play: 6000,
			generatePagination: false
		});

		$(".retailers-slides").slides({
			effect: 'slide',
			slideSpeed: 0,
			randomize: true,
			play: 6000,
			generatePagination: false
		});

	}else{
		// Awesome browsers - I love you..
		$(".testimonials-slides").slides({
			crossfade: false,
			effect: 'fade',
			fadeEasing: "easeOutExpo",
			fadeSpeed: 500,
			randomize: true,
			autoHeight: true,
			play: 6000,
			generatePagination: false
		});

		$(".retailers-slides").slides({
			crossfade: false,
			effect: 'fade',
			fadeEasing: "easeOutExpo",
			fadeSpeed: 500,
			randomize: true,
			play: 6000,
			generatePagination: false
		});

	}
	
	/* Find Soup
	---------------------------------------------*/

	var regionBtn = $('#map area');
	var regionNavBtn = $('.nav-regions a');
	var region = $('.regions div.region');
	var regionRetailer = $('.map-retailers li');

	// Region hover - JS on CSS3 action!
	function regionScroll(){ 
			$('html, body').animate({'scrollTop' : 825}, 500, 'easeInOutExpo');
	}

	function regionOver(){
		var id = $(this).attr('id');
		$(region + '[id="' + id + '"]').addClass('hover');
		$(region + '[id="' + id + '"]:not(.current)').children('.tooltip').stop().animate({'top' : "10%"}, 300, 'easeOutExpo');
	}

	function regionOut(){
		var id = $(this).attr('id');
		$(region + '[id="' + id + '"]').removeClass('hover');
		$(region + '[id="' + id + '"]:not(.current)').children('.tooltip').stop().animate({'top' : "20%"}, 400, 'easeOutExpo');
	}

	// Current states - Cause its helpful, duh!
	function regionActivate(){
		var id = $(this).attr('id');
		// - Navigation current
		$('.nav-regions .current').removeClass('current');
		$(regionNavBtn + '[id="' + id + '"]').addClass('current');
		// - Region current
		$('.regions .current').removeClass('current');
		$(region + '[id="' + id + '"]').addClass('current');
		// - Retailers current
		$('.map-retailers .current:not(.' + id + ')').removeClass('current');
		$(regionRetailer + '[class*="' + id + '"]').addClass('current');
		// - Scroll to retailers
		regionScroll();
	}

	// Instantiate the functions!
	regionBtn.hover(regionOver, regionOut);
	regionNavBtn.hover(regionOver, regionOut);
	regionNavBtn.click(regionActivate);
	regionBtn.click(regionActivate);

	/* Our Soups
	---------------------------------------------*/
	var tagBtn = $('.soup-tags a');
	var soupBtn = $('.nav-soups a');
	var soupItem = $('.nav-soups li');

	tagBtn.hover(
		function(){
			$(this).next('.tooltip-tag').fadeIn(200, 'easeOutExpo');
		},
		function(){
			$(this).next('.tooltip-tag').fadeOut(300, 'easeOutExpo');
		}
	);

	soupItem.hover(
		function(){
			$(this).children('.tooltip-soup').fadeIn(200, 'easeOutExpo');
		},
		function(){
			$(this).children('.tooltip-soup').fadeOut(300, 'easeOutExpo');
		}
	);

	soupBtn.click(function(){
	// Ugliest IF statement - ever...
	$('.tooltip-soup').fadeOut(200, 'easeOutExpo');
	if($(this).parent().prev().hasClass('current') || $(this).parent().prev().prev().hasClass('current') || $(this).parent().prev().prev().prev().hasClass('current') || $(this).parent().prev().prev().prev().prev().hasClass('current') || $(this).parent().is(':last-child') || $('.nav-soups li:first').hasClass('current')){
			$(this).parent().prev('li').children('.poof').fadeIn(200, 'easeOutExpo', function(){
				$(this).fadeOut(200, 'easeOutExpo');
			});
		}else{
			$(this).parent().next('li').children('.poof').fadeIn(200, 'easeOutExpo', function(){
				$(this).fadeOut(200, 'easeOutExpo');
			});
		}
	});

	// Slider
	var soupImg = $('.soup-slides-image');

	if($.browser.msie){
		// Seriously though - you suck IE
		$(".soup-slides").slides({
			effect: 'slide',
			slideSpeed: 0,
			generatePagination: false,
			paginationClass: 'soup-pagination'
		});
	}else{
		// 21st Century Browsers
		$(".soup-slides").slides({
			crossfade: false,
			effect: 'fade',
			fadeSpeed: 400,
			generatePagination: false,
			paginationClass: 'soup-pagination',
			animationComplete: function(){
				introSoups();
			} 
		});
	}

	function introSoups(){
		soupImg.css({'top' : '-40px'});
		soupImg.animate({'top' : '-10px', 'opacity' : '1'}, 400, 'easeOutBounce');
	};

	/* Suppliers & Sustainability
	---------------------------------------------*/

	$(".sustainability-slides").slides({
		crossfade: false,
		effect: 'fade',
		fadeSpeed: 400,
		generatePagination: false,
		hoverPause: true,
		play: 5000,
		paginationClass: 'sustainability-pagination'
	});
});
