Event.observe(window, 'load', 
	function() 
	{
 		if ($('news_sp'))
 		{
			var news_scrollArea	= new ScrollArea('news_sp','news_sp_clip', 150, 5);
 		}
 		
 		if ($('brend_sp'))
 		{
			var ref_scrollArea	= new ScrollArea('brend_sp','brend_sp_clip', 150, 5);
 		}
 		
 		$A($$('a')).each(function(element) {element.observe('focus', blur)} );
 		refreshLayout();
 		initTicker();
	}
);

function blur(event)
{
	Event.element(event).blur();
}

function showSidePanel(id)
{
	$(id + '_link').addClassName('selected');
	$(id + '_sp').style.visibility = "visible";
	$(id + '_sp').show();
	
	var	id_combo  = id == 'brend' ? 'news' : 'brend';
	 
	$(id_combo + '_link').removeClassName('selected');
	$(id_combo + '_sp').hide();
}


function refreshLayout()
{
	var min_height = 327;
	if ($('helper_sp') && ($('article') || $('order'))  && $('banner_container_article'))
	{
		var column_1 = $('article') ? $('article') : $('order');
		var column_2 = $('helper_sp');
		var banner   = $('banner_container_article');
		
		column_2.style.height   = 'auto';
		column_2.style.owerflow = 'auto';
		
		var c1_padding = parseInt(column_1.getStyle('padding-top').gsub('px', '')) + parseInt(column_1.getStyle('padding-bottom').gsub('px', ''));
		var c2_padding = parseInt(column_2.getStyle('padding-top').gsub('px', '')) + parseInt(column_2.getStyle('padding-bottom').gsub('px', ''));
				
		var max_height = Math.max(column_1.getHeight(), column_2.getHeight() - banner.getHeight() , min_height) ;
		
		if(column_1.getHeight() < max_height)
		{
			column_1.style.height = max_height - c1_padding  + 'px';
		}
		column_2.style.height = max_height + banner.getHeight() - c2_padding + 'px';
	}	
}


function ScrollArea(element,element_content, sensitive_area, maxSpeed)
{	
	var pos       = Position.cumulativeOffset($(element));
	this.x        = pos[0];
	this.y        = pos[1];
	this.width    = $(element).getWidth();
	this.height   = $(element).getHeight();
	this.maxX     = this.x + this.width;
	this.maxY     = this.y + this.height;
	this.content  = $(element_content);
	this.speed    = 0;
	this.active   = false;
	this.maxSpeed = maxSpeed;
	this.sensitive_area = sensitive_area;
		
	Event.observe(document,'mousemove', this.activateScroller.bindAsEventListener(this));
} 

ScrollArea.prototype.activateScroller = function (event)
{
	this.mouseX = Event.pointerX(event);
	this.mouseY = Event.pointerY(event);
	
	if(this.isMouseOver())
	{
		if(!this.active)
		{
			this.active = true;
			this.pe = new PeriodicalExecuter(this.scroll.bindAsEventListener(this), 0.02);
		}
	}
	else
	{
		this.active = false;
		if(this.pe)
		{
			this.pe.stop();
		}
	}
}

ScrollArea.prototype.isMouseOver = function ()
{
	var y_dist = Math.min(this.mouseY - this.y, this.maxY - this.mouseY);
	if (this.x <= this.mouseX && this.maxX >= this.mouseX && y_dist <= this.sensitive_area && y_dist >= 0)
	{
		return true;
	}
	return false;
}

ScrollArea.prototype.calculateSpeed = function ()
{
	if (this.isMouseOver())
	{
		this.speed = ((this.mouseY - this.y) /	this.height);
	}
	this.speed = (this.speed - 0.5) * 2;
	this.speed = -this.speed * this.maxSpeed;
	return this.speed;
}

ScrollArea.prototype.scroll = function()
{
	this.calculateSpeed();
	
	var new_top =  Math.round(parseInt(this.content.getStyle('top')) + this.speed);
	if (new_top > 0) new_top = 0;
	
	var maxScrollOffset = this.content.getHeight() - this.height;
	if (maxScrollOffset < 0) maxScrollOffset = 0;

	if (new_top < - maxScrollOffset)
	{
		new_top = - maxScrollOffset;
	}
	this.content.style.top = new_top + 'px';
}




var ticker_write_delay =  50;
var ticker_show_delay = 2000;
var ticker_index = 0;
var ticker_timer;
var ticker_text_pos = -1;
var ticker_text;

function typewrite_ticker()
{
	ticker_text  = ticker[ticker_index];
	ticker_text_pos = (ticker_text_pos + 1) % (ticker_text.length + 1);	
	$('ticker_text').update(ticker_text.substr(0, ticker_text_pos));

	 if(ticker_text_pos == ticker_text.length) 
	 {
	 	if(ticker_count > 1)
	 	{
	  		ticker_timer = setTimeout("typewrite_ticker();", ticker_show_delay);
			ticker_index = (ticker_index + 1) % ticker.length;
			ticker_text_pos = 0;
	 	}
 	 } 
	 else 
	 {
	 	ticker_timer = setTimeout("typewrite_ticker();", ticker_write_delay);
	 	
  	 }
}

function initTicker()
{
	if(ticker.length == null || ticker.length == 0) 
	{
		ticker[0] = "текст";
	}
	ticker_timer = setTimeout("typewrite_ticker();", ticker_write_delay);
}


var out_color;
var over_color = "#ffffe6";
function overLine(obj)
{
	out_color = obj.style.backgroundColor;
	obj.style.backgroundColor = over_color;
}

function outLine(obj)
{
	obj.style.backgroundColor = out_color;
}


function toNum( num )
{
  str = new String(num);

  if (str.indexOf( "," ) > 0)
  {
    new_str = str.split(',');
    res_str = new_str[0] + "." + new_str[1];
    return res_str;
  }
return num
}

function calcCondPower()
{
  c = document.forms['FormCond'].elements;
  			
  S = toNum( c['S'].value );
  H = toNum( c['H'].value );
  G = toNum( c['G'].options[c['G'].options.selectedIndex].value );
  T = toNum( c['T'].value );
  P = toNum( c['P'].value );
  
  c['Q'].value = Math.ceil( ((G/1000)*S*H+T*0.3+P*0.1)*10)/10;
  OutCond();
}


function OutCond()
{
  c = document.forms['FormCond'].elements;
  SText = ""
}