function journal_calendar_init($) {
	$('#calendar_inner .month.ctrl').unbind('click');
	$('#calendar_inner .month.ctrl').not('.disabled').click(function() {
		var $this = $(this);
		var date = $this.attr('rel');
		$.get('/as/modules/user/', {todo: 'journal_calendar', cat: journal_cat, date: date}, function(data) {
			$('#calendar_inner').html(data);
			journal_calendar_init($);
		});
		return false;
	});
}

function journal_auto_switch() {
	var $ = jQuery;
	var $next_li = $('.l-body_journal .b-switcher UL LI.cur').next();
	if (!$next_li.get(0)) {
		$next_li = $('.l-body_journal .b-switcher UL LI:first');
	}
	journal_switch($next_li);
}

var journalSwitchInterval = null;

function journal_auto_switch_init() {
	var $ = jQuery;
	if ($('.l-body_journal .b-switcher').get(0)) {
		journalSwitchInterval = setInterval ("journal_auto_switch()", 5000);
	}
}

function journal_switch($li) {
	var $ = jQuery;
	var id = $li.attr('id').split('_');
	$('#main-new').html(main_news[id[1]]);
	$li.parent().find('LI').removeClass('cur');
	$li.addClass('cur');
}

function ajax_menu_init($) {
	$('.ajax_menu_item').unbind('click');
	$('A.ajax_menu_item').click(function () {
		var parts = $(this).attr('id').split('_');
		$('#level1_cat A').html($(this).html());
		$('#level1_cat').show();
		$('#level1_cats').hide();
		
		$.get('/as/modules/user/', {todo: 'ajax_menu', cat: parts[2]}, function(data) {
			$('#level2_cats .b-round__i').html(data);
			$('#level2_cats').removeClass('b-round_level').addClass('b-round_extranav');
			$('#level3_cats').remove();
			ajax_menu_init($);
		});
		return false;
	});
}

$(document).ready(function($){
	if ($('A#order_call_link').get(0)) {
		$('A#order_call_link').fancybox({
			scrolling: 'no'
		})
	}
	
	$('#hide_all').click(function(){
			k = $('#hide_all').parent().parent().height();
			$('#hide_all').parent().parent().animate({height:'5px'},200);
			$('#hide_all').css('cursor','default');
			$('#hide_all').css('float','right');
			$('#hide_all').removeClass('link2');
			$('#show_all').css('cursor','pointer');
			$('#show_all').addClass('link2');
			$('#show_all').css('float','left');
		});
	$('#show_all').click(function(){
			$('#show_all').parent().parent().animate({height:k+'px'},200);
			$('#show_all').css('cursor','default');
			$('#hide_all').addClass('link2');
			$('#hide_all').css('cursor','pointer');
			$('#show_all').removeClass('link2');
		});

	$('.searchHelp A').click(function () {
		$('#input_search').val($(this).html());
		return false;
	})

	$('#level1_cat.ajax_m A').click(function () {
		$('#level1_cat').hide();
		$('#level1_cats').show();
		return false;
	});
	
	ajax_menu_init($);

	// journal calendar
	journal_calendar_init($);

	// journal switch
	$('.l-body_journal .b-switcher LI A').click(function() {
		if (journalSwitchInterval) clearInterval(journalSwitchInterval);
		var $this = $(this);
		var $li = $this.parent().parent().parent();
		journal_switch($li);
		return false;
	});
	//underline linkks domains
	$(".tdLangC p a")
		.bind("mouseleave", function(){$(this).css("text-decoration","none");})
		.bind("mouseenter", function(){$(this).css("text-decoration","underline");});
	//help in search span
	$('.searchHelp span')
		.bind("mouseleave", function(){$(this).css("background","");})
		.bind("mouseenter", function(){$(this).css("background","url(dot_grey2.gif) 0 100% repeat-x;");}) 
		.bind("click", function(){$('#input_search').val($(this).text());});
	// text hints
	$(".text_hint[value!=]")
		.each(function(){this.hint=this.value})
		/*.bind("focus",function(){if(this.hint==this.value){$(this).attr("value","").removeClass("text_hint")}})*/
		.bind("focus",function(){if(this.value=="Поиск по 250 000 товаров и услуг" || this.value=="Поиск"){$(this).attr("value","")/*.removeClass("text_hint")*/}})
		.bind("blur",function(){if(this.value==""){if($(".text_hint").width()>200){$(this).attr("value","Поиск по 250 000 товаров и услуг").addClass("text_hint")}else{$(this).attr("value","Поиск").addClass("text_hint")}}}) 

	// easing
	$.extend($.easing, {
		swap1: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t + b;
		return c/2*((t-=2)*t*t + 2) + b;
			}
		});

	$(".b-cat__item .ci__pic A").hover(function(){
		$(this).parent().find("A").removeClass("first").removeClass("second")
		$(this).addClass("first").next().addClass("second")
		})

	var menuUL = $(".tabMenu UL")
	var menuLI = menuUL.find("LI")
	
	menuUL.css("display","block")
	menuUL.css("overflow","hidden")
	menuLI.css("display","block")
	menuLI.css("float","left")
	menuLI.css("clear","right")
	
	var k = 100 / menuUL.width()
	var addWidth = (menuUL.width() - menuLI.filter(":last-child").position().left - menuLI.filter(":last-child").width()) / menuLI.length

	menuLI.each(function(){$(this).css("width", ($(this).width() + addWidth) * k + "%")})

	if (!$.browser.msie){
		menuUL.css("overflow","visible")
	menuUL.css("display","table")
	menuLI.css("display","table-cell")
	menuLI.css("float","none")
		}

	var biggerWidth = $(".b-top__item .ti__pic").width();
	$(".b-top__item .ti__pic IMG").each(function(){
		if($(this).width() > biggerWidth)
			biggerWidth = $(this).width();
		})
		.parent().width(biggerWidth)
		
	$(".b-ad-top__tale .link.question").click(function(){$(this).parent().find(".answer").toggleClass("answer_open")});
	
		var height = $(".b-search .mebel-search .body").height();
	$(".b-search .mebel-search .body").height(0);
		$(".b-search .mebel-search .tale .link").toggle(function(){
			$(".b-search .mebel-search").addClass("drop_open").find(".body").stop().animate({height:height},800,'swap1');
$(this).find("SPAN").empty().text("свернуть ");
			return false;
			}, function(){
			$(".b-search .mebel-search").removeClass("drop_open").find(".body").stop().animate({height:0},800,'swap1');
			$(this).find("SPAN").empty().text("развернуть ");			
return false;
			})
	
	$(".b-round_form .t-form .td-like LABEL").click(function(){
		$(".b-round_form .t-form .td-like LABEL.cur").removeClass("cur");
		$(this).addClass("cur");
		});
		
	$(".b-news-list__i LI A").hover(function(){$(this).parent().addClass("cur")},function(){$(this).parent().removeClass("cur")});
	
	$(".b-register__i .first LABEL").find(":radio")
		.each(function(){
			if(!$(this).is(":checked")) 
				$(this).parents().filter(".legal__i, .individual__i").addClass("fade1")
			})
	$(".b-register__i DIV").filter(".legal__i, .individual__i")
		.css("cursor", "pointer")
		.bind('click', function(){
			if ($(this).hasClass("individual__i")) {
				$('.t-form_reg_prodom').hide();
			} else {
				$('.t-form_reg_prodom').show();
			}
		
			if ($(this).hasClass("fade1")){
				$(".b-register__i .first LABEL")
					.find(":radio").removeAttr("checked").end()
					.parents().filter(".legal__i, .individual__i").addClass("fade1").find('UL').addClass('fade')
				$(this)
					.find(".first LABEL :radio").attr("checked","checked").end()
					.removeClass("fade1").find('UL').removeClass('fade')
				}
			})

	$(window).resize(function(){
		$(".b-cat-inpic__item:first-child").each(function(){
			if($(this).width() >= $(this).css("max-width"))
				$(this).css("width", $(this).css("max-width"))
			else
				$(this).css("width", "49.5%")
			})
		})
		
	$(".t-search .td-text > .promt > .link2").click(function(){
		$(this).parent().parent().find("input:text").focus().attr("value", $(this).text())
		return false
		})
	
	$("UL.tabs LI A").click(function(){
	 	var _li = $(this).parents().filter("LI").siblings().removeClass("selected").end().addClass("selected")
		var tabName = _li.attr("class").split(" ")[0]
		var tabIndex = tabName[tabName.length-1]

		$(".tab-content").removeClass("selected").filter(".tab-content"+tabIndex).addClass("selected")
		
		return false
	});
	
	function hoverRange(items, range) {
		if (range == null)
			range = 1;
			
		items.hover(function(){
				var hoverItem = items.index($(this)) + 1;
				var start = 0;
				var end = items.length;
				var halfRange = Math.floor(range / 2)
   
				if (range < end) {
					if (hoverItem <= halfRange + 1) {
						end = range;
					}
					else if ((end - hoverItem) <= halfRange) {
						start = end - range;
					}
					else {
						start = hoverItem - halfRange - 1;
						end = hoverItem + halfRange;
					}
				}
				items.slice(start, end).wrapAll('<SPAN class="hover"><SPAN></SPAN></SPAN>')
			}, 
			function(){
				$(this).unwrap().unwrap()
			})
		}
	
	hoverRange($("#calend .b-calend__i SPAN.active").nextAll("A.day"));
	hoverRange(Array.prototype.reverse.call($("#calend .b-calend__i SPAN.active").prevAll("A.day")));
	
	var imgHeight = 0;
	$(".b-cat__item .ci__pic A > I").each(function(){
		imgHeight = $(this).parent().find("IMG").height()
		$(this)
			.css({
				"height": imgHeight - 2,
				"margin-bottom": -imgHeight})
	}).css("display", "block")
	
	// present
	var selected_link = $(".l-present__panel LI.selected > .link[label!=]")
	if (selected_link) {
		$(".l-present__i DIV[class='" + selected_link.attr('label') + "']").show()
	}

	$(".l-present__panel LI:not('selected') > .link").click(function() {
		selected_link.parent().removeClass("selected")
		$(".l-present__i DIV[class='" + selected_link.attr('label') + "']").hide()
		selected_link = $(this)
		selected_link.parent().addClass("selected")
		$(".l-present__i DIV[class='" + selected_link.attr('label') + "']").show()
	})
	
	$('#gift H3 > .pseudolink').mouseenter(function(){
		var ballon = $(this).parent().next('.b-ballon_type2')
		$('#gift .b-ballon_type2').not(ballon).stopTime().fadeOut('slow')
		ballon.show()
	})	
	
	$('#gift H3 > .pseudolink').mouseleave(function(){
		var ballon = $(this).parent().next('.b-ballon_type2')
		rand_num = Math.floor( Math.random() * 11 )
		ballon.oneTime(2000, rand_num, function(){ballon.fadeOut('slow')})
	})

	$('#stores .l-store-map .map-link > .link').click(function(){
	
		var popup = $(this).parent().next('.d-shadow')
		
		$('#stores .l-store-map .d-shadow').not(popup).hide()
		$('#stores .l-store-map .map-link > .link').not($(this)).find('.b-arr').removeClass('b-arr_up');
		
		$(this).find('.b-arr').toggleClass('b-arr_up')
		popup.toggle()
	})

/*
	$('DL.b-tabs DT').click(function() {
		$(this)
			.siblings().removeClass('selected').end()
			.next('DD').andSelf().addClass('selected');
	})
*/
	
	$('#t-compare .quest').hover(
		function(){
			$(this)
				.find('.b-icon_quest').addClass('b-icon_quest_hov')
				.next('.b-ballon_t3').show();
		},
		function(){
			$(this)
				.find('.b-icon_quest').removeClass('b-icon_quest_hov')
				.next('.b-ballon_t3').hide();
		}
	)	
});
