$(document).ready(function() {
	var nowplayingDown = false;
	var playingElem;
	var galleryElem;
	init();
	/* Event handler for sound samples in "work" section
	(btw. do not use opacity attribute and/or jquery effects like fadeTo for elements containing text, causes very ugly text display in all ie versions because of the cleartype technology */
	$('a.track').click(function(e) {
		var elem = $(this).parent().parent().parent();
		var actObj = this;
		if (playingElem) {
			if (playingElem.attr('id') != $(elem).attr('id')) {
				$(playingElem).removeClass('itemDisabled');
				$(playingElem).find('div.bar ul.links a').unbind('click');
				$(elem).addClass('itemDisabled');
				$(elem).find('div.bar ul.links a').click(function(e) {
					e.preventDefault();
					return false;
				});
				playingElem = elem;
				playTrack($(this).attr('href'), $(elem).children('h3').text());
				addNowplaying(actObj);
			}
		} else {
			$(elem).addClass('itemDisabled');
			$(elem).find('div.bar ul.links a').click(function(e) {
				e.preventDefault();
				return false;
			});
			playingElem = elem;
			playTrack($(this).attr('href'), $(elem).children('h3').text());
			addNowplaying(actObj);
		}
		e.preventDefault();
		return false;
	});
	/* Event handler for gallery thumbnails in "studio" section */
	$('a.gallery').click(function(e) {
		var actObj = $(this);
		if (galleryElem) {
			if ($(galleryElem).children('img').attr('alt') != $(this).children('img').attr('alt')) {
				$(galleryElem).css('opacity', 1);
				actObj.css('opacity', 0.2);
				galleryElem = actObj;
				$('div#gallery').empty();
				$('div#gallery').append('<img src="' + actObj.attr('href') + '" alt="' + actObj.children('img').attr('title') + '" />');
				$('div#gallery').append('<p>' + actObj.children('img').attr('title') + '</p>');
			}
		} else {
			$(this).css('opacity', 0.2);
			galleryElem = $(this);
			$('div#gallery').empty();
			$('div#gallery').append('<img src="' + $(this).attr('href') + '" alt="' + $(this).children('img').attr('title') + '" />');
			$('div#gallery').append('<p>' + $(this).children('img').attr('title') + '</p>');
		}
		e.preventDefault();
		return false;
	});
	/* Init validator plugin for contact form */
	$("div#contact form").validate({
		rules: {
			message: {required: true},
			firstname: {required: true},
			mail: {required: true, email: true}
		},messages: {
			message: {
				required: 'Bitte Mitteilung angeben!'
			},
			firstname: {
				required: 'Bitte Vorname angeben!'
			},
			mail: {
				required: 'Bitte Mail angeben!',
				email: 'Diese Mail ist nicht gültig!'
			}
		}
	});
	// show first gallery image when page is loaded */
	$('a.gallery:first').trigger('click');
});

function init() {
	/* init scrollFollow plugin for flash mp3 player */
	$('div#player').scrollFollow({
		speed: 1000,
		offset: 40
	});
	/* add a pagination for "work" section */
	$('div#work').pager('div.page', {
		navId : 'pager',
		navClass: '',
		prevText: 'zurück',
		nextText: 'weiter',
		highlightClass: 'current'
	});
	$('div#player').append('<div id="nowplaying"></div>');
	$('div#nowplaying').css('display', 'none');
	$('div.bg').addClass('jsBg');
	$('div.bg').removeClass('bg');
	$('div.jsBg').append('<p>Bitte wählen Sie einen Track aus ...</p>');
}

/* Adds current track infos to the nowplaying container */
function addNowplaying (elem) {
	var title = $(elem).parent().parent().parent().children('h3').html();
	var content = $(elem).parent().parent().parent().children('p').html();
	var links = $(elem).parent().parent().children('ul').html();
	$('div#nowplaying').empty();
	$('div#nowplaying').append('<h4><span class="hide">Now Playing: </span>' + title + '</h4>');
	$('div#nowplaying').append('<p>' + content + '</p>');
	if (links != null) {
		$('div#nowplaying').append('<ul>' + links + '</ul>');
	}
	if (!this.nowplayingDown) {
		$('div#nowplaying').slideDown("normal");
		this.nowplayingDown = true;
	}
}

/* Create Flash Object */
function playTrack(mp3, text) {
	$('div.jsBg').empty();
	$('div.jsBg').flash(
	{
		src: 'http://skytone.lnz.ch/skytone/player/player.swf',
		width: 275,
    	height: 34,
		flashvars: {
			bgcolor: '#0b8ad9',
			txtcolor: '#ffffff',
			barbgcolor: '#0b8ad9',
			loadbar: '#3ca1e1',
			posbar: '#ffffff',
			loop: false,
			mp3url: mp3,
			txt: text,
			autoplay: true
		}
	}, {
		expressInstall: true
	});
}

