var ib_data = null;
var ib_num = 0;
var ns_data = null;
var ns_num = 0;
var ns_iter = true;
var ns_news = 0;
var ns_rate = '7s';
var phone_num = 0;

$(document).ready(function(){
	// Table rows pairs highligter
	$('#content table.details').each(function(){
		$(this).find('tr').each(function(i){
			if (i % 2 == 0)
				$(this).toggleClass('high');
		});
	});

	// Table rows over highlighter
	$('#content table.details tr').each(function(){
		$(this).mouseover(function(){
			$(this).addClass('over');
		});
		$(this).mouseout(function(){
			$(this).removeClass('over');
		});
	});

	// Phone changer
	phone_num = parseInt($('#phones li').length * Math.random());
	drawPhone();
	$('#phones').everyTime('15s', function(){
		if (++phone_num >= $('#phones li').length) phone_num = 0;
		drawPhone();
	});
	function drawPhone(){
		$('#phones li').each(function(id){
			if (id == phone_num) $(this).css('display', 'block');
			else $(this).css('display', 'none');
		});
	}

	// Content page header's top link
	$('.content_out .left h2, .content_out .left h3').not('.notl').each(function(){
		$(this).append("<a href='#top_link' title='наверх'>.</a>");
	});

	// Main page news viewer
	if ($('#news_line').text().length){
		$.getJSON('/rss/index.json', function(data){
			ns_data = data;
			ns_num = ns_data.length - 1;
			animateRedraw();
		});
	}
	function animateRedraw(){
		$('#news_line').stopTime('news_timer');
		changeNews();
		redrawNews();
		ns_iter = true;
	}
	function changeNews()
	{
		if (ns_data != null){
			if (++ns_num >= ns_data.length) ns_num = 0;
		}
	}
	function changeNewsBack()
	{
		if (ns_data != null){
			if (--ns_num < 0) ns_num = ns_data.length - 1;
		}
	}
	function redrawNews(){
		if (ns_data != null){
			if (ns_iter)
			{
				$('#news_line').toggle('blind',{},500,function(){
					$('#news_line').html(ns_data[ns_num].date + ' - <a href="' + ns_data[ns_num].link + '" title="' + ns_data[ns_num].title + '">' +
							ns_data[ns_num].title + '</a>');
					$('#news_line').toggle('blind',{},500);
					$('#news_line').oneTime(ns_rate, 'news_timer', animateRedraw);
				});
			}
			else $('#news_line').html(ns_data[ns_num].date + ' - <a href="' + ns_data[ns_num].link + '" title="' + ns_data[ns_num].title + '">' +
					ns_data[ns_num].title + '</a>');
			ns_iter = true;
		}
	}
	$('#news_line, #prevNews, #nextNews').each(function(){
		$(this).mouseenter(function(){
			$('#news_line').stopTime('news_timer');
		});
		$(this).mouseleave(function(){
			$('#news_line').oneTime(ns_rate, 'news_timer', animateRedraw);
		});
	});
	$('#prevNews').click(function(){
		$('#news_line').stopTime('news_timer_show');
		changeNewsBack();
		ns_iter = false;
		redrawNews();
		return false;
	});
	$('#nextNews').click(function(){
		$('#news_line').stopTime('news_timer_show');
		changeNews();
		ns_iter = false;
		redrawNews();
		return false;
	});
	
	// Images box
	if ($('#images_box').text().length){
		$.getJSON('files/theme/modern/images/home/images.json', function(data){
			ib_data = data;
			if (ib_data.length > 0) {
				$('#images_box').append("<ul></ul>\n");
				for(var i = 0; i < ib_data.length; i++) {
					var text = "<li><a href='#' rel='"+i+"'";
					if (i == 0) text += " class='selected'";
					text += '>'+ib_data[i].name+'</a></li>';
					$('#images_box ul').append(text);
				}
			}
			$('#images_box li:not(#prevImage, #nextImage) a').each(function(){
				$(this).mouseover(function(){
					ib_num = $(this).attr('rel');
					drawImage();
					return false;
				});
			});
			drawImage();
			for(var i = ib_data.length - 1; i >= 0; i--){
				var image = new Image();
				image.src = ib_data[i].image;
			}
		});
	}
	$('#images_box').everyTime('10s', 'image_timer', changeImage);
	$('#images_box').mouseenter(function(){
		$(this).stopTime('image_timer');
	});
	$('#images_box').mouseleave(function(){
		$('#images_box').everyTime('10s', 'image_timer', changeImage);
	});
	function changeImagePrev(){
		if (--ib_num < 0)
			ib_num = ib_data.length - 1;
		drawImage();
	}
	function changeImage(){
		if (++ib_num >= ib_data.length)
			ib_num = 0;
		drawImage();
	}
	$('#prevImage').click(function(){
		changeImagePrev();
		return false;
	});
	$('#nextImage').click(function(){
		changeImage();
		return false;
	});
	function drawSelector(){
		$('#images_box li a').each(function(){
			if ($(this).attr('rel') == ib_num)
				$(this).css({
					'background-color': ib_data[ib_num].color2,
					color: ib_data[ib_num].color1
				});
			else $(this).css({
				'background-color': ib_data[ib_num].color1,
				color: ib_data[ib_num].color2
			});
		});
	}
	function drawImage(){
		$('#images_box .comment').fadeOut('fast');
		$('#images_box .image').fadeOut('fast',function(){
			$('#images_box .image').css('background-image', "url('"+ib_data[ib_num].image+"')");
			$(this).fadeIn('fast',function(){
				$('#images_box .comment h4').text(ib_data[ib_num].text.title);
				$('#images_box .comment p').text(ib_data[ib_num].text.text);
				$('#images_box .comment a').attr('href',ib_data[ib_num].text.link);
				$('#images_box .comment').css({
					top: ib_data[ib_num].text.top,
					left: ib_data[ib_num].text.left,
					width: ib_data[ib_num].text.width,
					'background-color': ib_data[ib_num].color1,
					color: ib_data[ib_num].color2,
					'visibility': 'visible'
				});
				$('#images_box .comment').fadeIn('fast');
				$('#images_box ul li a').css({
					'background-color': ib_data[ib_num].color1,
					color: ib_data[ib_num].color2
				});
				$('#images_box ul li a').mouseenter(function(){
					$(this).css({
						'background-color': ib_data[ib_num].color2,
						color: ib_data[ib_num].color1
					});
				});
				$('#images_box ul li a').mouseleave(drawSelector);
				$('#images_box .selector_back').css({
					'background-color': ib_data[ib_num].color1
				});
				drawSelector();
			});
		});
	}
	
	//Search form
	$('#search1, #search2').each(function(){
		$(this).focus(function(){
			if ($(this).attr('value') == 'поиск')
				$(this).attr('value', '');
			$(this).removeClass('inactive');
		});
		$(this).blur(function(){
			if ($(this).attr('value') == '')
				$(this).attr('value', 'поиск');
			$(this).addClass('inactive');
		});
	});
	// Mail
	$('a.mail').each(function(){
		re = / at /;
		$(this).text($(this).text().replace(re, "@"));
		$(this).attr('href','mailto:'+$(this).text().replace(re, "@"));
	});
	// Price
	if (window.location.pathname.indexOf('/price/', 0) >= 0)
	{
		$("div.price a[href="+window.location.pathname.replace('#', '')+"]").each(function(){
			$(this).addClass('selected');
		});
	}
	// ICQ
	/*$('.info_box .icq p a').each(function(){
		var number = $(this).parent().attr('id');
		$('#'+number+' img').load('http://web.icq.com/whitepages/online?img=5&amp;icq='+number, function(response, status, xhr){
			if (xhr.statusText == 'OK')
				$('#'+number+' img').attr('src','http://web.icq.com/whitepages/online?img=5&amp;icq='+number);
		});
	});*/
});

