jQuery(document).ready(function($){ // START

  jQuery('input[title], textarea[title]').each(function() {if($(this).val() === '') {$(this).val($(this).attr('title'));}
    $(this).focus(function() {if($(this).val() == $(this).attr('title')) {$(this).val('').addClass('focused');}});
    $(this).blur(function() {if($(this).val() === '') {$(this).val($(this).attr('title')).removeClass('focused');}});
  });


  jQuery('.fade').hover(
    function() {$(this).fadeTo("medium", 1);},
    function() {$(this).fadeTo("medium", 0.5);}
  );


	$('.accordion-content').hide();
	$('.accordion-title').click(function() {
  $('.accordion-content').slideUp('normal');
    if($(this).next().is(':hidden') == true) {
      $(this).next().toggle('normal');
    } 
  });

  jQuery('.post-button').corner('4px');

  $("#aside input, #aside textarea, #aside select, #aside button, .section input, .section textarea, .section select, .section button, #respond input, #respond textarea, .web-forms input, .web-forms textarea, .web-forms select").uniform();

}); // END


Cufon.replace('.sidebox h3, #ngg-tag-filter h3', {fontFamily: 'halo'});
		
function showVideo(id) {
  document.write("<div class=\"centered\">");
  document.write("<object width=\"580\" height=\"326\">");
  document.write("<param name=\"movie\" value=\"http://www.youtube.com/v/"+id+">&amp;hl=en&amp;fs=1\"></param>");
  document.write("<param name=\"allowFullScreen\" value=\"true\"></param><param name=\"allowscriptaccess\" value=\"always\"></param>");
  document.write("<embed src=\"http://www.youtube.com/v/"+id+"&amp;hl=en&amp;fs=1\" type=\"application/x-shockwave-flash\" allowscriptaccess=\"always\" allowfullscreen=\"true\" width=\"580\" height=\"326\"></embed></object>");
  document.write("</div>");
}

jQuery(document).ready(function($) {
   $(".ngg-gallery-thumbnail a").click(function(event) {
     event.preventDefault();
     var id = $(this).attr('id');
     updateSlideshow(id);
     $("body").scrollTop(230);
   });
   $("#slideshow-nav .nav a").live('click', function(event) {
     var id = $(this).attr('id');
     if (id != '') {
       event.preventDefault();
       updateSlideshow(id);
       $("body").scrollTop(230);
     }
   });
   
   $("#filter-button").click(function() {
     var delimiter = '';
     var basePage = getBasePage();
     var searchQuery = '?gallery=';
     
     $("#ngg-tag-filter").find("select").each(function() {
       if (this.value != 'all') {
         searchQuery += delimiter + this.value;
         delimiter = ',';
       }
     });
     
     if (searchQuery != '?gallery=') {
       location.href = '/' + basePage + searchQuery + ',' + basePage;
     } else {
       location.href = '/' + basePage;
     }
   });
   
   if ($("#slideshow").length) {
     if (getParameterByName("prev-nav")) {
       updateSlideshow(image.length-1);
     } else {
       updateSlideshow(0);
     }
     $("#slideshow-nav .back a").attr('href', '/' + getBasePage());
   }
});

function getBasePage() {
  var parts = new Array();
  var basePage = 'runway';
  
  // Get base page from first select element
  jQuery("#ngg-tag-filter").find("select").each(function() {
    parts = jQuery(this).attr('id').split("-");
    basePage = parts[1];
  });
  
  return basePage;
}

function updateSlideshow(id) {
  var $container = jQuery("#slideshow");
  var $caption = jQuery("#image-caption");
  var imagePath = image[id];
  var imageCaption = image_caption[id];
  var max = 12;
  
  $caption.html(imageCaption);
  var captionText = $caption.text();
  
  var imageTag = '<img src="' + imagePath + '" alt="' + captionText + '" width="585px" />';
  $container.html(imageTag);
  
  updateSlideshowNavigation(id, max);
}

function updateSlideshowNavigation(id, max) {
  id = parseInt(id);
  currentPage = parseInt(currentPage);
  max = parseInt(max);
  
  var nextId = id + 1;
  var previousId = id - 1;
  var imageNumber, previousLink, nextLink = null;
  
  if (currentPage == 1) {
    imageNumber = id + 1;
  } else {
    imageNumber = (id + 1) + max * (currentPage - 1);
  }
  
  if (requestUri.search(/\?/) != -1) {
    queryOperator = '&';
  } else {
    queryOperator = '?';
  }
  
  if ( currentPage == 1 ) {
    if ( totalPages == 1 ) {
      if ( previousId < 0 ) {
        previousId = image.length-1;
      }
      if ( nextId >= image.length ) {
        nextId = 0;
      }
    } else {
      if ( previousId < 0 ) {
        previousLink = requestUri + queryOperator + 'nggpage=' + totalPages + '&prev-nav=1';
      } else if ( nextId >= max ) {
        nextLink = requestUri + queryOperator + 'nggpage=' + (currentPage + 1);
      }
    }
  } else if ( currentPage >= totalPages ) {
    if ( nextId >= image.length ) {
      nextLink = requestUri + queryOperator + 'nggpage=1';
    } else if ( previousId < 0 ) {
      previousLink = requestUri + queryOperator + 'nggpage=' + (currentPage - 1) + '&prev-nav=1';
    }
  } else {
    if ( (nextId >= max) && (currentPage < totalPages) ) {
      nextLink = requestUri + queryOperator + 'nggpage=' + (currentPage + 1);
    } else if ( (previousId < 0) && (currentPage < totalPages) ) {
      previousLink = requestUri + queryOperator + 'nggpage=' + (currentPage - 1) + '&prev-nav=1';
    }
  }
  
  var navContainer = '<div class="nav"></div>';
  var previousTag = '<a href="' + image[previousId] + '" id="' + previousId + '" class="previous">&laquo; previous</a>';
  var currentLocationTag = ' <span class="nav-location">' + imageNumber + ' of ' + totalImages + '</span> ';
  var nextTag = '<a href="' + image[nextId] + '" id="' + nextId + '" class="next">next &raquo;</a>';
  
  if (previousLink != null) {
    previousTag = '<a href="' + previousLink + '" class="previous">&laquo; previous</a>';
  }
  if (nextLink != null) {
    nextTag = '<a href="' + nextLink + '" class="next">next &raquo;</a>';
  }
  
  jQuery("#slideshow-nav .nav").html(previousTag + currentLocationTag + nextTag);
}

function getParameterByName(name) {
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return decodeURIComponent(results[1].replace(/\+/g, " "));
}
