var SEARCH_DEFAULT = 'Поиск по сайту';

$(function(){
	$('form#search input').attr('value', SEARCH_DEFAULT);

	$('form#search input').focus(function(){
		if (this.value == SEARCH_DEFAULT) this.value='';
	});
	$('form#search input').blur(function(){
		if (this.value == '') this.value = SEARCH_DEFAULT;
	});

	$('form#route-subscribe input[type=text]').focus(function(){
		if (this.value == 'Введите Ваш e-mail') {
			this.value='';
			this.style.color = '#000';
		}
	});
	$('form#route-subscribe input[type=text]').blur(function(){
		if (this.value == '') {
			this.value = 'Введите Ваш e-mail';
			this.style.color = '#666';
		}
	});
	
	$('*[width]').removeAttr('width');
	$('*[border]').removeAttr('border');
	$('*[align]').removeAttr('align');

	$('#question-form p#org-name').hide();
	$('#question-form p#org-job').hide();
	$('#question-form p#user-city-wrapper').hide();
	$('#question-form p#user-district-wrapper').hide();

	$('#content table').find('tr:first').addClass('first-child');
	$('#content table tr').find('> :odd').addClass('even');
	$('#content table tr').find('> :even').addClass('odd');
	
	$('.photo').hover(function()	{
		$(this).find('p').show();
	},function(){
		$(this).find('p').hide();
	});
});

$(document).ready(function() {
	$('#slideshow .slide').css({float: 'none', position: 'absolute'}).append('<span class="control play" />');
	$('#slideshow .slide').not(':eq(1)').find('.control').hide();
	$('#slideshow .slide:eq(1)')
		.prev().css({marginTop: '70px', borderWidth: '3px', left: 0}).end()
		.css({borderWidth: '0', left: '200px'})
		.nextAll('.slide').each(function(i) {
			$(this).css({marginTop: '70px', borderWidth: '3px', left: 800 + 52 + 200*i});
		});
	;
	$('#slideshow .slide:eq(1)').append('<span class="caption">' + $('#slideshow .slide:eq(1)').find('img').attr('alt') + '</span>').prev().find('.caption').remove();

	$('#slideshow .slide').not(':eq(1)').find('img').attr('src', function() { return this.src.replace('slide-', 'preview/preview-'); });
	$('#slideshow .slide').click(function() {
		if	($(this).prev().css('left') == '200px') {
			$(this).find('img').attr('src', function() { return this.src.replace('preview/preview-', 'slide-'); });
			$(this).find('.control').show();
			$(this).prev().prevAll().hide();
			$(this).prev().find('img').attr('src', function() { return this.src.replace('slide-', 'preview/preview-'); });
			$(this).prev().find('.control').hide();
			$(this).prev().animate({marginTop: '70px', borderWidth: '3px', left: '-=200px'}, {duration: 500, easing: 'jswing'}).find('img').animate({width: '142px', height: '45px'}, {duration: 500, easing: 'jswing'});
			$(this).prev().prevAll().animate({left: '-=200px', opacity: '0'}, {duration: 500, easing: 'jswing'});
			$(this).animate({marginTop: '0', borderWidth: '0', left: '-=652px'}, {duration: 500, easing: 'jswing'}).find('img').animate({width: '600px', height: '190px'}, {duration: 500, easing: 'jswing'});
			$(this).next().animate({left: '-=200px', opacity: '1'}, {duration: 500, easing: 'jswing'}).show();
			$(this).next().nextAll().animate({left: '-=200px'}, {duration: 500, easing: 'jswing'}).hide();
			$(this).append('<span class="caption">' + $(this).find('img').attr('alt') + '</span>').prev().find('.caption').remove();
			$(this).find('.caption').css({display: 'none'}).show();
		};
		if	($(this).next().css('left') == '200px') {
			$(this).find('img').attr('src', function() { return this.src.replace('preview/preview-', 'slide-'); });
			$(this).find('.control').show();
			$(this).next().find('img').attr('src', function() { return this.src.replace('slide-', 'preview/preview-'); });
			$(this).next().animate({marginTop: '70px', borderWidth: '3px', left: '+=652px'}, {duration: 500, easing: 'jswing'}).find('img').animate({width: '142px', height: '45px'}, {duration: 500, easing: 'jswing'});
			$(this).next().next().hide();
			$(this).next().find('.control').hide();
			$(this).next().nextAll().animate({left: '+=200px', opacity: '0'}, {duration: 500, easing: 'jswing'});
			$(this).animate({marginTop: '0', borderWidth: '0', left: '+=200px'}, {duration: 500, easing: 'jswing'}).find('img').animate({width: '600px', height: '190px'}, {duration: 500, easing: 'jswing'});
			$(this).prev().animate({left: '+=200px', opacity: '1'}, {duration: 500, easing: 'jswing'}).show();
			$(this).prev().prevAll().animate({left: '+=200px'}, {duration: 500, easing: 'jswing'});
			$(this).append('<span class="caption">' + $(this).find('img').attr('alt') + '</span>').next().find('.caption').remove();
			$(this).find('.caption').css({display: 'none'}).show();
		};
		return
	});
	$('#slideshow .control').click(function() {
		if ($(this).hasClass('play'))	{
			interval = setInterval(rightToLeft, 5000);
			$('#slideshow .control').removeClass('play').addClass('pause');
		}
		else if ($(this).hasClass('pause'))	{
			clearInterval(interval);
			$('#slideshow .control').removeClass('pause').addClass('play');
		}
	});
	$('#slideshow img').not(':eq(1)').each(function(i) {
		$(this).css({width: '142px', height: '45px'});
	});
	
	$('#photo-gallery li a.albom').wrap('<div class="alb-offset"><div class="alb-r"><div class="alb-b"></div></div></div>');
	
	$('#resume-form .add-row').click(function() {
		var table = this.parentNode.parentNode.parentNode.parentNode
		var tr = $("tbody tr:last-child", table).get(0)
		tr.parentNode.insertBefore(tr.cloneNode(true), tr);
		return false
	})
	
	$("#global-nav ul li").hover(
		function () {liOnly=this.id; setTimeout("mainNavIn("+this.id+")", 500);},
		function () {liOnly=0; setTimeout("mainNavOut("+this.id+")", 1);}
	);
	$("#global-nav ul li ul li").hover(
		function () {mainNavIn(this.id);},
		function () {mainNavOut(this.id);}
	);
});

function mainNavIn(id){
	if(liOnly!=id) return;
	ob = document.getElementById(id)
	$(ob).css("z-index","99");
	$(ob).children(".submenu").fadeIn(100);	
	$(ob).addClass('hover-li');
	$(ob).children(".submenu").children(".shadow-left").height($(ob).children(".submenu").height());			
}
function mainNavOut(id){
	ob = document.getElementById(id)
	$(ob).css("z-index","9");
	$(ob).children(".submenu").fadeOut(100);
	$(ob).removeClass('hover-li');		
}
liOnly = 0;

var clickableElement = 2;

function rightToLeft()	{
	if	($('#slideshow .slide:last').css('left') == '200px')
		clickableElement = 0;
	else if ($('#slideshow .slide:first').css('left') == '0px')
		clickableElement = 2;

	$('#slideshow .slide:visible:eq(' + clickableElement + ')').trigger('click');
}

function updateOrgFields(userType) {
	if (userType == 'person') {
		$('#question-form p#org-name').hide('slow');
		$('#question-form p#org-job').hide('slow');
	}
	else {
		$('#question-form p#org-name').show('slow');
		$('#question-form p#org-job').show('slow');
	}
}

function updateUserValue(value, wrapper_id, input_id) {
	if (value == -1) {
		$('#'+input_id).attr('disabled', '');
		$('#'+wrapper_id).show('slow');
	}
	else {
		$('#'+wrapper_id).hide('slow');
		$('#'+input_id).attr('disabled', 'disabled');
	}
}

function is_email(email_str) {
	var checkTLD=1;
	var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
	var emailPat=/^(.+)@(.+)$/;
	var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
	var validChars="\[^\\s" + specialChars + "\]";
	var quotedUser="(\"[^\"]*\")";
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	var atom=validChars + '+';
	var word="(" + atom + "|" + quotedUser + ")";
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
	var matchArray=email_str.match(emailPat);
	if (matchArray==null) return false;
	var user=matchArray[1];
	var domain=matchArray[2];
	for (i=0; i<user.length; i++)	if (user.charCodeAt(i)>127) 	return false;
	for (i=0; i<domain.length; i++)	if (domain.charCodeAt(i)>127)	return false;
	if (user.match(userPat)==null) 	return false;
	var IPArray=domain.match(ipDomainPat);

	if (IPArray!=null) {
		for (var i=1;i<=4;i++)
			if (IPArray[i]>255) return false;
		return true;
	}

	var atomPat=new RegExp("^" + atom + "$");
	var domArr=domain.split(".");
	var len=domArr.length;

	for (i=0;i<len;i++) if (domArr[i].search(atomPat)==-1) return false;
	if (checkTLD && domArr[domArr.length-1].length!=2 && domArr[domArr.length-1].search(knownDomsPat)==-1) return false;
	if (len<2) return false;

	return true;
}

function showVideo(index) {
	var div = document.getElementById('videos-' + index);
	if (div)
		div.style.display = (div.style.display == 'block') ? 'none' : 'block';
	return false;
}

function vote() {
	var answer = 0
	$('input:radio', $('#oprosnik')).each(function(i){answer = this.checked ? this.value : answer})
	if (!answer) {
		alert('Выберите один из вариантов ответа!')
	}
	else {
		$("#oprosnik").submit();
	}
	return false;
}

function poll_vote() {
	var is_error = false

	$('div.poll-question').each(function(i){
		var answered = false;
		$('input:radio', this).each(function(i){answered = this.checked ? true : answered})
		is_error = is_error || !answered;
	})
	
	if (is_error) {
		alert('Выберите для каждого вопроса один из вариантов ответа!');
	}
	else {
		$('#poll-form').submit();
	}
	
	return false;
}
