// Project Cycler

  var slider =  null;
  var controls = null;
  var current = null;
  var currentPosition = -1;
  var timeout = null;
  var tooltips = ["Welcome To Australia",
                  "Opinions into Action",
                  "Justice is a verb", 
                  "Hosted by...",
                  "Featuring music by...",
                  "With amazing talent",
                  "Watch the video"];
  
  var prepare = function() {
    $("body").addClass("javascript");
    $("#slider").wrap("<div id='sliderCrop'></div>");
    // Set up the slider
    slider = $("#slider li");
    $("#slider").css({width: (slider.length * 760) + "px"});
    // Insert the paging controls
    controlCode = '<ul id="sliderControls">';
    tooltipCode = '';
    for (var x = 0; x < slider.length; x++) {
      controlCode += '<li class="nav"><a class="control'+x+'" href="#'+x+'" title="'+tooltips[x]+'">'+(x+1)+'</a></li>';
      controlCode += '<li id="control'+x+'" class="tooltip"><p>'+tooltips[x]+'</p></li>';
    }
    controlCode += "</ul>";
    controlCode += tooltipCode;
    controls = $(controlCode);
    $("#sliderCrop").after(controls);
    controls.click(click);
    
    // Tooltips
    if ($("#sliderControls").length > 0) {
      $("#sliderControls a[title]").tooltip({tip: 'li', effect: 'slideup', slideOffset: 10});
    }
    
    // Input Default Text
      $('input[type="text"]').addClass("idleField");  
      $('input[type="text"]').focus(function() {  
          $(this).removeClass("idleField").addClass("focusField");  
          if (this.value == this.defaultValue){  
              this.value = '';  
          }  
          if(this.value != this.defaultValue){  
              this.select();  
          }  
      });  
      $('input[type="text"]').blur(function() {  
          $(this).removeClass("focusField").addClass("idleField");  
          if ( !this.value.length ){
            this.value = (this.defaultValue ? this.defaultValue : '');  
          }
      });  
      
    // Artist Slider
      $('#artistList a').click(function() {
        curHash = this.hash
        $.scrollTo( curHash, 1000, { onAfter:function(){location.hash = curHash;} } );
        return false;
      });
      $('a.totop').click(function() {
        $.scrollTo( '#artists', 1000);
        return false;
      });
    };
  
  var goToCurrent = function(goTo) {
    animating = true;
    $("#sliderControls a").removeClass('current');
    
    if (goTo != undefined) {
      currentPosition = parseInt(goTo);
    } else if (currentPosition == (slider.length - 1)) {
      currentPosition = 0;
    } 
    else {
      currentPosition += 1;
    }
    
    timeout = window.setTimeout("goToCurrent()", 7000);
    $("#slider").animate({left: (currentPosition * (-760))+"px"}, 1000, 'easeInOutExpo', animationComplete );
  };
  
  var animationComplete = function() {
    animating = false;
    $(".control"+currentPosition).addClass('current');
  };
  
  var click = function(e) {
    if (!animating) {
      var target = e.target;
      target = target.href.match(/\#(\S+)$/)[1];
      window.clearTimeout(timeout);
      goToCurrent(target);
    }
    return false;
  };
  
  // Fire it all off
  $(document).ready(prepare);
  $(window).load(function() {
    goToCurrent();
  });

