$(document).ready(function() {
	
	$("#hor-menu li.first-level").hover(function() {
		$($(this).find("a")[0]).addClass("active");
		$("#" + $(this).attr("id") + "-childs").show();
	}, function() {
		$($(this).find("a")[0]).removeClass("active");
		$("#" + $(this).attr("id") + "-childs").hide();
	});
	
	
	
	$("#search-text").focus(function() {
		($(this).val() !== "Поиск по сайту") || $(this).val("");
	}).blur(function() {
		($(this).val() !== "") || $(this).val("Поиск по сайту");
	});

	$('.main_services .service_title').click(function(){
		$('.main_services .mainpage_block').hide();
		$('.main_services .service_title').removeClass('active');
		$(this).addClass('active').parent().children('.mainpage_block').show();
	});

	$('.video_previews .preview a').click(function(){
		$('.video_block').hide();
		$('#video_'+$(this).attr('rel')).show();
		return false;
	});

	
	$("#popup-scheme").fancybox({
		"width" : "75%",
		"height" : "75%",
		"autoScale" : false,
		"transitionIn" : "none",
		"transitionOut" : "none",
		"type" : "iframe"
	});
	
	$("a:has('img')").each(function() {
		var aLink = $(this).attr("href");
		
		if (/\.(jpg|jpeg|gif|png)$/i.test(aLink)) {
			$(this).addClass("fancy-img");
		}
	});
	
	$("a.fancy-img").attr("rel", "gallery-photos-list").fancybox({
		"zoomOpacity" : true,
		"overlayShow" : true,
		"zoomSpeedIn" : 500,
		"zoomSpeedOut" : 500,
		"titlePosition" : "inside",
		"hideOnContentClick" : true,
		"titleFormat" : function(title) {
			var tmp = title.split(" — ");
			return (tmp.length > 1) ? ("<strong>" + tmp[0] + "</strong><br/>" + tmp[1]) : ("<strong>" + title + "</strong>");
		}
	});
	
	/*$("td.center a").bind('click',function(){
		var rel = $(this).attr('rel');
		if (rel) {
			$(this).after($("div#" + rel).html());
		}
	});*/
	$("td.center a.slidedown").toggle(function(){
		var rel = $(this).attr('rel');
		if (rel) {
			$(this).after("<div id='insert_"+rel+"'></div>");
			var insert_div = $("div#insert_"+rel);
			$(insert_div).css({display : 'none'});
			if (!$(this).hasClass('yes'))
				$(insert_div).addClass("description");
			$(insert_div).append($("div#" + rel).html());
			$(insert_div).slideDown();
		}		
	},function(){
		var rel = $(this).attr('rel');
		if (rel) {
			var insert_div = $("div#insert_"+rel);
			$(insert_div).slideUp('normal',function(){$(this).remove()});
			//$(insert_div).remove();
		}				
	});
	$("td.center a.yes").bind('click',function(){
		$("td.center a.no").remove();
		$(this).after("<b>ДА!</b>");
		$(this).remove();
	});
	
	new Ya.share({
        element: 'ya_widget',
		elementStyle: {
			'type': 'button',
			'text': 'Рассказать друзьям',
			'quickServices': ['yaru','vkontakte','twitter','facebook']
		},
		title: $('#ya_widget_title').text(),
		description: $('#ya_widget_description').text(),
		image: 'http://'+window.location.hostname+'/images/logo_new.png',
		popupStyle: {
			blocks: {
				'Рассказать друзьям': ['vkontakte','twitter','yaru','facebook','moikrug', 'moimir', 'myspace', 'lj', 'odnoklassniki', 'blogger' ]
			}
		}
	});
	

	var mX = 0;
	var scrollHorTimer = null;
	var outerGallery = $(".video_previews_slider .video_previews");
	var innerGallery = $(".video_previews_slider .video_previews .video_previews_container");
	var sw = $(outerGallery).width();
	var tw = $(innerGallery).width();
	var nx = parseInt($(innerGallery).css("left"), 10);

	var scrollThumbsHor = function() {
		if (scrollHorTimer) {
			if (tw > sw) {
				if (mX < (sw / 2)) {
					nx = nx + 1 + (sw / 2 - mX) / 70;
				} else if (mX > (sw / 2)) {
					nx = nx - 1 - (mX -  sw / 2) / 70;
				}
				if (nx > 0) {
					nx = 0;
				} else if (nx < -(tw - sw)) {
					nx = -(tw - sw);
				}
				$(innerGallery).css("left", nx + "px");
			}
		}
	};


	$(".video_previews_slider .video_previews").mousemove(function(e) {
		mX = e.pageX - parseInt($(this).offset().left, 10);
	}).mouseover(function() {
		scrollHorTimer = window.setInterval(scrollThumbsHor, 20);
	}).mouseout(function() {
		clearInterval(scrollHorTimer);
	});
	$('.video_previews_slider .slide-left').mousedown(function(e){
		mX = e.pageX - parseInt($(this).offset().left, 10);
		scrollHorTimer = window.setInterval(scrollThumbsHor, 20);
	}).mouseup(function(){
		clearInterval(scrollHorTimer);
	});
	$('.video_previews_slider .slide-right').mousedown(function(e){
		mX = e.pageX;
		scrollHorTimer = window.setInterval(scrollThumbsHor, 20);
	}).mouseup(function(){
		clearInterval(scrollHorTimer);
	});

});

