var gallery;
 	
var VimeoEmbed = {};

VimeoEmbed.init = function(e)
{
    //Listen to the load event for all the iframes on the page
    $('iframe').each(function(index, iframe){
       // iframe.addEvent('onLoad', VimeoEmbed.playerLoaded);
		addEvent('onLoad', VimeoEmbed.playerLoaded);
    });
};

VimeoEmbed.playerLoaded = function(player_id)
{
    VimeoEmbed.setupAPIEventListeners($('#'+player_id).get(0));
};

VimeoEmbed.setupAPIEventListeners = function(target)
{
    target.addEvent('onPlay', VimeoEmbed.onPlay);
    target.addEvent('onPause', VimeoEmbed.onPause);
    target.addEvent('onSeek', VimeoEmbed.onSeek);
    target.addEvent('onProgress', VimeoEmbed.onProgress);
};

VimeoEmbed.onPlay = function(player_id)
{
	gallery.pause();
};

VimeoEmbed.onPause = function(player_id)
{
	var number = ($.address.value() == "") ? 0 : parseInt($.address.value().substr(1));
	gallery.run(number);
};

VimeoEmbed.onSeek = function(time, player_id)
{
	gallery.pause();
};

VimeoEmbed.onProgress = function(time, player_id)
{
};

jQuery(document).ready(function()
{
	$( "#accordion" ).accordion();
		
	if(navigator.platform == 'iPad' || navigator.platform == 'iPhone' || navigator.platform == 'iPod')
	{
		$("#footer-spacer").hide();
		$("#footer").css("position", "static");
	}
	
	$.address.strict(false);	

	if($(location).attr('pathname') == "/")
	{
		gallery = new Gallery($("div[class='four-column-box']").children("a"), $, {hashPrefix:"/"});
		
		gallery.run(0, true);

		$("a[class='left']").click(function(event)
		{
			gallery.prev();
			event.preventDefault();
		});

		$("div[class='right'] a").click(function(event)
		{
			gallery.next();
			event.preventDefault();
		});
	} else if($(location).attr('pathname').substr(0, 6) == "/work/")
	{
		// without fade
		//gallery = new Gallery($("div[class='four-column-box']").children("a"), $, {hashPrefix: "/"});
		// with fade
		gallery = new Gallery($("div[class='four-column-box']").children("a"), $, {hashPrefix: "/", prevTransition: 'Fade', nextTransition: 'Fade', gotoTransition: 'Fade'});
		
		var number = ($.address.value() == "") ? 0 : parseInt($.address.value().substr(1));	
		gallery.goto(number);
		
		$("div[class='four-column-box'] a[class='image']").click(function(event)
		{
			if($(this).attr('href') == "#")
			{
				gallery.next();
				$('.pagination a').css("text-decoration", "none");	
				$('.pagination a').eq(gallery.currentNumber).css("text-decoration", "underline");				
				event.preventDefault();
			}
		});

		$("div[class='pagination'] a").click(function(event)
		{
			gallery.goto(parseInt($(this).attr("href").substr(2)) - 1);
			$('.pagination a').css("text-decoration", "none");	
			$('.pagination a').eq(gallery.currentNumber).css("text-decoration", "underline");
			event.preventDefault();
		});
		
		$(window).keydown(function(event)
		{
			switch(event.keyCode)
			{
			  	case 37:
			    	gallery.prev();
					$('.pagination a').css("text-decoration", "none");	
					$('.pagination a').eq(gallery.currentNumber).css("text-decoration", "underline");
			    	break;
			  	case 39:
			    	gallery.next();
					$('.pagination a').css("text-decoration", "none");	
					$('.pagination a').eq(gallery.currentNumber).css("text-decoration", "underline");
			    	break;	
			}
		});
		$('.pagination a').css("text-decoration", "none");	
		$('.pagination a').eq(gallery.currentNumber).css("text-decoration", "underline");	
	}
	
	VimeoEmbed.init($);
});
