$(document).ready(function() {

	browser();
	fields_text();
	menu_control();
	galleryScroll();
});

function galleryScroll() {	
	$(".store_f_slide_next").click(function(){
		
		$(".store_f_slide_list_wrap").animate({ scrollTop: $(".store_f_slide_list_wrap").scrollTop()+150 }, 500);
		
	})
	$(".store_f_slide_prev").click(function(){		
		
		$(".store_f_slide_list_wrap").animate({ scrollTop: $(".store_f_slide_list_wrap").scrollTop()-150 }, 500);
	})
}
function menu_control() {
	/*
	$(".c_nav_link",'#catalog_nav').each(function() {
		$(this).replaceWith("<span class='c_nav_link'>" + $(this).text() + "</span>");
	});
	*/
	$(".c_nav_link",'#catalog_nav').live('click', function() {		
		if ($(this).parent().hasClass("open")) {
			$(this).parent().find('ul').slideUp(400);
			$(this).parent().removeClass("open");
		}
		else {
			$(this).parent().addClass("open");
			$(this).parent().find('ul').slideDown(400);
		}
		return false;
	});
}

function browser(){
	var u = navigator.userAgent.toLowerCase(),

			is = function(t) {return (u.indexOf(t) != -1)};

	$("html").addClass([

		(!(/opera|webtv/i.test(u)) && /msie (\d)/.test(u)) ? ('ie ie' + RegExp.$1)

				: is('firefox/2') ? 'gecko ff2'

				: is('firefox/3') ? 'gecko ff3'

				: is('firefox/4') ? 'gecko ff4'

				: is('firefox/5') ? 'gecko ff5'

				: is('gecko/') ? 'gecko'

				: is('opera/9') ? 'opera opera9' : /opera (\d)/.test(u) ? 'opera opera' + RegExp.$1

				: is('konqueror') ? 'konqueror'

				: is('applewebkit/') ? 'webkit safari'

				: is('mozilla/') ? 'gecko' : '',

		(is('x11') || is('linux')) ? ' linux'

				: is('mac') ? ' mac'

				: is('win') ? ' win' : ''

	].join(''));
}

function fields_text(){
	$("input[type='text'], textarea").each(function() {
		if ($(this).attr("value") == "")
			$(this).attr("value", $(this).attr("title"))
	});

	$("input[type='text'], textarea").focus(function() {
		if ($(this).attr("value") == $(this).attr("title"))
			$(this).attr("value", "")
	});

	$("input[type='text'], textarea").blur(function() {
		if ($(this).attr("value") == "")
			$(this).attr("value", $(this).attr("title"))
	});
}

//		var start = new Date();
// 		var end = new Date();
//		alert('Скорость ' + (end.getTime()-start.getTime()) + ' мс');

function soc_bubble_popup(){

	$(".soc_trigger").click(function() {
		if($(this).parent().hasClass('active_bubble_w')){
			$(this).parent().removeClass('active_bubble_w');
			$(this).parent().find('div').fadeOut(200).removeClass('active_bubble');
			$(this).parent().parent().parent().removeClass('active_bubble_wrap');
		}
		else{
			if($(this).parent().parent().parent().hasClass('active_bubble_wrap')){
				$('.active_bubble').each(function() {
					$(this).removeClass('active_bubble').fadeOut(200);
					$(this).parent().removeClass('active_bubble_w');
				});
			}
			else
			{
				$('.active_bubble').each(function() {
					$(this).removeClass('active_bubble').fadeOut(200);
					$(this).parent().removeClass('active_bubble_w');
					$(this).parent().parent().parent().removeClass('active_bubble_wrap');
				});
			}

			$(this).parent().addClass('active_bubble_w');
			$(this).parent().find('div').fadeIn(200).addClass('active_bubble');
			$(this).parent().parent().parent().addClass('active_bubble_wrap');
		}
		return false;
	});

	$(".active_bubble").live('mouseenter',function() {
		$(this).addClass('bubble_hover');
	});

	$(".bubble_hover").live('mouseleave',function() {
		$(this).removeClass('bubble_hover');
	});

		$("#deals_list").click(function() {
			$('.active_bubble').each(function() {
				if ($(this).hasClass('bubble_hover')) {
					return true;
				}
				else {
					$(this).removeClass('active_bubble').fadeOut(200);
					$(this).parent().removeClass('active_bubble_w');
					$(this).parent().parent().parent().removeClass('active_bubble_wrap');
				}
			});
			return true;
		});
}

function filter_anim2() {
	var allwidth = 0,
		filter_list = $(".filter_ver_2 #filter_list"),
		filter_list_wrap = $(".filter_ver_2 #filter_list_wrap"),
		filter_list_wrap_width = filter_list_wrap.width(),
		filter_list_c = filter_list.children(),
		filter_list_c_length = filter_list_c.length;

	for (allwidth, test=1, i = 1; i <= filter_list_c_length; i++)
	{
		test = $(".filter_ver_2 #filter_item_"+i).width();
		allwidth =	allwidth+test;
	}

	$(filter_list).css('width',allwidth);

	if(allwidth > filter_list_wrap_width){
		var difference = allwidth - filter_list_wrap_width;
		$(".filter_ver_2 #filter_prev").hover(
			function () {
				$(filter_list).animate({
					right:-difference,
					left: 0
				}, 700, "linear");
			},
			function () {
				$(filter_list).stop();
			});
		$(".filter_ver_2 #filter_next").hover(
			function () {
				$(filter_list).animate({
					left:-difference,
					right: 0
				}, 700, "linear");
			},
			function () {
				$(filter_list).stop();
			});
		}
}

function filter_anim() {
	//Get our elements for faster access and set overlay width
	var div = $('.filter_ver_1 #filter_list_wrap'),
			ul = $('.filter_ver_1 .filter_list'),
		// unordered list's left margin
			ulPadding = 15;

	//Get menu width
	var divWidth = div.width();
	//Remove scrollbars
	div.css({overflow: 'hidden'});

	//Find last image container
	var lastLi = ul.find('li:last-child');

	//When user move mouse over menu
	div.mousemove(function(e) {

		//As images are loaded ul width increases,
		//so we recalculate it each time
		var ulWidth = lastLi[0].offsetLeft + lastLi.outerWidth() + ulPadding;

		var left = (e.pageX - div.offset().left) * (ulWidth - divWidth) / divWidth;
		div.scrollLeft(left);
	});
}

function search_anim(){

	$("#f_search_trigger").live('click', function() {
	$("#search_butt").css('display','block');
		$("#filter_search_block").animate({
						width: 935
					}, 400, "linear");
		$("#filter_list_wrap").animate({
						height: 0
					}, 400, "linear");
	});

	$("#search_block_close").live('click', function() {
		$("#search_butt").css('display','none');
		$("#filter_search_block").animate({
						width: 0
					}, 400, "linear");
		$("#filter_list_wrap").animate({
						height: 37
					}, 400, "linear");
	});


}

function form_slider() {

	$("#slider-price").slider({
		range: true,
		min: 0,
		step: 50,
		max: 8000,
		values: [ 300, 5000 ],
		slide: function(event, ui) {
			$("#amount_price_to").text(ui.values[ 1 ]);
			$("#amount_price_from").text(ui.values[ 0 ]);
		}
	});
	$("#amount_price_from").text($("#slider-price").slider("values", 0));
	$("#amount_price_to").text($("#slider-price").slider("values", 1));

	$("#slider-percent").slider({
		range: true,
		min: 0,
		step: 10,
		max: 100,
		values: [ 0, 100 ],
		slide: function(event, ui) {
			$("#amount_percent_from").text(ui.values[ 0 ]);
			$("#amount_percent_to").text(ui.values[ 1 ]);
		}
	});
	$("#amount_percent_from").text($("#slider-percent").slider("values", 0));
	$("#amount_percent_to").text($("#slider-percent").slider("values", 1));
}

function deal_section_list() {
// скрытые при загрузке блоки
//	s_l_menu_depiction
//	s_l_menu_comment
//	s_l_menu_debate
//	s_l_menu_map
	var selector = 's_l_menu_depiction';
	var active = $('#'+selector);
	var active_wrap = $('#'+selector+'_wrap');
	var s_l_menu = $("li[id^=s_l_menu_]");

	$(active).addClass('select');

	$("div[id^=s_l_menu_]").each(function() {
		var elm_id = this.id;
		if ( elm_id != selector+'_wrap') {
			$(this).hide();
		}
	});
	
	$(s_l_menu).live('click', function() {
		// s_l_menu_%NAME%
		// s_l_menu_%NAME%_wrap
		var el = this.id.split('_')[3];
		var elm_id = this.id;

		$(s_l_menu).each(function() {
			if ($(this).hasClass('select')) {
				var el_select = this.id.split('_')[3];
				$('#s_l_menu_' + el_select + '_wrap').hide();
			}
			if (this.id != elm_id) {
				$(this).removeClass('select');
			}
		});

		$('#s_l_menu_' + el + '_wrap').show();
		$(this).addClass('select');
	});
}

function question(){
	$(".question_wrap").click(function(){
		if($(this).hasClass("question_closed"))
		{
			$(this).removeClass("question_closed");
			$(this).addClass("question_opened");
			$(this).find(".answers_list").slideDown(400);
		}
	});

	$(".item_comments .status").click(function()
	{
		var comments_item = $(this).closest(".question_wrap");
		if(comments_item.hasClass("question_opened"))
		{
			comments_item.find(".answers_list").slideUp(200, function(){comments_item.removeClass("question_opened"); comments_item.addClass("question_closed");} );
		}
	});

}

function carusel_img(){

	$('#static_carousel').slides({
//		preload: true,
		preloadImage: 'i/loading.gif',
		play: 5000,
		pause: 2500,
		hoverPause: true
	});
}



function select_rediz(){

	$('#region_wrap, #region_wrap .link, #subway_wrap, #subway_wrap .link, #shares_wrap, #shares_wrap .p_stat_link, #shares_wrap .p_coupons_link').click(function() {
		return false;
	});

	$('#subway').live('change', function() {
		$('#subway_wrap .link').text($('option[value="' + this.value + '"]', this).text());
	});

	$('#region').live('change', function() {
		$('#region_wrap .link').text($('option[value="' + this.value + '"]', this).text());
	});

	$('#shares').live('change', function() {
		$('#shares_wrap .p_stat_link').text($('option[value="' + this.value + '"]', this).text());
	});

	$('#shares').live('change', function() {
		$('#shares_wrap .p_coupons_link').text($('option[value="' + this.value + '"]', this).text());
	});
}

function datepicker() {
	$.datepicker.setDefaults(
			$.extend($.datepicker.regional["ru"])
	);

	$("#datepicker, #datepicker2").datepicker({
		showOn: "button",
		buttonImage: "i/calendar.png",
		dateFormat: 'dd.mm.yy',
		buttonImageOnly: true
	});
}

function popup() {

	$("#send_request").click(function() {
		var top_h = $(document).scrollTop();
		var window_h = $(window).height();
		$("#popup_main").fadeIn(200);
		var popup_h = $("#popup").height();
		var test_h = window_h + top_h;
		if(popup_h > test_h){
			$("#popup").css('top', '10px');
		}
		else{
			var real_top_h = top_h + (window_h - popup_h)/2;
			$("#popup").css('top', real_top_h);
		}
		return false;
	});

	$("#popup_close").click(function() {
		$("#popup_main").fadeOut(200);
	});

}
