var img_dir='/i/images/';
var _selectorOver=false;

window.onload=function()
{
	offGalleryImg();

	if ($('skins-new-notice'))	initSkins();

	if ($('run-metro'))	$('run-metro').onclick=function(){show('metro-moscow');};
	if ($('geo'))
	{
		$('geo-cancel').onclick=$('geo-save').onclick=function() {
			hide('metro-moscow');
			hide('geo');
		}
	}

	if ($('add-place') && $('add-place').tagName.toLowerCase()=='span')
	{
		$('add-place').onclick=function()
		{
			show('geo');
			initMetroMoscow();
		}
	}


	if ($('search')) $('search').focus();

	if ($('super-pager'))	initSuperPager();

	if ($('example'))	$('example').getElementsByTagName('span')[0].click=add_example;

	if ($('selector-button'))
	{
		//preloadImage(img_dir+'selector_up.jpg');
		//preloadImage(img_dir+'selector_down.jpg');
		$('selector-button').onclick=actionSelector;
		$('selector-button').onmouseover=selectorOnOver;
		$('selector-button').onmouseout=selectorOnOverOut;
	}

	if ($('geo-container') && $('geo-img-closer'))
	{
		$('geo-closer').onclick=$('geo-img-closer').onclick=function()
		{
			hide('geo');
			hide('metro-moscow');
		}
	}

	initCA();

	if ($('prepare-new-field'))
	{
		$('prepare-new-field').getElementsByTagName('span')[0].onclick=function()
		{
			show('form-new-field');
			hide('prepare-new-field');

			$('add-new-field').onclick=function()
			{
				hide('form-new-field');
				show('prepare-new-field');
			}
		}
	}

	if ($('popular-look') && $('popular-look'))
	{
		$('popular-look').onclick=function()
		{
			$('popular-look').className='selected';
			$('popular-offer').className='';

			show('keywords-look');
			hide('keywords-offer');
		}

		$('popular-offer').onclick=function()
		{
			$('popular-look').className='';
			$('popular-offer').className='selected';

			show('keywords-offer');
			hide('keywords-look');
		}
	}

	if ($('places'))
	{
		var img=$('places').getElementsByTagName('img');
		for (var i=0; i<img.length; i++)
		{
			img[i].onclick=function()
			{
				alert('hello world!');
			}

			img[i].onmouseout=function(e)
			{
				var el=getTarget(e);
				el.className='';
			}

			img[i].onmouseover=function(e)
			{
				var el=getTarget(e);
				el.className='selected';
			}
		}
	}

	if ($('look-radio1'))
	{
		if ($('look-radio').checked)
		{
			$('look-radio1').className='selected';
			$('submit-create').className="look";
		}
		else
		{
			$('offer-radio1').className='selected';
			$('submit-create').className="offer";
		}

		$('look-radio').onclick=function()
		{
			$('look-radio1').className='selected';
			$('offer-radio1').className='';
			$('submit-create').className="look";
		}

		$('offer-radio').onclick=function()
		{
			$('look-radio1').className='';
			$('offer-radio1').className='selected';
			$('submit-create').className="offer";
		}
	}

	if ($('at-page'))
	{
		$('at-page-select').onclick=function()
		{
			$('at-page-input').value=$('at-page-select').innerHTML;
			$('at-page').className='show';

			hide('at-page-select');
			show('at-page-form');
			$('at-page-input').focus();
		}

		$('at-page-action').onclick=function()
		{
			var def_num=10;
			var max_num=200;

			$('at-page').className='';

			var num=parseInt($('at-page-input').value);

			if (isNaN(num))	num=def_num;
			if (num<1 || num>max_num)	num=def_num;

			$('at-page-select').innerHTML=num;

			hide('at-page-form');
			show('at-page-select');
		}


		function go_ctrl(e)
		{
			var ev=getEvent(e);

			var href='';

			if (ev.ctrlKey)
			{
				var a;
				var CURSOR_LEFT=37;
				var CURSOR_RIGHT=39;

				if (ev.keyCode==CURSOR_LEFT)
				{
					a=$('prev-page').getElementsByTagName('a');
				}
				else if (ev.keyCode==CURSOR_RIGHT)
				{
					a=$('next-page').getElementsByTagName('a');
				}

				if (a && a.length)	href=a[0].href;

				if (href)
				{
					window.location=href;
					alert(href);
				}
			}
		}

		if ($('ctrl'))
		{
			document.onkeydown=go_ctrl;
		}
	}

	if ($('mod-words'))
	{
		var mw=$('mod-words');
		var li=mw.getElementsByTagName('li');
		var a=mw.getElementsByTagName('a');
		var img=mw.getElementsByTagName('img');
		for (var i=0; i<li.length; i++)
		{
			li[i].onmouseover=a[i].onmouseover=img[i].onmouseover=function(e)
			{
				var ob=getTarget(e);
				var a;
				var img;
				var img_cl='show';

				switch(ob.tagName.toLowerCase())
				{
					case 'li':
						a=ob.getElementsByTagName('a')[0];
						img=ob.getElementsByTagName('img')[0];
					break;
					case 'a':
						a=ob;
						img=ob.parentNode.getElementsByTagName('img')[0];
					break;
					case 'img':
						img=ob;
						a=ob.parentNode.getElementsByTagName('a')[0];
						img_cl='selected';
						if (getEvent(e).stopPropagation)
						{
							getEvent(e).stopPropagation();
						}
						//getEvent(e).preventDefault();
						//getEvent(e).preventDefault();
					break;
				}

				a.className='';
				img.className=img_cl;
			}

			img[i].onclick=function(e)
			{
				alert('Вы выбрали - '+getTarget(e).parentNode.getElementsByTagName('a')[0].innerHTML);
			}

			li[i].onmouseout=a[i].onmouseout=img[i].onmouseout=function(e)
			{
				var ob=getTarget(e);
				var a;
				var img;

				switch(ob.tagName.toLowerCase())
				{
					case 'li':
						a=ob.getElementsByTagName('a')[0];
						img=ob.getElementsByTagName('img')[0];
					break;
					case 'a':
						a=ob;
						img=ob.parentNode.getElementsByTagName('img')[0];
					break;
					case 'img':
						img=ob;
						a=ob.parentNode.getElementsByTagName('a')[0];
					break;
				}

				a.className='black';
				img.className='';
			}
		}
	}

	if ($('cab-common-table') && $('global-switcher'))
	{
		$('global-switcher').onclick=function(e)
		{
			var gs=$('global-switcher').checked;

			var inputs=$('cab-common-table').getElementsByTagName('input');
			for (var i=0; i<inputs.length; i++)
			{
				if (inputs[i].type=='checkbox')
				{
					inputs[i].checked=gs;
					if (inputs[i].parentNode.tagName.toLowerCase()=='td')
					{
						var cl='';
						if (gs)	cl='selected';
						inputs[i].parentNode.parentNode.className=cl;
					}
				}
			}
		}


		var tds=$('cab-common-table').getElementsByTagName('td');
		for (var i=0; i<tds.length; i++)
		{
			tds[i].onclick=function(e)
			{
				var cl='';
				var gs=false;

				var target=getTarget(e).tagName.toLowerCase();
				if (target!='td')	return;

				var el=getTarget(e).parentNode.getElementsByTagName('input')[0];

				if (el.checked)	el.checked=false;
				else	el.checked=true;

				if (el.checked)
				{
					cl='selected';
					gs=true;

					var inputs=$('cab-common-table').getElementsByTagName('input');
					for (var i=1; i<inputs.length; i++)
					{
						if (!inputs[i].checked)
						{
							gs=false;
							break;
						}
					}
				}

				$('global-switcher').checked=gs;
				var temp=el.parentNode.parentNode.className;
				if (temp=='selected hide' || temp=='hide')	cl+=' hide';

				el.parentNode.parentNode.className=cl;
			}
		}


		var inputs=$('cab-common-table').getElementsByTagName('input');
		for (var i=1; i<inputs.length; i++)
		{
			if (inputs[i].type=='checkbox')
			{
				inputs[i].onclick=function(e)
				{
					var event=getEvent(e);
					event.cancelBubble=true;

					var cl='';
					var gs=false;

					var el=getTarget(e);

					if (el.checked)
					{
						cl='selected';
						gs=true;

						var inputs=$('cab-common-table').getElementsByTagName('input');
						for (var i=1; i<inputs.length; i++)
						{
							if (!inputs[i].checked)
							{
								gs=false;
								break;
							}
						}
					}

					$('global-switcher').checked=gs;
					var temp=el.parentNode.parentNode.className;
					if (temp=='selected hide' || temp=='hide')	cl+=' hide';

					el.parentNode.parentNode.className=cl;
				}
			}
		}
	}

	if ($('in-switcher') && $('in-text-switcher'))
	{
		$('in-switcher').onclick=$('in-text-switcher').onclick=show_reg;
	}

	if ($('pm-switcher') && $('pm-text-switcher'))
	{
		$('pm-switcher').onclick=$('pm-text-switcher').onclick=show_pm;
	}

	function show_reg()
	{
		show('in-open');
		hide('in-close');
		$('login').focus();
		$('in-switcher').onclick=$('in-text-switcher').onclick=hide_reg;
		$('in-switcher').src=img_dir+'in_close.gif';
	}

	function hide_reg()
	{
		show('in-close');
		hide('in-open');
		$('in-switcher').onclick=$('in-text-switcher').onclick=show_reg;

		$('in-switcher').src=img_dir+'in_open.gif';
	}

	function show_pm()
	{
		show('pm-messages');
		$('pm-switcher').onclick=$('pm-text-switcher').onclick=hide_pm;

		$('pm-switcher').src=img_dir+'pm_close.gif';
		$('personal-panel').className='';
	}

	function hide_pm()
	{
		hide('pm-messages');
		$('pm-switcher').onclick=$('pm-text-switcher').onclick=show_pm;

		$('pm-switcher').src=img_dir+'pm_open.gif';
		$('personal-panel').className='closed';
	}
}

_loadedNewNotice=false;
var _ajax_loaded = false;
function actionSelector()
{
	_loadedNewNotice = false;
	_ajax_loaded = false;

	$('selector-updown').className='down';

	$('notice-look-for-fly2').innerHTML=$('notice-look-for-fly1').innerHTML;
	$('notice-offer-for-fly2').innerHTML=$('notice-offer-for-fly1').innerHTML;

	$('selector-button').onclick=null;

	animNewNotice(
		function(){
			checkNewNotice();

			_ajax_loaded = true;

			setTimeout('_loadedNewNotice=true;', 2000);
		}
	);
}

function selectorOnOver()
{
	_selectorOver=true;
	if ($('selector-updown').className!='down')	$('selector-updown').className='up';
}

function selectorOnOverOut()
{
	_selectorOver=false;
	if ($('selector-updown').className!='down')	$('selector-updown').className='';
}

function checkNewNotice()
{
	if (_loadedNewNotice)
	{
		$('selector-button').onclick=actionSelector;
		if (_selectorOver) {
			$('selector-updown').className='up';
		} else {
			$('selector-updown').className='';
		}
		_ajax_loaded = false;
	}
	else
	{
		!_ajax_loaded && ajax_load();
		animNewNotice(function(){checkNewNotice();});
	}

}

function animNewNotice(onEnd)
{
	var a=2;
	var h=200;

	var c=new Changer('notice-look-for-fly1', 'top', 0, h);
	c.a=a;
	c.start();

	var c2=new Changer('notice-look-for-fly2', 'top', -h, 0);
	c2.a=a;
	c2.start();

	var c3=new Changer('notice-offer-for-fly1', 'top', 0, -h);
	c3.a=a;
	c3.start();

	var c4=new Changer('notice-offer-for-fly2', 'top', h, 0);
	c4.a=a;
	c4.onEnd=onEnd;
	c4.start();
}

function initCA()
{
	if ($('ac-ball'))
	{
		$('ac-1').onclick=$('ac-2').onclick=$('ac-3').onclick=$('ac-4').onclick=$('ac-5').onclick=setAC;
	}
}

function setAC(ev)
{
	var e=getTarget(ev);
	var n=parseInt(e.id.replace(/ac\-/, ''))+1;

	$('ac-1').src=img_dir+'notice/star1.png';
	$('ac-2').src=img_dir+'notice/star2.png';
	$('ac-3').src=img_dir+'notice/star3.png';
	$('ac-4').src=img_dir+'notice/star4.png';
	$('ac-5').src=img_dir+'notice/star5.png';

	for (var i=n; i<6; i++)
	{
		$('ac-'+i).src=img_dir+'notice/star0.png';
	}

	$('ball').value=n;
	$('ac-ball').innerHTML=n-1;
}

function add_example()
{
	$('search').value=$('example').getElementsByTagName('span')[0].innerHTML;
}

function change_rss()
{
	$('urss').getElementsByTagName('a')[0].href='/rss/?words='+$('search').value;
}

function initSkins()
{

}

function updateSkin()
{

}

