$(function(){	
	$('div.menu div.menu_item a').bind('click',function(){
		$('div.menu div.menu_active').removeClass('menu_active').addClass('menu_item');
		$(this).parent().removeClass('menu_item').addClass('menu_active');
	})				
	
	var hash = location.href.split('#');
	
	
	if(hash[1] !== undefined){		
		$('table.program_selector tr td.curent').removeClass('curent');
		$('div.selector_item').hide();
		$('#'+hash[1]+'_selector_item').show();
		$('#'+hash[1]+'_selector').addClass('curent');
	}
					
	$('table.program_selector tr td').bind('click',function(){					
	
		var id = $(this).attr('id');
		
		el = id.split('_selector');		
		location.href = '#'+el[0];
		//alert(el[0]);
	
		$('table.program_selector tr td.curent').removeClass('curent');
		$('div.selector_item').hide();
		
		$('#'+$(this).attr('id')+'_item').show();
		
		$(this).addClass('curent');
	})			
	
	$("#juitterContainer").tweet({
          join_text: "auto",
          username: "kmgroup",
          avatar_size: 48,
          count: 5,
          auto_join_text_default: "we said,", 
          auto_join_text_ed: "we",
          auto_join_text_ing: "we were",
          auto_join_text_reply: "we replied",
          auto_join_text_url: "we were checking out",
          loading_text: "Загрузка сообщений...",
		  query: "#TCI2011"
        });
        
        $(".wrapper").everyTime(5000, 'timer2', function(i) {						
        	getPartner($('#limit').val(),$('#version').val());
  		}, 300);

})


function getPartner(limit,lang){
	if($('#partner_placeholder').parent().hasClass('show') == true){
		$.ajax({
			url: '/home/partnerList',
			data: 'limit='+limit+'&lang='+lang,
			type: 'POST',
			success: function(response){
				if(response !== 'empty'){										
					var is_start = $('#height').attr('start');
					if(is_start == 'true'){												
						var new_height = $('#height').height();
						$('#height').html(response);					
						$('#height').removeAttr('start');						
					}else{
						$('#height').html(response);					
						var new_height = $('#height').height();
					}
					
					$('#partner_placeholder').empty();//.html(response);									
					
					 /*$('#partner_placeholder').animate({					    
					    height: new_height
					  }, 400, function() {*/
					    $('#partner_placeholder').hide().html(response);					
						$('#partner_placeholder').fadeIn(400);	
					  /*});*/
				}
			}
		})
	}
}
function UserForm(lang){	
	$.ajax({
		url: '/register/renderUserForm',		
		data: 'lang='+lang,
		type:'POST',
		success: function(response){
			$("#add_anchor").remove();
			$('#register_user').append(response);
		}
	})
}

function deleteUserFrom(code,lang){	
	$("#"+code).remove();
	var count = $(".user_from").size();
	if(count == 0){
		$("#register_info").prepend('<a id="add_anchor" style="margin-left:212px" onclick="UserForm(\''+lang+'\')">Добавить участника</a>');
	}
}

function ProcessRegistration(){
	$("#lock").val('false');
	$(".error_message").hide();
	$(".fio").each(function(index){		
		ValidateProcess(this);
	})
	$(".post").each(function(index){		
		ValidateProcess(this);
	})
	$(".work_phone").each(function(index){		
		ValidateProcess(this);
	})
	$(".mobile_phone").each(function(index){		
		ValidateProcess(this);
	})
	$(".email").each(function(index){		
		ValidateProcess(this);
	})
	var lock = $("#lock").val();
	if(lock == 'false'){	
	var form =  $("#register_form");
		$.ajax({
			url: form.attr('action'),
			type: form.attr('method'),
			data: form.serialize(),
			beforeSend: function(){
				
			},
			success: function(response){
				var data = eval('('+response+')');			
				if(data.status == 'ok'){				
					$(".registration_form").html(data.message);				
				}
			}
		})
	}
}

function ValidateProcess(el){
	if($(el).val() == ''){
			$(el).addClass('error_input').focus().next().show();			
			$(el).prev().addClass('error_label');
			$(".error_message").show();
			$("#lock").val('true');
		}else {
			$(el).removeClass('error_input').prev().removeClass('error_label');
		}
}
function Register(el){
	var form = $('#register').serialize();
	var name = $('#name');
	var surname = $('#surname');
	var phone = $('#phone');
	var email = $('#email');
	
	if(email.val() == ''){		
		email.focus();
	}else{
		if(check_email(email.val())){
			$.ajax({
				type: 'POST',
				url :'/home/register',
				data: form,
				beforeSend: function(){
					$('#submit').hide();
					$(el).after('<img id="ajax_indicator" style="margin-right:141px" src="/km_style/css/images/register_ajax.gif">');	
				},
				
				error: function(){
					$("#ajax_indicator").remove();
				},
				success: function(response){
					if(response == true){
						$("#ajax_indicator").remove();
						$('.register').text('Ваша заявка добавлена в базу');
					}else $('.register').text('Произошла ошибка.');
				}
			})
		}
		else email.focus();
	}
}

function check_email(email) {
	var template = /^[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z])+$/;
	email = drop_spaces(email); //функцию drop_spaces() см. выше
	if (template.test(email)) {
		return true;
	}
	return false; 
}

function drop_spaces(str) {
	var newstr = trim(str); 
	return newstr.replace(/(\s)+/g, ""); 
}
				
function trim(str) {
	var newstr = str.replace(/^\s*(.+?)\s*$/, "$1");
	if (newstr == " ") {
		return "";
	}
	return newstr;
}

function toggle(element){
	//$(element).next().toggleClass('hide')
	var el = $(element).next();
	
	if(el.hasClass('show')){
		el.slideUp();
		el.addClass('hide');
		el.removeClass('show');
		
		$('.arrow').removeClass('open');
		$('.arrow').addClass('close');
		
		$(element).children(':first').removeClass('open');
		$(element).children(':first').addClass('close');
		
		
	}else{
		$('.show').slideUp();
		$('.show').removeClass('show');		
		el.slideDown();
		el.addClass('show');
		el.removeClass('hide');
		
		$('.arrow').removeClass('open');
		$('.arrow').addClass('close');
		
		$(element).children(':first').removeClass('close');
		$(element).children(':first').addClass('open');
	}
}
