// 
//  application.js
//  juanlopez
//  
//  Created by Jorge on 2008-07-10.
//  Copyright 2008 Infonova Informática e Comunicacions, S,L.. All rights reserved.
// 

//
// i18n
//

var i18n = {
  locale: null,
  initialize: function(){
    if(meta = $$('meta').find(function(e) { return e.name == "language";})){
      i18n.locale = meta.content;
    }
    if(i18n.locale){      
      document.write('<script src="/javascripts/i18n/'+i18n.locale+'.js" type="text/javascript"></script>');
    }
  },
  _: function(s){
    if (typeof(_i18n)!='undefined' && _i18n[s]) {
      return _i18n[s];
    }
    return s;    
  }
}
i18n.initialize();

//
// Presentación de imáges
//


var PresentationShow = Class.create();
 PresentationShow.prototype = {
 initialize: function(element, options) {
   this.element = $(element);
   this.options = Object.extend({className: 'presentation', duration: 4}, options);
   this.presentations = $$("#"+element+" img."+this.options.className);

   this.preparePresentation();
   this.registerCallback();
 },

 preparePresentation: function() {
   this.currentPresentation = this.presentations.first();
   this.element.style.position = 'relative';

  this.element.style.height = this.presentations.max(function(presentation) {
    var visible = Element.visible(presentation), height;

    if (!visible) {
      Element.setStyle(presentation, {position: 'absolute', right: '-800px'});
      Element.show(presentation);
    }

    height = Element.getHeight(presentation);
    if (!visible) Element.hide(presentation);

    Element.setStyle(presentation, {position: 'absolute', right: '0pt', top: '0pt'});
    return height;
  }).toString() + 'px';
 },

 nextPresentation: function() {
   return this.presentations[(this.presentations.indexOf(this.currentPresentation) + 1) % this.presentations.length];
 },

 registerCallback: function() {
   window.setTimeout(this.tick.bind(this), this.options.duration * 1000);
 },

 tick: function() {
   var currentPresentation = this.currentPresentation, nextPresentation = this.nextPresentation();

   new Effect.Parallel([
     new Effect.Fade(currentPresentation, {sync: true}),
     new Effect.Appear(nextPresentation, {sync: true})
   ], {
     duration: 2,
     afterFinish: (function(effect) {
       this.currentPresentation = nextPresentation;
       this.registerCallback();
     }).bind(this)
   })
 }
}


//
// Google maps
//

var Map = {
  map: null,
  geocoder: null,
  initialize: function(id){
    if (GBrowserIsCompatible()) {
      this.map = new GMap2($(id));
      this.map.addControl(new GSmallMapControl());
      this.map.addControl(new GMapTypeControl());

      this.map.setCenter(new GLatLng(42.78314959323138, -8.88645887374878), 10);
      this.geocoder = new GClientGeocoder();
      Map.address($F('locality'));
    }
  },
  address: function(address){
    var url = '/mapa/properties?locality_id='+address.match(/\d*$/)[0];
    address = address.gsub(/\-\d*$/, "");

    new Ajax.Request(url, {
      method: 'post',
      onSuccess: function(transport) {
        Map.markers(eval(transport.responseText));
      }
    });

    if (this.geocoder) {
      this.geocoder.getLatLng( address, function(point){
        if (!point) {
          alert(address + " not found");
        } else {
          Map.map.setCenter(point, 14);
        }
      });
    }        
  },
  markers: function(properties){
    var marker = null;
    properties.each(function(item){
      Map.map.addOverlay(Map.createMarker(item));
    });
  },
  createMarker: function(item){
    var marker = new GMarker(new GLatLng(item.property.geopoint.latitude, item.property.geopoint.longitude), {icon:Map.customIcon()});
    
    GEvent.addListener(marker, "click", (function() {
      var html = "";
      if(item.property.pictures.length > 0){
        html = '<img  src="/pictures/'+item.property.pictures.first().id+'/thumb/'+item.property.pictures.first().picture_file_name+'" alt="" class="marker-picture"/>';
      }
      html += '<b>'+i18n._('Tipo')+':</b> '+item.property.kind.name+'<br/>';
      html += '<b>'+i18n._('Condición')+':</b> '+item.property.condition.name+'<br/>';
      html += '<a href="/inmuebles/detalle/'+item.property.id+'">'+i18n._('Más información')+'</a><div class="clear">&nbsp;</div>';

      marker.openInfoWindowHtml(html);
    }));
    return marker;
  },
  customIcon: function(){
    var icon = new GIcon();
    icon.image = "/images/house.png";
    icon.iconSize = new GSize(32, 32);
    icon.iconAnchor = new GPoint(32/2, 32/2);
    icon.infoWindowAnchor = new GPoint(12, 15);
    icon.infoShadowAnchor = new GPoint(18, 25);
    return icon;
  }
}

//
// Navegación Buscador
//

var SearchTab = {
  toggle: function(id){
    $$("#search-tab li").invoke('removeClassName', 'current');
    $(id).addClassName('current');
    if(id.match(/^map/)){
      $("map-search").show();
      $('property-search-wrapper').hide();
    }else{
      $('property-search-wrapper').show();
      $("map-search").hide();
    }
  },
  submit: function(form){
    if(window.location.pathname != "/mapa"){
      form.submit();
    }else{
      Map.address($F('locality'));
    }
  }
}