// to avoid flashing between two states, this adds a 'js' class to the html element
// this effectively determines if javascript is loaded, and allows CSS changes to be
// applied before the intial loading of the document
document.getElementsByTagName('html')[0].className = 'js';

$(document).ready( function() {
	//highlight current page in navigation
	var url = document.location.href;

	// highlight current page in secondary navigation
	var url = document.location.href;
	$("#page nav li a").each( function() {
		if (url.indexOf($(this).attr("href")) != -1) {
			$(this).addClass("current");
		}
	});

	// exception for /2012/Poster page
	if (url.indexOf('/2012/Posters/') !== -1) {url='/2012/'}

	longest_match=0;
	$("header nav li a").each( function() {
		if (url.indexOf($(this).attr("href")) != -1) {
			match_length=$(this).attr("href").length;
			if (match_length>longest_match) {
				longest_match=match_length;
				best_match=this;
			};
		};
	});
	if (typeof best_match != "undefined") {$(best_match).addClass("current")};
	
	
	//slideshow for videos on home page
	setInterval( "slideSwitch()", 3000 );

	/*** Clear searchfield on focus ****/
	/*** taken from http://bassistance.de/2007/01/23/unobtrusive-clear-searchfield-on-focus/ ***/
	/* apply this to the search field in header of all pages */
	
	$.fn.search = function() {
		return this.focus(function() {
			if( this.value == this.defaultValue ) {
				this.value = "";
			};
			$(this).css({color:'#000'}); 
		}).blur(function() {
			if( !this.value.length ) {
				this.value = this.defaultValue;
				$(this).css({color:'#7F8085'});
			}
		});
	};

	$("#q").search();

});

	// stuff to read URL parameters, taken from:
	// http://jquery-howto.blogspot.com/2009/09/get-url-parameters-values-with-jquery.html
	$.extend({
	getUrlVars: function(){
		var vars = [], hash;
		var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
		for(var i = 0; i < hashes.length; i++)
		{
			hash = hashes[i].split('=');
			vars.push(hash[0]);
			vars[hash[0]] = hash[1];
		}
		return vars;
		},
		getUrlVar: function(name){
			return $.getUrlVars()[name];
		}
	});
	
// define :random
// http://blog.mastykarz.nl/jquery-random-filter/
jQuery.jQueryRandom = 0;
jQuery.extend(jQuery.expr[":"],
{
    random: function(a, i, m, r) {
        if (i == 0) {
            jQuery.jQueryRandom = Math.floor(Math.random() * r.length);
        };
        return i == jQuery.jQueryRandom;
    }
});	

/*** Simple jQuery Slideshow Script
Released by Jon Raasch (jonraasch.com) under FreeBSD license: free to use or modify, not responsible for anything, etc. Please link out to me if you like it :)
***/
// http://jonraasch.com/blog/a-simple-jquery-slideshow

function slideSwitch() {
	var $active = $('#slideshow DIV.active');
	if ( $active.length == 0 ) {
		var rndNum = Math.floor(Math.random() * $('#slideshow DIV').length );
		$active = $( $('#slideshow DIV')[ rndNum ] );
		$active.addClass('active');
	};

	//if ( $active.length == 0 ) $active = $('#slideshow DIV:last');
	// use this to pull the divs in the order they appear in the markup
	//var $next = $active.next().length ? $active.next()
	//: $('#slideshow DIV:first');
	// uncomment below to pull the divs randomly
	var $sibs  = $active.siblings();
	var rndNum = Math.floor(Math.random() * $sibs.length );
	var $next = $( $sibs[ rndNum ] );

	$active.addClass('last-active');
	$next.css({opacity: 0.0})
		.addClass('active')
		.animate({opacity: 1.0}, 1000, function() {
			$active.removeClass('active last-active');
		});
};





