var anis = {
  'menu' : {
    'left' : -519,
    'ele' : 'menu',
    'visible' : false
  },
  'content' : {
    'left' : -139,
    'ele' : 'content',
    'visible' : false
  },
  'right' : {
    'left' : 0,
    'ele' : 'right',
    'visible' : false
  }
};

var _animating = false;
var _done = null;
var _firstrun = true; //IE hack?
var _html = false;
var USE_ANIMATION = true;

var _effectDuration = .8;

var initialize = function() {
  addB();
  /*$$('.content').each(function(c){
    c.style.left = '-919px';
  })*/
  showAll(function(){
    addB();
  });
}

var addB = function() {
  $$('a').each(function(ele){
    ele.onclick = function() {
      var ret = true;
      switch(this.className) {
        case 'linkTop':
          var str = this.innerHTML.toLowerCase();
          var wowLink = $('topLeftMenu').select('a')[0];
          if (str) {
            if (wow && str != 'wow') {
              if (wow[str]) {
                if (!wowLink.hasAttribute('orig')) {
                  wowLink.setAttribute('orig',wowLink.href);
                }
                wowLink.href = wow[str];
              } else {
                if (wowLink.hasAttribute('orig')) {
                  wowLink.href = wowLink.getAttribute('orig');
                  wowLink.setAttribute('orig','');
                  wowLink.removeAttribute('orig');
                }
              }
            }
          }
        case 'linkEmne':
          if (this.orig) {
            window.open(this.href);
            ret = false;
            break;
          }
          loadAjax(this.href,this.className,this.target);
          $$('a.'+this.className).each(function(a){
            if (a.up('li')) {
              a.up('li').removeClassName('sel');
            }
          });
          if (this.up('li')) {
            this.up('li').addClassName('sel');
          }
          ret = false;
          break;
        case 'linkPicture':
          showPicture(this.href);
          ret = false;
          break;
      }
      return ret;
    }.bind(ele)
  });
  if ($('menu').down('.contentHolder').childElements().length == 0) {
    //$('content').down('.contentHolder').style.width = '549px';
    $('content').down('.contentHolder').style.width = '749px';
  } else {
    //$('content').down('.contentHolder').style.width = '355px';
    $('content').down('.contentHolder').style.width = '555px';
  }
  return;
}

var addB2 = function() {
  Event.addBehavior({
    'a:click' : function(e) {
      var a = this;
    },
    'a:click' : function() {
      var ret = true;
      switch(this.className) {
        case 'linkTop':
          var str = this.innerHTML.toLowerCase();
          var wowLink = $('topLeftMenu').select('a')[0];
          if (str) {
            if (wow && str != 'wow') {
              if (wow[str]) {
                if (!wowLink.hasAttribute('orig')) {
                  wowLink.setAttribute('orig',wowLink.href);
                }
                wowLink.href = wow[str];
              } else {
                if (wowLink.hasAttribute('orig')) {
                  wowLink.href = wowLink.getAttribute('orig');
                  wowLink.setAttribute('orig','');
                  wowLink.removeAttribute('orig');
                }
              }
            }
          }
        case 'linkEmne':
          if (this.hasAttribute('orig') && this.getAttribute('orig')) {
            window.open(this.href);
            ret = false;
            break;
          }
          loadAjax(this.href,this.className,this.target);
          $$('a.'+this.className).each(function(a){
            if (a.up('li')) {
              a.up('li').removeClassName('sel');
            }
          });
          if (this.up('li')) {
            this.up('li').addClassName('sel');
          }
          ret = false;
          break;
        case 'linkPicture':
          showPicture(this.href);
          ret = false;
          break;
      }
      return ret;
    }
  });
  if ($('menu').down('.contentHolder').childElements().length == 0) {
    //$('content').down('.contentHolder').style.width = '549px';
    $('content').down('.contentHolder').style.width = '749px';
  } else {
    //$('content').down('.contentHolder').style.width = '355px';
    $('content').down('.contentHolder').style.width = '555px';
  }
}

var showPicture = function(imgsrc) {
  var img = new Image();
  img.src = imgsrc;
  var tmp = new Element('div',{className:'contentHolder'});
  tmp.update(img);
  var prev = $('content').down('.contentHolder').cloneNode(true);
  img.style.cursor = 'pointer';
  img.onclick = function() {
    loadPicture(_html);
    _html = false;
  }
  if (_html == false) {
    _html = prev;
  }
  loadPicture(tmp);
}

var loadPicture = function(tmp) {
  hideAni(anis['content'],function(obj){
    $(obj.ele).update(tmp);
    showAni(anis['content'],function(){
      addB();
    });
  });
}

var putContent = function(json) {
  if (json['menu']) {
    var test = {};
    test['content'] = json['content'];
    test['right'] = json['right'];
    test['menu'] = false;
    json['content'] = false;
    hideAni(anis['menu'],function(obj){
      $(obj.ele).down('.contentHolder').update(json['menu']);
      addB();
      showAni(anis['menu'],function(){
        putContent(test);
      });
    });
  }
  if (json['content']) {
    hideAni(anis['right'],function(obj){
      $(obj.ele).down('.contentHolder').update('');
      hideAni(anis['content'],function(obj){
        $(obj.ele).down('.contentHolder').update(json['content']);
        addB();
        if ($('menu').down('.contentHolder').childElements().length == 0) {
	  $('menu').down('.contentHolder').scrollTop=0;
          $(obj.ele).down('.contentHolder').style.width = '749px';
        } else {
//          $(obj.ele).down('.contentHolder').style.width = '355px';
          $(obj.ele).down('.contentHolder').style.width = '555px';
	  $(obj.ele).down('.contentHolder').scrollTop=0;
        }
        showAni(anis['content'],function(obj){
          if (json['right']) {
            $(anis['right'].ele).down('.contentHolder').update(json['right']);
            addB();
            showAni(anis['right']);
          }
        })
      })
    });
  }
}

var loadAjax = function(href,get,target) {
  var ax = new Ajax.Request(href, {
    method: 'post',
    parameters: '&ax=true&get='+get,
    onSuccess: function(xml,json) {
      var html = xml.responseText;
//      var json = html.evalJSON();
      json['content'] = html;
      if (get == 'linkTop') {
        hideAll(function(){
          putContent(json);
        });
      } else {
        putContent(json);
      }
    }
  });
}

var showAll = function() {
  var cB = arguments[0] || function(){};
  showAni(anis['menu'],function(){
    showAni(anis['content'],function(){
      showAni(anis['right'],function(){
        cB();
      });
    })
  })
}
var hideAll = function() {
  var cB = arguments[0] || function() {};	
  hideAni(anis['right'],function(obj){
    $(obj.ele).down('.contentHolder').update('');
    hideAni(anis['content'],function(obj){
      $(obj.ele).down('.contentHolder').update('');
      hideAni(anis['menu'],function(obj){
        $(obj.ele).down('.contentHolder').update('');
        cB();
      });
    })
  })
}

var hideAni = function() {
  var obj = arguments[0];
  var cB = arguments[1] || function(){};
  /*if (_animating == true) {
    var when = _done || 10;
    setTimeout(function(){hideAni(obj)},when);
    return;
  }*/
  if (obj['visible'] == false) {
    _animating = false;
    cB(obj);
    return;
  }
  _animating = true;
  if (ele = $(obj['ele'])) {
    obj['visible'] = false;
    var l = -919;
    if (USE_ANIMATION == false) {
      _animating = false;
      ele.style.left = 1+'px';
      cB(obj);
      return;
    }
    var fx = new Effect.Morph(ele,{
      style : {
        left : l+'px'
      },
      duration: _effectDuration,
      afterFinish : function() {
        _animating = false;
        obj['visible'] = false;
        cB(obj);
      }
    });
    _done = fx.startOn - fx.finishOn;
  }
}

var showAni = function() {
  var obj = arguments[0];
  var cB = arguments[1] || function(){};
  if (!cB) cB = function() {};
  /*if (_animating == true) {
    var when = _done || 10;
    setTimeout(function(){showAni(obj)},when);
    return;
  }*/
  var ele = $(obj['ele']);
  if ((obj['visible'] == true || !obj['visible']) && _firstrun == false) {
    _firstrun = false;
    _animating = false;
    cB(obj);
    return;
  }
  if (ele.down('.contentHolder').childElements().length == 0) {
    _animating = false;
    cB(obj);
    return;
  }
  if (ele) {
    var l = obj['left'];
    if (USE_ANIMATION == false) {
      _animating = false;
      ele.style.left = 1+'px';
      cB(obj);
      return;
    }
    _animating = true;
    var fx = new Effect.Morph(ele,{
      style : {
        left : l+'px'		
      },
      duration: _effectDuration,
      afterFinish : function() {
        _animating = false;
        obj['visible'] = true;
        cB(obj);
      }
    });
    _done = fx.startOn - fx.finishOn;
  } else {
    _animating = false;
    cB(obj);
    return false;
  }
}