var animspeed = 400;
var animfunction = 'swing';
var width = 0;

function handle(item) {
	item = $(item).attr('id');
	
	switch (item) {
		case 'artist':
		case 'ambassador':
		case 'entertainer':
		case 'philosopher':
			if (item == topmost) return false;
			display_panel(item);
			return true;
		break;
		default:
			switch (topmost) {
				case 'artist':
				case 'ambassador':
				case 'entertainer':
				case 'philosopher':
					display_panel(item);
				break;
			}
		break;
	}
}

function display_panel(item) {
	width = $(document).width();
	pnoffset = 10;
	if (width > 1425) {
		pnoffset = (width - 1425);
		width = 1425;
	}

	
	$('#panel-newsletter').css('right', pnoffset + 'px');
	$('#sponsors').css('right', ( pnoffset + 10 ) + 'px');

	if (item != topmost) $('.page-selector').hide();

	// Reset all backgrounds
	if (item != topmost) {
		$('#artist').css('backgroundImage', 'url(/public/images/artist/background-1.jpg)');
		$('#ambassador').css('backgroundImage', 'url(/public/images/ambassador/background-1.jpg)');
		$('#entertainer').css('backgroundImage', 'url(/public/images/entertainer/background-1.jpg)');
		$('#philosopher').css('backgroundImage', 'url(/public/images/philosopher/background-1.jpg)');
		switch (item) {
			case 'music':
			case 'video':
			case 'shows-tour':
			case 'fan-club':
				$('#main').css('backgroundImage', 'url(/public/images/main/background-darker.jpg)');
			break;
			default:
				$('#main').css('backgroundImage', 'url(/public/images/main/background-1.jpg)');
			break;
		}
	}

//	$('.holder div').fadeOut(animspeed);
	if (item != topmost) $('.holder').html('');
	
	switch (item) {
		case 'artist':
			$('#artist')		.animate({ left: "0px",					backgroundPosition: '( 0px 0px )',		width: '1024px'	}, animspeed, animfunction);
			$('#ambassador')	.animate({ left: (width - 400) + "px",	backgroundPosition: '( -484px 0px )',	width: '100px'	}, animspeed, animfunction);
			$('#entertainer')	.animate({ left: (width - 300) + "px",	backgroundPosition: '( -326px 0px )',	width: '100px'	}, animspeed, animfunction);
			$('#philosopher')	.animate({ left: (width - 200) + "px",	backgroundPosition: '( -264px 0px )',	width: '100px'	}, animspeed, animfunction);
			$('#main')			.animate({ left: (width - 100) + "px",	backgroundPosition: '( -465px 0px )',	width: '100px'	}, animspeed, animfunction);
			$('.menu-top a').removeClass('active');
			if (item != topmost) load_content( 'artist', 6, 1);
			$('#artist .page-selector').html(sections_menu[6]).show();
		break;
		case 'ambassador':
			$('#artist')		.animate({ left: "0px",					backgroundPosition: '( -506px 0 )',	width: '100px' 		}, animspeed, animfunction);
			$('#ambassador')	.animate({ left: "100px",				backgroundPosition: '( 0 0 )',		width: '1024px'		}, animspeed, animfunction);
			$('#entertainer')	.animate({ left: (width - 300) + "px",	backgroundPosition: '( -326px 0 )',	width: '100px' 		}, animspeed, animfunction);
			$('#philosopher')	.animate({ left: (width - 200) + "px",	backgroundPosition: '( -264px 0 )',	width: '100px' 		}, animspeed, animfunction);
			$('#main')			.animate({ left: (width - 100) + "px",	backgroundPosition: '( -465px 0 )',	width: '100px' 		}, animspeed, animfunction);
			$('.menu-top a').removeClass('active');
			if (item != topmost) load_content( 'ambassador', 7, 1);
			$('#ambassador .page-selector').html(sections_menu[7]).show();
		break;
		case 'entertainer':
			$('#artist')		.animate({ left: "0px",					backgroundPosition: '( -506px 0 )',	width: '100px' 		}, animspeed, animfunction);
			$('#ambassador')	.animate({ left: "100px",				backgroundPosition: '( -484px 0 )',	width: '100px' 		}, animspeed, animfunction);
			$('#entertainer')	.animate({ left: "200px", 				backgroundPosition: '( 0 0 )',		width: '1024px'		}, animspeed, animfunction);
			$('#philosopher')	.animate({ left: (width - 200) + "px",	backgroundPosition: '( -264px 0 )',	width: '100px' 		}, animspeed, animfunction);
			$('#main')			.animate({ left: (width - 100) + "px",	backgroundPosition: '( -465px 0 )',	width: '100px' 		}, animspeed, animfunction);
			$('.menu-top a').removeClass('active');
			if (item != topmost) load_content( 'entertainer', 8, 1);
			$('#entertainer .page-selector').html(sections_menu[8]).show();
		break;
		case 'philosopher':
			$('#artist')		.animate({ left: "0px",					backgroundPosition: '( -506px 0 )',	width: '100px' 		}, animspeed, animfunction);
			$('#ambassador')	.animate({ left: "100px",				backgroundPosition: '( -484px 0 )',	width: '100px' 		}, animspeed, animfunction);
			$('#entertainer')	.animate({ left: "200px",				backgroundPosition: '( -326px 0 )',	width: '100px' 		}, animspeed, animfunction);
			$('#philosopher')	.animate({ left: "300px", 				backgroundPosition: '( 0 0 )',		width: '1024px'		}, animspeed, animfunction);
			$('#main')			.animate({ left: (width - 100) + "px",	backgroundPosition: '( -465px 0 )',	width: '100px' 		}, animspeed, animfunction);
			$('.menu-top a').removeClass('active');
			if (item != topmost) load_content( 'philosopher', 9, 1);
			$('#philosopher .page-selector').html(sections_menu[9]).show();
		break;
		default:
			$('#artist')		.animate({ left: "0px",					backgroundPosition: '( -506px 0 )',	width: '100px' 		}, animspeed, animfunction);
			$('#ambassador')	.animate({ left: "100px",				backgroundPosition: '( -484px 0 )',	width: '100px' 		}, animspeed, animfunction);
			$('#entertainer')	.animate({ left: "200px",				backgroundPosition: '( -326px 0 )',	width: '100px' 		}, animspeed, animfunction);
			$('#philosopher')	.animate({ left: "300px",				backgroundPosition: '( -264px 0 )',	width: '100px' 		}, animspeed, animfunction);
			$('#main')			.animate({ left: "400px", 				backgroundPosition: '( 0 0 )',		width: '1024px'		}, animspeed, animfunction);
		break;
	}
	
	switch(item) {
		case 'main':
			if (item != topmost) load_content( 'main', 1, 1);
			$('#main .page-selector').html(sections_menu[1]).show();
		break;
		case 'bio':
			if (item != topmost) load_content( 'main', 1, 1);
			$('#main .page-selector').html(sections_menu[1]).show();
		break;
		case 'music':
			if (item != topmost) load_content( 'music', 2, 1);
			$('#main .page-selector').html(sections_menu[2]).show();
			
			$('#main').css('backgroundImage', 'url(/public/images/main/background-darker.jpg)');
		break;
		case 'video':
			if (item != topmost) load_content( 'main', 3, 1);

			$('#main .page-selector').html(sections_menu[3]).show();
			$('#main').css('backgroundImage', 'url(/public/images/main/background-darker.jpg)');
		break;
		case 'shows-tour':
			if (item != topmost) load_content( 'main', 4, 1);
			$('#main .page-selector').html(sections_menu[4]).show();
			$('#main').css('backgroundImage', 'url(/public/images/main/background-darker.jpg)');
		break;
		case 'fan-club':
			if (item != topmost) load_content( 'main', 5, 1);
			$('#main .page-selector').html(sections_menu[5]).show();
			$('#main').css('backgroundImage', 'url(/public/images/main/background-darker.jpg)');
		break;
	}
	
	// Set pointer on retracted panels, default pointer on topmost
	$('#main').css('cursor', 'pointer');
	$('#artist').css('cursor', 'pointer');
	$('#ambassador').css('cursor', 'pointer');
	$('#entertainer').css('cursor', 'pointer');
	$('#philosopher').css('cursor', 'pointer');
	switch (item) {
		case 'artist':
		case 'ambassador':
		case 'entertainer':
		case 'philosopher':
			$('#' + item).css('cursor', 'default');
		break;
		default:
			$('#main').css('cursor', 'default');
		break;
	}

	// Deactivate / activate links
	$('.menu-bottom ul li a').removeClass('active');
	$('.menu-top ul li a').removeClass('active');
	$('#link-' + item).addClass('active');
	if (item == 'main') {
		$('#link-bio').addClass('active');
	}

	// Set topmost item to be the one we clicked on
	topmost = item;
	
}

function load_music() {
	$('#main .page-selector a').removeClass('active');
	$('#main .page-selector a.panel-1').addClass('active');


	
//	$('#main .holder').fadeOut(animspeed, function(){
		$('#main .page-selector').show();
		$('#main .holder').html(pages[2][1]);
//			$('#main .holder').fadeIn(animspeed);
//	});

		$('#player').stop();

}

function load_content( section, section_id, index ) {
//	alert('url(/public/images/fonds/' + section + '/' + id + '.jpg)');

	switch (section) {
		case 'bio':
			section = 'main';
			$('#' + section).css('backgroundImage', 'url(/public/images/' + section + '/background-' + index + '.jpg)');
		break;
		case 'video':
		case 'shows-tour':
		case 'fan-club':
		case 'music':
			section = 'main';
			$('#' + section).css('backgroundImage', 'url(/public/images/main/background-darker.jpg)');
		break;
		default:
			$('#' + section).css('backgroundImage', 'url(/public/images/' + section + '/background-' + index + '.jpg)');
	}


	$('#' + section + ' .page-selector a').removeClass('active');
	$('#' + section + ' .page-selector a.panel-' + index).addClass('active');

		$('#' + section + ' .holder').html(pages[section_id][index]);
//			alert('loaded');
			if ( (section_id == 9) && ( index == 3) ) {
				$('a.advitam').media({ width: 540, height: 303, flashvars: { image: '/public/files/movies/advitam.jpg' } });
			}
			
			if (section_id == 3) {
				$('a.gal-1').lightBox();
				$('a.gal-2').lightBox();
				$('a.gal-3').lightBox();
			}
		
//	$('#' + section + ' .holder').fadeOut(animspeed, function(){
//		$('#' + section + ' .holder').html(pages[section_id][index], function(){
//				$('#' + section + ' .holder').fadeIn(animspeed);
//		});
		//, function(){
		//	$('#' + section + ' .holder').fadeIn(animspeed);
		//} );
//	});
}


function show_album(id) {
//	$('#main .holder').fadeOut(animspeed, function(){
		$('#main .page-selector').hide();
		$('#main .holder').load('/Content/get_album_page/' + language + '/' + id, function(){
//			$(".mp3").jmp({
//				backcolor: "5d5d54",
//				forecolor: "b3b3aa",
//				width: 24,
//				showdownload: "false",
//				showfilename: "false"
//			});
			$('a.foxycart').unbind('click');
			fc_tb_init('a.foxycart');
//			$('#main .holder').fadeIn(animspeed);
		} );
//	});
}


function play_preview( track, id ) {
	state = $('#' + id + ' img').attr('src');
	if (state == '/application/lib/images/command-play.gif') {
		$('.button-preview img').attr('src', '/application/lib/images/command-play.gif');
		$('#' + id + ' img').attr('src', '/application/lib/images/command-stop.gif');
		$('#player').setFile(track).play();
	}
	else {
		$('.button-preview img').attr('src', '/application/lib/images/command-play.gif');
		$('#player').stop();
	}
}

$(document).ready(function(){
	geo();
	
	// Newsletter
	$('a#submitnews').click(function(){
		$('#panel-newsletter').load('/Content/store_email/' + language + '/' + $('#email').val());
		return false;
	});
	
	// MP3 Player
	$('#player').jPlayer({
  		ready: function () {
   		},
   		oggSupport: false,
		swfPath: '/lib/js/jquery/jplayer',
	    cssPrefix: "my_jp_class"
	})
	
	.onProgressChange( function(lp,ppr,ppa,pt,tt) {
   			if (lp < 100) {
   				// display load percent
   			}
   			if (ppa == 100) {
   				$('#player').stop();
				$('.button-preview img').attr('src', '/application/lib/images/command-play.gif');
   			}
   	});
	
	// Main Menu
	$('#link-bio').			click(function(){ topmost = ''; display_panel('bio'); return false;});
	$('#link-music').		click(function(){ topmost = ''; display_panel('music'); return false;});
	$('#link-video').		click(function(){ topmost = ''; display_panel('video'); return false;});
	$('#link-shows-tour').	click(function(){ topmost = ''; display_panel('shows-tour'); return false;});
	$('#link-fan-club').	click(function(){ topmost = ''; display_panel('fan-club'); return false;});
	
	// Bottom menu
	$('#link-artist').		click(function(){ topmost = ''; display_panel('artist'); return false;});
	$('#link-ambassador').	click(function(){ topmost = ''; display_panel('ambassador'); return false;});
	$('#link-entertainer').	click(function(){ topmost = ''; display_panel('entertainer'); return false;});
	$('#link-philosopher').	click(function(){ topmost = ''; display_panel('philosopher'); return false;});

	$('.panel').			click(function(){ handle(this); });
	
	display_panel(topmost_first);
	topmost = topmost_first;

	$(window).smartresize(function(){
		display_panel(topmost);
	});
});
