$(document).ready(function(){
		   
  // Open external links in new tab/window
  $("a[href*='http://']:not([href*='"+location.hostname+"'])").click(function(){
    this.target = "_blank";
  });
    
  // Return inputs to default value on blur.
  $('input[type=text]').focus(function(){
    if ($(this).attr("value") === $(this).attr("defaultValue")) {
      $(this).attr("value", "");
    }
  }).blur(function(){
    if ($(this).attr("value") === "") {
     $(this).attr("value", $(this).attr("defaultValue"));
    }
  });


	//email replacement
	$('.safemail').each(function(){
		var emaillink = $(this).text().
		      replace(/\sat\s/g, "@").
		      replace(/\sdot\s/g, ".");

    if ($(this).attr('title')) {
 			emaillink += "?subject=" + $(this).attr('title').replace(/\s/g,"%20");
	  }
	
		$(this).replaceWith("<a href=\"mailto:" + emaillink + "\">" + emaillink.split("?")[0] + "</a>");
	});
	
	
	//Typekit
	$("#navigation, #slideshow li a, h3, #filters, #romance-copy, #share, #directions-nutrition, .select-flavour, .select-friend, .select-theme, .button, .buttons, #thankyou, .preview, #print-save, .button-download, .button-download-all, .button-1024, .button-1280, #download, #timeline").addClass("tk-proxima-nova-soft");

  
  //subnav dropdown
  $(".nav-dropdown").hide();
  $(".has_dd").hover(function(){
    $(this).find('.nav-dropdown').stop(true, true).toggle();
  });
	
	
	$('html').click(function(){
		if($("#subscribeform").css("display") == "block"){
  	  $("#subscribeform").hide();
  	  $("#show_subscribe").show();
  	}
		if($("#searchform").css("display") == "block"){
  	  $("#searchform").hide();
  	  $("#show_search").show();
  	}
	});
	
	
	//Subscribe form hide and show
	$("#show_subscribe").click(function(){
	  $("#subscribeform").show();
	  $("#searchform").hide();
	  $("#show_search").show();
	  $(this).hide();
	  return false;
	});

	$("#subscribeform .close_me").click(function(){
	  $("#subscribeform").hide();
	  $("#show_subscribe").show();
	  return false;
	});


	//Search form hide and show
	$("#show_search").click(function(){
	  $("#searchform").show();
	  $("#subscribeform").hide();
	  $("#show_subscribe").show();
	  $(this).hide();
	  return false;
	});

	$("#searchform .close_me").click(function(){
	  $("#searchform").hide();
	  $("#show_search").show();
	  return false;
	});

  // Keep Forms from closing
  $("#searchform").click(function(event){
		event.stopPropagation();
	});
	$("#subscribeform").click(function(event){
		event.stopPropagation();
	});

	
	// Banner Carousel
	$("#slideshow").cycle({
    speed: 5000,
    next: 'a#next',
    prev: 'a#prev',
    pause: 1,
    speed: 400,
    fx: 'scrollHorz',
    cleartypeNoBg: true
  });

	// Recipe Book Carousel
	jQuery("#carousel2 ul").jcarousel({
	  scroll: 4,
    animation: 400,
    wrap: "circular"
  });

	// My Favourite Recipes Carousel
	jQuery("#myfavourites").jcarousel({
	  scroll: 6,
    animation: 400,
    wrap: "circular"
  });

	// My Favourite Recipes Carousel
	jQuery("#editbook carousel2 ul").jcarousel({
	  scroll: 5,
    animation: 400,
    wrap: "circular"
  });

	// Product Search Results Carousel
	jQuery("#results-products #carousel ul").jcarousel({
	  scroll: 1,
    animation: 400,
    wrap: "circular"
  });

	// Parties Search Results Carousel
	jQuery("#parties-fun #carousel2 ul").jcarousel({
	  scroll: 5,
    animation: 400,
    wrap: "circular"
  });


  
  //Rating hide and show
  $(".frames .rating").hide();
  $(".frames li a").live("hover", function(){
    $(this).find(".rating").slideToggle(100);
  });
  
  //filter dropdown
  $(".dropdown ul li:last-child a").css('border', 'none'); //remove last border
  $(".dropdown").hide();
  $("#filters li").hover(function(){
    $(this).find('a').toggleClass("active");
    $(this).find('.dropdown').slideToggle();
    return false;
  });
  
  // Products Landing Page
  $('.short-desc').hide();
  $('#alljelly').show();
  $('.jellylink').hover(function(){
    $('.short-desc').hide();
    $($(this).attr("id")).show();
  });

  // Product Detail Dropdown
  $('#range-flavours, #range-themes, #range-friends').hide();
  $("#select-flavour, #select-theme, #select-friend").hover(function(){
    $('#range-flavours, #range-themes, #range-friends').toggle();
    return false;
  });

  // Product Detail Image/Nutrition/directions Page
  $('.hide_me').hide();
  $("#recipe-img").show();
  $("#directions-nutrition a, #title a").click(function(){
    $('.hide_me').hide();
    $($(this).attr("href")).show();
    return false;
  });
  
  
  //Overlays of ass
  $("#recipe-book .button").colorbox({width: "550px", height: "520px",iframe:true, inline:true, scrolling: false}); //subscribe recipe book
  $('#colorbox').appendTo('form');
  //$('#cboxLoadedContent input[type=submit]').appendTo('form'); // Added Line to fix colorbox ASP issue
  //$("#cboxLoadedContent input[type=submit]").live("click", function(){
  //  $.colorbox({inline:true, href:"#thankyou"});
  //  return false;
  //});
  
  //$("#enquiryform input[type=submit], #submitideaform input[type=submit], #savebook input[type=submit]").colorbox({inline:true, href:"#thankyou"}); //enquiry form
  

  $("#gallery ul a, .pop-image").colorbox({rel:'party', photo:true});
  $(".pop-image").colorbox({width: "640px", height: "520px"});
  $(".prev-invite").colorbox({width:"466px", height:"642px", iframe:true, inline:true, scrolling: false});
  
  //Forms styling
  $('#enquiryform select, #submitidea select, #recipebooksubscribe select, #subscribeform select, #invite-generator select').styledSelect(); //select boxes
  $('#recipebooksubscribe select').hide();
  $("form input:file").uniform(); //upload


  // FAQs/Party Themes hide and show
  $(".answer, .idea").hide();
  $(".answer:first, .idea:first").show();
  $("#faqs h3:first, #ideas h3:first").addClass("open");
  $("#faqs h3, #ideas h3").click(function(){
    $(this).toggleClass('open').next('.answer, .idea').slideToggle();
  });
	
	/*  //click anywhere to hide search and subscribe
  $("body").click(function(e){
    var target = e.originalEvent.explicitOriginalTarget;
    // $(e.originalTarget).parents("#subscribeform,#searchform").length || 
    if (!$(target).parents("#subscribeform,#searchform").length || $(target).is("#subscribeform,#searchform")) {
      $("#subscribeform, #searchform").hide();
      $("#show_subscribe, #show_search").show();
    }
  });
  */
	
  //select invitation
  $("#templates li a, #templates2 li a, #sheets li a").click(function(){
    $("#templates li a, #templates2 li a, #sheets li a").removeClass("selected");
    $(this).toggleClass("selected");
    return false;
  });


  //Bertie Flying
  var currentOffset = {};
  $(window).scroll(function () {

    var scrollTop = $(this).scrollTop();
    
    if(scrollTop > 255 && scrollTop < 305 ){
      var newOffset = { top: 325, left: 430 };
      if(currentOffset == newOffset){ return; }
      $(".history-page #bertie").attr("src", "/public/aeroplane/assets/fun/bertie-nobanner.png");
      $("#bertie:not(:animated)").animate(newOffset, {queue: false, duration: 500}, function(){
        currentOffset = newOffset;
      });
    }
    else if (scrollTop > 305 && scrollTop < 500){
      var newOffset = { top: 540, left: 100 };
      if(currentOffset == newOffset){ return; }
      $("#bertie:not(:animated)").animate(newOffset, {queue: false, duration: 500}, function(){
        currentOffset = newOffset;
      });
    }
    else if (scrollTop > 500 && scrollTop < 605){
      var newOffset = { top: 785, left: 400 };
      if(currentOffset == newOffset){ return; }
      $("#bertie:not(:animated)").animate(newOffset, {queue: false, duration: 500}, function(){
        currentOffset = newOffset;
      });
    }
    else if (scrollTop > 605 && scrollTop < 1020){
      var newOffset = { top: 1120, left: 120 };
      if(currentOffset == newOffset){ return; }
      $("#bertie:not(:animated)").animate(newOffset, {queue: false, duration: 500}, function(){
        currentOffset = newOffset;
      });
    }
    else if (scrollTop > 1020 && scrollTop < 1200){
      var newOffset = { top: 1390, left: 420 };
      if(currentOffset == newOffset){ return; }
      $("#bertie:not(:animated)").animate(newOffset, {queue: false, duration: 500}, function(){
        currentOffset = newOffset;
      });
    }
    else if (scrollTop > 1200 && scrollTop < 1500){
      var newOffset = { top: 1660, left: 100 };
      if(currentOffset == newOffset){ return; }
      $("#bertie:not(:animated)").animate(newOffset, {queue: false, duration: 500}, function(){
        currentOffset = newOffset;
      });
    }
    else if (scrollTop > 1500 && scrollTop < 1750){
      var newOffset = { top: 1850, left: 420 };
      if(currentOffset == newOffset){ return; }
      $("#bertie:not(:animated)").animate(newOffset, {queue: false, duration: 500}, function(){
        currentOffset = newOffset;
      });
    }
    else if (scrollTop > 1750 && scrollTop < 2000){
      var newOffset = { top: 2100, left: 100 };
      if(currentOffset == newOffset){ return; }
      $("#bertie:not(:animated)").animate(newOffset, {queue: false, duration: 500}, function(){
        currentOffset = newOffset;
      });
    }
    else if (scrollTop > 2000 && scrollTop < 2160){
      var newOffset = { top: 2260, left: 420 };
      if(currentOffset == newOffset){ return; }
      $("#bertie:not(:animated)").animate(newOffset, {queue: false, duration: 500}, function(){
        currentOffset = newOffset;
      });
    }
    else if (scrollTop > 2160 && scrollTop < 2400){
      var newOffset = { top: 2500, left: 100 };
      if(currentOffset == newOffset){ return; }
      $("#bertie:not(:animated)").animate(newOffset, {queue: false, duration: 500}, function(){
        currentOffset = newOffset;
      });
    }
    else if (scrollTop > 2400 && scrollTop < 2560){
      var newOffset = { top: 2660, left: 420 };
      if(currentOffset == newOffset){ return; }
      $("#bertie:not(:animated)").animate(newOffset, {queue: false, duration: 500}, function(){
        currentOffset = newOffset;
      });
    }
    else if (scrollTop > 2560 && scrollTop < 2740){
      var newOffset = { top: 2840, left: 100 };
      if(currentOffset == newOffset){ return; }
      $("#bertie:not(:animated)").animate(newOffset, {queue: false, duration: 500}, function(){
        currentOffset = newOffset;
      });
    }
    else if (scrollTop > 2740 && scrollTop < 2970){
      var newOffset = { top: 3070, left: 410 };
      if(currentOffset == newOffset){ return; }
      $("#bertie:not(:animated)").animate(newOffset, {queue: false, duration: 500}, function(){
        currentOffset = newOffset;
      });
    }
    else if (scrollTop > 2970){
      var newOffset = { top: 3265, left: 120 };
      if(currentOffset == newOffset){ return; }
      $("#bertie:not(:animated)").animate(newOffset, {queue: false, duration: 500}, function(){
        currentOffset = newOffset;
      });
    }
    else if (scrollTop < 150) {
      var newOffset = { top: 145, left: 291 };
      if(currentOffset == newOffset){ return; }
      $("#bertie:not(:animated)").animate(newOffset, {queue: false, duration: 500}, function(){
        currentOffset = newOffset;
      });
      $(".history-page #bertie").attr("src", "/public/aeroplane/assets/fun/bertie-banner.png");
    }
  });




  // McCormick JS

  //Select List Item Properly
  $("#templates li").click(function(){
    var item = $(this).index();
    $("#templates li:lt(" + item + ")").removeClass("selected");
    $("a", this).toggleClass("selected");
    $("#templates li:gt(" + item + ")").removeClass("selected");
  });
  

  //CREATE A RECIPE BOOK SORTABLE / DELETE STUFF
  $(".sortable").sortable();
  $(".delete").click(function(){
    $(this).parent("li").fadeOut(300, function(){
      $(this).remove();
    });
    return false;
  });
  $(".add").click(function(){
    $(this).parent("li").fadeOut(300, function(){
      $(this).remove();
    });
    return false;
  });
  
  
  //STAR RATING HOVERS / SELECT
 
  $("#star_rating").hover(function(){
    $(this).toggleClass("active");
  });
   
  $("#star_rating li").hover(function(){
    var star = $(this).index();
    $("#star_rating li:lt(" + star + ") a").toggleClass("active");
    $("a", this).toggleClass("active");
  });

  $("#star_rating li").click(function(){
    var star = $(this).index();
   
    $("#star_rating li:lt(" + star + ") a").addClass("rated");
    $("a", this).addClass("rated");
    $("#star_rating li:gt(" + star + ") a").removeClass("rated");
  });


  // Form Clear
  $("#clear_contact, #clear_question, #clear_book, #clear_submit").click(function () {
    var $inputs = $(this).closest("div").find(".required");
    $inputs.each(function () {
      switch (this.type) {
          case 'password':
          case 'select-multiple':
          case 'select-one':
          case 'text':
          case 'textarea':
            $(this).val('');
            break;
          case 'checkbox':
          case 'radio':
            this.checked = false;
      }
    });
  });


  //DETAIL ACCORDION SHOW/HIDE
  $(".recipeSearch, .recipeSearchMore").hide();
  $(".recipeSearchMore, .recipeSearchMore1").click(function(){
    $(this).next(".recipeSearch").slideToggle(100);
    $(this).next().next(".recipeSearchMore").show();
    $(this).hide();
    return false;
  });


  //RECIPE BOOK CAROUSEL
  $(".carousel .wrap ul").jcarousel({
    scroll: 1,
    wrap: "circular"
  });
  
  $("#image-carousel").cycle({
    next: '#image-carousel-controls .next',
    prev: '#image-carousel-controls .prev'
  });
  
  
  // Site Search Clear
  $("#keywords").keypress(function(){
    if ($("#keywords").val() === "KEYWORD SEARCH") {
      $("#keywords").attr("value", "");
    }
  });

});


function getCookie(c_name)
{
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++)
{
  x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
  y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
  x=x.replace(/^\s+|\s+$/g,"");
  if (x==c_name)
    {
    return unescape(y);
    }
  }
}

