_$ = function(id){
	return document.getElementById(id);
}
function changHoroBlock(elm, common)
{
	var common = common||false;
	if (!_$('allZodiac') || _$('allZodiac').innerHTML == '')
	{
		return true;
	}
	var url = elm.href, reg = /^(?:http:\/\/)?[^\/]*\/(.*)$/;
	var matches = url.match(reg);
	var uri = matches[1].split('/');
	var sign = uri[0] == 'sign' && uri[2] ? uri[2] : 0, type = uri[1] ? uri[1] : 'daily';
	if (sign)
	{
		if (global_sign != sign) refreshOtherHoroAll(sign);
	}
	else if (!common && global_sign)
	{
		url = '/sign/'+type+'/'+global_sign;
		sign = global_sign;
	}
	else
	{
		refreshOtherHoroAll(0);
	}
	var div_id = 'zodiac_'+type+'_'+sign;
	var types = ['daily', 'weekly', 'monthly', 'yearly'];
	//var types = ['daily', 'weekly', 'monthly'];
	for(var i=0, n=types.length; i<n; i++)
	{
		_$(types[i]+'Tab').className = types[i] == 'yearly' ? 'next' : '';
		//_$(types[i]+'Tab').className = types[i] == 'monthly' ? 'next' : '';
	}
	_$(type+'Tab').className = type == 'yearly' ? 'current next' : 'current';
	//_$(type+'Tab').className = type == 'monthly' ? 'current next' : 'current';
	if (_$(div_id) && _$(div_id).innerHTML)
	{
		showHoroBlock(div_id);
	}
	else
	{
		showAjaxLoader();
		JsHttpRequest.query(
				url,
				{'ajax': '1', 'block_name': 'commonHoroBlock'}, 
				function(data){
					//var data = eval('('+data+')');
					if (!_$(div_id))
					{
						var element = document.createElement('div');
						element.style.display = 'none';
						element.className = 'zodiac';
						element.id = div_id;
						_$('allZodiac').appendChild(element);
					}
					_$(div_id).innerHTML = data.html;
					showHoroBlock(div_id);
					hideAjaxLoader();
				}, true
			);	
	}
	return false;
}
function showHoroBlock(div)
{
	var parent = _$('allZodiac'),
	allZodiac = parent.getElementsByTagName('div');
	for(var i=0, n=allZodiac.length; i<n; i++)
	{
		if (allZodiac[i].className == 'zodiac')
		{
			allZodiac[i].style.display = 'none';
		}
	}
	_$(div).style.display = 'block';
}
var attachElemEvent = function (elem, type, handle)
{
	if (elem.addEventListener)
		elem.addEventListener(type, function () {handle.apply(elem, arguments)}, false);
	else if (elem.attachEvent)
		elem.attachEvent("on" + type, function () {handle.apply(elem, arguments)});
}

function listNames(options)
{
	var name = options.name||'names';
	var inputText = options.elm ? (typeof options.elm == 'string' ? _$(options.elm) : options.elm) : _$(name+'InputName');
	var divList = options.divList ? 
				(typeof options.divList == 'string' ? _$(options.divList) : options.divList) : 
				_$(name+'Prompting');
	var url = options.url||'/index/';
	var block_name = options.block_name||'smallNamesBlock';
	var form = options.form||name+'Form';
	var itemId = options.itemId||_$(name+'ItemId');
	var list_names = {};
	var listLi = [], listA = [], names = [], current = -1;
	var userAgent = navigator.userAgent.toLowerCase();
	var msie =  /msie/.test( userAgent ) && !/opera/.test( userAgent );
	
	attachElemEvent(inputText, 'keyup', function (e){
			var keyCode = e.keyCode||e.which;
			if (keyCode==38||keyCode==40||keyCode==13)
			{
				return;
			}
			var name = inputText.value;
			if (list_names[name])
			{
				names = list_names[name];
				refreshNames();
				return;
			}
			JsHttpRequest.query(
					url,
					{ajax: '1', block_name: block_name, params:{name:name}}, 
					function(data){
						if (data.names)
						{
							names = list_names[name] = data.names;
							refreshNames();
							return;
						}
					}, true
				);	

		});

	if (!msie) attachElemEvent(inputText, 'keypress', function (e) {inputKeyPress(e);})
	else  attachElemEvent(inputText, 'keydown', function (e) {inputKeyPress(e);});
	
	var inputKeyPress = function(e){
		var keyCode = e.keyCode||e.which;
		if (keyCode == 40) {
			if (current != -1) listA[current].className = '';
			if (current<listA.length-1)current++;
			listA[current].className = 'current';
			return false;
		};
		if (keyCode == 38) {
			if (current > -1)
			{
				listA[current].className = '';
				if (current>-1)current--;
				if (current > -1)listA[current].className = 'current';
			}
			return false;
		};
		if (keyCode == 13) {
			if (current != -1)
			{
				listA[current].className = '';
				inputText.value = names[current][0];
				if (names[current][1]) itemId.value = names[current][1];
				current = -1;
			}
			return false;
		};
	}

	var refreshNames = function ()
	{
		current=-1;itemId.value = 0;
		if (names.length==0)
		{
			divList.style.display = 'none';
			return;
		}
		var li;
		while(listLi.length){li = listLi.pop(); listA.pop(); divList.removeChild(li)}
		for(var i in names)
		{
			var li = document.createElement('li');
			var a = document.createElement('a');
			a.href='javascript:void(0)';
			if (names[i][1]) a.rel=names[i][1];
			attachElemEvent(a, 'click', selectName);
			attachElemEvent(a, 'mouseover', function (){
				if (current != -1) listA[current].className = '';
				this.className = 'current';
				var i = 0;
				while (listA[i] && listA[i++]!=this);
				if (i<=listA.length)current = i-1;
			});
			attachElemEvent(a, 'mouseout', function (){
				if (current != -1) listA[current].className = '';
				this.className = '';
				current = -1;
			});
			var text = document.createTextNode(names[i][0]);
			a.appendChild(text);
			li.appendChild(a);
			divList.appendChild(li);
			listLi.push(li);
			listA.push(a);
		}
		divList.style.display = 'block';
	}

	var selectName = function ()
	{
		inputText.value = this.innerHTML;
		if (this.rel) itemId.value = this.rel;
		divList.style.display = 'none';
		window.location.href = _$(form).getAttribute('action')+(this.rel ? this.rel : this.innerHTML)+'/';
	}

}

function refreshOtherHoroAll(sign)
{
	//showAjaxLoader();
	JsHttpRequest.query(
			'/sign/daily/'+sign+'/',
			{ajax: '1', block_name: 'otherAllHoroBlock', params:{}}, 
			function(data){
				if (data.html)
				{
					_$('otherAllHoroBlock').innerHTML = data.html;
				}
				//hideAjaxLoader();
			}, true
		);	
	if (!global_sign && sign || global_sign && !sign)
	{
		if (sign)
		{
			//_$('moonHoroBlock').style.display = 'none';
			_$('horoTitle').innerHTML = 'Гороскоп';
		}
		else //if (_$('moonHoroBlock').innerHTML)
		{
			//_$('moonHoroBlock').style.display = 'block';
			_$('horoTitle').innerHTML = 'Общий гороскоп';
		}
		/*else
		{
			_$('horoTitle').innerHTML = 'Общий гороскоп';
			JsHttpRequest.query(
					'/common/daily/',
					{ajax: '1', block_name: 'moonHoroBlock', params:{}}, 
					function(data){
						if (data.html)
						{
							_$('moonHoroBlock').innerHTML = data.html;
							return;
						}
						_$('moonHoroBlock').innerHTML = '';
						hideAjaxLoader();
					}, true
				);	
		}*/
	}
	var forum_list = [86909, 204988, 204989, 204990, 6269, 4267, 204991, 204992, 204993, 5629, 204994, 129779, 72015];
	_$('forum_link').href='http://friends.qip.ru/groups/groupinfo/'+forum_list[sign]+'/';
	global_sign = sign;
}
function getClientWidth()
{
  return (navigator.userAgent.indexOf("MSIE") == -1)?document.documentElement.clientWidth:document.body.clientWidth;
}

function getClientHeight()
{
	return (navigator.userAgent.indexOf("MSIE") == -1) ? document.documentElement.clientHeight : document.body.clientHeight;
}

function getBodyScrollTop()
{
	return (document.documentElement && document.documentElement.scrollTop) || (document.body && document.body.scrollTop);
}
function showAjaxLoader()
{
	if (!_$("popup_common"))
	{
		var iframe = document.createElement('iframe');
		iframe.id = 'popup_common_iframe';
		iframe.className = 'popup';
		iframe.style.display = 'none';
		var div = document.createElement('div');
		div.id = 'popup_common';
		div.className = 'popup';
		var img = document.createElement('img');
		img.src = '/skin/img/ajaxloader.gif';
		img.id = 'ajaxloader';
		document.getElementsByTagName('body')[0].appendChild(iframe);
		document.getElementsByTagName('body')[0].appendChild(div);
		document.getElementsByTagName('body')[0].appendChild(img);
	}
	var width = getClientWidth();
	var scroll = getBodyScrollTop();
	var height = getClientHeight();
	_$("ajaxloader").style.top = (scroll + 10) + (height-66)/2 + "px";
	_$("ajaxloader").style.left = width / 2 - 33 + "px";
	_$("ajaxloader").style.display = "block";
	_$("popup_common").style.display = "block";
	_$("popup_common_iframe").style.display = "block";
}
function hideAjaxLoader()
{
	_$("ajaxloader").style.display = "none";
	_$("popup_common").style.display = "none";
	_$("popup_common_iframe").style.display = "none";
}
function showBigCard(id, alt)
{
	var bigCardBox = _$('bigCardBox');
	var bigCardImg = _$('bigCardImg');
	bigCardImg.src = '/images/cards/'+id+'_big.jpg';
	bigCardImg.alt = alt;
	bigCardImg.title = alt;
	var width = getClientWidth();
	var scroll = getBodyScrollTop();
	var height = getClientHeight();
	bigCardBox.style.top = (scroll + 10) + (height-534)/2 + "px";
	bigCardBox.style.left = width / 2 - 182 + "px";
	bigCardBox.style.display = 'block';
}
function closeBigCard()
{
	_$('bigCardImg').src = '';
	_$('bigCardBox').style.display = 'none';
}
function changeTaroBlock(elm)
{
	if (!_$('taroBlockBox').innerHTML || _$('taroBlockBox').innerHTML == '')
	{
		return true;
	}
	
	var url = elm.href;
	var reg = /^(?:http:\/\/)?[^\/]*\/(.*)$/;
	var matches = url.match(reg);
	var uri = matches[1].split('/');
	var when = uri[2] ? uri[2] : 'today', type = uri[1] ? uri[1] : 'common';
	switch(when)
	{
		case 'yesterday': index_date = 0; break;
		case 'tomorrow': index_date = 2; break;
		default: index_date = 1; break;
	}
	_$('curDate').innerHTML = rusdates[index_date];
	var div_id = 'taroBlockBox_'+type+'_'+when;
	if (_$(div_id) && _$(div_id).innerHTML)
	{
		showTaroBlock(div_id);
		return false;
	}
	showAjaxLoader();
	JsHttpRequest.query(
			url,
			{'ajax': '1', 'block_name': 'TaroBlock'}, 
			function(data){
				//var data = eval('('+data+')');
				if (!_$(div_id))
				{
					var element = document.createElement('div');
					element.style.display = 'none';
					element.id = div_id;
					element.className = "toroBox";
					_$('taroBlockBox').appendChild(element);
				}
				_$(div_id).innerHTML = data.html;
				showTaroBlock(div_id);
				hideAjaxLoader();
			}, true
		);
		return false;
}
function showTaroBlock(div)
{
	var parent = _$('taroBlockBox'),
	allTaro = parent.getElementsByTagName('div');
	for(var i=0, n=allTaro.length; i<n; i++)
	{
		if (allTaro[i].className=="toroBox")
		{
			allTaro[i].style.display = 'none';
		}
	}
	_$(div).style.display = 'block';
}
function openmenu(what) {
	if (document.getElementById("extralist").style.display  != "block") {
		document.getElementById("extralist").style.display  = "block";
		what.innerHTML = "свернуть";
		what.className = "closelink";
		setCookie('horo_extalist_open', 1, new Date(new Date().getFullYear()+1, 1, 1), '/', '.horo.qip.ru');
	} else {
		document.getElementById("extralist").style.display  = "none";
		what.innerHTML = "еще";
		what.className = "link";
		deleteCookie('horo_extalist_open', '/', '.horo.qip.ru');
	}
}
function setCookie(name, value, expires, path, domain, secure)
{
	var curCookie = name + "=" + escape(value) +
		((expires) ? "; expires=" + expires.toGMTString() : "") +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		((secure) ? "; secure" : "");
	document.cookie = curCookie;
}
function getCookie(name)
{
	var prefix = name + "=";
	var cookieStartIndex = document.cookie.indexOf(prefix);
	if (cookieStartIndex == -1)
		return null;
	var cookieEndIndex = document.cookie.indexOf(";", cookieStartIndex + prefix.length);
	if (cookieEndIndex == -1)
		cookieEndIndex = document.cookie.length;
	return unescape(document.cookie.substring(cookieStartIndex + prefix.length, cookieEndIndex));
}
function deleteCookie(name, path, domain)
{
	if (getCookie(name))
	{
		document.cookie = name + "=" +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		"; expires=Thu, 01-Jan-70 00:00:01 GMT";
	}
}
