var is_menu=false;
var cur_menu='';
var pos;
var curr_selected='';

document.body.onmousemove=function(e){checkPos(e);}

function load_logo(id, base, fn)
	{
	if (!base) {var base='';}
	if (!fn)
		{
		var logos=new Array(9);
		//logos[0]='logo_orange.png';
		logos[0]='logo_yellow.png';
		logos[1]='logo_dblue.png';
		logos[2]='logo_cyan.png';
		logos[3]='logo_green.png';
		logos[4]='logo_lime.png';
		logos[5]='logo_blue.png';
		logos[6]='logo_red.png';
		logos[7]='logo_pink.png';
		logos[8]='logo_orange.png';
		var x=Math.floor( Math.random( ) * 9);
		if (document.getElementById(id))
			{document.getElementById(id).src=base + logos[x];}
		}
		else
	    {
		if (document.getElementById(id))
		  {document.getElementById(id).src=base + fn;}
		}
	}

function showMenu(id, owner)
	{
	if (is_menu) {hideMenu(cur_menu);}
	if (document.getElementById('wrap_menu'))
	  {document.getElementById('wrap_menu').style.visibility='visible';
	  document.getElementById('wrap_menu').style.display='block';}
	
	document.getElementById(id).style.visibility='visible';
	document.getElementById(id).style.display='block';
	
	if (owner)
		{var menu_pos=getPos(owner);
		document.getElementById(id).style.left=document.getElementById(owner.id).offsetLeft-document.getElementById('wrap_menu').offsetLeft+'px';}
	
	is_menu=true;
	cur_menu=id;
	pos=getPos(document.getElementById(cur_menu));
	}

function getPos(el)
	{
	var left=0, top=0, width=0, height=0;
	width=el.clientWidth;
	height=el.clientHeight;
	var el_id=el.id;
	if(el.offsetParent) {
		do {
			left += el.offsetLeft;
			top += el.offsetTop;
			} while(el=el.offsetParent);
		}
	return {'left':left,'top':top,'width':width,'height':height};
};

function checkPos(e)
	{
	var ev = e||window.event;
	var x=(ev.clientX)||ev.pageX;
	var y=(ev.clientY)||ev.pageY;
	var scr_top=document.documentElement.scrollTop||document.body.scrollTop;
	var scr_left=document.documentElement.scrollLeft||document.body.scrollLeft;
	x+=scr_left;
	y+=scr_top;
	var marg=15;
	if (pos)
		{if ((x<pos.left-marg) | (y<pos.top-2*marg) | (x<1) | (y<1)) {hideMenu(); return;}
		if ((x>pos.width+pos.left+marg) | (y>pos.height+pos.top+marg)) {hideMenu(); return;}
		}
	}

function hideMenu(id)
	{
	if (!is_menu) {return;}
	is_menu=false;
	if (document.getElementById(id))
	  {document.getElementById(id).style.visibility='hidden';
	  document.getElementById(id).style.display='none';}
	    else
		{if (document.getElementById(cur_menu))
		  {document.getElementById(cur_menu).style.visibility='hidden';
		  document.getElementById(cur_menu).style.display='none';}
		  }
	if (document.getElementById('wrap_menu'))
	  {document.getElementById('wrap_menu').style.visibility='visible';
	  document.getElementById('wrap_menu').style.display='block';}
	}

function addLZ(i)
	{
	if (i<10) {return "0".concat(i);}
		else {return i;}
	}

function formatCurr(val)//расставляет разделители
	{
	var thousands=new Array();
	var i=0;
	while ((/[-]?[0-9]{1,3}$/).test(val) == true)
		{
		thousands[i]=(/[-]?[0-9]{1,3}$/).exec(val);
		val = String(val).replace(/[0-9]{1,3}$/, '');
		i++;
		}
	var result='';
	for (i=thousands.length-1; i>=0; i--)
		{
		if (i>0)
			{result=result.concat(thousands[i], '&nbsp;');}
				else {result=result.concat(thousands[i]);}
		}
	return String(result);
	}

function getCurrData(data)
	{
	if ((/^[0-9]{1,4}\.[0-9]{2,8}[A-Z]+[0-3][0-9]\.[0-1][0-9]\.2[0-9]{3}$/i).test(data))//формат корректен?
		{
		var data_arr=data.match(/^([0-9]{1,4}\.[0-9]{2,8})([A-Z]+)([0-3][0-9]\.[0-1][0-9]\.2[0-9]{3})$/i);
		var val=data_arr[1];//величина курса
		var char_code=data_arr[2];//единица измерения
		var date_req=data_arr[3];//дата
		var cur=char_code;//отображение наименования валюты (по умолчанию соответстует коду ISO)
		if (char_code == 'USD') {cur='$';}
		if (char_code == 'EUR') {cur='&euro;';}//выводить коды ISO - не айс
		if (char_code == 'BYR') {cur='Br';}
		if (char_code == 'UAH') {cur='грн';}
		jQuery('.price_convert').html(function(){return String(formatCurr(Math.round(jQuery(this).attr('title').replace(/[^0-9^-]/g, '')/val))).concat('&nbsp;', cur);});//пересчитываем значения (цены) всех элементов класса price_convert
		//подстветка
		jQuery("#currency a").css({'color':'', 'cursor':'', 'border-bottom':''});
		jQuery("#currency a:contains('" + char_code + "')").css({'color':'#F00000', 'cursor':'default', 'border-bottom':'none'});
		//jQuery("#currency a:contains('" + char_code + "')").attr('title', 'Переведено по курсу ЦБ РФ на '.concat(date_req));
		}
	}

function setPrice(curr)
	{
	if (!curr)//валюта по умолчанию
		{
		jQuery('#currency').css('visibility','visible');
		jQuery('#currency a').css({'color':'', 'cursor':'', 'border-bottom':''});
		jQuery('.price_convert').html(function(){return jQuery(this).attr('title');});
		jQuery("#currency a:contains('RUR')").css({'color':'#F00000', 'cursor':'default', 'border-bottom':'none'});
		jQuery('.price_convert').bind('ajaxSend', function(){jQuery(this).html("<img src='/image/interface/loading2_old.gif'></img>");});
		jQuery('.price_convert').bind('ajaxError', function(){jQuery(this).html(jQuery(this).attr('title'));});
		img=new Image;
		img.src='/image/interface/loading2_old.gif';//кэшируем картинку
		return;
		}
	var today = new Date();
	if (curr != curr_selected)
		{
		curr_selected=curr;
		jQuery.get('/get_currency.php?numcode='.concat(curr, '&date_req=', addLZ(today.getDate()), '/', addLZ(today.getMonth()+1), '/', today.getFullYear()), getCurrData);//ЦБ РФ
		}
	}

function toggleRows(id, cls)
	{
	return;
	if (jQuery('.'.concat(cls, ' td:visible')).length > 0)
		{
		jQuery('.'.concat(cls, ' td')).slideUp(200);
		jQuery('#'.concat(id)).html('&nbsp;<b>&darr;</b>');
		}
			else {
				jQuery('.'.concat(cls, ' td')).slideDown(200);
				jQuery('#'.concat(id)).html('');
				}
	}