$(document).ready(function(){

	$("input:checkbox, input:radio, input:text, select").uniform();
	
	$(".fancybox").fancybox({
		padding: 0,
		margin: 13,
		width:727,
		minWidth : 727,
		minHeight: 50,
		autoSize : true,
		closeClick : false,
		openEffect : "none",
		closeEffect	: "none",
		topRatio: 0
	});	
	$(".fancybox2").fancybox({
		padding: 0,
		margin: 13,
		minHeight: 100,
		autoSize : true,
		closeClick : false,
		closeBtn: false,
		openEffect : "none",
		closeEffect	: "none",
		topRatio: 0.3,
		helpers:{overlay:{opacity:0.2}}
	});

	
	$("#login-dlg").tabs({
		//select: function(event, ui){
			//$("#rules").tabs("option", "selected", -1);
		//},
		collapsible:false,
		selected:0
	});
	$("#accordion").accordion({
			autoHeight: false,
			navigation: true,
			collapsible: true,
			create: function(event, ui){
				//$(".fancybox").fancybox().update;
				$("#accordion").accordion("activate", false);
			}
	});
	
		
	$("#info").tabs({select: function(event, ui){
			$("#rules").tabs("option", "selected", -1);
		},
		collapsible: true, selected: -1, fx:{opacity:"toggle"}
	});
	$("#rules").tabs({select: function(event, ui){
			$("#info").tabs("option", "selected", -1);
		},
		collapsible: true, selected: -1, fx:{opacity:"toggle"}
	});
	
	var current = 0;
	$.validator.addMethod("pageRequired", function(value, element){
		var $element = $(element)
		function match(index){
			return current == index && $(element).parents("#tabs-3"+(index+1)).length;
		}
		if (match(0) || match(1) || match(2) || match(3) || match(4)) return !this.optional(element);
		return "dependency-mismatch";
	}, $.validator.messages.required);
	
	var v = $("#tForm").validate({
	//	debug: true,
		errorElement: "div",
		errorClass: "error",
		onkeyup: false,
		onblur: false,
		errorPlacement: function(error, element){
     		error.insertBefore(element);
			$("div.error").wrapInner("<p />");
			$("<em></em>").insertBefore("div.error p");
   		}
		//submitHandler: function() {
		//	alert("Submitted, thanks!");
		//}
	});
	
	if($("div").is("#steps")){	
		var root = $("#steps").scrollable();
		var scrolpane = root.scrollable();
		
		scrolpane.onBeforeSeek(function(event, i){
			if (scrolpane.getIndex() < i){
				if(v.form()){
					current = scrolpane.getIndex()+1;	
					if(scrolpane.getIndex() == 0){
						var currency = "USD", destination;
						switch ($("#select-dlg input:checked").val()){
							case "Z1":
								currency = "EUR";
								destination = $("#select-dlg input:checked").parent().parent().parent().text();
							break;
							case "Z2": destination = $("#select-dlg input:checked").parent().parent().parent().text();
							break;
							case "Z3": destination = $("#select-dlg input:checked").parent().parent().parent().text();
							break;
							case "Z4": destination = $("#select-dlg input:checked").parent().parent().parent().text();
							break;
							case "Z5": destination = $("#select-dlg input:checked").parent().parent().parent().text();
							break;
						}
						$(".destination").text(destination);
						$(".amount").text($("#isumm input:checked").val()+" "+currency);
						$(".amount2").text(currency+" "+$("#isumm input:checked").val());
						$(".curr").text(currency);
						//$(".date-from").text($("#policy_travel_abroad_new_travel_period_from").val());
						//$(".date-to").text($("#policy_travel_abroad_new_travel_period_to").val());
					}
				}
				else return false;
			}
			$("#status li").removeClass("active").eq(i).addClass("active");
		});
		
		root.find("button.next").keydown(function(e){
			if (e.keyCode == 9){
				scrolpane.next();
				e.preventDefault();
			}
		});
	}
		
	
    jwplayer("cfplayer12").setup({
                flashplayer: "js/player.swf",
				file: "video/12.mp4",
				image: "video/12.jpg",
                autostart: false,
                height: 340,
                width: 640,
                skin: "js/skins/whotube.zip",
                screencolor: "ffffff",
	});
	jwplayer("cfplayer13").setup({
                flashplayer: "js/player.swf",
				file: "video/13.flv",
				image: "video/13.jpg",
                autostart: false,
                height: 340,
                width: 640,
                skin: "js/skins/whotube.zip",
                screencolor: "ffffff",
	});
	
		
	$("#rec").click(function(){
		var i = $("#current").val();
		if(i == 5) i = 0;
		else i++;
		$("#current").val(i);
		$("html").css("background", "url(../images/bg"+i+".jpg) no-repeat center center fixed");
	});
	
	
	// Change destination
		var maps = {
			"Z1": "europe",
			"Z2": "egypt",
			"Z3": "asia",
			"Z4": "africa",
			"Z5": "america"
		};
		
	function switchDirection(obj){
		$("#direction p").find("img:eq(0)").attr("src", "images/"+maps[$(obj).val()]+".png");
			$("#isumm").find("label > span").each(function(){
				if($(obj).val() == "Z1"){
					$(this).text("EUR");
					$("#shengen").css("visibility", "visible");
				}
				else {
					$(this).text("USD");
					$("#shengen").css("visibility", "hidden");
				}
				var radio = $(this).parent().find("div > span > input");
				var li = $(radio).parent().parent().parent().parent();
				if ($(obj).val() == "Z1"){
					if ($(radio).val() != 30000){
						$(radio).removeAttr("checked", "disabled");
						$(radio).parent().removeClass("checked");
						$(li).hide("slide");
					}
					else {
						$(radio).attr("checked", "checked");
						$(radio).parent().addClass("checked");
					}
				}
				else {
					$(radio).removeAttr("disabled");
					$(li).show("slide");
				}
			});
	}
	
	$("#select-dlg input").change(function(){
		switchDirection(this);
		$.fancybox.close();
	});

	$("#shengen-close").click(function(){
		$("#shengen").css("visibility", "hidden");
	});
	
	// --- { Datepicker for travel period: from/to }		
	$("#travel_from_dp").datepicker({
		dateFormat: "dd-mm-yy",
		minDate: "-0",
		altField: ".date-from",
		altFormat: "dd MM yy",
		onSelect: function(dateText, inst){
			$("#policy_travel_abroad_new_travel_period_from").val(dateText);
			$("#travel_to_dp").datepicker("option", "minDate", dateText);
		}
	});
		
	$("#travel_to_dp").datepicker({
		dateFormat: "dd-mm-yy",
		minDate: "-0",
		altField: ".date-to",
		altFormat: "dd MM yy",
		onSelect: function(dateText, inst){
			$("#policy_travel_abroad_new_travel_period_to").val(dateText);
			$("#travel_from_dp").datepicker("option", "maxDate", dateText);
		}
	});
	
	
	
	
	
	function wfd_wi_customer_birthday_date_read_linked(){
		$("#wi_customer_birthday_date_jquery_control").val($("#wi_customer_birthday_date_year").val()+"-"+$("#wi_customer_birthday_date_month").val()+"-"+$("#wi_customer_birthday_date_day").val());
		return {};
	}
	  
	function noLeadingZero(num){
		return num.replace(/^0+/, '');
	}
	function month2letter(num){
		switch(num){
			case "1": num = "января";
			break;
			case "2": num = "февраля";
			break;
			case "3": num = "марта";
			break;
			case "4": num = "апреля";
			break;
			case "5": num = "мая";
			break;
			case "6": num = "июня";
			break;
			case "7": num = "июля";
			break;
			case "8": num = "августа";
			break;
			case "9": num = "сентября";
			break;
			case "10": num = "октября";
			break;
			case "11": num = "ноября";
			break;
			case "12": num = "декабря";
			break;
		}
		return num;
	}
	
	function wfd_wi_customer_birthday_date_update_linked(date){
		$("#wi_customer_birthday_date_year").val(date.substring(0, 4));
		$("#uniform-wi_customer_birthday_date_year span").text(date.substring(0, 4));
		$("#wi_customer_birthday_date_month").val(noLeadingZero(date.substring(5, 7)));
		$("#uniform-wi_customer_birthday_date_month span").text(month2letter(noLeadingZero(date.substring(5, 7))));
		$("#wi_customer_birthday_date_day").val(noLeadingZero(date.substring(8)));
		$("#uniform-wi_customer_birthday_date_day span").text(noLeadingZero(date.substring(8)));
	}	

	$("#wi_customer_birthday_date_jquery_control").datepicker({
		dateFormat: "yy-mm-dd",
		minDate:    new Date(1942, 1 - 1, 1),
		maxDate:    new Date(1994, 12 - 1, 31),
		beforeShow: wfd_wi_customer_birthday_date_read_linked,
		onSelect:   wfd_wi_customer_birthday_date_update_linked,
		showOn:     "button",
		buttonText: "...",
		changeMonth:true,
		changeYear:true,
		showOtherMonths:true,
		selectOtherMonths:true,
		showButtonPanel:true,
		yearRange:'c-70:c',
		firstDay:1
	});


	function wfd_wi_customer_birthday_date_check_linked_days(){
		var daysInMonth = 32 - new Date($("#wi_customer_birthday_date_year").val(), $("#wi_customer_birthday_date_month").val() - 1, 32).getDate();    
		if ($("#wi_customer_birthday_date_day").val() > daysInMonth) $("#wi_customer_birthday_date_day").val(daysInMonth);
	}
	$("#wi_customer_birthday_date_day, #wi_customer_birthday_date_month, #wi_customer_birthday_date_year").change(wfd_wi_customer_birthday_date_check_linked_days);
	
	
	
	
	
	
	
	
	function personchanger(first, cur){
		if(first < cur){
			for(i=(first+1); i<=cur; i++){
				$(".wrap_selectors").eq(i).slideToggle("slow");
			}
		}
		else {
			for(i=first; i>cur; i--){
				$(".wrap_selectors").eq(i).slideToggle("slow");
			}
		}	
	}
	
	$("#insured_line span").click(function(){
		var f = $("#insured_line span").filter(".active").last().index();
		var j = $(this).index();
		$("#insured_line span").removeClass("active").filter(function(index){
			return index < (j+1);
		}).addClass("active");
		$("#insured_qty").text("x"+(j+1));
		personchanger(f, j);
	});
	
	$("#policy_travel_abroad_new_is_me_insured").change(function(){
		if($(this).attr("checked")){
			$("#policy_travel_abroad_new_is_not_insured").removeAttr("checked").parent().removeClass("checked");
		}
	});
	$("#policy_travel_abroad_new_is_not_insured").click(function(){
		if($(this).attr("checked")) $("#policy_travel_abroad_new_is_me_insured").removeAttr("checked").parent().removeClass("checked");
		else $("#policy_travel_abroad_new_is_me_insured").attr("checked", "checked").parent().addClass("checked");
	});
	
	// жонглируем в п. 3-2 Выбор полиса и услуг
	$(".btn").click(function(){
		if($(this).parent().parent().parent().parent().hasClass("addon")){
			$(this).val("отменить");
			$(this).parent().parent().appendTo(".base tbody");
		}
		else {
			$(this).val("добавить");
			$(this).parent().parent().appendTo(".addon tbody");
		}
	});
	
	$("#autocomplete_addrRegForm_city").autocomplete({
		source: "https://www.finshop.com.ua/admter/cities.html",
		minLength: 3
	});
	
	
	
	
	
	
	
});
