var fancyBoxConf = {
	cyclic: true,
	padding: 0,
	margin: 15,
	overlayOpacity: 0 
}

// Mark links
$(document).delegate('.ajax-links a', 'click', function(){
	var t = $(this);
	// Skip for admin mode or image lists or music files
	if(window.adminMode || t.closest('.list-images').length || t.hasClass('music-play'))
		return false;

	href = t.attr('href');

	// Allow anchors and file download
	if('#'==href.substr(0, 1) || t.hasClass('button-download'))
		return true;
	
	if('http://'==href.substr(0, 7))
		return !window.open(href);

	$.get(href, {}, pageOnLoad, 'html');

	location.hash = '#' + href;
	return false;
});

// History support
var oldHash;
var hash = location.hash;
function checkHash()
{
	hash = location.hash;
	if(hash && hash!=oldHash)
	{
		oldHash = hash;
		$.get(hash.substr(1), {}, pageOnLoad, 'html');

	}
}
setInterval(checkHash, 100);

// Page onLoad
function pageOnLoad(data, status)
{
	$(data).each(function(i, e){
		if($(e).is('#body')){
			$('#body').replaceWith(e);
			initPage($(e));
			
			if(!window.adminMode && _gaq)
				_gaq.push(['_trackPageview']);
		}
	});
}

function initPage(target)
{
	// Custom scroll
	$(".scroll").jScrollPane();

	// Fancybox
	$('.image').not('.no-zoom').fancybox(fancyBoxConf);
	$('.button-play').not('.music-button').click(function(){
		$.fancybox({
			'padding'		: 0,
			'autoScale'		: false,
			'transitionIn'	: 'none',
			'transitionOut'	: 'none',
			'title'			: this.title,
			'width'		: 680,
			'height'		: 495,
			'href'			: this.href.replace(new RegExp('watch\\?v=', 'i'), 'v/'),
			'type'			: 'swf',
			'swf'			: {
			'wmode'		: 'transparent',
			'allowfullscreen'	: 'true'
			}
		});

		return false;
	});

	// Site credits
	siteCreditsBox = $('.site-credits-box');
	$('.site-credits-button').click(function(){
		console.log('credit on', siteCreditsBox);
		siteCreditsBox.css('margin-top', -siteCreditsBox.outerHeight()/2);
		siteCreditsBox.fadeIn('slow');
		return false;
	});
	siteCreditsBox.find('.button-close').click(function(){
		siteCreditsBox.fadeOut('fast');
		return false;
	});
}

// Ajax forms
$(document).delegate('form', 'submit', function(){
	var t = $(this);
	var target = t.attr('action');
	if(!target){
		if(location.hash)
			target = location.hash.substr(1);
		else
			target = location.pathname;
	}
	$.post(target, t.serialize(), pageOnLoad, 'html');
	return false;
});

/* Music player */
var player = $('#player');
var playedItem;
var jPlayerReady = false;

$(document).delegate('.music-play', 'click', function(){
	var t = $(this);
	var href = t.attr('href');

	if(t.hasClass('music-is-plaing'))
	{
		player.jPlayer('pause');
		t.removeClass('music-is-plaing')
			.addClass('music-is-paused');
		if('Pause'==t.text())
			t.text('Play');
	}
	else if(t.hasClass('music-is-paused'))
	{
		player.jPlayer('play');
		t.removeClass('music-is-paused')
			.addClass('music-is-plaing');
		if('Play'==t.text())
			t.text('Pause');
	}
	else
	{
		// Clear if plaing
		if(playedItem)
		{
			playedItem.removeClass('music-is-plaing')
				.removeClass('music-is-paused');
			if('Pause'==playedItem.text())
				playedItem.text('Play');
			player.jPlayer('clearMedia');
			playedItem = null;
		}

		// Play file
		player.jPlayer('setMedia', {
			mp3: href
		}).jPlayer('play');

		// Mark played
		t.addClass('music-is-plaing');
		if('Play'==t.text())
			t.text('Pause');
		playedItem = t;
	}
	return false;
});

player.jPlayer({
	ready: function(){
		if(!jPlayerReady) // FF bug
		{
			if(!window.adminMode)
				$('.music-autoplay').first().click();
			jPlayerReady = true;
		}
	},
	supplied: 'mp3',
	swfPath: '/templates/nataliasafran.com/swf/'
}).bind($.jPlayer.event.ended, function(e){
	var currentHref = playedItem.attr('href');
	var found = false;
	var list = playedItem.closest('.music-list').find('.music-play');
	if(list.length)
	{
		list.each(function(){
			if(found)
			{
				$(this).click();
				return found = false;;
			}
			else if(currentHref == $(this).attr('href'))
				found = true;
		});

		if(found)
			list.first().click();
	}
		
});

initPage($(document));
