$(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[alt]').focus(function(){
    if ($(this).attr("value") === $(this).attr("defaultValue")) {
      $(this).attr("value", "");
    }
  }).blur(function(){
    if ($(this).attr("value") === "") {
     $(this).attr("value", $(this).attr("defaultValue"));
    }
  });
  
  //TABLE STRIPING
  $("tr:odd").addClass("odd");
  
  // CATEGORY EXTRAS SHOW/HIDE
  
  $(".listing .extras").hide();
  $(".listing li").live("hover", function(){
    $(this).find(".extras").slideToggle(100);
  });
  
  // HEADER SHOW/HIDE
  
  $(".header_search, .header_login, .header_lost_password, #search-close-button, #login-close-button").hide();
  
  $("#search-button").click(function(){
    $(this).hide();
    $("#header, .header_login, .header_lost_password, #login-close-button").hide();
    $("#search-close-button, #nav-login").fadeIn(100);
    $(".header_search").fadeIn(750);
    $("#keywords").focus();
  });
  
  $("#search-close-button").click(function(){
    $(this).hide();
    $(".header_search").hide();
    $("#search-button").fadeIn(100);
    $("#header").fadeIn(750);
  });
  
  $("#nav-login").click(function(){
    $(this).hide();
    $("#header, .header_search, .header_lost_password, #search-close-button").hide();
    $("#login-close-button, #search-button").fadeIn(100);
    $(".header_login").fadeIn(750);
  });
  
  $("#forgotten_password").click(function(){
    $(".header_login").hide();
    $(".header_lost_password").fadeIn(750);
  });
  
  $("#login-close-button").click(function(){
    $(this).hide();
    $(".header_login, .header_lost_password").hide();
    $("#nav-login").fadeIn(100);
    $("#header").fadeIn(750);
  });
  
  // HOMEPAGE BANNER
  
  $('#banner .wrap').before('<div class="controls"><ul class="nav"></div>').cycle({
    fx: 'fade', 
    speed: 'fast', 
    timeout: 10000, 
    pager:  '.nav',
    // callback fn that creates a thumbnail to use as pager anchor 
    pagerAnchorBuilder: function(idx, slide) { 
      var slideSRC= $(slide).find('img').attr('src');
      return '<li><a href="#"><img src="' + slideSRC + '" height="37" width="54" /></a></li>'; 
    } 
  });
  
  //CATEGORY CAROUSEL
  $(".carousel .wrap ul").jcarousel({
    scroll: 1,
    wrap: "circular"
  });
  
  //PRODUCTS IMAGE CYCLE
  $("#image-carousel").cycle({
    next: '#image-carousel-controls .next',
    prev: '#image-carousel-controls .prev'
  });
  
  //DROPDOWN NAV ANIMATION
  
  $(".dropdown-nav").hide();
  $(".dropdown").mouseover(function(){
    $(".dropdown-nav", this).stop(true, true).fadeIn(100);
  });
  
  $(".dropdown").mouseleave(function(){
    $(".dropdown-nav", this).stop(true, true).fadeOut(100);
  });
  
  //POPUP SHOW/HIDE
  
  $(".popup").hide();
  $(".popup-button").click(function(){
    $(this).next(".popup").stop(true, true).fadeToggle(100);
    return false;
  });
  
  $(".popup .close").click(function(){
    $(this).parent(".popup").fadeToggle(100, function(){
      $(this).find(".mail_share").hide();
      $(this).find(".share-links").show();
    });
    return false;
  });
  
  $(".mail_share").hide();
  $(".popup .send-email").click(function(){
    var common_parent = $(this).parents(".popup");
    $(common_parent).find(".share-links").slideToggle(200, function(){
      $(common_parent).find(".mail_share").slideToggle(200);
    });
    return false;
  });
  
  $(".mail_share .close-button").click(function(){
    var common_parent = $(this).parents(".popup");
    $(common_parent).find(".mail_share").slideToggle(200, function(){
      $(common_parent).find(".share-links").slideToggle(200);
    });
    return false;
  });
  
  $(".print_page .close-button").click(function(){
    $(this).parents(".popup").fadeToggle(100);
    return false;
  });
  
  //RECIPE SEARCH FORM SHOW/HIDE
  
  $(".recipe_search fieldset ol.hide").hide();
  $(".recipe_search fieldset h5").click(function(){
    $(this).toggleClass("active");
    $(this).parents("fieldset").find("ol").toggle(100);
    return false;
  });
  
  //HISTORY SLIDER
  
  $("#history .history-slide").hide();
  $("#history #year-1889").show();
  $("#timeline .history-slider").slider({
    min: 1,
    max: 8,
    step: 1,
    value: 1,
    slide: function(event, ui){
      $("#history .history-slide").hide();
      $($("#history .history-slide")[ui.value -1]).fadeIn();
    }
  });
  
  //FORM SLIDERS
  
  $(".slider_select, .slider_mins").remove();
  $("#cooking_time .slider").after('<input id="cookTime" type="hidden" value="120" /> <p><strong>Up to: <span class="minutes">120</span> <span>Mins</span></strong></p>');
  $("#cooking_time .slider").slider({
    min: 30,
    max: 120,
    step: 30,
    value: 120,
    slide: function(event, ui){
      $(this).parents("li").find("span.minutes").html(ui.value);
      $(this).parents("li").find("input").val(ui.value);
      
      var width = ((ui.value / 30) -1) * 33 + 1;
      $(this).find(".slider_highlight").css('width', width + "%");
    }
  });
  
  $("#preperation_time .slider").after('<input id="prepTime" type="hidden" value="60" /> <p><strong>Up to: <span class="minutes">60</span> <span>Mins</span></strong></p>');
  $("#preperation_time .slider").slider({
    min: 15,
    max: 60,
    step: 15,
    value: 60,
    slide: function(event, ui){
      $(this).parents("li").find("span.minutes").html(ui.value);
      $(this).parents("li").find("input").val(ui.value);
      
      var width = ((ui.value / 15) -1) * 33 + 1;
      $(this).find(".slider_highlight").css('width', width + "%");
    }
  });
  
  //COLORBOX POPUP
  
  $('.colorbox').colorbox({inline: true});
  $('#colorbox').appendTo('form'); // Added Line to fix colorbox ASP issue
  $('#submit-a-tip .close').click(function () {
    $.fn.colorbox.close();
    return false;
  });
  $('.video-popup').colorbox({width: "660px", height: "630px", iframe: true, scrolling: false});
  $('.register-popup').colorbox({width: "680px"});
  $('.sign-in-popup').colorbox({width: "680px"});
  $('.nutrition-popup').colorbox({width: "600px", inline: true});
  $('.share-book-popup').colorbox({width: "860px", height: "580px", iframe: true});
  $('.review-book-popup').colorbox({width: "900px", height: "800px", iframe: true, scrolling: false});
  $('.thankyou-popup').colorbox({width: "680px", height: "400px", iframe: true});
  
  //DETAIL ACCORDION SHOW/HIDE
  
  $(".accordion").hide();
  $(".accordion-title").click(function(){
    $(this).next(".accordion").slideToggle(100);
    $("a", this).toggleClass("active");
    return false;
  });
  
  //UNIFORM STUFFS
  
  $(".filters input:checkbox, form input.uniform_checkbox, form input:file").uniform();
  
  $(".filters .checked").parents("li").addClass("active");  
  $(".filters .checker input").change(function(){
    $(this).parents("li").toggleClass("active");
  });
  
  $("form select").uniform();
  
  //FORM VALIDATION
  /*
	$("#copyright .mail_share").validate();
	$("#content .mail_share").validate();
	$(".register_now").validate();
	$(".contact_us").validate();
	$(".ask_a_question").validate();
	$(".save_recipe_book").validate();
	$(".download_book_signup").validate();
	$(".submit_tip").validate();
	*/
	  
  //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");
  });


  $("#keywords").keypress(function(){
    if ($("#keywords").val() === $("#keywords").attr("defaultValue")) {
      $("#keywords").attr("value", "");
    }
  });
  
  $("#notfound_keywords").keypress(function(){
    if ($("#notfound_keywords").val() === $("#notfound_keywords").attr("defaultValue")) {
      $("#notfound_keywords").attr("value", "");
    }
  });


  // 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().next(".recipeSearchMore").show();
    $(this).hide();
    return false;
  });

});

