/*
 * Munich Javascript
 */


	  	function publishStream() {
			FB.ui({
					method: 'feed',
					name: 'MUNICH',
					link: 'http://munichband.dk',
					picture: 'http://munichband.dk/images/cover.jpg',
				//	caption: 'Download our new EP for free!',
					description: 'Download vores nye EP gratis!'
				},
			function(response) {
				if (response && response.post_id) {
					loadDownloadInIFrame(); // Start Download
				} else {
					alert('Post to facebook to start the download'); // Meddelelsen vises hvis brugeren ikke laver et post
				}
			});
		}

		function loadDownloadInIFrame() {

				location.href='http://munichband.dk/ep_download/download.php';
		}


	function muteOrUnmute() {
	var video = document.getElementById('video');
	var audio = document.getElementById('audio');
			video.muted = !video.muted;
			audio.muted = !audio.muted;
			if(video.muted) {
				document.getElementById('muteText').innerHTML ='<b>SOUND: OFF</b>';
				video.muteState = 1;
			} else {
				document.getElementById('muteText').innerHTML ='<b>SOUND: ON</b>';
				video.muteState = 0;
			}

	}


function doResize() {

	var deviceAgent = navigator.userAgent.toLowerCase();
	var agentIDiPhone = deviceAgent.match(/(iphone|ipod)/);
	var agentIDiPad = deviceAgent.match(/(ipad)/);
	
	if (agentIDiPad) {

//		$('#video_background_div').css('width', '100%');
//		$('#video_background_div').css('height', '635px');
		
//		$('#video').css('width', '1024px');
		$('#video').css('height', '618px'); // SAFE: 675 px + no top margin
		
		document.getElementById('video_background_div').style.marginTop = "22px";
	
	} else if (agentIDiPhone) {
		// iPhone specific resize here !!
		
	} else {
	
		var video_ratio = (1280/720);
            var x = 0;
            if (self.innerHeight) {
                      x = self.innerWidth;
            } else if (document.documentElement && document.documentElement.clientHeight) {
                      x = document.documentElement.clientWidth;
            } else if (document.body) {
                      x = document.body.clientWidth;
            }
			var y = 0;
			if (self.innerHeight) {
                      y = self.innerHeight; }
			else if (document.documentElement && document.documentElement.clientHeight) {
                      y = document.documentElement.clientHeight;
			} else if (document.body) {
                      y = document.body.clientHeight;
			}
			// den y-værdi, der ud fra vinduets x-værdi ville give videoen sin rette ratio
			var fs_y = x / video_ratio;

		if (document.getElementById('audio').active == 1) {
			document.getElementById('video_background_div').style.marginTop = "0px";
		} else if (fs_y < y && x > 1100) {
			var margin = parseInt((y-fs_y)/2);
			margin += "px";
			document.getElementById('video_background_div').style.marginTop = margin;
			
		} else if (fs_y < y && x <= 1100) {
			var margin = parseInt((y-619)/2);
			margin += "px";
			document.getElementById('video_background_div').style.marginTop = margin;
			
/*			$('#downloadBackground').css('right', '');
			$('#downloadText').css('right', '');
			$('#downloadBackground').css('left', '658px');
			$('#downloadText').css('left', '658px');
*/

		} else {
			document.getElementById('video_background_div').style.marginTop = "0px";
		}

		if (x <= 1100) {
			$('#downloadBackground').css('right', '');
			$('#downloadText').css('right', '');
			$('#downloadBackground').css('left', '577px');
			$('#downloadText').css('left', '577px');
			$('#like').css('right', '');
			$('#like').css('left', '577px');
			$('#controls').css('right', '');
			$('#controls').css('left', '840px');
			$('#cover').css('right', '');
			$('#cover').css('left', '936px');
		} else {
			$('#downloadBackground').css('left', '');
			$('#downloadText').css('left', '');
			$('#downloadBackground').css('right', '163px');
			$('#downloadText').css('right', '163px');
			$('#like').css('left', '');
			$('#like').css('right', '103px');
			$('#controls').css('left', '');
			$('#controls').css('right', '20px');
			$('#cover').css('left', '');
			$('#cover').css('right', '64px');
		}
	}

}

	function loadVideo1() {
		var video = document.getElementById('video');
		var isMuted = 0;
		if (video.muteState == null) {
				video.muteState = 0;
		}
		isMuted = video.muteState;
		
		
		if(navigator.userAgent.toLowerCase().indexOf('chrome') > -1) {
			video.src="video/video1.webm";
        	video.load();
        	video.play();

			if (isMuted == '1') {
				muteOrUnmute();
				muteOrUnmute();
			}
//			document.getElementById('loadBW').style.color='#CCFFCC';
//			document.getElementById('loadColor').style.color='#FFFFFF';
			video.addEventListener('ended',loadVideo2,false);
			
		} else if (video.canPlayType('video/mp4; codecs="avc1.42E01E, mp4a.40.2"') == "probably") { // or "maybe" or ""
		
			video.src="video/video1.mp4";
        	video.load();
        	video.play();
			
			if (isMuted == '1') {
				muteOrUnmute();
				muteOrUnmute();
			}
//			document.getElementById('loadBW').style.color='#CCFFCC';
//			document.getElementById('loadColor').style.color='#FFFFFF';
			video.addEventListener('ended',loadVideo2,false);
			
		} else if (video.canPlayType('video/ogg; codecs="theora, vorbis"') == "probably") {
			
			video.src="video/video1.ogv";
        	video.load();
        	video.play();

			if (isMuted == '1') {
				muteOrUnmute();
				muteOrUnmute();
			}
//			document.getElementById('loadBW').style.color='#CCFFCC';
//			document.getElementById('loadColor').style.color='#FFFFFF';
			video.addEventListener('ended',loadVideo2,false);

		}
		document.getElementById('video1title').innerHTML = '<strike>Tell Me All About The Future</strike>';
		document.getElementById('video2title').innerHTML = 'The Young Ones';
		doResize();
	}


	function loadVideo2() {
		var video = document.getElementById('video');
		var isMuted = 0;
		if (video.muteState == null) {
				video.muteState = 0;
		}
		isMuted = video.muteState;
		
		if (navigator.userAgent.toLowerCase().indexOf('chrome') > -1) {
			video.src="video/video2.webm";
        	video.load();
        	video.play();

			if (isMuted == '1') {
				muteOrUnmute();
				muteOrUnmute();
			}
//			document.getElementById('loadBW').style.color='#CCFFCC';
//			document.getElementById('loadColor').style.color='#FFFFFF';
			video.addEventListener('ended',loadVideo2,false);
			
		} else if (video.canPlayType('video/ogg; codecs="theora, vorbis"') == "probably") {
			video.src="video/video2.ogv";
        	video.load();
        	video.play();

			if (isMuted == '1') {
				muteOrUnmute();
				muteOrUnmute();
			}
//			document.getElementById('loadColor').style.color='#CCFFCC';
//			document.getElementById('loadBW').style.color='#FFFFFF';
			video.addEventListener('ended',loadVideo1,false);

		} else if (video.canPlayType('video/mp4; codecs="avc1.42E01E, mp4a.40.2"') == "probably"){
			video.src="video/video2.mp4";
        	video.load();
        	video.play();
			
			if (isMuted == '1') {
				muteOrUnmute();
				muteOrUnmute();
			}
//			document.getElementById('loadColor').style.color='#CCFFCC';
//			document.getElementById('loadBW').style.color='#FFFFFF';
			video.addEventListener('ended',loadVideo1,false);

		} else {
			alert('mp4 or ogg not supported');
		}
		document.getElementById('video1title').innerHTML = 'Tell Me All About The Future';
		document.getElementById('video2title').innerHTML = '<strike>The Young Ones</strike>';
		doResize();
	}


	function loadAudio1() {
		var audio = document.getElementById('audio');
		var video = document.getElementById('video');
		var isMuted = 0;
		if (video.muteState == null) {
				video.muteState = 0;
		}
		isMuted = video.muteState;		
		if (!!audio.canPlayType && audio.canPlayType('audio/mpeg;').replace(/no/, '')) {
			audio.src="audio/audio1.mp3";
        	audio.load();
       		audio.play();
			if (isMuted == '1') {
				muteOrUnmute();
				muteOrUnmute();
			}
			audio.addEventListener('ended',loadAudio2,false);
		} else { //if (audio.canPlayType('audio/ogg; codecs="vorbis"') == "probably") {
			audio.src="audio/audio1.ogg";
        	audio.load();
       		audio.play();
			if (isMuted == '1') {
				muteOrUnmute();
				muteOrUnmute();
			}
			audio.addEventListener('ended',loadAudio2,false);
		} 
		document.getElementById('audio1title').innerHTML = '<strike>Tell Me All About The Future</strike>';
		document.getElementById('audio2title').innerHTML = 'Stories About The Stars';
		document.getElementById('audio3title').innerHTML = 'Car Crash';
		document.getElementById('audio4title').innerHTML = 'The Young Ones (Acoustic)';
	}
	
	function loadAudio2() {
		var audio = document.getElementById('audio');
		var video = document.getElementById('video');
		var isMuted = 0;
		if (video.muteState == null) {
				video.muteState = 0;
		}
		isMuted = video.muteState;	
		if (!!audio.canPlayType && audio.canPlayType('audio/mpeg;').replace(/no/, '')) {
			audio.src="audio/audio2.mp3";
        	audio.load();
       		audio.play();
			
			if (isMuted == '1') {
				muteOrUnmute();
				muteOrUnmute();
			}
			audio.addEventListener('ended',loadAudio3,false);
		} else { //if (audio.canPlayType('audio/ogg; codecs="vorbis"') == "probably") {
			audio.src="audio/audio2.ogg";
        	audio.load();
       		audio.play();
	
			if (isMuted == '1') {
				muteOrUnmute();
				muteOrUnmute();
			}
			audio.addEventListener('ended',loadAudio3,false);
		}
		document.getElementById('audio1title').innerHTML = 'Tell Me All About The Future';
		document.getElementById('audio2title').innerHTML = '<strike>Stories About The Stars</strike>';
		document.getElementById('audio3title').innerHTML = 'Car Crash';
		document.getElementById('audio4title').innerHTML = 'The Young Ones (Acoustic)';
	}
	
	function loadAudio3() {
		var audio = document.getElementById('audio');
		var video = document.getElementById('video');
		var isMuted = 0;
		if (video.muteState == null) {
				video.muteState = 0;
		}
		isMuted = video.muteState;	
		if (!!audio.canPlayType && audio.canPlayType('audio/mpeg;').replace(/no/, '')) {
			audio.src="audio/audio3.mp3";
        	audio.load();
       		audio.play();
	
			if (isMuted == '1') {
				muteOrUnmute();
				muteOrUnmute();
			}
			audio.addEventListener('ended',loadAudio4,false);
		} else { //if (audio.canPlayType('audio/ogg; codecs="vorbis"') == "probably") {
			audio.src="audio/audio3.ogg";
        	audio.load();
       		audio.play();

			if (isMuted == '1') {
				muteOrUnmute();
				muteOrUnmute();
			}
			audio.addEventListener('ended',loadAudio4,false);
		}
		document.getElementById('audio1title').innerHTML = 'Tell Me All About The Future';
		document.getElementById('audio2title').innerHTML = 'Stories About The Stars';
		document.getElementById('audio3title').innerHTML = '<strike>Car Crash</strike>';
		document.getElementById('audio4title').innerHTML = 'The Young Ones (Acoustic)';
	}


	function loadAudio4() {
		var audio = document.getElementById('audio');
		var video = document.getElementById('video');
		var isMuted = 0;
		if (video.muteState == null) {
				video.muteState = 0;
		}
		isMuted = video.muteState;	
		if (!!audio.canPlayType && audio.canPlayType('audio/mpeg;').replace(/no/, '')) {
			audio.src="audio/audio4.mp3";
        	audio.load();
       		audio.play();

			if (isMuted == '1') {
				muteOrUnmute();
				muteOrUnmute();
			}
			audio.addEventListener('ended',loadAudio1,false);
		} else { //if (audio.canPlayType('audio/ogg; codecs="vorbis"') == "probably") {
			audio.src="audio/audio4.ogg";
        	audio.load();
       		audio.play();

			if (isMuted == '1') {
				muteOrUnmute();
				muteOrUnmute();
			}
			audio.addEventListener('ended',loadAudio1,false);
		}
		document.getElementById('audio1title').innerHTML = 'Tell Me All About The Future';
		document.getElementById('audio2title').innerHTML = 'Stories About The Stars';
		document.getElementById('audio3title').innerHTML = 'Car Crash';
		document.getElementById('audio4title').innerHTML = '<strike>The Young Ones (Acoustic)</strike>';
	}

/*
function supports_h264_baseline_video() {
  if (!supports_video()) { return false; }
  var v = document.createElement("video");
  return v.canPlayType('video/mp4; codecs="avc1.42E01E, mp4a.40.2"');
}

function supports_ogg_theora_video() {
  if (!supports_video()) { return false; }
  var v = document.createElement("video");
  return v.canPlayType('video/ogg; codecs="theora, vorbis"');
}

function supports_webm_video() {
  if (!supports_video()) { return false; }
  var v = document.createElement("video");
  return v.canPlayType('video/webm; codecs="vp8, vorbis"');
}
*/

	

	function goAudio() {
		document.getElementById('audio').active = 1;
//		$('#background_pic_div').css('display', 'inherit');
//		$('#background_pic_div').css('width', '100%');		
		$("#background_pic_div").fadeTo(1000, 1);
		document.getElementById('toggleAudio').innerHTML = '<b><u>AUDIO</u></b>';
		document.getElementById('toggleVideo').innerHTML = '<b>VIDEO</b>';
		var video = document.getElementById('video');
		$("#video").fadeOut(1000);
		$("#playlist_r1").fadeOut(1000);
		$("#playlist_r2").fadeOut(1000, function() {
    		$("#playlist_r3").fadeTo(1000, 1);
			$("#playlist_r4").fadeTo(1000, 1);
			$("#playlist_r5").fadeTo(1000, 1);
			$("#playlist_r6").fadeTo(1000, 1);
			video.src="";
			video.load();
			loadAudio1();

  		});
		doResize();
	}


	function goVideo() {
		document.getElementById('audio').active = 0;
		$('#background_pic_div').css('display', 'none');
		document.getElementById('toggleAudio').innerHTML = '<b>AUDIO</b>';
		document.getElementById('toggleVideo').innerHTML = '<b><u>VIDEO</u></b>';
		var audio = document.getElementById('audio');
		$("#video").fadeTo(1000, 1);
		$("#playlist_r3").fadeOut(1000);
		$("#playlist_r6").fadeOut(1000);
		$("#playlist_r5").fadeOut(1000);
		$("#playlist_r4").fadeOut(1000, function() {
			$("#playlist_r1").fadeTo(1000, 1);
			$("#playlist_r2").fadeTo(1000, 1);
			audio.src="";
			audio.load();
			loadVideo1();
  		});
	}


	function start() {
/*	
		document.getElementById('loadVideo1Marker').innerHTML ='->';
		document.getElementById('video').addEventListener('ended',loadVideo2,false);
*/
		// flyt evt op som første step i start() ??

		if($.browser.msie) {
			$("#downloadText").css('font-size', '19px');
			if($.browser.version=="6.0") {
//				alert("Im the annoying IE6");
				location.href="old_ie.html";
			}
		}
/*
		var ua = $.browser;
		if ( ua.msie) { //Internet Eksplorer
			alert("INTERNET EXPLORER");
			alert(parseInt(ua.version));
		}
			if (parseInt(ua.version, 10) == 5.0) { // virker det?
				alert(" test! ");
				location.href="old_ie.html";
			} else if (parseInt(ua.version) == 533 || parseInt(ua.version, 10) == 8.0 ) {
				// fade all controls and load the alert text - with links - in the control iframe
				alert("To enjoy all functionality on this website, use a HTML5-compatible browser such as Mozilla Firefox or Google Chrome");
				$("#controls").fadeOut(2000); // virker det?
			}
		} else if (ua.mozilla) { // Mozilla
			alert("FIREFOX");
		} else if (ua.webkit) { // Mozilla
			
			$("#on_off_r1").fadeOut(1000);
			$("#fullscreen_row").fadeOut(1000);
			$("#playlist_r1").fadeOut(1000);
			$("#playlist_r2").fadeOut(1000);
			$("#playlist_r3").fadeOut(1000);
			$("#playlist_r4").fadeOut(1000);
			$("#playlist_r5").fadeOut(1000);
*/
		doResize(); // setTimeout til et par sekunder ?? - så videoen har nået at loade lidt..



		if (!(!!document.createElement('video').canPlayType)) {
			$("#controls").fadeOut(3000, function() {
				document.getElementById('mainText').innerHTML ='To enjoy all functionality on this website, use a HTML5-compatible browser such as <a href="http://mozilla.com/firefox"><u>Mozilla Firefox</u></a>, <a href="http://www.google.com/chrome"><u>Google Chrome</u></a> or <a href="http://windows.microsoft.com/ie"><u>Internet Explorer 9</u></a>';
			})
		} else {
			
			loadVideo1();
		}

		var deviceAgent = navigator.userAgent.toLowerCase();
		var agentIDiPhone = deviceAgent.match(/(iphone|ipod)/); // deviceAgent.match(/(iphone|ipod|ipad)/);
		var agentIDiPad = deviceAgent.match(/(ipad)/);
		// iPad, iPod eller iPad
		if (agentIDiPhone) {
			// lav et fake click der starter video + resize bagefter så video fylder hele skærmen
			goAudio();
			document.getElementById('audio1title').innerHTML = 'Tell Me All About The Future';
			$("#toggleVideo").fadeTo(2000, 0.2);
			$("#toggleSeparator").fadeTo(2000, 0.2);
			$("#toggleVideo").unbind(); // unbind("click", goVideo); // WHY IS IT NOT WORKING ??? Check bindings in index.php
			$("#on_off_row").fadeTo(2000, 0.2);
			$("#toggleVideo").unbind("click");
		} else if (agentIDiPad) {
			document.getElementById('video1title').innerHTML = 'Tell Me All About The Future';
			$("#on_off_row").fadeTo(2000, 0.2);
			$("#on_off_row").unbind("click");
		}
	}

