var isColumnActive = false;

window.onload = function()
{

  if($('Content').className.indexOf('RightColumnOff') > -1)
  {
    isColumnActive = true;
  }
  CheckSize();

  var envMenu = new SubMenu($('EnvironmentalMenu').parentNode, $('EnvironmentalMenu'));
  var arbMenu = new SubMenu($('ArboricultureMenu').parentNode, $('ArboricultureMenu'));
  var insMenu = new SubMenu($('ServicesForInsurersMenu').parentNode, $('ServicesForInsurersMenu'));
  var newsScroller = new VerticalScroller($('NewsContainer')); 
  var messageScroller = new HorizontalScroller($('MessageContainer')); 
  
  document.getElementById('JScriptFormElement').value = 'True';

}

window.onresize = function()
{

  CheckSize();

}

function CheckSize()
{
  if(isColumnActive)
  {
		var content = $('Content');
		var rightColumn = $('RightColumn');
		var width = document.body.clientWidth;
		var isOff = false;

		if(content.className.indexOf('RightColumnOff') > -1)
		{
			isOff = true;
		}

		if(width > 1024 && isOff)
		{
			content.className = '';
			rightColumn.className = '';
		}
		else if(width <= 1024 && !isOff)
		{
			content.className = 'RightColumnOff';
			rightColumn.className = 'RightColumnOff';
		}
  }
}

var VerticalScroller = Class.create();
VerticalScroller.prototype = {

  container: null,
  direction: -1,
  timer: null,
  isScrolling: true, 

  initialize: function(container)
  {
    this.container = container;
    this.eventScroll = this.scroll.bindAsEventListener(this);
    this.timer = setInterval(this.eventScroll, 80);

    this.eventOver = this.mouseOver.bindAsEventListener(this);
    Event.observe(container, "mouseover", this.eventOver);
    this.eventOut = this.mouseOut.bindAsEventListener(this);
    Event.observe(container, "mouseout", this.eventOut);

	this.height = container.getStyle('height');
	var wrapper = container.parentNode;
    this.wrapperHeight = wrapper.offsetHeight;
  },
  
  mouseOver: function()
  {
    this.isScrolling = false;
  },
  
  mouseOut: function()
  {
    this.isScrolling = true;
  },
  
  scroll: function()
  {
    if(this.isScrolling)
    {
      var current = parseInt(this.container.getStyle('top'));
      current = (current * 1) + this.direction;
      this.container.setStyle({top: current + 'px'});

      if(parseInt(current) < parseInt(this.wrapperHeight) - parseInt(this.height) || current > 0)
      {
        this.direction = this.direction * -1;
      }
    }
  }
}

var HorizontalScroller = Class.create();
HorizontalScroller.prototype = {

  container: null,
  direction: -3,
  timer: null,
  isScrolling: true, 

  initialize: function(container)
  {
    if(container == undefined) return;
    
    this.container = container;
    this.eventScroll = this.scroll.bindAsEventListener(this);
    this.timer = setInterval(this.eventScroll, 80);

    this.eventOver = this.mouseOver.bindAsEventListener(this);
    Event.observe(container, "mouseover", this.eventOver);
    this.eventOut = this.mouseOut.bindAsEventListener(this);
    Event.observe(container, "mouseout", this.eventOut);

  	this.width = container.getStyle('width');
  	var wrapper = container.parentNode;
    this.wrapperWidth = wrapper.offsetWidth;
    this.container.setStyle({left: this.wrapperWidth + 'px'});
  },
  
  mouseOver: function()
  {
    this.isScrolling = false;
  },
  
  mouseOut: function()
  {
    this.isScrolling = true;
  },
  
  scroll: function()
  {
    if(this.isScrolling)
    {
      var current = parseInt(this.container.getStyle('left'));
      current = (current * 1) + this.direction;
      this.container.setStyle({left: current + 'px'});

      if(parseInt(current) < parseInt(this.width) * -1)
      {
        this.container.setStyle({left: this.wrapperWidth + 'px'});
      }
    }
  }
}

var SubMenu = Class.create();
SubMenu.prototype = {

  button: null,
  menu: null,
  buttonActive: false,
  menuActive: false,
  content: null,
  closeTimer: null,
  IE: false,
  eventBlankOver: { top: 0, bottom: 0 },

  initialize: function(button, menu)
  {
    this.content = $('Content');
    this.button = button;
    this.menu = menu;
    
    this.eventButtonClick = this.buttonClick.bindAsEventListener(this);
    Event.observe(button, "click", this.eventButtonClick);

    this.eventMenuOver = this.menuOver.bindAsEventListener(this);
    Event.observe(menu, "mouseover", this.eventMenuOver);
    this.eventMenuOut = this.menuOut.bindAsEventListener(this);
    Event.observe(menu, "mouseout", this.eventMenuOut);

    var blanks = this.menu.getElementsByClassName('Blank');
    
    for(i = 0; i < blanks.length; i++)
    {
      blanks[i].setStyle({opacity: 0.75});
      this.eventBlankOver[i] = this.blankOver.bindAsEventListener(this);
      Event.observe(blanks[i], "mouseover", this.eventBlankOver[i]);
    }
    
  },

  blankOver: function()
  {
    this.menuActive = false;
    this.fadeDown();
  },

  buttonClick: function()
  {
    if(this.menu.style.display != 'block')
    {
      this.fadeUp();
    }
  },
  
  menuOver: function()
  {
    this.menuActive = true;  
  },

  menuOut: function()
  {
    this.menuActive = false;  
    this.close();
  },
  
  close: function()
  {
    if(this.closeTimer == null)
    {
      this.eventCloseTimer = this.tryClose.bindAsEventListener(this);
      this.closeTimer = setTimeout(this.eventCloseTimer, 100);
    }
  },
  
  tryClose: function()
  {
    if(!this.menuActive)
    {
      this.fadeDown();
    }
    clearTimeout(this.closeTimer);
    this.closeTimer = null;
  },
  
  fadeUp: function()
  {

    this.menu.setStyle({opacity: 0});
    this.menu.style.display = 'block';
    this.increment = 0.1;
    this.eventChangeOpacity = this.changeOpacity.bindAsEventListener(this);
    this.timer = setTimeout(this.eventChangeOpacity, 20);
  },

  fadeDown: function()
  {

    this.increment = -0.1;
    this.eventChangeOpacity = this.changeOpacity.bindAsEventListener(this);
    this.timer = setTimeout(this.eventChangeOpacity, 20);
  },
  
  changeOpacity: function()
  {
    var current = this.menu.getStyle('opacity');
    current = (current * 1) + this.increment;
    
    this.menu.setStyle({opacity: current});
    
    if(current >= 1 || current <= 0)
    {
      clearTimeout(this.timer);
    }
    else
    {
      this.eventChangeOpacity = this.changeOpacity.bindAsEventListener(this);
      this.timer = setTimeout(this.eventChangeOpacity, 20);
    }    
    
    if(current <= 0)
    {
      this.menu.style.display = 'none';
    }

  }
  
}