var muted = false;

// Youtube API controls.
var ytplayer;


$(document).ready(function(){
	// Load the youtube player
	var params = { allowScriptAccess: "always", bgcolor: "#ffffff" };
	var atts = { id: "myytplayer" };
	swfobject.embedSWF("http://www.youtube.com/apiplayer?enablejsapi=1&playerapiid=ytplayer","ytapiplayer", "494", "277", "8", null, null, params, atts);
	
	// start off muted...
	// Rob
	// muteVideo(muted);
	
	// hide the content...
	$('div#myspace').hide();
	$('div#flickr').hide();
	$('div#youtube').hide();
	$('div#twitter').hide();
	
	// preload Flickr images...
	// $('div#flickr ul li a').map(function(){
	// 	$.preloadImages($(this).attr('href'));
	// });
	
	// each menu tab calls up a different content panel
	/*$('li#tabMyspace').click(function(){
		$('div#myspace').show();
		resizeContent('div#myspace','div#myspaceBody',0);  // 40
		// Re bind the external links
		return false;
	});*/
	
	
	
	/*$('li#tabFlickr').click(function(){
		// alert('show flickr');
		$('div#flickr').show();
		resizeContent('div#flickr','ul', 100); // 410
		$('div#flickr div#mainPicHolder').addClass('showImage');
		$('div#flickr div#mainPicHolder').css('background-image','url('+ $('div#flickr ul li:first a').attr('href') +')');
		return false;
	});	
	
	$('#mainPicHolder').hide();
	$('#flickr_back').hide();*/
	
	/*$('li#tabYoutube').click(function(){
		$('div#youtube').show();
		resizeContent('div#youtube','ul',386); // 380
		swfobject.getObjectById("flash")._pauseVideo(true);
		return false;
	});*/
	
	
	/*$('li#tabTwitter').click(function(){
		$('div#twitter').show();
		resizeContent('div#twitter','div#tweet',60); //20
		return false;
	});*/
	
	// close tabs
	$('a.tabClose').click(function(){ 
		$(this).parent().parent().hide();
		$('#ul_flickr').show();
		$('div#flickr div#mainPicHolder').hide();
		return false;
	});
	
	// clean up the animation on the youtube panel...
	$('div#youtube a.tabClose').click(function(){ 
		$('div#youtube ul li a').removeClass('selectedItem');									   
		$(this).parent().children('div#playerwrap').hide();
		swfobject.getObjectById("flash")._pauseVideo(false);
		return false; 
	});

	$('div#youtube ul li a').click(function(){
		$('div#youtube ul li a').removeClass('selectedItem');
		$(this).addClass('selectedItem');
		var videoID = $(this).attr('rel');
		playVideo(videoID,0);
		return false;
	});

	// show the larger images from Flickr
	$('div#flickr ul li a').click(function(){
		$('div#flickr div#mainPicHolder').addClass('showImage');
		$('div#flickr div#mainPicHolder').css('background-image','url('+ $(this).attr('href') +')');
		$('#ul_flickr').hide();
		$('div#flickr div#mainPicHolder').show();
		$('#flickr_back').show();		
		return false;
	});

	// Hide the larger image and bring back the thumbs
	$('#flickr_back').click(function(){
		$('#ul_flickr').show();
		$('div#flickr div#mainPicHolder').hide();
		$('#flickr_back').hide();		
		return false;
	});

	
	// mute the sound on the background video?
	$('a#soundControl').click( 
		function(){
			toggleMute(); 
			return false;
		}
	);
	
	relExternal();

	
});



	// get the content area sized up right...
	function resizeContent(element, contentElement, height){ 
		var headerHeight = 150; 
		var winHeight = $(window).height();//window.innerHeight;
		var isMSIE = /*@cc_on!@*/false;
		if( isMSIE ){
			/*winHeight = (winHeight-50);*/
			headerHeight = 200;
		}
		//alert('height: '+height+', winHeight: '+ winHeight +', content height:'+ ((winHeight-headerHeight)-40) +', ul height: '+ ((winHeight-headerHeight)-height));
		$(element).css('height',(winHeight-headerHeight)+'px');
		///alert((winHeight-headerHeight)+'px');
		$(element).children('div.contentBody').css('height',((winHeight-headerHeight)-40)+'px');		
		$(element).children('div.contentBody').children(contentElement).css('height',((winHeight-headerHeight)-height)+'px');
		//alert(contentElement +': '+ $(element).children('div.contentBody').children(contentElement).height());
		//alert($(element).height());
		return false;
	}



	// Function to parse the URL - Rob - going to use it to get the videoString if its in the url.
	function gup( name )
	{
	  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	  var regexS = "[\\?&]"+name+"=([^&#]*)";
	  var regex = new RegExp( regexS );
	  var results = regex.exec( window.location.href );
	  if( results == null )
	    return "";
	  else
	    return results[1];
	}
	
	
	
	function onYouTubePlayerReady(playerId) {
	    ytplayer = document.getElementById('myytplayer');
		if(ytplayer){
			setInterval(updateytplayerInfo, 250);
			updateytplayerInfo();
			ytplayer.addEventListener('onStateChange', 'onytplayerStateChange');
		}
	 	// Load a video on player ready. If there is a video id in the url use that else load the default band vid.
		
	}

	function onytplayerStateChange(newState) {
	     setytplayerState(newState);
	}
	
	function updateytplayerInfo() {
	    if (ytplayer) {
	        updateTimebar();
			updateBufferbar();
	    }
	}
	
	function updateTimebar() {
	    var all = ytplayer.getDuration();
	    var part = ytplayer.getCurrentTime();
	    var percent = getPercent(all, part);
	    var timebarWidth = 325;
	    document.getElementById('timebarIndicator').style.width = percent * (timebarWidth / 100) + "px";
	}
	
	function getPercent(all, part) {
	   return (all > 0) ? (100 / all) * part : 0;
	}
	
	function updateBufferbar() {
	    var all = ytplayer.getVideoBytesTotal();
	    var part = ytplayer.getVideoBytesLoaded();
	    var percent = getPercent(all, part);
	    var bufferbarWidth = 325;
	    document.getElementById('bufferbarIndicator').style.width = percent * (bufferbarWidth / 100) + "px";
	}

	function toggleMute() {
		muted = !muted;
		muted ?	bImg = 'mute_on' : bImg = 'mute_off';
		muted ?	txt = 'Turn sound on' : txt = 'Turn sound off';
		muteVideo(muted);
		//$('a#soundControl').css('background-image',"url('../_graphics/" + bImg + ".png')");
		$('a#soundControl').html(txt);
	}
	
	function muteVideo(mute){
		swfobject.getObjectById("flash")._muteVideo(mute);
		// return false;
	}
	
	function pauseVideo(pause){
		if (!pause && ytplayer){
			ytplayer.pauseVideo();
			// swfobject.getObjectById("flash")._pauseVideo(pause);
		}
		// return false;
	}
	
	function playVideo(video,num){		
		if (ytplayer){
			ytplayer.loadVideoById(video,num);
		}
	}

	function play(){
		ytplayer.playVideo();
	}


jQuery.preloadImages = function(){
  for(var i = 0; i<arguments.length; i++){
    jQuery("<img>").attr("src", arguments[i]);
  }
}


function relExternal(){
	//External links.
	$('a[rel="external"]').click( function() {
		window.open( $(this).attr('href') );
		return false;
	});
}
