var site_root = '/picor/';
var playerready = false;
var currentId = 0;
var totalPres = 6;
var presHeight = 450;
var presWidth = 900;
var totalPs = 0;
var currentP = 0;
var interval = 0;
var rememberTrack = 'none';


// FOR EXTERNAL INTERFACE WITH FLASH OBJECT
function initializeMP() {
	if(navigator.appName.indexOf("Microsoft") != -1) {
		flashMP = window.flashObjectID;  //flashObjectID is the id of your flash object
	}
	else {
		flashMP = window.document.flashObjectID;
	}
}





$(document).ready(function() {
	$('div#search').hide();
	$('div.presentation_inner span').hide();
	$('div.text_wrapper').slideUp();


	//initializeMP();
	//var myFlashMovie = document.getElementById("flashObjID");
	//var firstmp = site_root+'assets/mp3/Picor_Intro.mp3';
	//myFlashMovie.playMyTrackInFlash(firstmp);
	
	
	
	///flashMP.playMyTrackInFlash(firstmp);
	
	///////////////////////////// J PLAYER /////////////////////////*/
	
	/* 
	
	$("div#jquery_jplayer").jPlayer({
			
			//alert('test');
			ready: function () {
				var firstmp = site_root+'assets/mp3/Picor_Intro.mp3';
				var firstogg = site_root+'assets/mp3/Picor_Intro.ogg';
				playerready = true;//alert(firstmp);
				//alert(firstogg);
				$(this).setFile(firstmp, firstogg).play();
				//$(this).setFile("http://www.miaowmusic.com/mp3/Miaow-07-Bubble.mp3", "http://www.miaowmusic.com/ogg/Miaow-07-Bubble.ogg").play();

				$("div#jquery_jplayer").volumeMax();
				demoInstanceInfo($(this), $("#jplayer_info"));
			},
			//volume: 100,
			oggSupport: true
	});
	*/
	/*
		.jPlayerId("play", "play")
		.jPlayerId("pause", "pause")
		.jPlayerId("stop", "stop")
		.jPlayerId("volumeMin", "vmute")
		.jPlayerId("volumeMax", "vmax")
		.jPlayerId("volumeBar", "vhalf")
		.onProgressChange( function(lp,ppr,ppa,pt,tt) {
			$("#pcent").text(parseInt(ppa)+"%");
		});
	
		$("#track-01").click(function() {
	
			$("#trackname").text($(this).text());
			$("#jquery_jplayer").setFile('http://www.miaowmusic.com/mp3/Miaow-05-The-separation.mp3').play();
			return false;
		});
	
		$("#track-02").click(function() {
	
			$("#trackname").text($(this).text());
			$("#jquery_jplayer").setFile('http://www.miaowmusic.com/mp3/Miaow-04-Lismore.mp3').play();
			return false;
		});
	
		$("#track-03").click(function() {
	
			$("#trackname").text($(this).text());
			$("#jquery_jplayer").setFile('http://www.miaowmusic.com/mp3/Miaow-10-Thin-ice.mp3').play();
			return false;
		});

	*/

	////////////////////////////// J PLAYER //////////////////////////


	
	$("a#search_button").click(function () { 
		//$(this).hide('slow');
   		$('div#search').show('medium', function(){
			//$('a#search_button').hide(5000);			   
		});
	});
   
	$("a#mute").click(function () { 
		
		var iframerel = $('iframe#mp3_iframe').attr('rel');
		//alert(iframerel);
		if(iframerel == 'on'){
			$('iframe#mp3_iframe').attr('rel', 'off');
			var trackabc = $('iframe#mp3_iframe').attr('src');
			$('iframe#mp3_iframe').attr('track', trackabc);
			$('iframe#mp3_iframe').attr('src', 'none');
			$(this).addClass('off');
		}
		else {
			$('iframe#mp3_iframe').attr('rel', 'on');
			$(this).removeClass('off');
			var newiframtrack = $('iframe#mp3_iframe').attr('track');
			//alert(newiframtrack);
			$('iframe#mp3_iframe').attr('src', newiframtrack);

		}
		/*	*/
	});
   
   
   
	function center_presentation_text(currentId) {
		
		$('div#presentation div.presentation_inner[rel*='+currentId+'] div.text_wrapper').each(function(index) {
			var text_h = $(this).height();
			var pres_h = $(this).parent().height();
			var top = (pres_h - text_h)/2;
			if(top < 0){ top = 0;}
			//alert('top: '+top);		
			$(this).animate({marginTop: top}, 1000);
			
			//alert(index);
			
		});
	}
   
	//center_presentation_text();



	function animate_p_manual(){
		
			$('div.presentation_inner div.text_wrapper li').unbind("click");
			$('div.presentation_inner div.text_wrapper li').click(function(){
				clearInterval(interval);
				$(this).find('p').slideDown();												   
				$(this).find('h5').fadeTo("slow", 0.95, function(){ center_presentation_text(currentId); });
				$(this).siblings().find('p').slideUp();												
				$(this).siblings().find('h5').fadeTo("slow", 0.3);
				$(this).siblings().css({backgroundImage: 'url("assets/img/presentation_line_bg.png")'});
				$(this).css({backgroundImage: 'url("assets/img/presentation_li_focus.png")'});
				//alert(totalPs);
			});
			
	}
	
	animate_p_manual();


	function animate_p(){
			
			currentP++;
			if(currentP>totalPs){ currentP = 1; }
			//alert(currentId+'--'+totalPs+'--'+currentP);
			var thisP = currentP-1;
			var thisThis = $('div.presentation_inner[rel*='+currentId+'] li:nth('+thisP+')');
			thisThis.find('p').slideDown('slow');
			thisThis.find('h5').fadeTo("slow", 0.95, function(){ center_presentation_text(currentId); });
			thisThis.siblings().find('p').slideUp('slow');												
			thisThis.siblings().find('h5').fadeTo("slow", 0.3);
			thisThis.siblings().css({backgroundImage: 'url("assets/img/presentation_line_bg.png")'});
			thisThis.css({backgroundImage: 'url("assets/img/presentation_li_focus.png")'});
		
	}
   
   
   




	function close_pres_p() {
	   $('div#presentation p').hide();
	}
	
	close_pres_p();

	function close_pres_inner() {
		//alert('hello');
		$('div.presentation_inner_wrapper').animate({opacity: 0}, 50);
	}
	
	close_pres_inner();

	function navigate_presentation() {
		$('a.navigate_pres').unbind("click");
		$('a.navigate_pres').click(function(){
			//alert('navigate_presentation');
			// next or prev
			$('div.text_wrapper').attr('rel', 'not');
			$('div.text_wrapper').slideUp();
			$('div.text_wrapper p').slideUp();
			
			
			$('div#presentation div#model_small').fadeOut();
			//alert('start: '+interval);
			clearInterval(interval);
			
			var direction = $(this).attr('alt');
			if(direction == 'next')   { currentId++; }
			if(direction == 'prev')   { currentId--; }
			if(currentId <= 0)        { currentId = totalPres; }
			if(currentId > totalPres) { currentId = 1; }
			
			
			center_presentation_text(currentId);
			// count P's
			totalPs = 0;
			$('div.presentation_inner[rel*='+currentId+'] p').each(function(index){ totalPs++; });
			
			currentP = 0;
			
			
			// (site_root+'assets/mp3/Picor_Intro.mp3', site_root+'assets/mp3/Picor_Intro.ogg').play();
			
			var l = $('div.presentation_inner[rel*='+currentId+'] span').attr('alt');
			var t = $('div.presentation_inner[rel*='+currentId+'] span').attr('rel');
			
			//alert("l: "+l+" t: "+t);
			var bp = '"(-'+l+'px -'+t+'px)"';
			//alert("bp: "+bp);
			$('div.presentation_inner[rel*='+currentId+']').animate({backgroundPosition: bp}, 50);
			
			var margin = ((currentId-1)*presHeight)*-1;
		
			$('div.presentation_inner_wrapper').animate({opacity: 0}, 900, function(){ 
				$('div.presentation_inner_wrapper').animate({marginTop: margin}, 50);
				$('div#presentation').animate({backgroundPosition: bp}, 2000, function(){ 
					animate_p();
					
					$('div.presentation_inner_wrapper').animate({opacity: 1}, 2000, function(){ 
						
						$('div.presentation_inner[rel*='+currentId+'] div.text_wrapper').slideDown('slow');
						$('div.presentation_inner[rel*='+currentId+'] div.text_wrapper').attr('rel', 'active');
						clearInterval(interval);
						interval = setInterval(animate_p, 8500);
						
						
						//MP3 Player
						var mpthree = $('div.presentation_inner[rel*='+currentId+']').attr('alt');
						var newmpthree = 'assets/mp3/'+mpthree+'.mp3';
						var iframerel = $('iframe#mp3_iframe').attr('rel');
						
						if(iframerel == 'on'){
							$('#mp3_iframe').attr('src', 'mp3player.php?track='+newmpthree);
						}
						
						$('#mp3_iframe').attr('track', 'mp3player.php?track='+newmpthree);
						
						
						//flashMP.playMyTrackInFlash(newmpthree);

						
						// jPlayer
			
							//alert('test');
							/*if(playerready) {
								var mpthree = $('div.presentation_inner[rel*='+currentId+']').attr('alt');
								var newmpthree = site_root+'assets/mp3/'+mpthree+'.mp3';
								var newogg = site_root+'assets/mp3/'+mpthree+'.ogg';
								$("div#jquery_jplayer").setFile(newmpthree, newogg).play();
							}*/
						//alert('start: '+interval);
					});
				});
			});
						
		});
		
	}
	
	navigate_presentation()
   
   	
   
   
});

