// common js

$(document).ready(function(){

	// hide addressbar in Android
	if (navigator.userAgent.match(/Android/i)) {
	window.scrollTo(0,0); // reset in case prev not scrolled  
	var nPageH = $(document).height();
	var nViewH = window.outerHeight;
	if (nViewH > nPageH) {
	  nViewH -= 250;
	  $('BODY').css('height',nViewH + 'px');
	}
	window.scrollTo(0,1);
	}
  
	// icons
	$('a.slide').append('<i></i>');
	$('header nav').append('<i></i>');
	$('a.navigation').append('<i></i>');
	$('header nav ul li.dd ul li:first-child').prepend('<span></span>');
	$('.tab-bar ul li ul li:first-child').prepend('<span></span>');
	$('.dotted-box .top').prepend('<i></i>');
	$('.dotted-box .top').append('<b></b>');
	$('.dotted-box .btm').prepend('<i></i>');
	$('.dotted-box .btm').append('<b></b>');
	$('a.more').append('<i></i>');
	$('a.read').append('<i></i>');
	$('.sub-nav ul li.selected a').append('<i></i>');
	$('ul.account-settings li').append('<i></i>');
	$('.shopping-categories h4').append('<i></i>');
	$('.delivery-notes ul li').prepend('<i></i>');
	$('.big-trivia ol li').prepend('<i></i>');
	$('.faqs h4').prepend('<i>?</i>');
	
	// more about easiyo
	$('a.slide').click(function() {
	/*var $marginLefty = $(this).parent('.tab-bar').find('ul');
	$marginLefty.animate({
	  marginLeft: parseInt($marginLefty.css('marginLeft'),10) == 0 ?
		$marginLefty.outerWidth():
		0
	});*/
	$(this).toggleClass('off');
	});
	
	// more reviews
	$('a.more-reviews').click(function () {
	$(this).next('ul.more-reviews-list').stop(true, true).slideToggle("fast");
	if($(this).text() == 'Read More Reviews'){
           $(this).text('Read Less Reviews');
       } else {
           $(this).text('Read More Reviews');
       }
	}),
	
	// opinion box
	$('a.opinion').click(function () {	
	$(this).closest('.content').find(".opinion-box").stop(true, true).slideToggle("fast");
	
	if($(this).html() == 'Submit Your Own Review<i></i>'){
           $(this).html('Close Opinion Box<i></i>');
       } else {
           $(this).html('Submit Your Own Review<i></i>');
       }
	}),
	
	// trivia info
	$('a.trivia-rules').click(function () {
	$(this).next('div').stop(true, true).slideToggle();
	if($(this).html() == 'Show Trivia Rules and FAQs<i></i>'){
           $(this).html('Hide Trivia Rules and FAQs<i></i>');
       } else {
           $(this).html('Show Trivia Rules and FAQs<i></i>');
       }
	}),
  
	// ipad portrait, mobile dd
	$('.mobile-nav').click(function () {
	$(this).find("ul.trigger").toggleClass("on");
	}),
	
	$('.mobile-nav li.dd').click(function () {
	$(this).find('ul').stop(true, true).slideToggle("fast");
	}),

	$('.mobile-nav li.dd').mouseleave(function () {
	$(this).find('ul').stop(true, true).slideUp("fast");
	}),
	
	$('.mobile-nav').mouseleave(function () {
	$(this).find("ul.trigger").removeClass("on");
	}),
	
	// header nav dd
	$('header nav ul li.dd').hover(function () {
	$(this).find('ul').stop(true, true).slideToggle("fast");
	}),

	$('header nav ul li.dd').mouseleave(function () {
	$(this).find('ul').stop(true, true).slideUp("fast");
	})
	
	// more easiyo nav dd
	$('.tab-bar ul li.dd').hover(function () {
	$(this).find('ul').stop(true, true).slideToggle("fast");
	}),

	$('.tab-bar ul li.dd').mouseleave(function () {
	$(this).find('ul').stop(true, true).slideUp("fast");
	})
	
	// select dd
	$('.dd-list ul li.dd').click(function () {
	$(this).find('ul').stop(true, true).toggle();
	}),
	
	$('.dd-list ul li.dd').mouseleave(function () {
	$(this).find('ul').stop(true, true).hide();
	})
	
	// accordions
	$(".expand").click(function()
	{
		$(this).next("li.item").stop(true, true).slideToggle().siblings("li.item").slideUp();
		$(this).toggleClass("on").siblings().removeClass("on");
	});
	
	$(".shopping-categories h4").click(function()
	{
		$(this).next(".item").stop(true, true).slideToggle().siblings(".item").slideUp();
		$(this).toggleClass("close").siblings().removeClass("close");
	});
	
	$('.faqs h4').click(function () {
	$(this).next('.feature-box').stop(true, true).slideToggle().siblings(".feature-box").slideUp();
	$(this).toggleClass("selected").siblings().removeClass("selected");
	});
	
	$(".big-trivia ol li").click(function()
	{
		$(this).toggleClass("selected").siblings("li").removeClass("selected");
	});
	
	// subnav
	$(function() {
		$(".sub-nav ul li.selected ol li.selected a").each(function(){
		$(this).closest('nav.sub-nav').find("li.dd.selected").addClass("parent");
	  })
	});
	
	// send as gift
	$('input#sendasgift').click(function () {
	$(this).is(':checked') && $('.gift-message').slideDown() || $('.gift-message').slideUp();
	}),
	
	// admin - existing product description
	$('input#existing').click(function () {
	$(this).is(':checked') && $('.expanded').slideDown() || $('.expanded').slideUp();
	}),
	
	// select recipe category
	$(".recipe-category-001").click(function() {
		$(".select-recipe-category").html("Dips &amp; Spreads <b></b>");
	});
	$(".recipe-category-002").click(function() {
		$(".select-recipe-category").html("Dressings &amp; Sauces <b></b>");
	});
	$(".recipe-category-003").click(function() {
		$(".select-recipe-category").html("Summertime Salads <b></b>");
	});
	$(".recipe-category-004").click(function() {
		$(".select-recipe-category").html("Light Meals <b></b>");
	});
	$(".recipe-category-005").click(function() {
		$(".select-recipe-category").html("Main Courses <b></b>");
	});
	$(".recipe-category-006").click(function() {
		$(".select-recipe-category").html("Vegetable Extras <b></b>");
	});
	$(".recipe-category-007").click(function() {
		$(".select-recipe-category").html("Sweet Treats <b></b>");
	});
	$(".recipe-category-008").click(function() {
		$(".select-recipe-category").html("Cakes &amp; Bakes <b></b>");
	});
	$(".recipe-category-009").click(function() {
		$(".select-recipe-category").html("Frostings &amp; Fillings <b></b>");
	});
	$(".recipe-category-010").click(function() {
		$(".select-recipe-category").html("Refreshing Drinks <b></b>");
	});
	$(".recipe-category-011").click(function() {
		$(".select-recipe-category").html("Yogurt Ice Cream <b></b>");
	});
	
	// select reference category
	$(".reference-category-001").click(function() {
		$(".select-reference-category").html("Online search <b></b>");
	});
	$(".reference-category-002").click(function() {
		$(".select-reference-category").html("Recommendation <b></b>");
	});
	$(".reference-category-003").click(function() {
		$(".select-reference-category").html("Offline advertising <b></b>");
	});
	$(".reference-category-004").click(function() {
		$(".select-reference-category").html("Infomercial television <b></b>");
	});
	$(".reference-category-005").click(function() {
		$(".select-reference-category").html("In store <b></b>");
	});
	
	// select subject category
	$(".subject-category-001").click(function() {
		$(".select-subject-category").html("General enquiry <b></b>");
	});
	$(".subject-category-002").click(function() {
		$(".select-subject-category").html("Production information <b></b>");
	});
	$(".subject-category-003").click(function() {
		$(".select-subject-category").html("Customer services <b></b>");
	});
	$(".subject-category-004").click(function() {
		$(".select-subject-category").html("Distribution enquiries <b></b>");
	});
	$(".subject-category-005").click(function() {
		$(".select-subject-category").html("Promotional opportunities <b></b>");
	});
	$(".subject-category-006").click(function() {
		$(".select-subject-category").html("Trivia quiz problem <b></b>");
	});
	$(".subject-category-007").click(function() {
		$(".select-subject-category").html("Website problem <b></b>");
	});
	$(".subject-category-008").click(function() {
		$(".select-subject-category").html("Other <b></b>");
	});
	
	// random home testimonial bubble
	function swapTestimonials() {
	var random = Math.floor(Math.random()*3),
		$current = $('#randomTestimonial img:visible');
	$current.hide();
	if($current.index() == random) {
		random = ++random % 3;
	}
	$('#randomTestimonial img').eq(random).show();
	}
	setInterval(swapTestimonials, 4000);

	// equal columns
	var biggestHeight = 0;  
	$('.equal').each(function(){   
		if($(this).height() > biggestHeight){   
			biggestHeight = $(this).height();  
		}  
	});  
	$('.equal').height(biggestHeight);
	
});

// end doc ready

$(document).mouseup(function (e) {
    var container = $(".main-nav .dd ul");
    if (container.has(e.target).length === 0) {
        container.hide();
    }
});

$(window).scroll(function() {
    if ($(this).scrollTop()) {
        $('#toTop:hidden').stop(true, true).fadeIn();
    } else {
        $('#toTop').stop(true, true).fadeOut();
    }
});