$(document).ready(function ()
{
	$('.menu-item-title').mouseover(function()
	{
		if(!($(this).hasClass('current')))
		{
			$('.current a').css('color', '#999');
		}
		$('.sub-menu').hide();
	});
	$('#main-menu ul li').mouseover(function ()
	{
		$(this).find('.menu-item-title').addClass('hover');
		$(this).find('.sub-menu').show();

	});
	$('#main-menu ul li').mouseleave(function ()
	{
		$(this).find('.menu-item-title').removeClass('hover');
		$(this).find('.sub-menu').hide();
	});
	
	$('#main-menu ul').mouseleave(function ()
	{
		$('.current').parents('li').find('.sub-menu').show();
		$('.current a').css('color', '#ec2027');
	});

	initSliderArrows();

	$('#lecturer-slider .edge-image').mouseenter(function ()
	{
		$(this).find('.pop-tip, .pop-tip-back').show();
	});
	$('#lecturer-slider .edge-image').mouseleave(function ()
	{
		$(this).find('.pop-tip, .pop-tip-back').hide();
	});

	$('#'+$('#event-month-select #selected').val()).show();
	$('.month').not('#'+$('#event-month-select #selected').val()).hide();

	$('#event-month-select select').change(function ()
	{
		$('#'+$(this).val()).show();
		$('.month').not('#'+$(this).val()).hide();
	});


	$('.profile-event .month').show();
	$('#event-month-select-profile select').change(function ()
	{
		$('.month').show();
		var val = $(this).val();
		if(val == 'All')
		{
			$('.month').show();
			return false;
		}
		$('#'+$(this).val()).show();
		$('.month').not('#'+$(this).val()).hide();
	});

	$('.day .description').parents('.day').mouseenter(function ()
	{
		$(this).find('.dark-layer, .day-number').hide();

		if ( $('.image img', this).length > 1 )
		{
			if ( $('.image', this).get(0).cyclePause === 1 )
			{
				$('.image', this).cycle('resume');
			}
			else
			{
				$('.image', this).cycle(
				{
					fx: 'fade',
					timeout: 1200,
					speed: 750
				});
			}
		}

		$('#description-popup').css('top', $(this).offset().top - $('.calendar').offset().top + 50 );
		$('#description-popup').css('left', $(this).offset().left - $('.calendar').offset().left - 60);
		$('#description-popup .rnd-block-content').html($('.description', this).html());
		$('#description-popup').show();
	});
	$('.day .description').parents('.day').mouseleave(function ()
	{
		$(this).find('.dark-layer, .day-number').show();

		if ( $('.image img', this).length > 1 )
		{
			$('.image', this).cycle('pause');
		}

		$('#description-popup').hide();
	});

	
	$('.friend').hover(function(){
		$(this).addClass('rnd block-f8');
		$(this).find('.corners').toggle();

		$('.friend-info div:last', this).toggle();
		$('.friend-info .friend-hidden',this).toggle();
			},
		function(){
			$('.friend').removeClass('rnd block-f8');
			$(this).find('.corners').toggle();
			$('.friend-info div:last', this).toggle();
			$('.friend-info .friend-hidden',this).toggle();
	});

	$('.message').hover(function(){
		$(this).addClass('rnd block-f8');
		$(this).find('.corners').toggle();

		$('.message-info .message-body', this).toggle();
		$('.message-info .message-hidden',this).toggle();
			},
		function(){
			$('.message').removeClass('rnd block-f8');
			$(this).find('.corners').toggle();
			$('.message-info .message-body', this).toggle();
			$('.message-info .message-hidden',this).toggle();
	});
	
	$('.login, #login').click(function ()
	{
		$('.popup-bg').css('width', $(document).width());
		$('.popup-bg').css('height', $(document).height());
		$('.popup-bg').show();
		$('.popup').alignCenter().show();
		return false;
	});

	
	$('#auth-cb, #rss-cb').click(function ()
	{
		if ( $(this).hasClass('active') )
		{
			$(this).removeClass('active');
			$('input', this).removeAttr('checked');
		}
		else
		{
			$(this).addClass('active');
			$('input', this).attr('checked', 'checked');
		}
		return false;
	});

	$.fn.alignCenter = function()
	{

	  var marginLeft = Math.max(100, parseInt($(window).width()/2 - $(this).width()/2)) + 'px';
	  var marginTop = Math.max(100, parseInt($(window).height()/2 - $(this).height()/2)) + 'px';
		if($.browser.msie)
		{
			marginTop = Math.max(200, parseInt($(window).height()/2 - $(this).height()/2)) + 'px';
		}
	  return $(this).css({'margin-left':marginLeft, 'margin-top':marginTop});
	};
	$.fn.alignLeft = function()
	{
	  var marginLeft = Math.max(100, parseInt($(window).width()/2 - $(this).width()/2)) + 'px';
	  return $(this).css({'margin-left':marginLeft});
	};

	$('.article-hover').mouseover(function(){
		if($.browser.safari)
		{
			$('.content-visible', $(this).parent()).hide();
			$('.content-hidden', $(this).parent()).show();
		}
		else
		{
			$('.content-visible', $(this).parent()).hide('slow');
			$('.content-hidden', $(this).parent()).show('slow');
		}

		});

	
	$('.content-hidden').mouseleave(function(){
		if($.browser.safari)
		{
			$('.content-visible', $(this).parent()).show();
			$('.content-hidden', $(this).parent()).hide();
		}
		else
		{
			$('.content-visible', $(this).parent()).show('slow');
			$('.content-hidden', $(this).parent()).hide('slow');
		}

	});

	$('.rules-popup').click(function(){
		$('.popup-bg-rules').css('width', $(document).width());
		$('.popup-bg-rules').css('height', $(document).height());
		$('.popup-bg-rules').show();
		$('.popup-rules').alignLeft().show();
	});

	$('.rules-scroll-bottom').click(function(){
		var top_pos = parseInt($('.rules-text').css('top'));
		if(top_pos <= -400) return false;
		top_pos = top_pos - 50;
		$('.rules-text').stop(true, true).animate({ top : top_pos } , 500);
	});


	$('.rules-scroll-top').click(function(){
		var top_pos = parseInt($('.rules-text').css('top'));
		if(top_pos >= 0) return false;
		top_pos = top_pos + 50;
		$('.rules-text').stop(true, true).animate({ top : top_pos } , 500);
	});

	$('#checked').parent().addClass('active');
		$('.search-filter li').click(function(){
			$('.search-filter li').removeClass('active');
			$('input').removeAttr('checked');
			$('input', this).attr('checked', 'checked');
			$(this).addClass('active');
		});

  if ( $('.search-input').hasClass('en') )
  {
    var search_val = 'Search on a site';
  }
  else
  {
    var search_val = 'Поиск по сайту';
  }
  
  $('.search-input').val(search_val);

	$('#search-button, .search-input').hover(function(){
		if($('.search-input').val() == search_val)
		{
			$('.search-input').val('');
		}
	}, function(){
		if($('.search-input').val() == '')
		{
			$('.search-input').val(search_val);
		}
	});
	
	$('tr.passive').click(function(){
		$('tr.active').removeClass('active');
		$(this).addClass('active');
	});

	var image_width = $('.image-width').width();
	if(image_width > 580)
	{
		$('.image-width').width(580);
	}
	$('.vote-title').click(function()
	{
		$(this).hide();
		$(this).parent('.rnd').find('.block-content').fadeIn('slow');
	});
	$('.vote .vote-submit').click(function()
	{
		var block_content = $(this).parents('.block-content:first');
		var question_id =  $(this).parents('.vote:first').find('table').attr('id');
		var answer_id = $(this).parents('.vote:first').find('.active').attr('id');
		var route = $(this).parents('.vote:first').find('input').val();
		$.post(route, { question_id: question_id, answer_id: answer_id}, function(responce){
			block_content.empty();
			block_content.append(responce);
		});
	});
	$('.vote-title:first').ready(function()
	{
		$('.vote-title:first').hide();
		$('.vote-title:first').parent('.rnd').find('.block-content').fadeIn();
	});

  var menu_home_attr = $('.menu-home').attr('src');
  $('.menu-home').hover(function()
  {
    $(this).addClass('active');
    $(this).attr('src', '/images/menu-home-hover.gif');
  }, function()
  {
    $(this).removeClass('active');
    $(this).attr('src', menu_home_attr);

  });
});

var isAnimate = false;
function initSliderArrows()
{
	$('.arrow-left').click(function (){if ( !isAnimate ) moveSlider('#lecturer-slider .container', -1, this, 9);});
	$('.arrow-right').click(function (){if ( !isAnimate ) moveSlider('#lecturer-slider .container', 1, this, 9);});
}
function moveSlider(selector, dir, anchor, multiplier )
{
	var collection_left = parseInt($(selector + ' .slide-collection').css('left'));
	var slide_count = $(selector + ' .slide-collection .slide').length;
	var collection_width = parseInt($(selector + ' .slide-collection').css('width'));
	var slider_step_width = (collection_width / slide_count) * 2;
	if ( dir > 0 )
	{
		if ( (( collection_left - slider_step_width ) >= -( collection_width - slider_step_width * multiplier / 2  )) || ( (( collection_left - slider_step_width) + (collection_width - slider_step_width * multiplier / 2)) == -100 ) )
		{
			if( (( collection_left - slider_step_width) + (collection_width - slider_step_width * multiplier / 2)) == -100 )
			{
				isAnimate = true;
				$(selector + ' .slide-collection').animate({left: (collection_left - slider_step_width + 100)+'px'}, 500, function ()
				{
					isAnimate = false;
				});
			}
			else
			{
				isAnimate = true;
				$(selector + ' .slide-collection').animate({left: (collection_left - slider_step_width)+'px'}, 500, function ()
				{
					isAnimate = false;
				});
			}

		}
	}
	else
	{
		if ( ( collection_left + slider_step_width ) <= 0 )
		{
			isAnimate = true;
			$(selector + ' .slide-collection').animate({left: (collection_left + slider_step_width)+'px'}, 500, function ()
			{
				isAnimate = false;
			});
		}
		else if( collection_left + slider_step_width == 100 )
		{
			isAnimate = true;
			$(selector + ' .slide-collection').animate({left: (collection_left + slider_step_width - 100)+'px'}, 500, function ()
			{
				isAnimate = false;
			});
		}
	}

}

function ShowPopupView(id, route)
{
	$('#popup-bg-message'+id).css('width', $(document).width());
	$('#popup-bg-message'+id).css('height', $(document).height());
	$('#popup-bg-message'+id).show();
	if($.browser.msie) $('#event-month-select-profile').hide();
	$('#popup-message'+id).alignCenter().show();
	$.post(route);
	return false;
}
function ClosePopup()
{
	$('.popup-bg-message').hide();
	$('.popup-message').hide();
	if($.browser.msie) $('#event-month-select-profile').show();
	return false;
}
function ClosePopupRules()
{
	$('.popup-bg-rules').hide();
	$('.popup-rules').hide();
	$('.rules-text').css('top', '0');
	return false;
}

function ShowPopupWrite(id)
{
	$('#popup-bg-message-write').css('width', $(document).width());
	$('#popup-bg-message-write').css('height', $(document).height());
	$('#popup-bg-message-write').show();
	$('#message_body').val('');
	$('#popup-message-write').alignCenter().show();
	$('#popup-message-write form input').val(id);
	if($.browser.msie)
	{
		$('#event-month-select-profile').hide();
	}
	return false;
}

function SubmitPopupForm(a)
{
  var tmp = trim($('#message_body').val(), ' ');
	if( tmp.length == 0)
	{

	}
	else
	{
		$(a).parents('.popupForm').submit();
	}
	return false;

}

function make_vote(mark, id, url, a)
{
	if(mark <= 0 || mark > 5 || id < 0)
	{
		return false;	
	}
	else
	{
		$.post(url, '', function(response){
			var parent = $(a).parent();
			$(parent).empty();
			$(parent).append("<div style=\"float: left;\">Рейтинг:</div> <div class=\"mark\">"+response+"</div>");
		});
	}
}


function trim(str, chars) {
 return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
 chars = chars || "\\s";
 return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
 chars = chars || "\\s";
 return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}
