$(document).ready(function() {

  // open external link in new tab/window
  $("a[href*='http://']:not([href*='"+window.location.hostname+"'])").click(function(){
    this.target = "_blank";
  });

	/* hide n show */
	$('#hide-n-show li').eq(0).show().addClass('opened').siblings('li').find('.description').hide();
	$('#hide-n-show li a.hide_show').click(function() {
		$(this).parent().toggleClass('opened');
		$(this).siblings('.description').slideToggle(300);
		return false;
	});

	$('input[alt]').focus(function () {
        if ($(this).attr("value") == $(this).attr("defaultValue")) {
                $(this).attr("value", '');
        }
	});
	$('input[alt]').blur(function () {
        if ($(this).attr("value") == '') {
          $(this).attr("value", $(this).attr("defaultValue"));
      }
	});

	//ADDING SNEAKY NAVIGATION HOVERS FOR DROP-DOWNS & hover for ie6

	$('.dropdown-hover').append('<div class="sneaky-hover"></div>');
	$('.dropdown-hover').mouseover(function(){
    $(this).addClass('active');
  }).mouseout(function(){
    $(this).removeClass('active');
  });


	// CYCLE NAVIGATION

	$('#prev').after('<div id="pager"></div>');

	$('#banner-wrap').cycle({
			next: '#next',
			prev: '#prev',
			pager: '#banner-nav #pager',
			speed: 400,
			timeout: 5000
	});

	$('#slideshow #cycle').cycle({
			next: '#next',
			prev: '#prev',
			pager: '#slideshow-nav #pager',
			speed: 400
	});

	$('#recipes-list').cycle({
			next: '#next',
			prev: '#prev',
			pager: '.recipe-book-nav #pager',
			speed: 400,
			timeout: 0
	});

	//ROUNDED CORNERS

	$('.dropdown').corner("bottom 5px");
	$('.sub-nav ul li a').corner("right 2px");

	//TRAINING & SERVICES TOGGLES

	$('.hide_show li ul').hide();

  $('.hide_show > li > a').click(function(){
    $(this).next('ul').slideToggle(200);
		$(this).toggleClass('active');
		return false;
  });
  

	//CAROUSEL

	$(".carousel").cycle({
    fx:     'scrollHorz',
    speed:  600,
    timeout: 5000,
    next:   '#carousel-next',
    prev:   '#carousel-prev',
		pause:   1
	});

	//REPLACES STUPID SELECTS WITH STYLABLE DIVS

	$('form select').sSelect();
	
	$('.filter-category ul.newList').hide();
	$('.filter-category .selectedTxt').click(function(){
    $('.filter-category ul.newList').toggle();
		return false;
  });

	//FORM VALIDATION

	//$('.validate').validate();

	//$.validator.addMethod("security", function(value) {
	//	return value == "Delicious";
	//});

	//COLORBOX

	$(".nutritional-info").colorbox({width:"625", inline:true, href:function() { return $(this).attr("href"); }});

	//TABLE STRIPING
	$("tr:odd").addClass("odd");

	//RECIPE BOOK CONTROL HIDE SHOW

	$('li.recipe .controls').hide();

	$('li.recipe').mouseover(function(){
    $(this).find('.controls').show();
  });
  $('li.recipe').mouseout(function(){
    $(this).find('.controls').hide();
  });

  $('.recipe-book-links #share').click(function(){
    $('#recipe-book').hide();
    $('#recipe-book-share').fadeIn();
    return false;
  });
  $('#recipe-book-share .back').click(function(){
    $('#recipe-book-share').hide();
    $('#recipe-book').fadeIn();
    return false;
  });
  $('#recipe-book-share #cancel').click(function(){
    $('#recipe-book-share').hide();
    $('#recipe-book').fadeIn();
    return false;
  });


  //RELATED RECIPES HIDE N SHOW

  $('.related-recipes-dropdown ul').hide();

  $('.related-recipes').toggle(function(){
	  $(this).addClass('active');
	  $(this).next('ul').slideDown();},
  function(){
	  $(this).removeClass('active');
	  $(this).next('ul').slideUp();
  });


    // ---- Autofill   ----------------------------------------------------------------------------------------------------------
    $.fn.autofill = function () {
        $(this).focus(function () {
            if (this.value == this.defaultValue) {
                this.value = "";
            }
        }).blur(function () {
            if (!this.value.length) {
                this.value = this.defaultValue;
            }
        });
    };

    $('.global-searchtext,.product-keyword,.footer-keyword,.search-text,.recipe-keyword').autofill();
});

$(window).load(function() {
  if(window.location.hash){
    var p = window.location.hash.substring(2);
    $('#product-carousel #product-images>img[rel=p'+p+']').each(function(){
      $('#product-carousel #product-images').prepend($(this));
      $('div#description div.current').hide().removeClass('current');
      $('div.p'+p).show().addClass('current');
    });
  }else{
    $('div#description div.current').show();
  }

  var maxShown=3,img, pz, pw=1, pds=0.6, px=60, pdx=250, py=0, pdy=60, position=[], current=1;
  $('#product-carousel #product-images img').each(function(){
    img = $(this);
    pz = 1000-current;
    var animation = {
      left: px,
      top: py,
      width: pw*img.width()
    };
    var css = {
      zIndex: pz,
      position: 'absolute',
      display: (current > maxShown) ? 'none' : 'block'
    };
    img.css($.extend({}, animation, css));
    position[current] = {animate:animation, other:css};
    current++;
    px  += pdx;
    py  += pdy;
    pw  *= pds;
    pdx *= pds;
    pdy *= pds;
  });

  $('#product-carousel #carousel-prev').click(function(e){
    var current=1;
    var last = position.length-1;
    $('#product-carousel #product-images img').each(function(){
      if(current == 1) {
        // Change product details to next one
	showDetails(parseInt($(this).next().attr('rel').substring(1), 10));
        // Hide image then append to list
        $(this).css({zIndex:1000}).fadeOut('normal', function(){
          $(this).css($.extend(position[last].animate, position[last].other)).hide();
          $(this).parent().append($(this));
          if(maxShown >= last){
            $(this).fadeIn();
          }
        });
      }
      else {
        if(current == maxShown+1)
        {
          $(this).css($.extend(position[current-1].other, position[current-1].animate)).hide().fadeIn();
        }
        if(current > maxShown)
        {
          $(this).hide().css(position[current-1].other);
        }
        else
        {
          $(this).animate(position[current-1].animate).css(position[current-1].other);
        }
      }
      current++;
    });
    return false;
  });

  $('#product-carousel #carousel-next').click(function(e){
		e.preventDefault();
    var current = 1;
    var last = position.length-1;
    $('#product-carousel #product-images img').each(function(){
      if(current == last && last > maxShown)
      {
        // Change product details to next one
        showDetails(parseInt($(this).parent().children().last().attr('rel').substring(1), 10));

        // Prepend to list the fade in after
        $(this).parent().prepend($(this));
        $(this).css($.extend(position[1].other, position[1].animate)).hide().fadeIn();
      }
      else if(current == maxShown || (current == last && last < maxShown))
      {
        // Change product details to next one
        showDetails(parseInt($(this).parent().children().last().attr('rel').substring(1), 10));
        $(this).fadeOut('normal', function(){
          if(maxShown >= last)
          {
            $(this).parent().prepend($(this));
            $(this).css($.extend(position[1].other, position[1].animate)).hide().fadeIn();
          }
        });
      }
      else if(current > maxShown)
      {
        $(this).css(position[current+1].other).hide();
      }
      else
      {
        $(this).animate(position[current+1].animate).css(position[current+1].other);
      }
      current++;
    });
    return false;
  });

  function showDetails(p)
  {
		if(isNaN(p)) return;
    window.location.hash = 'p'+p;
    $('div#description div.current').stop().fadeOut('normal', function(){
      $(this).removeClass('current');
      $('div.p'+p).addClass('current').fadeTo('normal', 1);
    });
		return;
  }
});



