$(document).ready(function(){
  //init infield label
  $('#search-form label').inFieldLabels();

	//identify download formats
	$("a[href$=pdf]").addClass("pdf");
	$("a[href$=doc]").addClass("doc");
	$("a[href$=zip]").addClass("zip");
	$("a[href$=psd]").addClass("psd");
	
	//nthChild to elements
	var elements = $('#main-nav li');
	var index = 1
	$.each(elements, function(){
		$(this).addClass('nthChild' + index);
		if(index == 1){ 
			$(this).addClass('first');
		}
		index++;
	});
	
	//Toggling
	$('.individual-story .inside-story').hide();
	$('#story_23 .inside-story').show();
	$('.excerpt').hover(function(){$(this).css('cursor', 'pointer');});
	$('.excerpt').live('click', function(){
		if ( $(this).next('div.inside-story').is(':hidden') ) {
			$('div.inside-story:visible').slideUp(400);
			$(this).next('div.inside-story').slideDown(400);
		} 
		return false;
	});
	
	//main navigation hover effect
	$('#main-nav li').append('<div class="fader"></div>');
	$('#main-nav li:not(.here)').hover(function(){
		$(this).children('div').fadeIn(300);
		$('a', this).css('color', '#000');
	}, function(){
		$(this).children('div').fadeOut(300);
		$('a', this).css('color', '#fff');
	});
	
	//sub navigation hover effect
	$('#sub-nav li:not(.here) a').hover(function(){
		$(this).animate({backgroundColor: '#FFF'}, 300);
	}, function(){
		$(this).animate({backgroundColor: '#E6F2F0'}, 300);
	});
	
	//basic search input validation so that the user doesn't see the ugly EE server side one
	$('.globalsearch').submit(function(){
		
		if($('.keywords', this).val().length < 3){
			$('.keywords', this).addClass('error');
			return false;
		} else {
			return true;	
		}
		
	});
	
	// global video player, opens in modal
	$('a.playvideo').click(function() {
		
		var videoFile = $(this).attr('href'); //get the video file
		$('body').append('<div id="dynamic-video-frame"></div>');
		
		var soDyn = new SWFObject('/flash/player.swf','mpl','640','380','9');
		soDyn.addParam('allowfullscreen','true');
		soDyn.addParam('allowscriptaccess','always');
		soDyn.addParam('wmode','opaque');
		soDyn.addVariable('autostart','true');
		soDyn.addVariable('file',videoFile);
		
		$.colorbox({
			innerWidth:'640px', 
			innerHeight:'380px',
			inline:true, 
			href:"#dynamic-video-frame",
			onComplete:function(){ 
				soDyn.write('dynamic-video-frame');
			},
			onClosed:function(){
				$('#dynamic-video-frame').remove();	
			}
		});
		
		return false;
	});
	

	//var region = $("body#contact h2").attr('id');
	//region.replace(/\s+/g, '-');
	
	$('a#inline').fancybox({
	  overlayColor: '#000000',
	  hideOnOverlayClick: false,
	  titleShow: false,
	  centerOnScroll: true
	  
	});
	$('a#report-a-problem').fancybox({'titleShow':false,'height':560});
});
