var Superstars = {
   
   MONTH : ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'],

   map : undefined,
   current : '',
   KMLURL:'http://maps.google.com/maps/ms?ie=UTF8&oe=UTF8&msa=0&output=kml&msid=',
   INFO:{
      // 35.647127, 139.705064
      tokyo:{msid:'110406216745208749214.00049070cba4fe17b5ca1',
         latitude:35.647183,
         longitude:139.705126,
         zoom:17},
      paris:{msid:'110406216745208749214.00048ecea04edb3ea4fb8',
         latitude:48.865279,
         longitude:2.33379,
         zoom:16}
   },
   tz_offset: {},

   init: function() {
      $.browser.msie6 = $.browser.msie && parseInt($.browser.version) == 6;
      $(window).bind('resize',Superstars.resize);
      Superstars.resize();
      $('#copy').show();
      $('#map').show();
      Superstars.resize();
      Superstars.initmaps();
      Superstars.starttimer();
   },
   resize : function(evt) {
      // copy positioning
      var copy = $('#copy');
      var y_copy = Math.max($('#main-inner').innerHeight(),$(window).height()-copy.innerHeight());
      copy.css({position:'absolute',left:0,top:y_copy +'px'});
      // map positioning
      var map = $('#map');
      if($.browser.msie6) {
         map.css({
            position:'absolute',top:0,left:'208px',
            width:($(window).width()-$('#main').width())+'px',
            height:Math.max($(window).height(),$(document).height())+'px'
         });
         // not supported on msie6
         // ...
      } else {
         // fixed layout
         $('#map-menu').css({position:'fixed',zIndex:101,top:0,left:$('#main').width()+'px'});
         var w = Math.max($(window).width()-$('#main').width(),$('#map-menu').width());
         var h = $(window).height()-$('#map-menu').height();
         map.css({position:'fixed',zIndex:100,width:w+'px',height:h+'px',top:'37px',left:'208px'});
         $('#map-container').css({width:w+'px',height:h+'px'});
         $('#map-menu').show();
	  // facebook
	  var w_fb = $('#facebook').width();
	  var h_fb = $(window).height();
	  $('#facebook').css({position:'fixed',
			      zIndex:100000,
			      top:($(window).height()-80)+'px',
			      left:($(window).width()-302)+'px',
			      width:'292px',
			      height:'70px'});
	  var href_fb = 'http://www.facebook.com/plugins/likebox.php?' +
	      $.param(
		  {
		      href : 'http://www.facebook.com/pages/Superstars/165680990126809',
		      width:292,
		      height:70,
		      colorscheme:'light',
		      show_faces:'false',
		      stream:'false',
		      header:'false'
		  });
	  $('#facebook').attr('src',href_fb);
      }
   },
   initmaps : function() {
      var myOptions = {
         mapTypeControl: false,
         streetViewControl: true,
         navigationControl: true,
         navigationControlOptions: {style: google.maps.NavigationControlStyle.SMALL},
         mapTypeId : google.maps.MapTypeId.ROADMAP
      };
      Superstars.map = new google.maps.Map(document.getElementById("map-container"), myOptions);
      for(var p in Superstars.INFO) {
         Superstars.INFO[p].layer = new google.maps.KmlLayer(Superstars.KMLURL+Superstars.INFO[p].msid+
                                                            '&'+new Date().getTime(),
                                                             {preserveViewport: true});
         Superstars.INFO[p].layer.setMap(Superstars.map);
      }
      Superstars.changemaps('paris');
      // event
      for(var p in Superstars.INFO) {
        $('#btn-'+p).click(function() {
           var p = $(this).attr('id').substr("btn-".length);
           Superstars.changemaps(p);
        });
      }
   },
   changemaps : function(place) {
       if(Superstars.current != place) {
           $('#btn-'+place).removeClass('off').addClass('on');
           $('#btn-'+Superstars.current).removeClass('on').addClass('off');
           Superstars.current = place;
       }
      Superstars.gotomaps(place);
   },
   gotomaps : function(place) {
      var info = Superstars.INFO[place];
      var myLatLng = new google.maps.LatLng(info.latitude, info.longitude);
      Superstars.map.setCenter(myLatLng);
      Superstars.map.setZoom(info.zoom);
   },
   starttimer : function() {
      $.getJSON('/time',{},function(t) {
         Superstars.tz_offset.tokyo = t.jp*1000-new Date().getTime();
         Superstars.tz_offset.paris = t.fr*1000-new Date().getTime();
         //
         setInterval(function() {
         // Tokyo : 19 Sep. 2010     17;23
            var txt = '-';
            var t = new Date();
            t.setTime(new Date().getTime()+Superstars.tz_offset.tokyo);
            txt = 'Tokyo : ' + t.getUTCDate() + ' ' + Superstars.MONTH[t.getUTCMonth()] + '. '+t.getUTCFullYear()+' ' + Superstars.zerofill(t.getUTCHours(),2) + ':' + Superstars.zerofill(t.getUTCMinutes(),2);
            if($('#btn-tokyo').text() != txt) $('#btn-tokyo').text(txt);
            // paris
            t.setTime(new Date().getTime()+Superstars.tz_offset.paris);
            txt = 'Paris : ' + t.getUTCDate() + ' ' + Superstars.MONTH[t.getUTCMonth()] + '. '+t.getUTCFullYear()+' ' + Superstars.zerofill(t.getUTCHours(),2) + ':' + Superstars.zerofill(t.getUTCMinutes(),2);
            if($('#btn-paris').text() != txt) $('#btn-paris').text(txt);
         },1000/5);
         
      });
   },
   zerofill : function(number,width) {
      var ret = ''+number;
      while(ret.length < width) ret = '0'+ret;
         return ret;
   }
   
};
$(document).ready(Superstars.init);

