function vimeo_template(vimeo_id) {
  vimeo_template_pre = '<object width="680" height="385">'+
    '<param name="allowfullscreen" value="true" />' +
    '<param name="allowscriptaccess" value="always" />' +
    '<param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id=';
  vimeo_template_mid = '&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1" />'+
    '<embed src="http://vimeo.com/moogaloop.swf?clip_id=';
  vimeo_template_end = '&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="680" height="385" />'+
    '</object>';
    
  return vimeo_template_pre + vimeo_id + vimeo_template_mid + vimeo_id + vimeo_template_end;
}

prev = '<div class="prev"><a href="#previous"><img src="/images/prev.jpg" alt="Previous Videos" /></a></div>';
next = '<div class="next"><a href="#next"><img src="/images/next.jpg" alt="Next Videos" /></a></div>';
clear = '<hr class="clearfix"/>';

function do_video(hash) {
  $("#video_gallery").hide();
  $("#video_players, #video_players h1").fadeIn(1000);
  $(hash).fadeIn(1000);
}
function do_page(page) {
  hide_other_pages(page);
  $(page).fadeIn(1000);
}

function hide_other_pages(page) {
  pages = ["#about", "#share", "#contact"]
  // pages.splice(pages.indexOf(page),1);
  for(var i=0; i<=pages.length; i++) {
  	var p = pages[i];
  	if(p!=page) {
  	  $(p).hide();
    	$("#video_gallery").hide();
    	$("#video_players, #video_players article").hide();
  	}
  }
  return true;
}

function index_page() {
  if($("#video_gallery").is(":visible")) {return}
  if($("#video_players").is(":hidden")) {
    $("#video_players, #video_players h1").fadeOut(1000);
  }
  hide_other_pages("#index");
  $("#video_gallery").fadeIn(1000);
}

function do_location_aware(hash) {
  if(hash.match("video")) {
    do_video(hash);
  } else if(hash.match("about")){
    do_page("#about");
  } else if(hash.match("contact")){
    do_page("#contact");
  } else if(hash.match("share")){
    do_page("#share");
    // share
  } else if(hash.match("page") || (hash.match("stories"))){
    index_page();
  } else {//404??
  }
}

google.setOnLoadCallback(function() {
  if(hash=window.location.hash) {
    do_location_aware(hash);
  }

  $("nav a").click(function() {
    return true;
    do_location_aware(this.href)
  });
  
  targets = $("#video_players article");
  // $("#video_players *, #video_players").hide();
  $("#gallery li a").click(function() {
    target = $(this).attr("href");
    $("#video_gallery").fadeOut(1000);
    setTimeout(function(){
      $("#video_players, #video_players h1").fadeIn(1000);
      $(target).fadeIn(1000);
    }, 1000)
  });
  
  $("#video_players .prev a, #video_players .next a").click(function() {
    target = $(this).attr("href");
    if(target=="#page1") {
      $(this).parent().parent().fadeOut(1000);
      setTimeout(function(){
        $("#video_players h1").hide();
        $("#video_gallery").fadeIn(1000);
      }, 1000)
    } else {
      $(this).parent().parent().fadeOut(1000);
      setTimeout(function(){
        $(target).fadeIn(1000);
      }, 1000)
    }
  });
  
  $("#video_gallery li .notes p").fadeOut();
  $("#video_gallery li").hover(function(){
    $(this).children(".notes").children(" p").fadeIn();
  },function(){
    $(this).children(".notes").children("p").fadeOut();
  });
        
});
