var Util = {
  getWindowScroll: function(w) {
    var T, L, W, H;
    with (w.document) {
      if (w.document.documentElement && documentElement.scrollTop) {
        T = documentElement.scrollTop;
        L = documentElement.scrollLeft;
      } else if (w.document.body) {
        T = body.scrollTop;
        L = body.scrollLeft;
      }
      if (w.innerWidth) {
        W = w.innerWidth;
        H = w.innerHeight;
      } else if (w.document.documentElement && documentElement.clientWidth) {
        W = documentElement.clientWidth;
        H = documentElement.clientHeight;
      } else {
        W = body.offsetWidth;
        H = body.offsetHeight
      }
    }
    return { top: T, left: L, width: W, height: H };
  },
  
  disableSelection: function() {
    document.body.ondrag = function () { return false; };
    document.body.onselectstart = function () { return false; };
  },
  
  enableSelection: function() {
    document.body.ondrag = null;
    document.body.onselectstart = null;  
  }
} 

var Layout = {               
  init: function() { 
    this.headerHeight = $('header').getHeight();
    this.footerHeight = $('footer').getHeight(); 
    if ($('right')) {
      this.rightWidth   = $('right').getWidth();
      // Accordion 
      if ($('people_panel'))
        this.accordion = new Accordion("right",
                                       [{elements:["people_panel", "card_content"], header: "people_header", minHeight: 200},
                                        {elements:["tags_panel", "tags_content"],   header: "tags_header"}]);
    }             
    this.resize();  
  }, 
  
  resize: function() {   
    var size = Util.getWindowScroll(window);
    if ($('one_col')) {
      var w = size.width + "px";
      var h = size.height - this.headerHeight - this.footerHeight + "px";   
      $('one_col').setStyle({height: h, width: w}); 
      if ($('right'))
        $('right').setStyle({height: h})  
    }
    else if ($('right')) { 
      var w = size.width - this.rightWidth + "px";
      var h = size.height - this.headerHeight - this.footerHeight + "px"
      var oldH = $('right').getHeight();  
      if ($('message'))
        $('message').setStyle({width: w});   
        
      $('right').setStyle({height: h})  
      if ($('map')) {       
        var hMap =  parseFloat(h) - (messageHeight)  + "px"
        $('map').setStyle({height: hMap, width: w});
      }
      else
        $('content').setStyle({height: h, width: w});
      if (this.accordion)
        this.accordion.changeHeight(parseFloat(h) - oldH);
    }  
  },
  
  hideTags: function() {
    if (this.accordion)
      this.accordion.hide(1)
  },
  
  showTags: function() {
    if (this.accordion)
      this.accordion.show(1)
  },
  
  resetAccordion: function() {
    if (this.accordion) 
      this.accordion.reset()
  }
}  

var Search = {
  focus: function() {
    var field = $('city');
    if (field.value == field.title) { 
      field.value = ""               
      field.removeClassName("tooltip")
    }
  },
  
  blur: function() {
    var field = $('city');
    if (field.value.length == 0 || field.value == field.title) {
      field.value = field.title;  
      field.addClassName("tooltip")
    }
  },
  
  submit: function() {
    var field = $('city');
    if (field.value.length == 0 || field.value == field.title) 
      return;
    $('search_field').onsubmit()
  }
  
}

var Login = {
  open: function(html) {   
    var win = this._createWindow(html)
    if (win)
      win.setAjaxContent("/login", {method: 'get'}, true)
  },
  
  fromAbout: function(html) {
    var win = this._createWindow(html)
    if (win)
      win.setAjaxContent("/login", {method: 'get'}, true)
  },
  
  fromAddEvent: function(html) {
    var win = this._createWindow(html)
    if (win)
      win.setAjaxContent("/login", {method: 'get'}, true)    
  },
   
  _createWindow: function(html) {
    var win = Windows.getWindow("login_window");   
    if (win) {
      win.close();
      return null;
    } 
    else {
      var effect = new PopupEffect(html, {className: "popup_effect1"});
      var win = new Window({id: "login_window", className:"bluelighting", width: 300, height:null, showEffect:effect.show.bind(effect), hideEffect:effect.hide.bind(effect), 
                            minimizable: false, maximizable: false, resizable:false, destroyOnClose: true})
      return win;
    }    
  }
}

var Signup = {
  verifyAddress: function(forAR, id) {                                  
    var parameters= ""
    $A(["street", "zip", "city", "country_name"]).each(function(id) {   
      if ($(forAR + "_"+id))
        parameters += forAR + "[" + id + "]=" + $(forAR + "_"+id).value + "&"
    });                            
    if (id)
      parameters += forAR + "[id]=" + id
    new Ajax.Request("/" + forAR + "s/verify_address", {parameters:parameters});
  },
  
  showAddress: function(forAR) {                                  
    if (!map || !map.isLoaded()) {
      setTimeout(Signup.showAddress.bind(Signup, forAR), 100);
      return;
    }
    var parameters= "";    
    
    $A(["lat", "lon"]).each(function(id) {
      parameters += id + "=" + $(forAR + "_"+id).value + "&";
    })                    
    
    new Ajax.Request("/" + forAR + "s/show_address", {parameters:parameters});
  }  
}       
 
LocalEvent =  {
  addEventLogin: function(html) {
    var win = Login._createWindow(html)
    if (win)
      win.setAjaxContent("/login", {method: 'get', parameters: {login_action: "add_event"}}, true)    
  },
  
  registerEventLogin: function(html, id) {
    var win = Login._createWindow(html)
    if (win)
      win.setAjaxContent("/login", {method: 'get', parameters: {login_action: "register_event", event_id: id}}, true)    
  },
  
  registerEventLoginOk: function(url) {
    new Ajax.Request(url)
  },
  
  contactLogin: function(html) {
    var win = Login._createWindow(html)
    if (win)
      win.setAjaxContent("/login", {method: 'get', parameters: {login_action: "contact"}}, true)        
  }
}

Event.observe(window, "load", Layout.init.bind(Layout));
Event.observe(window, "resize", Layout.resize.bind(Layout));

Ajax.Responders.register({
  onCreate: function() {
    Ajax.activeRequestCount++;
    if (Ajax.activeRequestCount > 0 && $('progress'))
      $('progress').show();
  },
  onComplete: function() {
    Ajax.activeRequestCount--;
    if (Ajax.activeRequestCount == 0 && $('progress'))
      $('progress').hide();
  }
});
