/**** Add application wide javascripts below this point  ******/

/************* Configure images for rotating banner ********/
imgs = {  one:   	"/images/home-page-banners/isuzu.jpg",
					two:   	"/images/home-page-banners/stphillips.jpg",
					three: 	"/images/home-page-banners/footymums.jpg",
          four: 	"/images/home-page-banners/wwt_home_banner.jpg",
					five:  	"/images/home-page-banners/sg3_home_banner.jpg",
					six:  	"/images/home-page-banners/subaru_home_banner.jpg"
      };


$(document).ready(function() {
  
  //Handle the search box trickery
  if($('#search_input')){
    default_search = $('#search_input').val();
    $('#search_input').focus(function() {if($('#search_input').val()==default_search) {$('#search_input').val("");}});
    $('#search_input').blur(function() {if($('#search_input').val()=="") {$('#search_input').val(default_search)} });
  }
  
  // Make the forms interact more nicely
  $("#login input, #login textarea, #login select").focus(function() { $(this).parent().css("background", "#F8F2CB") });
  $("#login input, #login textarea, #login select").blur(function() { $(this).parent().css("background", "transparent") });
  
  $("#contact_me").example('Telephone or email', {
  	hide_label: true
  });
  
  
  // New Home page effects
  $("#homenav2 li a").click(function() {
    newid = $(this).attr("id");
    rotate_image(newid);
    $("#homenav2 li").removeClass("highlight");
    $("#"+newid).parent().addClass("highlight");
    return false;
  });
  
  $("#home_image").hover(function(){
    clearTimeout(autobrowse);
   	$("#overlay_"+image).fadeTo("fast", 0.8).show();
  }, function(){
    $("#overlay_"+image).hide();
    autobrowse = setTimeout("auto_rotate()", 5000);
  });
  autobrowse = setTimeout("auto_rotate()", 5000);
  
  
	/***** Handle image preloading for rotation *****/
  for(var i in imgs) {
    var tmpimg = new Image(820,294);
    tmpimg.src=imgs[i];
  }
  
  /***** Flickr Gallery ********/
  $("#flickr_gallery").jCarouselLite({
    btnNext: ".flickr_next",
    btnPrev: ".flickr_prev",
    visible: 8
  });
  $("#tweet_list").jCarouselLite({
    btnNext: ".tweet_next",
    btnPrev: ".tweet_prev",
    vertical: true,
    visible: 4,
    circular: false
  });
  
  $(".page_image_gallery").jCarouselLite({
    btnNext: ".page_next_button",
    btnPrev: ".page_previous_button",
    visible: 3,
    start: 0
  });
  
  
});


/* Allows an id with the pattern item_id to be parsed returning id */
function parse_id(full_id) {
  return full_id.substring(full_id.indexOf("_")+1);
}




image="one";
function auto_rotate() {
  if(image=="one") rotate_image("two");
  else if(image=="two") rotate_image("three");
  else if(image=="three") rotate_image("four");
	else if(image=="four") rotate_image("five");
	else if(image=="five") rotate_image("six");	
  else if(image=="six") rotate_image("one");
  $("#homenav2 li").removeClass("highlight");
  $("#"+image).parent().addClass("highlight");
}
function rotate_image(num) {
  clearTimeout(autobrowse);
  $("#home_image img.rotator").fadeTo("fast", 0.01, function(){
    $("#home_image img.rotator").attr("src", imgs[num]);
    $("#home_image img.rotator").fadeTo("fast", 1);
  });
  image=num;
	$(".overlay_banner").removeClass("current");
	$("#overlay_"+image).addClass("current");
  autobrowse = setTimeout("auto_rotate()", 5000);
	
}
