$(function() {

  var inputs = $(".search input[type='text'], .search2 input[type='text']");
  inputs.focus(function(){
      $("label[for='"+$(this).attr('id')+"']").hide();
      });
  inputs.blur(function(){
      var that = $(this);
      if (that.val() == ""){
      $("label[for='"+that.attr('id')+"']").show();
      }
      });
  inputs.blur();

  $(".b-filter-inner").hide();
  $(".filter-open").bind("click", function() {
    $(".b-filter-inner").slideDown();
    $(".filter-open").css({"display": "none"});
    return false;
    });

  if($.browser.msie && parseInt($.browser.version) == 6 ){ 
    $(document).ready(function() {
      $(".nav-level2").each(function() {$(this).css({"width": $(this).parents(".nav li").width()+12})});
      $(".nl2-inner2").each(function() {$(this).css({"width": $(this).parents(".nav-level2").width()})});
      $(".nav li")
      .bind("mouseenter", function() {
        $(this).addClass("nav-sphover");
      })
      .bind("mouseleave", function() {
        $(this).removeClass("nav-sphover");
      });
      
    })
  }

  $(".b-reg-form").hide();
  $(".reg-link").bind("click", function() {
      $(".b-reg").hide();
      $(".b-reg-form").show();
      return false;
      });
  $(".reg-close").bind("click", function() {
      $(".b-reg-form").hide();
      $(".b-reg").show();
      });

  $(".faq .answer").hide();
  $(".quest span").bind("click", function() {
      $(this).parents(".faq").toggleClass("ans-open");
      if($(this).parents(".faq").hasClass("ans-open")){
      $(".answer", $(this).parents(".faq")).show();
      }
      else{
      $(".answer", $(this).parents(".faq")).hide();
      }
      return false;
      })
    if ($(".send-btn").length) {
		$('#error').hide();
		$('.b-formfield_input').removeClass('form-error');
	}

	$(".send-btn").click(function() {
	  	if (login_errors()) return false;
	})
});

function login_errors() {
	var res = 0;
  	$('#error').hide();
	$('.b-formfield_input').removeClass('form-error');
  	if($('#email').val()=='') {
  		$('.form_result_error2').hide();
  		$('#email').parent().addClass('form-error');
		$('#error').show();
		res++;  		                       	
  	}
  	if($('#password').val()=='') {
  		$('.form_result_error2').hide();
  		$('#password').parent().addClass('form-error');
		$('#error').show();  		                       	
		res++;  		                       	
  	}
  	return res;
}
/*** custom select */
$(document).ready(function() {

	$('.customThis').customSelect({appendClass:'select_sort'});
	$('.customSelect').click(function() {
		$('.customSelect').removeClass('act');
	
	  $(this).addClass('act');
	   if ($(this).children('.customSelect_options').is(':visible')) {
			$('.customSelect_options').hide();
		}
		else {
			$('.customSelect_options').hide();
			$(this).children('.customSelect_options').show();
		}
	});


	$('.customSelect_option').live('click', function() {

		var option_text = $(this).html();
		$(this).parent('.customSelect_options').parent('.customSelect').children('.customSelect_selector').children('.customSelect_text').html(option_text);

		$(this).parent('.customSelect_options').children('.customSelect_option').removeClass('customSelect_option_now');
		$(this).addClass('customSelect_option_now');

		$('.customSelect').removeClass('act');
		var option_val = $(this).attr('value');
		option_val = typeof(option_val) != 'undefined' ? option_val : option_text;
//		$(this).parent('.customSelect_option').parent('.customSelect_options').parent('.customSelect').children('select').
		$(this).parents(".customSelect_wrap").children("select").children('option').removeAttr('selected').each(function() { 
			if ($(this).val() == option_val) {
				$(this).attr('selected', 'select');
			}
		});
//		$(this).parent('.customSelect_option').parent('.customSelect_options').parent('.customSelect').children('select').
		$(this).parents(".customSelect_wrap").children("select").trigger('change');
	});



	var selenter = false;
	$('.customSelect').live('mouseenter', function() {  selenter = true;});
	$('.customSelect').live('mouseleave', function() { 
		selenter = false;
		//$('.customSelect').removeClass('act');
	});
	$(document).click(function() {
		if (!selenter) {			        
			$('.customSelect_options').hide();
			$('.customSelect').removeClass('act');
		}
	});
});
(function($) {
	$.fn.customSelect = function(options) {
		var options = $.extend({
			appendClass: ''
		  },
		  options);
		return this.each(function(){
			
			var customSelect_options = '';
			var selected_option = false;
			var thisid=$(this).attr('id');
	
			$(this).wrap('<div class="customSelect_wrap '+ options.appendClass  +'"/>');
			$(this).find('option').each(function() {
				if($(this).is(':selected')){
					selected_option = $(this).index();
				}
				if (!$(this).attr('disabled')) {
				customSelect_options = customSelect_options + '<div class="customSelect_option" value="' + $(this).val() + '">' + $(this).html() + '</div>';
				}
			});
			var customSelect = '<div id="custom_' + thisid + '" class="customSelect">\
				<div class="customSelect_selector">\
					<div class="customSelect_text">' + $(this).children('option').eq(selected_option).html() + '</div>\
					<div class="customSelect_arrow"></div>\
				</div>\
				<div class="customSelect_options">' + customSelect_options + '</div>\
			</div>';
			$(this).before(customSelect);
		});
	}
})( jQuery );

	function CropDays(Id_day, Id_month, Id_year) {
	  	var sDay = document.getElementById(Id_day);
	  	var sMon = document.getElementById(Id_month);
		var sYear = document.getElementById(Id_year);

		var cdays=parseInt(sDay[sDay.selectedIndex].value, 10);
	    var days=daysInMonth(parseInt(sMon[sMon.selectedIndex].value, 10),parseInt(sYear[sYear.selectedIndex].value, 10));
	    for(var i=sDay.length-1; i>days;i--) {
			sDay[i]=null;
			$("#custom_"+Id_day).find(".customSelect_option[value='"+i+"']").hide();
		}
		for(i=sDay.length; i<=days;i++) {
		  	sDay[i]=new Option(i,i);
			$("#custom_"+Id_day).find(".customSelect_option[value='"+i+"']").show();
		}
        if (cdays>days)
		{
			$("#custom_"+Id_day).find(".customSelect_options").show();
			$("#custom_"+Id_day).find(".customSelect_option[value='"+days+"']").trigger('click');
		}
		
//		$("#"+Id_day).trigger('click');
	}


	function daysInFebruary (year){
	    if (!year) return 29;
	    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
	}

	function daysInMonth(m, y) {
	   if (m==4 || m==6 || m==9 || m==11) {return 30};
	   if (m==2) {return daysInFebruary(y);};
   	   return 31;
	}

 	function show_question(_num, _q_id, _type) {
		var keys = new Array();
		var err_text= 'Ошибка! Выберите вариант ответа для продолжения.';
		if (_type == 3) err_text= 'Ошибка! Выберите один или более одного варианта ответа для продолжения.';

		$("input").each(function(){
			if ($(this).attr("checked")==true) { 
				keys.push($(this).val());    
			} 
		});     
		if (_num!=0 && !keys.length){
			$('#error').show();
			$('#error').html(err_text);
		} else {
			$('#error').hide();
			$('#testing-block').load('/ajax/testing.php', {
				num: _num,
				qid: _q_id,
				answer: keys
			}, function() {
			});
	 	}
	}

 	function show_sertificate() {
		$('#sertificate-block').load('/ajax/sertificate_link.php');
	}

