/**
 * Object _data;
 * this is dummy data. It is supposed to be loaded from server through SBS.Map.load object
 */
 
/** 
var _data=[
//  {stage:'1', caption:'Buenos Aires to Santa Rosa', olat:-32.916485, olng:-68.851318, dlat:-33.339707, dlng:-66.357422, color:"#FF0000"},
  {stage:'1', caption:'Buenos Aires to Santa Rosa', lat:-32.916485, lng:-68.851318, color:"#FF0000", distance:'586'},
  {stage:'2', caption:'Santa Rosa to XXX', lat:-33.339707, lng:-66.357422, color:"#00FF00", distance:'700'}
];
*/

var SBS={
  _fs: "s",
  init: function(){
    this._fs = (jQuery.cookie('fs') == 'undefined')?"s":jQuery.cookie('fs');
    if(this._fs == "l"){
      this.increase();
    }else{
      $("img.fs").attr("src", "images/icon-font_s.png");
    }
  },
  log:function(msg){
    if(console){
      console.info(msg);
    }
  },
  changeFontSize:function(){
    if(this._fs == "s"){
      this._fs = "l";
      $(".content_block").addClass("big");
    }else{
      this._fs = "s";
      $(".content_block").removeClass("big");
    }
    jQuery.cookie('fs', this._fs, 365);
  },
  home:{
    init:function(){
      $(".video-hero").click = $(".play-l").click(function(){
        $("#video-container .mask").fadeOut();
        $("#video-gallery").animate({height:'462px'}, "slow");
        $("#video-player-block").slideDown();
        $("#video-title").fadeOut();
        //$(this).siblings(".mask").fadeIn();
        return false;
      });
        $(".play-s").click(function(){
        $("#video-container .mask").fadeOut();
        if ($("#launch-video").is(":hidden"))
	        $("#video-gallery").animate({height:'462px'}, "slow", function(){ $('#launch-video').show(); });
        $("#video-player-block").slideDown();
        $("#video-title").fadeOut();
        $(this).siblings(".mask").fadeIn();
        return false;
      });
      $(".video-icon").click(function(){
        $("#video-container .mask").fadeOut();
        if ($("#launch-video").is(":hidden"))
	        $("#video-gallery").animate({height:'462px'}, "slow", function(){ $('#launch-video').show(); });
        $("#video-player-block").slideDown();
        $("#video-title").fadeOut();
        $(this).prev(".mask").fadeIn();
        return false;
      });
      $(".video-link").click(function(){
        $("#video-container .mask").fadeOut();
        if ($("#launch-video").is(":hidden"))
	        $("#video-gallery").animate({height:'462px'}, "slow", function(){ $('#launch-video').show(); });
        $("#video-player-block").slideDown();
        $("#video-title").fadeOut();
        $(this).parent().prev('.overlay').children(".mask").fadeIn();
        return false;
      });
    }
  },
  /**
   * Google Map MashUp Class
   *
   * config data is in config.js
   */
  Map:{
    src:null,
    baseIcon:null,
    init:function(){
      if (GBrowserIsCompatible()){
        var settings = {
          size: new GSize(_config.mapWidth, _config.mapHeight)
        };
        // create map object
        SBS.Map.src = new google.maps.Map2(document.getElementById(_config.mapTarget), settings);
        SBS.Map.src.setCenter(new google.maps.LatLng(-33.48643545099988,-67.8515625), 6);
        SBS.Map.src.addControl(new GSmallMapControl());
        SBS.Map.src.setMapType(G_NORMAL_MAP);

        // load data
        // it is supposed to be loaded by the following ajax call...
        // $.ajax({type: "GET", url: 'xxx', dataType: "json", success: SBS.Map.loadCompleteHandler, fail: });
        SBS.Map.loadCompleteHandler(_data);
      }
    },

    /**
     * @params data:mixed object
     */
    loadCompleteHandler:function(d){
      // dummy data load
      var map = SBS.Map.src;
      var platlng; // previous

      for(var i=0; i<d.length; i++){
        // origination latlng
        var latlng = new GLatLng(d[i].lat, d[i].lng);
        var marker = SBS.Map.createMarker(latlng, "Stage " + d[i].stage + ": " + d[i].caption);

        marker.stage = d[i].stage;
        marker.caption = d[i].caption;
        marker.distance = d[i].distance;
        map.addOverlay(marker);

        // draw line
        if(platlng != undefined){
          var polyOptions = {geodesic:true};
          var polyline = new GPolyline([platlng, latlng], d[i].color, 10, 0.4, polyOptions);
          map.addOverlay(polyline);

/*
          if(pline != undefined){
            var v = pline.getLength();
            marker.distance = v/1000;
          }
          pline = polyline;
*/
        }

        // set event
        GEvent.addListener(map, 'click', function(overlay, latlng){
          if(overlay.caption){
            var item = SBS.Map.createItem(overlay);
            overlay.openInfoWindowHtml(item);
          }
        });

        GEvent.addListener(marker, "infowindowopen", function() {

          var iw = SBS.Map.src.getInfoWindow();
          iw.reset(iw.getPoint(),
                   iw.getTabs(),
                   new GSize(192,140),
                   iw.getPixelOffset(),
                   iw.getSelectedTab());
        });

        platlng = latlng;
      }


      var cr = $("#"+_config.mapTarget).children("div").get(2);
      cr.style.width = "296px";
//      cr.style.height = "30px";
//      cr.style['white-space'] = "normal !important";
    },
    createItem:function(overlay){
      var item = $(".map-item-base").clone();
      item.removeClass("map-item-base");
      item.find(".caption").html("Stage "+overlay.stage + ": " + overlay.caption);
      item.find(".distance").html("Distance: "+ overlay.distance + " km");
      return item.html();
    },

    createMarker:function(latlng, title){
      var icon = new GIcon(G_DEFAULT_ICON);
      /*
       icon.shadow = "http://www.google.com/mapfiles/shadow50.png";
        baseIcon.iconAnchor = new GPoint(9, 34);
       */
      icon.iconAnchor = new GPoint(10, 10);
      icon.iconSize = new GSize(16, 16);
      icon.shadowSize = new GSize(20, 20);
      icon.image = _config.mapIcon;
      icon.infoWindowAnchor = new GPoint(10, 10);
//      icon.infoShadowAnchor = new GPoint(10, 10);

      var marker = new GMarker(latlng, {title: title, icon:icon});
      return marker;
    }
  },

  video:{
    init:function(){
      $("#video-list li").mouseover(function(){
                                      $(this).addClass('over');
                                    });
      $("#video-list li").mouseout(function(){
                                     $(this).removeClass('over');
                                   });
      $(".scroll-pane").jScrollPane(
        {showArrows:true,
         scrollbarWidth:15,
         arrowSize:15,
         scrollbarMargin: 0,
         dragMaxHeight:37}
      );
    }
  },

  tabs:{
    init:function(){
      $('#tab-results').click(
        function(){
          $('#result-block').load('result.html');
          $('#result-block').ajaxComplete(
            function(){
              $('#tab-results').addClass('active');
              $('#tab-live').removeClass('active');
            }
          );
        }
      );

      $('#tab-live').click(
        function(){
          $('#result-block').load('live.html');
          $('#result-block').ajaxComplete(
            function(){
              $('#tab-results').removeClass('active');
              $('#tab-live').addClass('active');
            }
          );
        }
      );
    }
  }
};

$(document).ready(function(){
  $('img[@src$=.png]').ifixpng();
});


