$(document).ready(function() {
  $("#rotate-controls li a").css("display","none");
  $("#rotate-controls li a").click(function(e){
    e.preventDefault();
    return false;
  });
  
  $('#rotate-images li:first').css({'z-index':0});
  $('#rotate-images li').hide();
  var totalchilds = $('#rotate-images li').children().size() - 1;
  var $i = 0;
  var $playing = true;
  var $clicked_flag = false;
  var firstrun = true;
  var transSpeed = 500;
  var transWait = 2500;

  function dowait(time, callfader, number) {
    $("#timer").slideDown(time, function() {
      $(this).slideUp(1);
	  if (callfader==true) {
		rotatefader(number);
	  }
      if ($clicked_flag == true) {
		$clicked_flag = false;
	  }
	  });
  }

  function nextItem(num) {
     $('#rotate-images li').fadeOut(transWait / 4);
     firstrun = true;
     $i = num;
     $clicked_flag = true;
     $("#rotate-controls li.item1").css("background-image","");
     $("#rotate-controls li.item2").css("background-image","");
     $("#rotate-controls li.item3").css("background-image","");
     $("#rotate-controls li.item4").css("background-image","");
     $("#rotate-controls li.item5").css("background-image","");
	 var $j=$i+1;
	 var button_u_r_here = "#rotate-controls li.item"+$j;
	 var bg_img = "/images/rotator-hover-"+$j+".png";
	 var u_r_here_bg_full = "url('"+bg_img+"') no-repeat";
	 $(button_u_r_here).css("background",u_r_here_bg_full);
  }

  function rotatefader(i, firstrun, resume) {
    //$("#iterator").html($i); // Uncomment during debugging
    if ($playing == true) {

      if ($clicked_flag == false) {
        var button_u_r_here_prev = "#rotate-controls li.item"+i;
	    $(button_u_r_here_prev).css("background-image","");
        var $j=i+1;
	    var button_u_r_here = "#rotate-controls li.item"+$j;
	    var bg_img = "/images/rotator-hover-"+$j+".png";
	    var u_r_here_bg_full = "url('"+bg_img+"') no-repeat";
	    $(button_u_r_here).css("background",u_r_here_bg_full);
		// if ($j==1) {
          // $(button_u_r_here).css("margin","-3px 0 0 7px");
		// }else if ($j==2) {
          // $(button_u_r_here).css("margin","-3px 0 0 4px");
		// }else if ($j==3) {
          // $(button_u_r_here).css("margin","-3px 0 0 3px");
		// }else if ($j==4) {
          // $(button_u_r_here).css("margin","-3px 0 0 5px");
		// }else if ($j==5) {
          // $(button_u_r_here).css("margin","-3px 0 0 6px");
		// }
	    $(button_u_r_here).css("width","19px");
	    $(button_u_r_here).css("height","19px");
        if (i == 0) {
 	      var button_u_r_here_prev_last = "#rotate-controls li.item5";
	      $(button_u_r_here_prev_last).css("background-image","");
	    }
	  }
      if ($i == 1) {
        x = totalchilds;
      } else {
        x = $i - 1;
      }
      if (resume == true) {
        $('#rotate-images li').fadeOut(transSpeed);
      }
      $('#rotate-images li').eq($i).fadeIn(transSpeed);
      if (firstrun == true) {
        dowait(transWait);
      }
      if ($i == (totalchilds)) {
        $i = 0;
      } else {
        $i++;
      }
      if (firstrun == true) {
        //alert('this is the first time through');
        dowait(transWait, true, $i);
        firstrun = false;
      } else {
        $('#rotate-images li').eq(x).fadeOut(transSpeed, function(){
          dowait(transWait, true, $i);
        });
      }
    }
  }

  rotatefader(0, true);
  $('.pause').click(function(){$playing=false;});
  $('.play').click(function() {
   $playing = true;
      $i = $("#iterator").html();
      rotatefader($i, false, true);
     $("#rotate-controls li.item2").css("background-image","");
     $("#rotate-controls li.item3").css("background-image","");
     $("#rotate-controls li.item4").css("background-image","");
     $("#rotate-controls li.item5").css("background-image","");
});
  $('li.item1').click(function(){nextItem(0);});
  $('li.item2').click(function(){nextItem(1);});
  $('li.item3').click(function(){nextItem(2);});
  $('li.item4').click(function(){nextItem(3);});
  $('li.item5').click(function(){nextItem(4);});

});
