var track = new Array();
var photosPerPage = 20;
track['documentWidth'] = 0;

$(document).ready(function(){
  
  //make external links open in a new window
  $("a").each(function(){
    if($(this).attr('href')){
      href = $(this).attr('href');
      if( href.match('http://') && !href.match(basedir) ){
        $(this).click(function(){
          window.open($(this).attr('href'));
          return false;
        });
      }
    }
  });
  
  //navigation
  $("#nav li:has(li)").find("li:first").addClass("first");
  $("#nav li:has(li.current_page_item)").addClass("current_page_item");
  $("#nav li:has(li)")
    .mouseover(function(){ $(this).children('ul').show(); })
    .mouseout(function(){ $(this).children('ul').hide(); });
  //Safari's tooltip makes the dropdown flicker
  $(".page_item *[title!='']").removeAttr('title');
  //set dropdown links' width in IE
  if( $.browser.msie ){ $("#nav .page_item ul").each(function(){ $(this).width($(this).width()+"px"); }); }
  $("#nav .page_item ul").hide();
  
  //tooltips
  $("body").append("<div id='tooltip'><p>&nbsp;</p><div></div></div>");
  $("#tooltip").hide();
  $("*[title!='']")
    .each(function(i){
      $(this).data('tooltip', $(this).attr('title'));
      $(this).removeAttr('title');
    })
    .mouseover(function(){
      if( !$(this).hasClass("selected") ){
        $("#tooltip p").html($(this).data("tooltip"));
        $("#tooltip")
          .stop()
          .show()
          .fadeTo(300,1);
      }
    })
    .mousemove(function(e){
      $("#tooltip").css({ top: e.pageY*1-45, left: e.pageX+10 });
    })
    .mouseout(function(e){
      hideTooltip();
    });
    
  //don't display the browser default title tooltip for these elements:
  $(".gallery-thumbs").mousemove(function(){ return false; });
  
  //forms
  $("input[type='text'], input[type='password'], textarea")
    .focus(function(){ $(this).addClass("selected"); hideTooltip(); })
    .blur(function(){ $(this).removeClass("selected"); });
  $("button")
    .mouseover(function(){ $(this).addClass("hover"); })
    .mouseout(function(){ $(this).removeClass("hover"); });
  $("label:has(input[type='checkbox']), label:has(input[type='radio']), input[type='checkbox'], input[type='radio']").addClass("radio-and-checkbox");

  //gallery
  $(".post .gallery-thumbs a").lightBox();
  $(".post .gallery-thumbs a").click(function(){
    $(".post .gallery-thumbs a").removeClass("selected");
    $(this).addClass("selected");
  });
  $("a.disabled").click(function(){ return false; });
  
  //when an option is changed, update the page
  $("#sidebar-gallery input[type='radio']").click(function(){
    //look for a page number in the uri
    pgnum = "";
    uri = window.location.toString();
    options = uri.split('/');
    for(i=0;i<options.length;i++){
      if( isInt(options[i]) ){
        pgnum = "/"+options[i];
      }
    }
    //get the category
    gCategory = $("#sidebar-gallery .categories input:checked").attr('value');
    if( gCategory ){ gCategory = "/" + gCategory; }
    //get the filter
    gFilter = $("#sidebar-gallery .filter input:checked").attr('value');
    if( gFilter ){ gFilter = "/" + gFilter; }
    //call new page
    document.location = basedir + "/gallery" + gCategory + pgnum + gFilter;//*/
  });
  $("#sidebar-gallery input[type='radio']").parent()
    .mouseover(function(){ $(this).addClass("hover"); })
    .mouseout(function(){ $(this).removeClass("hover"); });
  $("#sidebar-gallery input[type='radio']:checked").parent().addClass("selected");

});

function isInt(str){
  var validChars = "0123456789";
    
  for(c=0; c<str.length; c++){
    if (validChars.indexOf(str.charAt(i)) == -1){
      return false;
    }
  }
  return false;
}

function markThumbnail(forImage) {
  $(".post .gallery-thumbs a").removeClass("selected");
  $(".post .gallery-thumbs a[href$='"+forImage+"']").addClass("selected");
  prevLinkCount = $(".post .gallery-thumbs a[href$='"+forImage+"']").parent().find('a').length - $(".post .gallery-thumbs a[href$='"+forImage+"'] ~ a").length;
  
  pageNumber = Math.ceil(prevLinkCount / photosPerPage);
  curPage = $(".post .gallery-thumbs a[href$='"+forImage+"']").parent().data('curPage');
  galleryUrl = $(".post .gallery-thumbs a[href$='"+forImage+"']").parent().data('galleryUrl');
  
  if( pageNumber > curPage ){
    nextGalleryPage(galleryUrl);
  }else if( pageNumber < curPage ){
    previousGalleryPage(galleryUrl);
  }
}

function hideTooltip(){
  $("#tooltip")
    .stop()
    .fadeTo(1, 0)
    .hide()
    .css({ top: -50, left: 0 });
}

function openAlbum(albumName){
  $('body').append('<div id="album-overlay"></div><div id="album-container"><div id="album"></div></div>');
  centerAlbum();
  $("#album-overlay")
    .css({
      opacity: 0,
      height: $("body").height()
    })
    .fadeTo(500, 0.9)
    .click(function(){ closeAlbum(); });
  $('#album').flash({
    src: themedir+'/book-loader.swf?book='+themedir+'/books/'+albumName,
    width: 740,
    height: 530,
    wmode: 'transparent'
  });
  $(window)
    .resize(centerAlbum)
    .scroll(centerAlbum);
}

function closeAlbum(){
  $("#album-container").remove();
  $("#album-overlay").fadeTo(500, 0, function(){ $(this).remove(); });
  $(window)
    .unbind('resize', centerAlbum)
    .unbind('scroll', centerAlbum);
}

function centerAlbum(){
  offset = $(window).scrollTop() - 85;
  available = $(window).height();
  if( available > 530 ){
    mtop = Math.floor((available - 530) / 2) + offset;
  }else{
    mtop = offset;
  }
  $("#album-container").css({ marginTop: mtop });
}//*/