$(document).ready(function(){
	
	$('#menu-topmenu li').hover(function(){
		$(this).addClass('hover');
	}, function(){
		$(this).removeClass('hover');	
	});
	
	$('#gallery .images img:first').css('opacity', 1).addClass('active');
		
	$('.fancyThis').fancybox();
	
	$('#sponsList li img').each(function(){
		$(this).css('padding-top', function(){
			return (103 - $(this).height()) / 2;
		});
	});
	
	$('.sub-menu li:first').addClass('first');
	$('.sub-menu li:last').addClass('last');
	
	if($('.current-menu-item').length <= 0)
		$('#menu-topmenu li:first').addClass('current-menu-item');
	
	$('.sponsorList .item .image').each(function(){
		var imgHeight  = $(this).height();
		var textHeight = $(this).siblings('.text').height();
		console.log(imgHeight + ' - ' + textHeight);
		if(textHeight > imgHeight){
			var padding = (textHeight - imgHeight) / 2;
			$(this).css('padding-top', padding + 'px');
		}		
	});
	
	/* Gallery */
	if($('#gallery .images img').length >= 2)
	{
		$('#gallery').append('<div class="leftArrow"></div><div class="rightArrow"></div>');
		$('#gallery .images img').each(function(){
			var div = $('<div/>');
			$(div).addClass('button');
			$('#gallery .controllers').append(div);
		});
		$('#gallery .controllers .button:first').addClass('active');
	
		$('#gallery .controllers .button').click(function(){
			var thisIndex = $(this).index() + 1;
			$('#gallery .images img.active').animate({opacity: 0}).removeClass('active');
			$('#gallery .images img:nth-child(' + thisIndex + ')').animate({opacity: 1}).addClass('active');
			$(this).siblings('.active').removeClass('active');
			$(this).addClass('active');
		});
		
		$('#gallery .leftArrow').click(function(){
			if($('#gallery .images img.active').is(':first-child'))
			{
				$('#gallery .images img.active').animate({opacity: 0}).removeClass('active');
				var picIndex = $('#gallery .images img:last').animate({opacity: 1}).addClass('active').index();
			}
			else
			{
				var picIndex = $('#gallery .images img.active').animate({opacity: 0}).removeClass('active').prev().animate({opacity: 1}).addClass('active').index();
			}
			$('#gallery .controllers .button.active').removeClass('active');
			$('#gallery .controllers .button:nth-child(' + (picIndex + 1) + ')').addClass('active');
		});
		
		$('#gallery .rightArrow').click(function(){
			if($('#gallery .images img.active').is(':last-child'))
			{
				$('#gallery .images img.active').animate({opacity: 0}).removeClass('active');
				var picIndex = $('#gallery .images img:first').animate({opacity: 1}).addClass('active').index();
			}
			else
			{
				var picIndex = $('#gallery .images img.active').animate({opacity: 0}).removeClass('active').next().animate({opacity: 1}).addClass('active').index();
			}
			$('#gallery .controllers .button.active').removeClass('active');
			$('#gallery .controllers .button:nth-child(' + (picIndex + 1) + ')').addClass('active');
		});
	}	
	
	/* Gallery End */
		
		$('.videoThumbs a').click(function(event){
			event.preventDefault();
			$(this).parent().siblings('.active').removeClass('active');
			$(this).parent().addClass('active');
			loadScript(oEmbedUrl + '?url=' + $(this).attr('href') + '&height=400&width=960&title=false&byline=false&amp;&portrait=false&callback=' + oEmbedCallback);
			return false;
		});
});

// Tell Vimeo what function to call
var oEmbedCallback = 'embedVideo';

// Set up the URL
var oEmbedUrl = 'http://vimeo.com/api/oembed.json';

// Load the first one in automatically?
var loadFirst = true;

// This function puts the video on the page
function embedVideo(video) {
	var videoEmbedCode = video.html;
	document.getElementById('embed').innerHTML = unescape(videoEmbedCode);
}

// This function loads the data from Vimeo
function loadScript(url) {
	var js = document.createElement('script');
	js.setAttribute('src', url);
	document.getElementsByTagName('head').item(0).appendChild(js);
}
