$(document).ready(function(){
	$('.gallery-item').each(function() {
		/*$('a', this).lightBox({
			fixedNavigation: true,
			overlayBgColor: '#fff',
			imageLoading: 'wp-content/themes/maartjev1/resources/images/lightbox/lightbox-ico-loading.gif',
			imageBtnPrev: 'wp-content/themes/maartjev1/resources/images/lightbox/lightbox-btn-prev.gif',
			imageBtnNext: 'wp-content/themes/maartjev1/resources/images/lightbox/lightbox-btn-next.gif',
			imageBtnClose: 'wp-content/themes/maartjev1/resources/images/interface/close_button.png',
			imageBlank: 'wp-content/themes/maartjev1/resources/images/lightbox/lightbox-blank.gif'
		});*/
	});
	
	var firstClick = true;
	$('.agenda h2 a, .music h2 a, .gallery h2 a, .info h2 a').click(function() {
		if (firstClick) {
			$('#page-bg img.bg').fadeOut('fast', function() {
				$(this).remove();
			});
			firstClick = false;
		}
		$(this).parent().siblings('.data').slideToggle('fast', 'easeInOutSine');
		return false;
	});
	
	$('.album .play').click(function() {
		var title = $(this).parent().text();
		var album_title = $($(this).parents(".album").find("h3")[0]).text();
		var art_location = $($(this).parents(".album").find("img")[0]).attr("src");
		var href = $(this).attr('href');
		
		var album_songs = $(this).parents("ol").find("a");
		var songs_string = "";
		for (var a = 0, z = album_songs.length; a < z; ++a) {
			var song = $(album_songs[a]).attr("href");
			songs_string += song+(a == z - 1 ? "" : "@@@@@");
		}
		
		
		var songs_titlestring = "";
		for (var a = 0, z = album_songs.length; a < z; ++a) {
			var tl = $(album_songs[a]).parent().text();
			songs_titlestring += tl+(a == z - 1 ? "" : "@@@@@");
		}
		
		
		swfobject.embedSWF(
			'wp-content/themes/maartjev1/resources/swf/xspf/xspf_player_slim.swf',
			'player',
			'367',
			'247',
			'9',
			'wp-content/themes/maartjev1/resources/swf/swfobject/expressInstall.swf',
			{
				song_title: title,
				song_titles: songs_titlestring,
				album_title: album_title,
				art_location: art_location,
				song_url: songs_string,
				autoplay: true
			},
			{
				wmode: "transparent"
			}
		);
		return false;
	});
	
	$('.album a[href="info"]').click(function() {
		$('#overlay').show();
		$('#overlay').fadeIn(500);
		$("#info-box").fadeIn(1000);
		$("#info-box").removeClass('gallery_mode');
		$('#info-box .about').addClass('show_about');
		$('#info-box .about').removeClass('show_desc');
		$('#info-box')
			.find('.content').html($(this).siblings('.content').html()).end()
			.find('#about_text').text($(this).siblings('h3').text()).end()
		.show();
		return false;
	});
	
	function return_price_info(content)
	{
		if (content == null) {
			content = "";
		}
		return '<h3>'+content+'</h3>';
	}
	
	$('.album a[href="buy"]').click(function() {
		last_album_choice = $(this).attr("rel");
		$('#overlay').show();
		$("#overlay").fadeIn(500);
		$("#info-box").fadeIn(1000);
		$("#info-box").removeClass('gallery_mode');
		$('#info-box .about').addClass('show_about');
		$('#info-box .about').removeClass('show_desc');
		$('#info-box')
			.find('.content').html(return_price_info($(this).parent().find('h3').html())+$('#buy').html()).end()
			.find('#about_text').text($(this).siblings('h3').text()).end()
		.show();
		$('#info-box').find('.subm_button').click(function()
		{
			//$('#info-box').find('.content').html('<h3>Sending order...</h3>');
			$("#status").html('Sending order...');
			
			var data = {
				order_album: last_album_choice,
				amount: $("#amount").val(),
				nm: $("#nm").val(),
				address: $("#address").val(),
				city: $("#city").val(),
				country: $("#country").val(),
				phone: $("#phone").val(),
				email: $("#email").val()
			};
			
			$.post('order-page', data, function(data, status)
			{
				if (data.error) {
					$("#status").html(data.response);
				} else {
					$("#info-box").find(".content").html('<h3>'+data.response+'</h3>');
				}
			}, "json");
			//$('#info-box').find('.content').html('<h3>Successfully sent the order! You\'ll hear from us soon.</h3>');
			return false;
		});
		return false;
	});
	
	function get_images(html, cont)
	{
		$html = $(html);
		if ($(cont).hasClass('image')) {
			thehtml = [];
			$(html).each(function()
			{
				if ($(this).attr("href") && !$(this).hasClass("front")) {
					thehtml.push('<span class="imgcontainer"><img src="'+$(this).attr("href")+'" height="480" /></span>');
				}
			});
			return '<div class="galleryitems">'+thehtml.join("")+'</div>';
		} else {
			$a = $('<div>'+html+'</div>');
			$vid = $a.find('object');
			return '<div class="galleryitems">'+$vid.html()+'</div>';
		}
	}
	
	$('a[rel^="lightbc"]').click(function() {
		var $desc = $(this).siblings(".desc");
		$('#overlay').show();
		$("#overlay").fadeIn(500);
		$("#info-box").fadeIn(1000);
		if ($(this).hasClass("video")) {
			$("#info-box").addClass('no_nav');
		} else {
			$("#info-box").removeClass('no_nav');
		}
		$("#info-box").addClass('gallery_mode');
		$("#info-box .gallery_content .galleryitems").css({marginLeft: "0"});
		$('#info-box .about').removeClass('show_about');
		$('#info-box .about').addClass('show_desc');
		$('#info-box')
			.find('.gallery_content').html(get_images($(this).parent('.single_gallery').html(), $(this).parent('.single_gallery'))).end()
			.find('#desc_ct').html($desc.html()).end()
		.show();
		return false;
	});
	
	$("#info-box .tools a").click(function()
	{
		var $this = $(this);
		var dir = 0;
		if ($this.hasClass("l")) {
			dir = -1;
		}
		if ($this.hasClass("r")) {
			dir = 1;
		}
		$bx = $("#info-box");
		var n = 0;
		if ($.data($bx[0], "gallery")) {
			n = $.data($bx[0], "gallery");
		} else {
			$.data($bx[0], "gallery", 0);
		}
		if (n + dir >= $(".galleryitems").children().length) {
			return false;
		}
		if (n + dir < 0) {
			return false;	
		}
		n += dir;
		$.data($bx[0], "gallery", n);
		$("#info-box .gallery_content .galleryitems").animate({marginLeft: -(720 * n)+"px"}, {duration: 1000, queue: false});
		return false;
		
	});
	
	$('#info-box .close a').click(function() {
		$("#info-box").fadeOut(1000, function()
		{
			$('#info-box').hide();
		});
		$('#overlay').fadeOut(500, function()
		{
			$('#overlay').hide();
		});
		return false;
	});
	
	swfobject.embedSWF(
			'wp-content/themes/maartjev1/resources/swf/xspf/xspf_player_slim.swf',
			'player',
			'367',
			'247',
			'9',
			'wp-content/themes/maartjev1/resources/swf/swfobject/expressInstall.swf',
			{
				player_title: 'Maartje Teussink'
			},
			{
				wmode: "transparent"
			}
		);
});