jQuery(function($){
        $.datepicker.regional['ru'] = {
                closeText: 'Закрыть',
                prevText: '&#x3c;Пред',
                nextText: 'След&#x3e;',
                currentText: 'Сегодня',
                yearRange: "2000:2011",
                showYearNavigation:true,
                monthNames: ['Январь','Февраль','Март','Апрель','Май','Июнь',
                'Июль','Август','Сентябрь','Октябрь','Ноябрь','Декабрь'],
                monthNamesShort: ['Янв','Фев','Мар','Апр','Май','Июн',
                'Июл','Авг','Сен','Окт','Ноя','Дек'],
                dayNames: ['воскресенье','понедельник','вторник','среда','четверг','пятница','суббота'],
                dayNamesShort: ['Вс','Пн','Вт','Ср','Чт','Пт','Сб'],
                dayNamesMin: ['Вс','Пн','Вт','Ср','Чт','Пт','Сб'],
                dateFormat: 'dd.mm.yy', firstDay: 1,
                //changeYear: true,
				readonly:true,
                //minDate: new Date(),
                isRTL: false};
        $.datepicker.setDefaults($.datepicker.regional['ru']);
});
function declOfNum(number, titles) {
  cases = [2, 0, 1, 1, 1, 2];  
  return titles[ (number%100>4 && number%100<20)? 2 : cases[(number%10<5)?number%10:5] ];
}

$(document).ready(function() {

	$(".captcha a").click(function (e) {
		e.preventDefault();
		var img_id = ".captcha img";
		var timestamp = new Date().getTime();
		var img_src = $(img_id).attr("src").split("?")[0] + "?" + timestamp;
		$.ajax({
			type: "GET",
			url: img_src,
			success: function(msg){$(img_id).attr('src',img_src);}
		});
	});

    $('.carousel-block').jcarousel();
	$("a[rel^='prettyPhoto']").prettyPhoto({animationSpeed:'slow',theme:'facebook',slideshow:5000, 'centerOnScroll' : false});
	$(".photoG:gt(0) a[rel^='prettyPhoto']").prettyPhoto({animationSpeed:'fast',slideshow:2000, 'centerOnScroll' : true});
	$("select").msDropDown();

 	$("#formsend").click(function(){
		$.post("modal/search.php",$("#searchform").serialize(),{});
								  
		$("#searchform").submit();						  
	});
	
	$(".search-result thead a").click(function(e){
		e.preventDefault();
		$.post("modal/order.php",
			   {"order":$(this).attr("rel")},
			   function(date){$("#orderform").submit();
		});
	});
	
	$(".link-sort").click(function(e){
		e.preventDefault();
		$.post("modal/order.php",
			   {"order":"price,ASC"},
			   function(e){$("#orderform").submit();
		});
	});
	
	$("#type_msdd a").click(function(e){
		e.preventDefault();
		$(".placement").html("");
		
		$.post("modal/getplacement.php",{"id":$(".type select").attr("value"),"hotel":$(".hotel").attr("value")},function(data){
			$(".placement").html(data);
			$(".placement select").msDropDown();
			
			ch_price();
			ch_placement();
		});
	});
	
	$(".button-booking a").click(function(e){
		//e.preventDefault();
		$.post("modal/setroom.php",{"id":$(this).attr("rel")},{});
		//window.location($(this).attr("href"));
		//$(document).location();
	});
 
    // datapicker
     if ($('.form-booking form').length) {
   $('#datein').datepicker({showOn: 'both', dateFormat: 'd MM yy, DD', buttonImage: 'img/css/calendar.png', buttonImageOnly: true, beforeShow: customRange1, altField: '#datein_tstmp', altFormat: '@'});
    $('#dateout').datepicker({showOn: 'both', dateFormat: 'd MM yy, DD', buttonImage: 'img/css/calendar.png', buttonImageOnly: true, beforeShow: customRange1, altField: '#dateout_tstmp', altFormat: '@'});
    }
     function customRange1(input) {
        var input_id = input.id;
                
                if ($("#datein").datepicker("getDate")) date=$("#datein").datepicker("getDate");
                else    date=new Date();
                
        if (input_id == "datein"){return {maxDate: $("#dateout").datepicker("getDate"), minDate:new Date()};}
        if (input_id == "dateout"){return {minDate: 'date+1d', maxDate: '+1y'};}
    }

    /* booking form date period */
    $('.form-booking .datepicker').change(function() {
      datein_tstmp = parseInt($("#datein_tstmp").val());
      dateout_tstmp = parseInt($("#dateout_tstmp").val());
	  
      if (datein_tstmp > 0 && dateout_tstmp > 0) 
	  {
        days_count = Math.round((dateout_tstmp - datein_tstmp)/(86400000));
        $(".form-booking .days-count").html(days_count +declOfNum(days_count, [' день', ' дня', ' дней']) + ' / ' + days_count +declOfNum(days_count, [' ночь', ' ночи', ' ночей']));
        $(".form-booking #nights").val((dateout_tstmp - datein_tstmp)/(86400));
		
		ch_price();
      }
    });
	
	function	ch_price()
	{
		$.post("modal/getprice.php",{"id":$(".type select").attr("value"),
									 "hotel":$(".hotel").attr("value"),
									 "placement":$(".placement select").attr("value"),
									 "dayfrom":parseInt($("#datein_tstmp").val()),
									 "dayto":parseInt($("#dateout_tstmp").val())},
				function(data){$(".priceper p").html(data);});	
		
		//alert(days);
	}
	
	function	ch_placement()
	{
		$("#placement_msdd a").click(function(e){
			e.preventDefault();
			ch_price();
		});
	}
	ch_placement();
 	
	var pager_width = $('.pager').width() * 0.52;
	$('.pager').css('margin-left', '-' + pager_width + 'px');
	
    /* Map */
    function map_markers() {
      $(".modal-box .map .s").hover(
        function () {
          num = $(this).attr("alt");
          $('.map .m[alt="'+ num +'"]').addClass('active');
        },
        function () {
          num = $(this).attr("alt");
          $('.map .m[alt="'+ num +'"]').removeClass('active');}
      );
    }

    /* metro map */
    function metromap_function() {
      map_markers();
      $('.metromap-modal .m').click(function (e) {
        e.preventDefault();
        var metro_id = $(this).attr('alt');
        $('#metro').val(metro_id);
        $("#metro").msDropDown();
        $.modal.close();
      });
    }
    $('.metro-map-js').click(function (e) {
      e.preventDefault();
      $.get( 'modal/modal_metromap.html', function(data){
        $.modal(data, {
          opacity: 90,
          autoPosition: false,
          onOpen: modalOnOpen,
          onClose: modalOnClose,
          onShow: function(dialog){metromap_function()}
        });
      });
      $('html, body').animate({scrollTop: $(".simplemodal-container").offset().top}, 200);
    });

	$(".price tr").mouseover(function(){
		$(this).addClass('active');
	});
	$(".price tr").mouseout(function(){
		$(this).removeClass('active');
	});

	$('.price tbody tr').cluetip({
		tracking: true
	});  
	$('.price.right tbody tr').cluetip({
		cluetipClass: 'right',
		tracking: true
	});  

    function modalOnOpen(dialog) {
        //Animate the open of the popup
        dialog.overlay.fadeIn('fast', function() {
            dialog.container.fadeIn('fast', function() {
                dialog.data.hide().slideDown('fast');
            });
        });
    }
    function modalOnClose(dialog) {
        //Animates the closing of the dialogs
       dialog.data.fadeOut('fast', function() {
           dialog.container.slideUp('fast', function() {
               dialog.overlay.fadeOut('fast', function() {
                   $.modal.close();
               });
           });
       });
    }

});
$(function()
	{
		var jInputs = $('input.labeled[type=text],input.labeled[type=password]');
		// jInputs.each();
		jQuery.each
		(
			jInputs,
			function(index, value)
			{
				value.labelValue = value.value;
			}
		);

		jInputs.focus
		(
			function ()
			{
				if (this.value == this.labelValue) this.value = '';
			}
		).blur
		(
			function ()
			{
				if (this.value == '') this.value = this.labelValue;
			}
		);
	}
);
//setupLabeledInputs();


