var Edimatica = {
  initialize: function() {
    var me = this;
    
    var windowHash = window.location.hash.substr(1);
    
    if (windowHash != '') {
      this.openPage(windowHash);
    }
    
    $$('h1 a', '#top-menu li a').each(function(e) {
      e.observe('click', function(event) {
        if (e.hasClassName('is-regular-link')) {
          return false;
        }
        
        if (e.hasClassName('is-item-box-item')) {
          event.stop();
          var id = e.href.replace(/.*\.it(.+)/, '!$1');
          window.location.hash = id;
          me.openPage(id);
        } else {
          me.openPage(e);
        }
      });
    });
    
    if ($('columns')) {
      var firstCol = $$('div#columns > div > div');
      var maxHeight = $$('div#columns > div').max(function(e) {
        return e.getHeight();
      });
      
      $$('div#columns > div').each(function(e) {
        e.setStyle({height: maxHeight + 'px'});
      });
      firstCol[1].setStyle({height: (maxHeight - firstCol[0].getHeight() - 10) + 'px'});
    } else if ($('category_list')) {
      var maxHeight = $$('div#category_list > div').max(function(e) {
        return e.getHeight();
      });
      
      $$('div#category_list > div').each(function(e) {
        e.setStyle({height: (maxHeight - 10) + 'px'});
      });
    }
    
    $('page-cerca').stopObserving('click').observe('click', function(event) {
      if (!$('page-cerca-form')) {
        $('breadcumb').insert({bottom: '<div id="page-cerca-form" style="display: none;"></div><div style="clear: right;"></div>'});
      }
      if (!$('page-cerca-form').visible()) {
        new Ajax.Updater('page-cerca-form', Edimatica.SITE_ROOT + 'read.php', {postBody: 'ajax=true&id=page-cerca', onComplete: function() {
          new Effect.BlindDown('page-cerca-form', {duration: 0.4});
        }});
      } else {
        new Effect.BlindUp('page-cerca-form', {duration: 0.4});
      }
      
      event.stop();
    });
  },
  
  openPage: function(e, o) {
    var options = o || {};
    
    BodyWrap.set();
    
	if ($(e)) {
	  e = $(e);
      if (e.up('h1')) {
	    var id = e.up('h1').id;
	  } else {
	    var id = e.id;
	  }
	  
      var use_iframe = e.hasClassName('use-iframe') ? true : false;
	} else {
      var id = e;
	}
	  
    if (use_iframe === true) {
      $('__bodywrapdiv__').update(
        '<div id="close-button"></div>' +
        '<div id="site-page-content"><iframe src="' + this.SITE_ROOT + 'read.php?id=' + id + '" style="width: 100%; height: 100%;"></iframe></div>'
      );
      
      return false;
    }
    
    if (Object.isString(e) && e.startsWith('!')) {
      var id = e.substr(1);
    }
    
    new Ajax.Request(this.SITE_ROOT + 'read.php', {postBody: 'ajax=true&id=' + id, onSuccess: function(t) {
      $('__bodywrapdiv__').update(
        '<div id="close-button"></div>' +
        '<div id="__bodywrapdiv__content__"><div id="site-page-content">' + t.responseText + '</div></div>'
      );
      
      (function() {
        if (options.resizeBox === true) {
          $('__bodywrapdiv__').setStyle({height: 'auto', marginTop: '50px'});
        }
        if (options.centerTitle === true) {
          $$('#site-page-content > #item-details > h1')[0].setStyle({textAlign: 'center'});
        }

        $('close-button').stopObserving().observe('click', function(event) {
          window.location.hash = '';
          
          BodyWrap.unset();
          event.stop();
        });
      }).defer();
    }, evalScripts: true});
  }
};

var BodyWrap = {
  set: function() {
    if ($$('.bodywrap').size() > 0) {
      $('__bodywrapdiv__').remove();
      
      var bodywrapAppearDuration = 0.0;
      var bodywrapExists = true;
    } else {
      var bodywrapAppearDuration = 0.4;
      var bodywrapExists = false;
    }
    
    var options = Object.extend({
      opacity: 6,
      css: ''
    }, arguments[0] || {});
    
    var mainBox = $$('div.site-main-box-content').first();
    var dim = mainBox.getDimensions();
    var global_dim = document.viewport.getDimensions();
    var height_from_top = $('header').getDimensions().height + $('breadcumb').getDimensions().height;
    var height_global = global_dim.height - height_from_top - $('footer').getDimensions().height;
    
    if (!bodywrapExists) {
      mainBox.insert({before: '<div class="bodywrap"></div>'});
    }
    mainBox.insert({after: '<div id="__bodywrapdiv__"></div>'});
    mainBox.setStyle({overflow: 'hidden', height: (height_global - 5) + 'px'});
    
    var bodywrap = $$('.bodywrap').first();
    var bodywrapdiv = $('__bodywrapdiv__');
    
    if (!bodywrapExists) {
      bodywrap.setStyle({
        opacity: 0,
        width: ((dim.width) + 1) + 'px',
        height: (height_global - 5) + 'px',
        position: 'fixed'
      }).appear({to: options.opacity/10, duration: bodywrapAppearDuration});
    }
    bodywrapdiv.update('<div id="loading"></div>').setStyle({
      opacity: 0,
      top: (height_from_top) + 2 + 'px',
      height: (height_global - 30) + 'px'
    }).appear({duration: 0.4});
  },
  
  unset: function() {
    if ($$('.bodywrap').size() <= 0 || !$('__bodywrapdiv__')) {
      return false;
    }
    
    var mainBox = $$('div.site-main-box-content').first();
  
    mainBox.setStyle({overflow: 'auto', height: '100%'});
    
    $$('.bodywrap', '#__bodywrapdiv__').invoke('fade', {duration: 0.4});
    (function() {
      $$('.bodywrap', '#__bodywrapdiv__').invoke('remove');
    }).delay(0.5);
  }
};

