//*******
//Sidebar
//*******
function toggleSideMenu(){
	resizeSideMenu();
	if($('#xsidemenu').is(':visible')){
		$('#xsidemenu').animate({'width':'0px'},function(){
			$('#xsidemenu_container').hide();
			$('#xsidemenu').hide();
		});
	}
	else {
		$('#xsidemenu_container').show();
		$('#xsidemenu').css('width','0px');
		$('#xsidemenu').show();
		$('#xsidemenu').animate({'width':'300px'});
	}
}
function getWindowSize(){
	var ww = $(window).width();
	var wh = $(window).height();
	
	var bodywidth = $('body').width();
	if(bodywidth && (bodywidth > ww)){
		wh *= (bodywidth/ww);
		ww = bodywidth;
	}
	
	return {width: ww, height: wh};
}
function resizeSideMenu(){
	var wsize = getWindowSize();
  var ww = wsize.width;
  var wh = wsize.height;
	
	//window height dependency with dynamic grid size on fixed headers
	//window size dependency with charting
	//aspect ratio / zoom ratio on mobile
	
	$('#xsidemenu').css('height',wh + 'px');
	$('body').css('min-height',wh + 'px');
	
	if(ww >= wh) $('body').removeClass('portrait').addClass('landscape');
	else $('body').removeClass('landscape').addClass('portrait');
}
$(window).resize(window.global_onResize);
$(document).ready(function(){
	window.global_onResize();
  $('#xsidemenu').click(function (e) {
    e.stopImmediatePropagation();
  });
});
window.global_onResize = function(){
	resizeSideMenu();
}
//**********
// Worldwide
//**********
function do_nothing(){
}
$(document).ready(function(){
  var langbox = $('#worldwide_languages');
  langbox.append('Worldwide');langbox.append('Spanish');langbox.append('Deutsch');langbox.append('Turkish');langbox.append('French');langbox.append('Czech ');langbox.append('Romanian');  $('#worldwide_link').hover(
    function(){ $('#worldwide_languages').show(); },
    function(){ $('#worldwide_languages').hide(); }
  );
  $('.querybox').focus(function(){ $(this).select(); $(this).css('color','#333'); });
  $('.querybox').mouseup(function(e){ e.preventDefault(); });
});
//*****
//Earth
//*****
//Test 15 vs 30 vs 60 fps
var FRAME_MAX = 239;
var FPS=20;
var FPS_MS = 1000/FPS;
vcache = new VideoCache();
vcache.StartLoad();
var curimg;
var swapimg;
var frame=0;
var lastframetime=0;
var wait_cache = false;
//Add caching on separate thread
function dock_close(){
	$('#dock').fadeOut(500);
}
function debuglog(txt){
//  $('#debug').prepend(txt+'
');
}
function itovframe(i){
  var pad = "000" + i;
  var rslt='/earth/'+pad.substring(pad.length-3)+'.png';
  return rslt;
}
$(document).ready(function(){
  debuglog('Starting...');
  curimg = $('#sw1');
  swapimg = $('#sw2');
  swapimg.load(swap_loaded);
  curimg.load(swap_loaded);
  
  load_next_frame();
	setTimeout("$('#dock').fadeIn(2000);",5000);
});
function swap_loaded(){
  var exptime = lastframetime + FPS_MS;
  var curframetime = (new Date()).getTime();
  if(curframetime >= exptime){
    swap_frames();
    debuglog('Frame '+frame+' not preloaded');
  }
  else{
    setTimeout("swap_frames()",exptime-curframetime);
  }
}
function swap_frames(){
  curimg.css('z-index',1);
  swapimg.css('z-index',2);
  swapimg.show();
  curimg.hide();
  var t = swapimg;
  swapimg = curimg;
  curimg = t;
  load_next_frame();
}
function load_next_frame(){
  //If frame is loaded, show it
  var targetframe = frame;
  if(wait_cache) targetframe = frame + Math.floor(FRAME_MAX/10);
  if(targetframe > FRAME_MAX) targetframe = FRAME_MAX;
  if(!vcache.IsLoaded(itovframe(targetframe))){
    wait_cache = true;
    setTimeout("load_next_frame()",FPS_MS);
    debuglog('Waiting 10% until ' + targetframe);
    return;
  }
  wait_cache = false;
  lastframetime = (new Date()).getTime();
  if(frame>FRAME_MAX) frame=0;
  swapimg.attr('src',itovframe(frame));
  frame++;
}
function VideoCache () {
  this.loadqueue = new Array();
  for(var i=0;i<=FRAME_MAX;i++){
    this.loadqueue.push(itovframe(i));
  }
  
  this.loaded = new Array();
  
  $('body').append('
');
  this.loaderimg = $('#VSlideLoader');
  
  this.PrependImages = function(imgarray){
    //Prepend array of images
    for(var i=0; i