(function($){
  $.fn.clearWithText = function(text){
    $(this).attr("value", text)
    $(this).focus(function(){
      var currentval = $(this).attr("value")
      if (currentval == "" || currentval == text)
        $(this).attr("value", "")
    })
    $(this).blur(function(){
      var newval = $(this).attr("value")
      
      if (newval == "" || newval == text)
        $(this).attr("value", text)
    })
    return $(this)
  }
}(jQuery));


(function($){
  $.fn.tabs = function(opts){
      
    var opts = opts || {}
    var options = {
      content: '#tabs-content',
      enabled: 'active',
      disabled: 'disabled',
      wrapper: '.tabs-container'
    }
    
    $.extend(options, opts, {})
    $container = $(options.wrapper)
    $tabs = $(this)
    $links = $('a', $tabs)
    
    if($links.length < 1) return
    
    $links.click(function(){
      $links.addClass(options.disabled).removeClass(options.enabled)
      $(this).addClass(options.enabled).removeClass(options.disabled)
      $(options.content + " > *", $container).hide()
      
      $($(this).attr("href")).show()
      
      return false
    })
    
    $('a.' + options.disabled, $tabs).each(function(i, el){
      var sel = $(el).attr("href")
      $(sel).hide()
    });
    
  }
}(jQuery));



function DistanceMeasurer(_id, distance){
  if($("#" + _id).length < 1) return

  var id = _id
  var geocoder = new GClientGeocoder()
  var mapIsInitialized = false
  var from_point, to_point, map
  var $distance = $(distance)
  
  this.addPoint = function(from_address, to_address){
    if($("#" + _id).length < 1) return
     geocoder.getLatLng(from_address, function(point){
        from_point = point
        didRecievePoint()
      })

      geocoder.getLatLng(to_address, function(point){
        to_point = point
        didRecievePoint()
      })
  }
  
  var didRecievePoint = function(){
    if(to_point === undefined || from_point === undefined) return
    
    drawMap(to_point, from_point)
  }
  
  var drawMap = function(to, from){
    if(!mapIsInitialized) initializeMap()
    
    var directions = new GDirections(map, null)
    map.setCenter(new GLatLng(from.y, from.x), 15);
    
    map.setCenter(new GLatLng(to.y, to.x), 15);
    map.addControl(new GSmallMapControl());
    
    
    directions.loadFromWaypoints([to, from], {travelMode:G_TRAVEL_MODE_DRIVING, getPolyline:true, locale:"sv_SE"})
    GEvent.addListener(directions, "load", function(){
     var speed = 17/60 * 1000
     var minutes = Math.ceil(directions.getDistance().meters / speed)
     $distance.html("").append("<strong>Beräknad tid med cykel: </strong>" + minutes + " minuter.<br />(" + directions.getDistance().html + ")")
    })
  }
  
  var initializeMap =  function(){
    if (GBrowserIsCompatible()) {
      	map = new GMap2(document.getElementById(id));
	   }
  }
}

/*
PriceHelper = {
  content: "<h3>Prissättning</h3><p>Studenter betalar mindre än andra bla bla bla</p>",
  show: function(x, y){
    var html = $("<div id='tool-tip'><div id='arrow'></div>" + this.content + "</div>")
    $(html).css({top: y - 75, left: x + 25})
    
    $('body').append(html)
  },
  
  hide: function(){
    $('#tool-tip').remove()
  }
}*/


$(document).ready(function(){
  $('.list-controls a').click(function(){
    $('.list-controls a').addClass('inactive')
    $(this).removeClass('inactive')
  })
  $('#location-input').clearWithText("Skriv din adress här")
  $('#newsletter-input').clearWithText("Nyhetsbrev?")
  $('#search-query').clearWithText("Sök")
 
  
  $('.tabs').tabs({
    disabled: "inactive",
    content: "#price-tables"
  })
  
  $('#sidebar-schedule').tabs({
    disabled: "inactive",
    content: "#sidebar-schedule-content"
  })
  
  
  /*$('#price-tables td').hover(
    function(event){
      PriceHelper.show(event.pageX, event.pageY)
    },
    function(event){
      PriceHelper.hide()
    })
  */
  
  
  $('#distance-maps').hide()
  $('#location-form').submit(function(){
    var address = $('input', this).attr("value")
    var bhusMap = new DistanceMeasurer('distance-map-blasenhus', '#distance-km-blasenhus')
    bhusMap.addPoint(address, "59.851237,17.629079")
    var spMap = new DistanceMeasurer('distance-map-science-park', '#distance-km-sp')
    spMap.addPoint(address, "Uppsala Science Park")
    
    $('#distance-maps').slideDown()
    return false
  })
  
  $('input[type=text]').addClass('text');
  
  // Tooltip
  $('.tooltip').tipsy({'gravity': 's', 'html': true});
  
  // Schedule
  var filterForm = $('form#schedule-filter');
  
  if (filterForm)
  {
      $('select', filterForm)
          .change(function(){
             filterForm.submit();
          });
          //.attr('multiple', 'multiple');
    
      $('button', filterForm).hide(0);
  }
  
  $('h2.schedule img').hover(
      function(){
          $(this).fadeTo(100, 0.5);
      },
      function(){
          $(this).fadeTo(100, 1.0);
      }
  );
    
})
