function rotate(el){
  var z_index = 0;
  var timer;
  var items = 'div';
  var interval = 5000;
  var fade = 2000;
  var curEl;
  var nextEl;
  var $this;

  $this = $(el);
  $this.find(items).each(function(){
    z_index++;

    $(this).css({
      position:'absolute',
      zIndex:z_index
    });

    if (z_index > 1){
      $(this).css({
        opacity:0,
        display:'none'
      })
    }
  });

  timer = setTimeout(startFade, interval);

  function startFade(){
    clearTimeout(timer);

    var children = $this.find(items);

    if (curEl == null){
      curEl = 0;
    }

    nextEl = curEl+1;

    if (nextEl == children.length){
      nextEl = 0;
    }

    $(children[nextEl]).css({display:'block'});
    $(children[curEl]).animate({
        opacity:0
      },
      fade
    );
    $(children[nextEl]).animate({
        opacity:1
      },
      fade,
      function(){
        $(children[curEl]).css({display:'none'});
        curEl = nextEl;
        timer = setTimeout(startFade, interval);
      }
    );
  };
};

$(document).ready(function () {

  /*
   *  Search box
   */
  var searchDefaultStr = "Type phrase";
  if ($("#sitesearch").val() == '')
    $("#sitesearch").val(searchDefaultStr);

  $("#sitesearch").focus(function() {
    if ($(this).val() == searchDefaultStr) {
      $(this).val('');
    }
  });

  $("#sitesearch").blur(function() {
    if ($(this).val() == '') {
      $(this).val(searchDefaultStr);
    }
  });

  /*
   *  Ensure modal links know to open as such
   */
  $("a.modal").each(function() {
    var href = $(this).attr('href');
    href += '&notemplate=true'
    $(this).attr('href', href)
  })
  //


	/*
  $("#indigo").fancybox({
  	frameWidth:'640',
		frameHeight:'580'
  });

  $("#litho").fancybox({
  	frameWidth:'640',
		frameHeight:'590'
  });

  $("#xpress").fancybox({
  	frameWidth:'700',
		frameHeight:'660'
  });
  */

  /* // Menu opacity
  if ($("#mainmenu li.li_selected").length > 0){
  	$("#mainmenu li:not(.li_selected)").css({'opacity':'0.75'});
	$("#mainmenu li:not(.li_selected)").hover(function(){
    	$(this).fadeTo(0, 1);
	},
	function(){
        $(this).fadeTo(0, 0.75);
	});
  }
  */

  // Disabled as we dont have enough ads atm
  // $('#newads div').each(rotate(this));

});

$(window).ready(function () {
  $('.slideshow').cycle({
    fx: 'scrollDown',
    speed: 700,
    timeout: 5500
  });

  $('div.sliderGallery').each(function () {
    var ul = $('ul', this);
    var productWidth = ul.innerWidth() - $(this).outerWidth() + 22;

    var slider = $('.slider', this).slider({
      handle: '.handle',
      minValue: 0,
      maxValue: productWidth,
      slide: function (ev, ui) {
        var movePx = (ui.value / 100) * productWidth;
        ul.css('left', '-' + movePx + 'px');
      },
      stop: function (ev, ui) {
        var movePx = (ui.value / 100) * productWidth;
        ul.animate({ 'left' : '-' + movePx + 'px' }, 700, 'linear');
      }
    });

    $(".goto_start").click(function() {
      slider.slider("moveTo", 0, 0);
    });

    $(".goto_end").click(function() {
      slider.slider("moveTo", 100, 0);
    });
  });
});