$.fn.exists = function () {
    return $(this).length !== 0;
};

$.fn.popupOpen = function(options) {
	var settings = {
		el:'',
		tab:''
	};
	settings = $.extend(settings, options);

	var apiTabs = $(".b-tabs-nav").data("tabs");
	
$(".b-popup-nav__link").eq(settings.el).addClass("b-popup-nav__link_active");
	$(".b-popup-i").eq(settings.el).show();
	if (settings.tab !== '') {
		apiTabs.click(settings.tab);
	};
};


$(document).ready(function(){
     
     $('div.h-doctorsList a.doctor-link').hover(function(){
	    $(this).find('img.grey-image').css('z-index', 1);
     }, 
     function(){
		$(this).find('img.grey-image').css('z-index', 3);
     });
	
	// Определение мобильников
	var userag = navigator.userAgent.toLowerCase();
	var isAndroid = userag.indexOf("android") > -1; 
	
	if ( (navigator.userAgent.indexOf('iPhone') != -1) || (navigator.userAgent.indexOf('iPad') != -1) ||  isAndroid ) {
		$('html, body, .l-body').css({'height': 'auto'});
		$('.b-hdr-phones').css({'margin-right' : '20'});
	}
	
	//console.log(screen.height);
	if(screen.height <= 768)
	{
		document.getElementById("flash_object").setAttribute("height", "605");
		document.getElementById("flash_embed").setAttribute("height", "605");
		//$('.l-cnt_index .l-wrap').addClass('min_height');
	}
	
	if ($("#js-bg").exists()) {
		var $bg = $("#js-bg"),
			$body = $("body"),
			body_w,body_h,body_ratio,bg_w,bg_h,bg_ratio;
		function bgsize(){
			body_w = $body.width();
			body_h = $body.height();
			body_ratio = body_w/body_h;
			bg_w = $bg.width();
			bg_h = $bg.height();
			bg_ratio = bg_w/bg_h;
			if (body_ratio > bg_ratio) {
				$bg.width(body_w);
				$bg.height(body_w/bg_ratio);
			} else
			if (body_ratio < bg_ratio) {
				$bg.height(body_h);
				$bg.width(body_h*bg_ratio);
			};
		};
		$(window).load(function(){
			bgsize();
		});
		$(window).resize(function(){
			bgsize();
		});
	};
	if ($(".editable-select").exists()) {
		$('.editable-select').editableSelect();
	};
	if ($(".b-region__trigger").exists()) {
		$(".b-region").click(function(event){
			event.stopPropagation();
		});
		$('.b-region__trigger').click(function() {
			$(".b-region-list").toggle();
		});
	};
	if ($(".b-news").exists()) {
		$(".b-news__trigger").click(function(){
			$(".b-news").toggleClass("b-news_open");
		});
	};
	if ($(".b-wheel").exists()) {
		$(".b-wheel-i").hover(function(){
			$(this).toggleClass("b-wheel-i_hover");
		});
	};
	if ($(".b-tabs").exists()) {
			$(".b-tabs-nav").tabs(".b-tabs-i", {
				current: 'b-tabs-nav__link_active'
			}); 
			var apiTabs = $(".b-tabs-nav").data("tabs");
		};
		if ($(".b-popup").exists()) {
			$(".b-popup-i").click(function(event){
				if ($(event.target).not(".editable-select")) {
					event.stopPropagation();
				};
			});
			$(".b-popup-nav__link").click(function(){
				var $this = $(this),
					id_this = $this.parent().index(),
					$popups = $(".b-popup").find(".b-popup-i"),
					$popup = $popups.eq(id_this);
				if ($(".b-popup-nav__link_active").exists() && $(".b-popup-nav__link_active").parent().index() !== id_this) {
					$(".b-popup-nav__link_active").removeClass("b-popup-nav__link_active");
					$(".b-popup-i").hide();
					$this.toggleClass("b-popup-nav__link_active");
					$popup.toggle();
					return false;
				}
				else {
					$this.toggleClass("b-popup-nav__link_active");
					$popup.toggle();
					return false;
				};
			});
		};
	if ($(".b-hospitalsSelect").exists()) {
		$(".b-hospitalsSelect").each(function(){
			var $this = $(this),
				$trigger = $this.children(".b-hospitalsSelect-trigger"),
				$list = $this.children(".b-hospitalsSelect-lists"),
				$links = $this.find("a"),
				$link_active = $this.find("a.b-hospitalsSelect-lists-list__active");
			$trigger.click(function(){
				$(".b-hospitalsSelect-lists").not($list).hide();
				$list.toggle();
			});
			$this.mouseleave(function(){
				timer = setTimeout(function(){
					$list.hide();
				}, 2000);
			});
			$this.mouseenter(function(){
				clearTimeout(timer);
			});
			$this.click(function(event){
				event.stopPropagation();
			});
			if ($link_active.exists()) {
				$trigger.val($link_active.text());
			} else {
				$link_active = $links.eq(0);
				$link_active.addClass("b-hospitalsSelect-lists-list__active");
				$trigger.val($link_active.text());
			};
			$links.click(function(){
				var $this = $(this);
				if ($link_active.exists()) {
					$link_active.removeClass("b-hospitalsSelect-lists-list__active");
				};
				$this.addClass("b-hospitalsSelect-lists-list__active");
				$trigger.val($this.text());
				$link_active = $this;
				//return false;
			});
		});
	};
	
	
	function openTab(index, link)
	{
		$('.b-inPage .b-tabs-nav_ a').removeClass('b-tabs-nav__link_active');
		
		$(link).addClass('b-tabs-nav__link_active');
		
		$('.b-inPage .b-tabs-i').hide();
		$('.b-inPage .b-tabs-i').eq(index).show();
		
		$('.b-inPage ul.b-snav').hide();
		$('.b-inPage ul.b-snav').eq(index).show();
	}
	
	$('.b-tabs-nav__link').click(function(){
		$('div.editable-select-options').hide();
	});
	
	//console.log(window.location.hash);
	if (window.location.hash != ''){
		var i = window.location.hash.substr(1);
		var link = $('.b-tabs-nav_ a').eq(i);
		openTab(i, link);
	}
	
	
	$('.b-inPage .b-tabs-nav_ a').click(function(){
		var i = $('.b-inPage .b-tabs-nav__link').index(this);
		openTab(i, $(this));
		document.title = "МЕДСИ | Услуги | " + $(this).text();
		//return false;
	});
	
	
	$('#form_2').click(function(){
		
		$(this).popupOpen(2, 2);
		return false;
	});
	
	$('html').click(function() {
		$(".b-hospitalsSelect-lists, .b-region-list, .b-popup-i").hide();
		$(".b-popup-nav__link_active").removeClass("b-popup-nav__link_active");
	});
});


