var startX = 0;
var startLeft = 0;
var clicking = false;
var moving = false;
var visible = false;
var numberLeft = 4;
var buttonOver = false;

$(document).ready(function(){

// main page
        if ( $('#tx-srfeuserregister-pi1-usergroup').val() == '') $('#tx-srfeuserregister-pi1-usergroup').val(1);
        
	$('ul.events-preview li').each(function(){
		//window.location=$(this).find("a").attr("href"); return false;
		//$(this).find("a:first").replaceWith($(this).find("a").html());
	});

	$('.main-event div.forum-heading').css({
		'marginLeft' : $('.main-event a.take-part').outerWidth() + 5 + 'px',
		'marginRight' : $('.main-event a.take-part').outerWidth() + 5 + 'px'
	});
	$('.subnav a').hover(function(){
		$(this).not('.active').find("img").each(function(i){
			this.src = this.src.replace('.gif','-hover.gif');
		});
	},function(){
		$(this).not('.active').find("img").each(function(i){
			this.src = this.src.replace('-hover.gif','.gif');
		});
	});

// text page

	$('ul.rootline li:first').css({'background':'none'});
	$('#content .img-right').each(function(i){
		$(this).css({
			'width' : $(this).find('img').outerWidth() + 'px'
		});
	});
	$('#content .img-left').each(function(i){
		$(this).css({
			'width' : $(this).find('img').outerWidth() + 'px'
		});
	});


// new user registration

	$('input.tx-srfeuserregister-pi1-submit').click(function(){
		$(this).val('Пожалуйста, подождите...');
		//$(this).attr('disabled', 1);
	});

	// 
	var currentGroupSelector = $('#tx-srfeuserregister-pi1-usergroup');
	if (currentGroupSelector) {		
		var currentGroupSelectorVal = currentGroupSelector.val();
		switch (currentGroupSelectorVal){
			case '2': 
				$('a#exhibitor').parent().parent().addClass('active-choice');
				$('a#delegate').parent().parent().removeClass('active-choice');
				$('a#visitor').parent().parent().removeClass('active-choice');
				break;
			case '3':
				$('a#visitor').parent().parent().addClass('active-choice'); 
				$('a#delegate').parent().parent().removeClass('active-choice');
				$('a#exhibitor').parent().parent().removeClass('active-choice');
				break;
			default: 
				$('a#delegate').parent().parent().addClass('active-choice'); 
				$('a#exhibitor').parent().parent().removeClass('active-choice');
				$('a#visitor').parent().parent().removeClass('active-choice');
				break;
		}
	}

	// Поведение формы "Юридическая информация" для старой регистрации
	var currentPersonalSelector = $('#tx-srfeuserregister-pi1-tx_medifeuserext_person');
	if (currentPersonalSelector) {
		var currentPersonalSelectorVal = currentPersonalSelector.val();
		switch (currentPersonalSelectorVal){
			case '1':
				$('a#juridical').parent().parent().removeClass('active-choice');
				$('a#physical').parent().parent().addClass('active-choice');
				$('.juridical-info').hide();
				break;
			default: 
				$('a#physical').parent().parent().removeClass('active-choice');
				$('a#juridical').parent().parent().addClass('active-choice');
				$('.juridical-info').show();
				break;
		}
	}

	// Переопределяем поведение формы "Юридическая информация" для Шага 2
	var user_group	= $('#sr_feuserregister_usergroup');
	var hide_form   = $('#sr_feuserregister_hide_form');
	
	if( user_group && hide_form && hide_form.val() )
	{
		user_group		= user_group ? user_group.val() : null;

		switch(user_group)
		{
			// Экспоненты
			case '2,1':
				$('.juridical-info').show(); 
				$('.sr_feuserregister__registration_step2__exhibitor_message').hide(); 
			//	$("#reg_step2_title").hide();
				break;
                        case '2':
                                $('.juridical-info').show(); 
                                $('.sr_feuserregister__registration_step2__exhibitor_message').hide(); 
                        //      $("#reg_step2_title").hide();
                                break;
			// Делегаты и остальные
			default:
				$('.juridical-info').hide(); 
				$('.sr_feuserregister__registration_step2__exhibitor_message').show(); 
			//	$("#reg_step2_title").show();
				break;
		};	
	}

	$('a#delegate,label[for=delegate]').click(function(){
		$('a#exhibitor').parent().parent().removeClass('active-choice');
		$('a#visitor').parent().parent().removeClass('active-choice');
		$(this).parent().parent().addClass('active-choice');
		$('#tx-srfeuserregister-pi1-usergroup').val(1);
		$('#jurphys').slideDown();
		return false;
	});
	$('a#exhibitor,label[for=exhibitor]').click(function(){
		$('a#delegate').parent().parent().removeClass('active-choice');
		$('a#visitor').parent().parent().removeClass('active-choice');
		$(this).parent().parent().addClass('active-choice');
		$('#tx-srfeuserregister-pi1-usergroup').val(2);
		$('#jurphys').slideDown();
		return false;
	});
	$('a#visitor,label[for=visitor]').click(function(){
		$('a#delegate').parent().parent().removeClass('active-choice');
		$('a#exhibitor').parent().parent().removeClass('active-choice');
		$(this).parent().parent().addClass('active-choice');
		$('#tx-srfeuserregister-pi1-usergroup').val(3);
		
		// Посетители все - физики
		$('a#physical').click();
		$('#jurphys').slideUp();
		return false;
	});

	$('a#juridical,label[for=juridical]').click(function(){
		$('a#physical').parent().parent().removeClass('active-choice');
		$(this).parent().parent().addClass('active-choice');
		$('input[id=tx-srfeuserregister-pi1-tx_medifeuserext_person]').val(0);
		$('.juridical-info:hidden').slideDown();
		return false;
	});
	$('a#physical,label[for=physical]').click(function(){
		$('a#juridical').parent().parent().removeClass('active-choice');
		$(this).parent().parent().addClass('active-choice');
		$('input[id=tx-srfeuserregister-pi1-tx_medifeuserext_person]').val(1);
		$('.juridical-info').slideUp();
		return false;
	});

// team business card appear/dissapear

	$('.business-card').hover(function(){
		$(this).find('a.card').fadeIn(300);
	},function(){
		$(this).find('a.card').fadeOut(300);
	});

// popup lightbox

	$('a[rel*=facebox]').facebox();


// vacancies

	$('ul.vacancy li').each(function(){
		$(this).not('.active').children('div.content').css({'opacity':0,'height':0});
		$(this).find('.heading').click(function(){
			if($(this).parent().is('.active')) {
				$(this).parent().removeClass('active');
				$(this).parent().children('div.content').animate({
					height:0,
					opacity:0
				});
			} else if($(this).parent().not('.active')) {
				$(this).parent().addClass('active');
				$(this).parent().children('div.content').animate({
					height:$(this).parent().find('table').outerHeight(),
					opacity:1
				});
			}
			return false;
		});
	});
	
	if ( $(".events-preview") != null )
	{
		var totalCount = $(".events-preview li").size();		
		
		if ( totalCount > 4 )
		{	
			
			/*
			$("#slider-left").mouseover(function(){
				buttonOver = true;
				$("#slider-left").css('background-position','left bottom');
				$("#slider-right").css('background-position','right top');
			})
			
			$("#slider-left").mouseout(function(){
				buttonOver = false;
				$("#slider-left").css('background-position','left top');
			})
			
			$("#slider-right").mouseover(function(){
				buttonOver = true;
				$("#slider-right").css('background-position','right bottom');
				$("#slider-left").css('background-position','left top');
			})
			
			$("#slider-right").mouseout(function(){
				buttonOver = false;
				$("#slider-right").css('background-position','right top');
			})
			*/
			
			$("#slider-left").click(function(){
			   if ( numberLeft - 4 > 0 ) 
				{
				   $(".events-preview").animate({"left": "+=245px"}, "slow");
				   numberLeft--;
				}   
			});
			
			$("#slider-right").click(function(){
				if ( numberLeft < totalCount ) 
			   {
				   $(".events-preview").animate({"left": "-=245px"}, "slow");
				   numberLeft++;
			
				} 
			});
			
			if ($(".events-preview") != null)
			{			
				$(".events-preview li").each(function () {
					var _item = $(this);	
					_item.mousedown(function(e) {
						moving = false
						clicking = true;
						startX = e.pageX;
						startLeft =  $(".events-preview").css('left').replace('px','') ;  
						if (startLeft == 'auto') startLeft = 0;
					});
					_item.click(function(e) {
						if (!moving) window.location=_item.find("a").attr("href"); return false;					
					});
				})
			}
		}
		else
		{
			$("#slider-left").hide();
			$("#slider-right").hide();
		}
	}	

});

//document.ondragstart = function() { return false }
//document.onselectstart = function() { return false }
document.onmouseup = OnMouseUp; 
document.onmousemove = OnMouseMove; 

function OnMouseUp(e) { 
	if (e == null) var e = window.event; 
	clicking = false;
	var target = e.target != null ? e.target : e.srcElement; 
	if (target.id == "slider-left" ||target.id == "slider-right"  ) return;		
	if ($(".events-preview") != null && $(".events-preview").css('left') != null)
	{
		var currentLeft =  $(".events-preview").css('left').replace('px','') ; 
		numberLeft = Math.round ( Math.abs(currentLeft) / 245 ) + 4;
		$(".events-preview").animate({"left": "-"+( ( numberLeft -4 )  *245)+"px"}, "slow");
	}
}

function OnMouseMove(e) { 
	if (e == null) var e = window.event; 
	if ( !clicking ) return
	var diff = startX - e.clientX;
	var left = startLeft - diff;
	var totalCount = $(".events-preview li").size();
	var maxLeft = (totalCount - 4) * 245;	
	left = Math.round (left);
	if (left <= 0 && left >= -maxLeft) $(".events-preview").css('left', left+'px');
	moving = true;
}




