$(document).ready(function(){
	$("#mcfglist").jcarousel({
		wrap: 'circular',
		scroll: 1,
		buttonPrevHTML: null
	});
	$("#hcslider").slides({
			preload: true,
			preloadImage: 'img/loading.gif',
			container: 'hcsinner',
			play: 5000,
			pause: 2500,
			hoverPause: true
	});
	$(".hsmtitem a").bind("mouseover", function(){
		var n = $(this).attr("id").replace("hsmti", "");
		$("#mPoint"+n).addClass("selected");
	});
	$(".hsmtitem a").bind("mouseout", function(){
		$(".mPoint").removeClass("selected");
	});
	$("#popup_wrapper form").bind("submit", function(){
		return formProcess($(this).attr("id"));
	});
	$(".hcr_title").bind("click", function(){
		// Записаться на прием
		popupShow('webform-form-111');
		return false;
	});
	$(".hscall a").bind("click", function(){
		// Мы вам перезвоним
		popupShow('webform-form-110');
		return false;
	});
	
	$(window).resize(resize_func);
});

function GetDefaultText(textboxid, defaultText){
	textbox = document.getElementById(textboxid);
	if (textbox.value == ""){textbox.value = defaultText;} 
}
function GetClear(textboxid, valueText){
	textbox = document.getElementById(textboxid);
	if (textbox.value == valueText){textbox.value = '';}
}
function getClientWidth(){
	var w = document.compatMode=='CSS1Compat' && !window.opera ? document.documentElement.clientWidth : document.body.clientWidth;
	if (document.compatMode == "CSS1Compat" && window.opera) w = window.innerWidth;
	return w;
}
function getClientHeight(){
	var h = document.compatMode=='CSS1Compat' && !window.opera ? document.documentElement.clientHeight : document.body.clientHeight;
	if (document.compatMode == "CSS1Compat" && window.opera) h = window.innerHeight;
	return h;
}
function popupShow(frmId){
	document.getElementById('popup_error').innerHTML = '';
	document.getElementById('popup_overlay').style.display = 'block';
	document.getElementById('popup_wrapper').style.display = 'block';
	document.getElementById(frmId).style.display = 'block';
	resize_func();
	switch (frmId){
		case 'webform-form-111':
				$("#popup_header div").html('Записаться на прием');
				break;
		case 'webform-form-110':
				$("#popup_header div").html('Мы вам перезвоним');
				break;
	}
	$(document).bind('keydown popup', function(e){if (e.keyCode == 27){popupHide();}});
	$("#close_popup").bind("click", function(){popupHide();});
}
function popupHide(){
	document.getElementById('popup_wrapper').style.display = 'none';
	document.getElementById('popup_overlay').style.display = 'none';
	$("#popup_wrapper form").css("display", "none");
	$(document).unbind('keydown popup');
}

function formProcess(frmId){
	var result = true;
	if (frmId == 'webform-form-111'){
		// Форма "Записаться на прием"
		var fio, phone, time, doc, info, error = false, emsg = '';
		fio   = $("#submitted[popup_frm1_fio]").val();
		phone = $("#submitted[popup_frm1_phone]").val();
		time  = $("#submitted[popup_frm1_time]").val();
		doc   = $("#submitted[popup_frm1_doc]").val();
		dop   = $("#submitted[popup_frm1_dop]").val();
		if (fio == ''){
			if (error) {emsg += ', ';}
			emsg += 'Ф.И.О.';
			error = true;
		}
		if (phone == ''){
			if (error) {emsg += ', ';}
			emsg += 'Телефон';
			error = true;
		}
		if (error){
			$("#popup_error").html("Заполните поля "+emsg);
		}
		result = !error;
	}
	else if(frmId == 'webform-form-110'){
		// Форма "Мы вам перезвоним"
		var fio, phone, time, info, error = false, emsg = '';
		fio   = $("#submitted[popup_frm2_fio]").val();
		phone = $("#submitted[popup_frm2_phone]").val();
		time  = $("#submitted[popup_frm2_time]").val();
		dop   = $("#submitted[popup_frm2_dop]").val();
		if (fio == ''){
			if (error) {emsg += ', ';}
			emsg += 'Ф.И.О.';
			error = true;
		}
		if (phone == ''){
			if (error) {emsg += ', ';}
			emsg += 'Телефон';
			error = true;
		}
		if (error){
			$("#popup_error").html("Заполните поля "+emsg);
		}
		result = !error;
	}
	if (result){
		
	}
	return false;
}

var resize_func = function(){
	var window_width = parseInt($(window).width());
	var window_height = parseInt($(window).height());
	var content_width = parseInt($("#popup_wrapper").outerWidth());
	var content_height = parseInt($("#popup_wrapper").outerHeight());
	var top = parseInt((window_height - content_height) / 2);
	var left = parseInt((window_width - content_width) / 2);
	if(window_width < content_width) left = 0;
	if(window_height < content_height) top = 0;
	$("#popup_wrapper").css({"left": left, "top": top});
}
