$(function() {
	if(!(!$.browser.msie || $.browser.version != 6.0)) {
		$(".block3-l .trigger").mouseover(function() {
			$(this).addClass("hover");
		}).mouseout(function() {
			$(this).removeClass("hover");
		});
	}

	$('.top-menu .trigger-btn').click(function() {
		$('.top-menu .preview').slideToggle();
		$('.top-menu .full').slideToggle(function() {
			$('.top-menu').toggleClass('expanded');
		});

		return false;
	});

	$('.action-bar-l .login').click(function() {
		var loginForm = $(this).prev();
		if(loginForm.css('display') != 'none') {
			loginForm.fadeOut();
		} else {
			loginForm.fadeIn();
		}
		$(this).parent().toggleClass('expanded');

		return false;
	});

	$('.action-bar-r .search').click(function() {
		var searchForm = $(this).prev();
		if(searchForm.css('display') != 'none') {
			searchForm.fadeOut();
		} else {
			searchForm.fadeIn();
		}
		$(this).parent().toggleClass('expanded');

		return false;
	});

	var accordion = $('.accordion-block'),
		headerpic = $('.header-pic'),
		accordionDelay = false,
		animationDelay = false,
		mouseDelay = false,
		hpmouseDelay = false,
		speed = 'slow',
		count1 = 0,
		count2 = 1;

	$('.column-title').each(function() {
		var el = $(this).data('width', $(this).width());

		el.click(function() {
			if(!animationDelay) {
				animationDelay = true;

				$('.expanded .column-title').animate({
					width: el.data('width')
				}, speed);
				$('.expanded .column-content').animate({
					width: 0
				}, speed).parent().removeClass('expanded');

				$(this).next().animate({
					width: $(this).parent().parent().width() - 10 - (($(this).data('width')) * 3)
				}, speed, function() {
					animationDelay = false;
					$(this).parent().addClass('expanded');
				});
				$(this).animate({
					width: 0
				});
			}

			return false;
		});

		accordion.mouseover(function() {
			mouseDelay = true;
		}).mouseout(function() {
			mouseDelay = false;
		});
	});

	$('.item', headerpic).each(function() {
		$(this).mouseover(function() {
			if(!accordionDelay) {
				accordionDelay = true;
				var el = $(this)
				$(this).parent().parent().children('.visible').fadeOut(speed).removeClass('visible');
				$(this).parent().children('.selected').removeClass('selected');
				$(this).addClass('selected').parent().parent().children('.img' + $(this).attr('rel')).fadeIn(speed, function() {
					accordionDelay = false;
					count2 = parseInt(el.attr('rel'), 10);
				}).addClass('visible');
			}

			return false;
		});

		headerpic.mouseenter(function() {
			hpmouseDelay = true;
		}).mouseleave(function() {
			hpmouseDelay = false;
		});
	});
	
	
	
	
	var accordionRuntime = setInterval(function() {
		if(!animationDelay && !mouseDelay) {
			count1 = (count1 > accordion.children().length - 1) ? 0 : count1;
			$(accordion.children()[count1]).children('.column-title').trigger('click');
			count1++;
		}
	}, 3000);
	
	
	
	
	var accordionRuntime2 = setInterval(
	function() 
	{
		if(!accordionDelay && !hpmouseDelay)
		{
			count2 = (count2 > headerpic.children().length - 2) ? 0 : count2;
			$($('.paginator', headerpic).children().get(count2)).trigger('mouseover');
			count2++;
		}
	}
	, 5000);
	
	
	
	$(accordion.children()[accordion.children().length - 1]).children('.column-title').trigger('click');
	$($('.paginator', headerpic).children().get(0)).trigger('mouseover');

	$('.c-menu > li').hover(function() {
		$('.tooltip-block i').css('top', $(this).position().top + 15);

		if(!(!$.browser.msie || $.browser.version != 6.0)) {
			$(this).addClass("hover");
			$('.tooltip-block', $(this)).height(($('.tooltip-block', $(this)).height() < 235) ? 235 : $('.tooltip-block', $(this)).height());
		}
	}, function() {
		if(!(!$.browser.msie || $.browser.version != 6.0)) {
			$(this).removeClass("hover");
		}
	});

	var loi = $('.loi-block'),
		gc = $('.line-of-interests', loi),
		cg = $('.center-gallery', loi),
		bi = $('.b-item', cg);

	cg.width((bi.width() + 5) * bi.length);
	loi.data({
		'totalcount': $('.b-item', cg).length,
		'isparity': false,
		'shift': 0,
		'maxshift': 0,
		'noshift': false
	});

	if((loi.data('totalcount') / 2) == Math.round(loi.data('totalcount') / 2)) {
		cg.css('left', 170 / 2);
		loi.data('shift', 1);
		loi.data('isparity', true);
	}

	$(window).bind('recalc-loi', function() {
		loi.data('maxshift', Math.abs(Math.ceil((cg.width() - loi.width()) / 170)));
		if(loi.data('isparity') && loi.data('maxshift') == 0) {
			loi.data('maxshift', 1);
		} else if(loi.data('isparity') && loi.data('maxshift') != 0) {
			loi.data('maxshift', loi.data('maxshift') - 1);
		}
	});

	$(window).trigger('recalc-loi');

	$('.line-of-interests-prew', loi).click(function() {
		if(loi.data('shift') < (loi.data('maxshift') - 1)) {
			cg.animate({
				left: parseInt(cg.css('left'), 10) + 170
			});
			loi.data('shift', loi.data('shift') + 1);
		}
	});

	$('.line-of-interests-next', loi).click(function() {
		if(loi.data('shift') > -(loi.data('maxshift'))) {
			cg.animate({
				left: parseInt(cg.css('left'), 10) - 170
			});
			loi.data('shift', loi.data('shift') - 1);
		}
	});

	$(window).bind('render_calendar', function(event, month, year, data) {
		$('.calendar-placeholder .calendar-content').renderCalendar({month: month, year: year}).prev().find('span').html(Date.monthNames[month]);
		if(data[year][month]) {
			for(type in data[year][month]) {
				for(day in data[year][month][type]) {
					var dimensions = $($('.calendar-placeholder .current-month').get(day-1)).position();
					$('<a>').addClass(type).css({
						top: dimensions.top,
						left: dimensions.left
					}).attr('href', data[year][month][type][day][0]).data('title', data[year][month][type][day][1]).html(day).appendTo('.calendar-placeholder .calendar-content').hover(function () {
							var ttl = $('<div>').addClass('b-callendar-title').addClass($(this).attr('class')).html('<div class="b-indent">' + $(this).data('title') + '</div>').css({
								width: '170px',
								top: $(this).position().top,
								left: $(this).position().left - 170,
								position: 'absolute'
							}).appendTo($(this).parent());
							$(this).data('title', ttl);
						},function () {
							$(this).data('title').remove();
						}
					);
				}
			}
		}
	}).resize(function() {
		var content = $('.expanded .column-content');
		content.width(content.parent().parent().width() - 10 - ((content.prev().data('width')) * 3));
		$(window).trigger('recalc-loi');
	});

	var date = new Date
		calendarData = null,
		month = date.getMonth(),
		year = date.getFullYear();
/*
	calendarData = {
		'2010': {
			'4': {
				'type1': {
					'6': ['http://test6.ru', 'comment #1'],
					'11': ['http://test11.ru', 'comment #2'],
					'15': ['http://test15.ru', 'comment #3']
				},
				'type2': {
					'27': ['http://test27.ru', 'comment #4']
				},
				'type3': {
					'7': ['http://test7.ru', 'comment #5'],
					'23': ['http://test23.ru', 'comment #6']
				}
			},
			'5': {
				'type3': {
					'5': ['http://test5.ru', 'comment #7']
				}
			}
		}
	}
*/

	$('.calendar-placeholder .prev').click(function() {
		month = (month == 0) ? 11 : month - 1;
		$(window).trigger('render_calendar', [month, year, calendarData]);
	});

	$('.calendar-placeholder .next').click(function() {
		month = (month == 11) ? 0 : month + 1;
		$(window).trigger('render_calendar', [month, year, calendarData]);
	});

	$('.b-vertical-callendar .vertical-btn').click(function() {
		var el = $(this).next();
		if(el[0].offsetWidth === 0) {
			$('.b-vertical-callendar').addClass('opened');
			el.addClass('loading').fadeIn(function() {
				/*if(!calendarData) {
					$.getJSON('/site/json/request.json', function(json) {
						if(json != '') {
							calendarData = json;
							el.removeClass('loading');
							$('.b-vertical-callendar').removeClass('opened');
							$(window).trigger('render_calendar', [month, year, calendarData]);
						} else {
							alert('Ошибка сервера! Попробуйте позже');
							el.fadeOut();
						}
					});
				} else {*/
					el.removeClass('loading');
					$(window).trigger('render_calendar', [month, year, calendarData]);
				//}
			});
		} else {
			el.fadeOut();
			$('.b-vertical-callendar').removeClass('opened');
		}

		$('body').click(function(event) {
			if($(event.target).parents('.calendar-placeholder').length == 0 && !$(event.target).hasClass('calendar-placeholder')) {
				el.fadeOut();
				$('.b-vertical-callendar').removeClass('opened');
			}
		});
		
		

		return false;
	});
});
