$(document).ready(function(){
	initHero();
	initPlaylist();
	$('a[rel="external"]').attr('target', '_blank');
	$('a[rel="image"]').bind('click', handleCarouselImage);
	$('a[rel="video"]').bind('click', handleCarouselVideo);
	$('a[rel="watch"]').bind('click', handlePlaylistVideo);
	$('a[rel="photos"]').fancybox({
		padding: 5,
		overlayOpacity: 0.9,
		overlayColor: '#e4e1d2'
	});
});

/* Click Handlers
===========================================================*/

function handleCarouselImage(event)
{
	var src = $(this).attr("href");
	loadImage(src, '#image-holder');
	bringToFront('#image-holder');
	$('#yt-play').hide();
	ytplayer.stopVideo();
	ytplayer.clearVideo();
	event.PreventDefault;
	return false;
};

function handleCarouselVideo(event)
{
	var src = $(this).attr("href");
	loadVideo(src);
	bringToFront('#video-holder');
	$('#yt-play').show();
	event.PreventDefault;
	return false;
};

function handlePlaylistVideo(event)
{
	var src = $(this).attr("href");
	var	title = $(this).text();
	loadVideo(src);
	$('#nowplaying').text(title);
	event.PreventDefault;
	return false;
};


/* Functions
===========================================================*/

function loadImage(src, div)
{
    var img = new Image();
	$(img).load(function () {
        $(this).hide();
        $(div).html('').append(this);
        $(this).fadeIn(500);
    }).error(function () {
        $(this).fadeOut(500);
    }).attr('src', src);
};

function loadVideo(src)
{
	if (!touch) {
		ytplayer.loadVideoByUrl(src);
	} else {
		updateYouTubeiFrame(src);
	};
};

function bringToFront(div)
{
	$(div).css('z-index', 998);
	$(div).siblings().css('z-index', 997);
};

/* YouTube® Player API
===========================================================*/

var ytplayer;

if (!touch) {
	google.load("swfobject", "2.1");
	google.setOnLoadCallback(createYouTubePlayer);
};

function createYouTubePlayer()
{
	var params = { wmode: "transparent", allowScriptAccess: "always" };
	var atts = { id: "ytplayer", bgcolor: "#000000" };
	// swfobject.embedSWF("http://www.youtube.com/apiplayer?&enablejsapi=1&playerapiid=ytplayer", "yt-embed", "100%", "100%", "8", null, null, params, atts);
	swfobject.embedSWF("http://www.youtube.com/apiplayer?version=3&enablejsapi=1&playerapiid=ytplayer", "yt-embed", "100%", "100%", "8", null, null, params, atts);
};

function onYouTubePlayerReady(playerId)
{	
	ytplayer = document.getElementById('ytplayer');
	ytplayer.addEventListener("onStateChange", "onYouPlayerStateChange");

	$('a#yt-play').click(function(event){
		playing = $(this).hasClass('playing');
		if (playing) {
			ytplayer.pauseVideo();
		} else {
			ytplayer.playVideo();
		};
		event.PreventDefault;
		return false;
	});

	firstMime = $('#thumbs li a:first').attr("rel");
	firstSrc = $('#thumbs li a:first').attr("href");

	if (firstMime == "image"){
		loadImage(firstSrc, '#image-holder');
		bringToFront('#image-holder');
	} else if (firstMime == "video"){
		loadVideo(firstSrc);
		bringToFront('#video-holder');
		$('#yt-play').show();
	};
	
	bodyid = $('body').attr('id');
	if (bodyid == 'videos') {
		firstPlaylistSrc = $('#playlist li a:first').attr("href");
		firstPlaylistTitle = $('#playlist li a:first').text();
		loadVideo(firstPlaylistSrc);
		$('#nowplaying').text(firstPlaylistTitle);
	};
};

function onYouPlayerStateChange(newState) {
	// unstarted (-1), ended (0), playing (1),
	// paused (2), buffering (3), video cued (5)
	if (newState == 1 || newState == 3) {
		$('a#yt-play').addClass('playing');
	} else if (newState == 0 || newState == 2) {
		$('a#yt-play').removeClass('playing');
	};
};

/* YouTube® for Mobile
===========================================================*/

function updateYouTubeiFrame(url)
{
	url = url.replace('http://www.youtube.com/v/', 'http://www.youtube.com/embed/');
	var ytiframe = document.getElementById('yt-iframe');
	ytiframe.src = url + "?autoplay=1";
}

/* Hero Carousel
===========================================================*/

function initHero()
{
	/* =carousel
	---------------------*/

	$('#hero #thumbs').jcarousel({
		vertical: true,
		scroll: 1,
		auto: 5,
		wrap: 'circular',
		animation: 350,
		easing: 'jswing', // easeInBack
		buttonNextHTML: null,
		buttonPrevHTML: null,
		initCallback: thumbs_initCallback,
		itemFirstInCallback: thumbs_itemFirstInCallback,
		itemLastOutCallback: { onAfterAnimation: thumbs_itemLastOutCallbackAfterAnimation },
		itemVisibleInCallback: { onBeforeAnimation: thumbs_itemVisibleInCallbackBeforeAnimation },
		itemVisibleOutCallback: { onBeforeAnimation: thumbs_itemVisibleOutCallbackBeforeAnimation }
	});

	function thumbs_initCallback(carousel, state) {
		$('#thumbs li:eq(1)').addClass("selected", 250);
		$('#thumbs li').mouseenter(function() {
			$('#thumbs').jcarousel('stopAuto');
		}).mouseleave(function() {
			$('#thumbs').jcarousel('startAuto');
		});
	}

	function thumbs_itemFirstInCallback(carousel, item, idx, state) {
		$(item).removeClass("selected", 250);
	};

	function thumbs_itemLastOutCallbackAfterAnimation(carousel, item, idx, state)
	{
		$(item).addClass("selected", 250);
	};

	function thumbs_itemVisibleInCallbackBeforeAnimation(carousel, item, idx, state) {
		$('img', item).show();
		$('span', item).show();
	};

	function thumbs_itemVisibleOutCallbackBeforeAnimation(carousel, item, idx, state)
	{
		$('img', item).fadeOut(250);
		$('span', item).fadeOut(250);
	};

	$('#thumbs li').mouseenter(function() {
		$('#thumbs').jcarousel('stopAuto');
	}).mouseleave(function() {
		$('#thumbs').jcarousel('startAuto');
	});

}

/* Playlist Carousel
===========================================================*/

function initPlaylist()
{

	$('#playlist').jcarousel({
		scroll: 1,
		auto: 0,
		wrap: null,
		animation: 350,
		easing: 'jswing',
		initCallback: playlist_initCallback
	});
	
	function playlist_initCallback(carousel, state) {

	}
}

/* =end of file
---------------------*/
