var currFeatured = 1;
var isAnimating = false;
var auto;

$(document).ready(function(){
  auto = setInterval("selfMove()", 5000);
  
  var totalFeatured = $("#frontPage-Featured-Small ul li").size();

  //FEATURED
  //Setup featured
  for(var i = 2; i <=totalFeatured ; i ++){
    $("#featured-itembig" + i).hide();
  }
  $("#frontPage-Featured-Small .nav-prev").hide();
  
  
  //Bind featured click functions
  $("#frontPage-Featured-Small ul li img").click(function(){
    if(isAnimating == false){
      move(parseInt($(this).attr("class")));
    }
    stopTimer();
  });
  
  $("#frontPage-Featured-Small .nav-next").click(function(){
    if(isAnimating == false){
      move(parseInt(currFeatured) + parseInt(1));
    }
    stopTimer();
  });

  $("#frontPage-Featured-Small .nav-prev").click(function(){
    if(isAnimating == false){
      move(parseInt(currFeatured) - parseInt(1));
    }
    stopTimer();
  });
  
  
  //RIMS
  $("#frontPage-Wheels .content").hide();
  $("#frontPage-Wheels-17").show();
  $("#frontPage-Wheels .nav ul li a").click(function(){
    return false;
  });
  $("#frontPage-Wheels .nav ul li").hover(
    function(){
      var size = $(this).attr("id");
      size = size.replace('frontPage-Wheels-nav-', '');
      $("#frontPage-Wheels .nav ul li.selected").removeClass("selected");
      $(this).addClass("selected");
      $("#frontPage-Wheels .content:visible").hide();
      $("#frontPage-Wheels-" + size).show();
    }
  );
  
  
  
  //QUICK SEARCH
  $("#frontPage-Quicksearch .content ul li").hover(
    function(){
      $(this).children("span").show();
    },
    function(){
      $(this).children("span").hide();
    }
  );
  
  $("#frontPage-Quicksearch .tabs ul li.option").click(function(){
    $("#frontPage-Quicksearch .content").hide();
    var tab = $(this).attr("id");
    tab = tab.replace(/qs-tab-/, "");
    $("#frontPage-Quicksearch-" + tab).show();
    $("#frontPage-Quicksearch .tabs ul li").removeClass("selected");
    $(this).addClass("selected");
    
    return false;
  });
  
  
  $("#frontPage-Quicksearch-Make .list ul li a").hover(
    function(){
      $("#frontPage-Quicksearch-Make .biglogo").html($(this).html());
    },
    function(){
      $("#frontPage-Quicksearch-Make .biglogo").html("");
    }
  );
  
  
 
  
  
  //POLL
  //if($("#frontPage-Poll .poll").length > 0)
  //{
    $("#frontPage-Poll .poll form").submit(function(event){
      event.preventDefault(); 
      var id = $("input[@name='poll']:checked").attr("value");  
      id = id.replace("opt",'');
      $("#frontPage-Poll .poll").empty().remove();
      $.getJSON("http://autouch.ca/home/poll_vote_ajax/"+id, loadPollResults);
    });
  //}
  
  
  //HIGHLIGHT
  
  $("#highlight01").show();
  $("#frontPage-Highlights .nav a").click(function(){
    var page = $(this).html();
    $("#frontPage-Highlights img:visible").hide();
    $("#highlight0" + page).show();
    return false;
  });
  
  /*
  $("#video02").hide();
  $("#video03").hide();
  $("#video04").hide();
  $("#frontPage-VideoSpotlight .nav a").click(function(){
    var page = $(this).html();
    $("#frontPage-VideoSpotlight .video:visible").hide();
    $("#video0" + page).show();
    return false;
  });
  */
 
  
});

function move(curr){
  var totalFeatured = $("#frontPage-Featured-Small ul li").size();
  var width = 70;


  if(curr <= totalFeatured){
    
    if(curr == totalFeatured){
      currentFeatured = 1;
      curr = 1;
    }
    //if(curr == totalFeatured){
    //  stopTimer();
    //}
    
    isAnimating = true;
    currFeatured = curr;
    
    //Enable and disable prev next nav buttons
    if(currFeatured == 1){
      $("#frontPage-Featured-Small .nav-prev").hide();
      $("#frontPage-Featured-Small .nav-next").show();
    } else if(currFeatured == totalFeatured){
      $("#frontPage-Featured-Small .nav-prev").show();
      $("#frontPage-Featured-Small .nav-next").hide();
    } else {
      $("#frontPage-Featured-Small .nav-prev").show();
      $("#frontPage-Featured-Small .nav-next").show();
    }
    
    //Calculate animation movement
    var skip = 0;
    if(totalFeatured - currFeatured > 3){
      skip = (currFeatured - 1) * width * (-1);
    } else {  //If moving next, and scroller reached its end
      skip = (totalFeatured - 5) * width * (-1);
    }
    
    //Animate
    $("#frontPage-Featured-Small ul").animate({marginLeft: skip + 'px'}, 400, fade);
  }
}

function fade(){
  var totalFeatured = $("#frontPage-Featured-Small ul li").size();
  
  //Highlight and show the contents
  for(var i = 1; i <=totalFeatured ; i ++){
    $("#featured-itembig" + i).hide();
    $("#frontPage-Featured-Small ul li").removeClass("on");
  }
  $("#featured-itemsmall" + currFeatured).addClass("on");
  $("#featured-itembig" + currFeatured + " .description").css("opacity", "0");
  $("#featured-itembig" + currFeatured).show();
  
  //Animate
  $("#featured-itembig" + currFeatured + " .description").animate({opacity: '1.0'}, 400, finishMove);
}

function finishMove(){
  isAnimating = false;
}

function selfMove(){
  if(isAnimating == false){
    move(parseInt(currFeatured) + parseInt(1));
  }
}

function stopTimer(){
  clearInterval(auto);
}


function loadPollResults(data){
  var result_html = '<div class="results"><dl>';
  
  for(var i = 0; i < data.length; i ++){
    result_html = result_html + '<dt>' + data[i]['text'] + '</dt>';
    result_html = result_html + '<dd>';
    result_html = result_html + '<div class="bar" style="width:' + data[i]['percentage'] + '%;"></div>';
    result_html = result_html + '<strong>' + data[i]['percentage'] + '%</strong>';
    result_html = result_html + '</dd>';
  }
  
  result_html = result_html + '</dl></div>';
  
  $("#frontPage-Poll").append(result_html);
}