// JavaScript Document

var page = 'news';
var lang = 'nl';
var vars = '';
var jsReady = false;
var active = false;
var pagetimer;

$(document).ready(function() { 
						   
	$("#go_backstage").click(function () {
		$.get ('/ajax.php', { cmd: 'goBackstage' }, function (data) { 
			window.location = data;
		});
	});
	
	$("#username").focus(function() { 
		if ($(this).val() == "E-mail address")
			$(this).val('');
	}).blur(function () { 
		if ($(this).val() == "")
			$(this).val('E-mail address');
	});
	
	$("#lang li a").click(function() {
								  
		if (lang != $(this).attr('rel')) {
			
			getContent (page, '', $(this).attr('rel'));
		} 
	});
	
	$("h1 a").click(function() {
			
		getContent ('news', '', lang);
		
	});
	
	$("#register").click(function() {
			
		getContent ('register', '', lang);
		
	});
	
	$("#a_forgot_password").click(function() {
			
		getContent ('forgot-password', '', lang);
		
	});
	
	$("#edit-profile").click(function() {
									  
		getContent ('edit-profile', '', lang);
		
	});
	
	$("#nav li a").click(function() {
			
		getContent (page, '', lang);
		
	});
	
	$("#band_biography").click(function () { $("#band_discography").children().attr('src', '/images/discography1.gif'); $(this).children().attr('src', '/images/biography2.gif'); getContent ('biography', '', lang); $(this).blur(); });
	$("#band_discography").click(function () { $("#band_biography").children().attr('src', '/images/biography1.gif'); $(this).children().attr('src', '/images/discography2.gif'); getContent ('discography', '', lang); $(this).blur(); });
	
	$("#videos_clips").click(function () { 
		$("#videos_live").children().attr('src', '/images/video_live1.gif'); 
		$("#videos_diaries").children().attr('src', '/images/video_diaries1.gif'); 
		$("#videos_media").children().attr('src', '/images/video_media1.gif'); 
		$(this).children().attr('src', '/images/video_clips2.gif'); 
		getContent ('videos', 'clips', lang, true); 
		$(this).blur(); 
	});
	
	$("#videos_live").click(function () { 
		$("#videos_clips").children().attr('src', '/images/video_clips1.gif'); 
		$("#videos_diaries").children().attr('src', '/images/video_diaries1.gif'); 
		$("#videos_media").children().attr('src', '/images/video_media1.gif'); 
		$(this).children().attr('src', '/images/video_live2.gif');
		getContent ('videos', 'live', lang, true); 
		$(this).blur(); 
	});
	
	$("#videos_diaries").click(function () { 
		$("#videos_clips").children().attr('src', '/images/video_clips1.gif'); 
		$("#videos_live").children().attr('src', '/images/video_live1.gif'); 
		$("#videos_media").children().attr('src', '/images/video_media1.gif'); 
		$(this).children().attr('src', '/images/video_diaries2.gif'); 
		getContent ('videos', 'diaries', lang, true); 
		$(this).blur(); 
	});
	
	$("#videos_media").click(function () { 
		$("#videos_clips").children().attr('src', '/images/video_clips1.gif'); 
		$("#videos_live").children().attr('src', '/images/video_live1.gif'); 
		$("#videos_diaries").children().attr('src', '/images/video_diaries1.gif'); 
		$(this).children().attr('src', '/images/video_media2.gif'); 
		getContent ('videos', 'media', lang, true); 
		$(this).blur(); 
	});
	
	$("#foto_official").click(function () {
		$("#foto_live").children().attr('src', '/images/live1.gif'); 
		$("#foto_backstage").children().attr('src', '/images/backstage1.gif'); 
		$("#foto_press").children().attr('src', '/images/press1.gif'); 
		$("#foto_fan").children().attr('src', '/images/fan1.gif'); 
		$(this).children().attr('src', '/images/official2.gif');
		getContent ('photos', 'official', lang, true); 
		$(this).blur(); 
	});
	
	$("#foto_live").click(function () {
		$("#foto_official").children().attr('src', '/images/official1.gif'); 
		$("#foto_backstage").children().attr('src', '/images/backstage1.gif'); 
		$("#foto_press").children().attr('src', '/images/press1.gif'); 
		$("#foto_fan").children().attr('src', '/images/fan1.gif'); 
		$(this).children().attr('src', '/images/live2.gif');
		getContent ('photos', 'live', lang, true); 
		$(this).blur(); 
	});
	
	$("#foto_backstage").click(function () {
		$("#foto_official").children().attr('src', '/images/official1.gif'); 
		$("#foto_live").children().attr('src', '/images/live1.gif');
		$("#foto_press").children().attr('src', '/images/press1.gif'); 
		$("#foto_fan").children().attr('src', '/images/fan1.gif'); 
		$(this).children().attr('src', '/images/backstage2.gif');
		getContent ('photos', 'backstage', lang, true); 
		$(this).blur(); 
	});
	
	$("#foto_press").click(function () {
		$("#foto_official").children().attr('src', '/images/official1.gif'); 
		$("#foto_live").children().attr('src', '/images/live1.gif'); 
		$("#foto_backstage").children().attr('src', '/images/backstage1.gif'); 
		$("#foto_fan").children().attr('src', '/images/fan1.gif'); 
		$(this).children().attr('src', '/images/press2.gif');
		getContent ('photos', 'press', lang, true); 
		$(this).blur(); 
	});
	
	$("#foto_fan").click(function () {
		$("#foto_official").children().attr('src', '/images/official1.gif'); 
		$("#foto_live").children().attr('src', '/images/live1.gif'); 
		$("#foto_backstage").children().attr('src', '/images/backstage1.gif'); 
		$("#foto_press").children().attr('src', '/images/press1.gif'); 
		$(this).children().attr('src', '/images/fan2.gif');
		getContent ('photos', 'fan', lang, true); 
		$(this).blur(); 
	});
	
	if (location.hash) {
		page = location.hash.replace("#", "");
		if (page.match(/\?/)) {
			array = page.split(/\?/);
			page = array[0];
			vars = array[1];
		}
		
		if (page.match('discography'))
			page = 'discography';
			
		if (page.match('videos'))
			page = 'videos';
			
		if (page.match('photos'))
			page = 'photos';
			
		if (page == "discography" || page == "biography")
			temp = "band";
		else if (page == 'home' || page == "register" || page == 'forgot-password' || page == 'edit-profile')
			temp = "news";
		else
			temp = page;
		
		$(".header").attr('src', '/images/header_' + temp + '.jpg');
	}
	
	getContent (page, vars, lang, true);
	
	$("#tweet").append('<script type="text/javascript">Tweet();</script>');
	
	jsReady = true;
	
	$(".login").val(' ').click(login);
	
});

function login () {
	
	var error = "";
		
	if ($("#username").val() == "" || $("#username").val() == "E-mail address")
		error += "Please fill in your e-mail address.\n";
	
	if ($("#password").val() == "")
		error += "Please fill in your password.\n";
		
	if (!error) {
		
		$.get("/ajax.php", { cmd: 'login', username: $("#username").val(), password: $("#password").val() }, function (data) {
																															   
			if (data.match('logintrue')) {
				
				person = data.replace('logintrue:', '');
				
				$("#login").css('display', 'none');
				$("#login_if").html('<p>You are now logged in as ' + person + '. <a href="#edit-profile" id="a_edit_profile">Edit profile </a>. </p><a href="#news" id="logout"><img src="/images/logout.gif" alt="" /></a><a href="#gobackstage"><img src="/images/gobackstage.gif" alt="go backstage" id="go_backstage" /></a>').css('display', 'block');
				
				$("#logout").click(function() {
					$("#login_if").css('display', 'none');
					$("#login").css('display', 'block');
					$("#webcam").remove();
				});
				
				$("#a_edit_profile").click(function () { 
					getContent ('edit-profile', '', lang, true);
				});
				
				$.get("/ajax.php", { cmd: 'areWebcam' } , function (data) { 
					if (data.match('true')) {
						$("#wrapper").append('<a href="/chat.php" target="_blank" id="webcam"></a>');
					}
				});
				
				$("#go_backstage").click(function () {
					$.get ('/ajax.php', { cmd: 'goBackstage' }, function (data) { 
						window.location = data;
					});
				});
				
				
			} else {
				
				alert ('Your login data is invalid.');
				
			}
			
		});
		
	} else {
		
		alert (error);
		
	}	
	
	return false;
		
}

function getContent (nieuw_page, vars, nieuw_lang, force) {
	
	if (page != nieuw_page || nieuw_lang != lang || force) {

		if (active == false) {
			
			clearInterval (pagetimer);
			
			active = true;
			
			page = nieuw_page;
			lang = nieuw_lang;
			
			$.page = page;
			
			if (page == "discography" || page == "biography")
				temp = "band";
			else if (page == "register" || page == 'forgot-password' || page == 'edit-profile')
				temp = "news";
			else
				temp = page;
			
			src = '/images/header_' + temp + '.jpg';
			$("#content_holder").before('<img src="' + src + '" alt="" class="header" style="z-index: 1;" />');
			$(".header:first").fadeOut(1000, function () { 
				$(this).next('.header').css('z-index', '2'); 
				$(".header:first").remove(); 
			});
			delete (src);
			
			delete (temp);
			
			/*
			if (page == "photos") {
				
				if ($("#fotos").css('display') != "block") {
					
					$("#content").empty().jScrollPane();
					$.get ("/ajax.php", { cmd: 'geefFotoalbums', lang: lang }, function (data) {
						$("#fotos").html(data);
						$('.fotoalbum_wrapper').hide();
						$('.fotoalbum').mouseover(function() {
							if (!$(this).hasClass('fotoalbum_active')) {
								$(this).siblings('.fotoalbum_active').each(function() { 
									var bg = $(this).css('background-image').replace('2.gif', '1.gif');
									$(this).css('background-image', bg).removeClass('fotoalbum_active').children('.fotoalbum_wrapper').hide(300); 
								});
								var bg = $(this).css('background-image').replace('1.gif', '2.gif');
								$(this).addClass('fotoalbum_active').css('background-image', bg).children('.fotoalbum_wrapper').show(300);
							}
						});

						$("#fotos").fadeIn(300);
						
						active = false;
						
						$("#fotos a").click(function () { 
							getContent ('photos', $(this).attr('rel'), lang, true);
						});
					});
						
				}
				
			}
			*/
			
			if (page == 'videos' && !vars)
				vars = 'clips';
		
			if (page == 'photos' && !vars)
				vars = 'official';
		
			$.get ("/ajax.php", { page: page, lang: lang, vars : vars }, function (data) {
				$.content = data;
				$("#content").fadeOut(300, function() { 
					$("#content").html($.content).fadeIn(300).jScrollPane();
					delete ($.content);
					
					$("#content img:not([rel=no-margin])").css('margin-top', '5px');
					$("#content img:[align=left]").css('margin-right', '10px');
					$("#content img:[align=right]").css('margin-left', '10px');
					
					if ($.page != "photos")
						$("#fotos").css('display', 'none');
						
					if ($.page != "videos")
						$("#videos").css('display', 'none');
						
					if ($.page == "news" || $.page == "blogs") {
						$("#kane10jaar").css('display', 'block');
						var nieuw = $("#dd").attr('src').replace('dd_other', 'dd_home');
					} else {
						$("#kane10jaar").css('display', 'none');
						var nieuw = $("#dd").attr('src').replace('dd_home', 'dd_other');	
					}
						
					$("#dd").fadeOut(120, function() {

						$("#dd").attr('src', nieuw).fadeIn(120);
					});
					delete (nieuw);
					
					if ($.page == "band" || $.page == "discography" || $.page == "biography") {
						
						if ($.page == "discography") {
							$("#band_discography").children().attr('src', '/images/discography2.gif'); 
							$("#band_biography").children().attr('src', '/images/biography1.gif'); 
						} else {
							$("#band_discography").children().attr('src', '/images/discography1.gif'); 
							$("#band_biography").children().attr('src', '/images/biography2.gif'); 	
						}
						$("#sub_nav").css('display', 'block');
					} else {
						$("#band_discography").children().attr('src', '/images/discography1.gif'); 
						$("#band_biography").children().attr('src', '/images/biography2.gif'); 
						$("#sub_nav").css('display', 'none');
					}
					
					if ($.page == "videos") {
						$("#videos").css('display', 'block');
					} else {
						$("#videos").css('display', 'none');
					}
					
					if ($.page == "photos") {
						$("#fotos").css('display', 'block');
					} else {
						$("#fotos").css('display', 'none');
					}
					
					if ($.page == "discography") {
						
						$('.discografie').each(function()
						{
							var height = $(this).height();
							$(this).css('height', '66px');
							$(this).mouseover(function() {
								if (!$(this).hasClass('disc_active')) {													   	
									$(this).siblings('.disc_active').removeClass('disc_active').animate({'height' : '66px'}, 300, 'swing');
									$(this).addClass('disc_active').animate({'height' : height+'px'}, 300, 'swing');
								}
							});
						});
						
						$("#content").jScrollPane();
						
					}
					
					sIFR.replace(vag, {
						selector: 'h2'
						,css: [
							'.sIFR-root { color: #000000; font-size: 29px; font-weight: bold; text-transform: uppercase; }'
						]
					});
					
					if ($.page == "videos")
						$(".colorbox").colorbox({fixedWidth:646, fixedHeight:480, iframe:true});
					else
						$(".colorbox").colorbox();
					
					active = false;
					
					delete ($.page);
					
					$('.video').children('a').css('background', 'none');
					$('.video').children('p').css('display', 'none');
					
					$('.video').mouseover(function()
					{
						$(this).children('a').css('background', 'url(/images/video_mouseover.png)');
						$(this).children('p').css('display', 'inline-block');
						$(this).mouseout(function()
						{
							$(this).children('a').css('background', 'none');
							$(this).children('p').css('display', 'none');
						});
					});
					
					$('.photo').children('a').css('background', 'none');
					$('.photo').children('p').css('display', 'none');
					
					$('.photo').mouseover(function()
					{
						$(this).children('a').css('background', 'url(/images/photo_mouseover.png)');
						$(this).children('p').css('display', 'inline-block');
						$(this).mouseout(function()
						{
							$(this).children('a').css('background', 'none');
							$(this).children('p').css('display', 'none');
						});
					});
					
					$(".photo a").click(function () {
						getContent ('photos', $(this).attr('rel'), lang, true);
					});
					
				});
			});
			
		}
		
	} else {
		
		pagetimer = setInterval (function () { getContent(nieuw_page, vars, nieuw_lang, force); }, 1000);
		
	}
	
}

function Tweet () {
	
	getTwitters('tweet', {
        id: 'kaneofficial', 
        count: 1, 
        withFriends: true, // currently disabled due to change in Twitter API
        enableLinks: false, 
        ignoreReplies: true,
        template: '<span class="image"><img height="34" width="34" src="/images/twitter.jpg" style="float: left; margin-right: 8px;" /></span><span class="twitterStatus">"%text%"</span>'
    });
	
}
			 
function isReady() {
	
	return jsReady;
}

function thisMovie(movieName) {
	if (navigator.appName.indexOf("Microsoft") != -1) {
		return window[movieName];
	} else {
		return document[movieName];
	}
}

function sendToActionScript(value) {
	thisMovie("flash_navigatie").sendToActionScript(value);
}

function sendToJavaScript(value) {
	if(value == 'getPage') {
		sendToActionScript(page);
	} else {
		location.hash = '#' + value;
		getContent (value, '', lang);
	}
}