$(function() {
	var theDate = new Date();
	var curYear = (theDate.getYear() < 2000) ? theDate.getYear()+1900 : theDate.getYear();
	$.datepicker.setDefaults( {dateFormat: 'dd.mm.yy', changeMonth: true, changeYear: true, duration: 'fast', yearRange: curYear+':'+(curYear*1+10),
		monthNamesShort: ['Январь','Февраль','Март','Апрель','Май','Июнь','Июль','Август','Сентябрь','Октябрь','Ноябрь','Декабрь'],
		dayNamesMin: ['Вс', 'Пн', 'Вт', 'Ср', 'Чт', 'Пт', 'Сб'] }, $.datepicker.regional['ru'] );
	$(".dateExPickerInputPrice").datepicker({
		showOn: 'both', buttonImage: '/images/a_calendar.gif', buttonImageOnly: true
	});
	$('#exFromDate, #exToDate').datepicker('option', 'dateFormat', 'dd.mm.y');
	$('#hoFromDate').datepicker('option', 'onSelect', function(dateText, inst){
			var selDate = dateText.split('.');
			$('#hoToDate').datepicker('option', 'minDate', new Date(selDate[2], selDate[1] - 1, selDate[0]*1 + 1) );
		}
	);
	$('#tuFromDate').datepicker('option', 'onSelect', function(dateText, inst){
			var selDate = dateText.split('.');
			$('#tuToDate').datepicker('option', 'minDate', new Date(selDate[2], selDate[1] - 1, selDate[0]*1 + 1) );
		}
	);

	$("#sliderHotel").slider({ range:true, min:0, max:1000, step:10, values:[((gup('hotelMinPrice') != '') ? gup('hotelMinPrice') : 0), ((gup('hotelMaxPrice') != '') ? gup('hotelMaxPrice') : 1000)],
		slide: function(event, ui) {
			$("#amountHotel").val('$' + ui.values[0] + ' - $' + ui.values[1]);
			$('#hotelMinPrice').val(ui.values[0]);
			$('#hotelMaxPrice').val(ui.values[1]);
		}
	});
	$("#amountHotel").val('$' + $("#sliderHotel").slider("values", 0) + ' - $' + $("#sliderHotel").slider("values", 1));
	$('#hotelMinPrice').val($("#sliderHotel").slider("values", 0));
	$('#hotelMaxPrice').val($("#sliderHotel").slider("values", 1));

	$("#sliderExcurs").slider({ range:true, min:0, max:500, step:10, values:[((gup('exMinPrice') != '') ? gup('exMinPrice') : 0), ((gup('exMaxPrice') != '') ? gup('exMaxPrice') : 500)],
		slide: function(event, ui) {
			$("#amountExcurs").val('$' + ui.values[0] + ' - $' + ui.values[1]);
			$('#exMinPrice').val(ui.values[0]);
			$('#exMaxPrice').val(ui.values[1]);
		}
	});
	$("#amountExcurs").val('$' + $("#sliderExcurs").slider("values", 0) + ' - $' + $("#sliderExcurs").slider("values", 1));
	$('#exMinPrice').val($("#sliderExcurs").slider("values", 0));
	$('#exMaxPrice').val($("#sliderExcurs").slider("values", 1));

	$("#sliderTours").slider({ range:true, min:0, max:3000, step:10, values:[((gup('tourMinPrice') != '') ? gup('tourMinPrice') : 0), ((gup('tourMaxPrice') != '') ? gup('tourMaxPrice') : 3000)],
		slide: function(event, ui) {
			$("#amountTours").val('$' + ui.values[0] + ' - $' + ui.values[1]);
			$('#tourMinPrice').val(ui.values[0]);
			$('#tourMaxPrice').val(ui.values[1]);
		}
	});
	$("#amountTours").val('$' + $("#sliderTours").slider("values", 0) + ' - $' + $("#sliderTours").slider("values", 1));
	$('#tourMinPrice').val($("#sliderTours").slider("values", 0));
	$('#tourMaxPrice').val($("#sliderTours").slider("values", 1));
});

function showSearchBlock(target) {
	$('#searchBlock').css({height:'365px'});
	if( !$('#'+target).is(':visible') ) {
		$('.showen').hide('blind').removeClass('showen');
		$('#'+target).show('blind').addClass('showen');
	}
}

function doLogin(){
	loader();
	$('#loginError').html('');
	$('#loginError').hide();
	var userMem = $("#userMem:checked").val();
	if ( $('#textinput').val() != '' && $('#passwordinput').val() != '' ) {
		$.post('/ajax/adminlogin.php',
			{ login:$("#textinput").val(), pswd:$("#passwordinput").val(), mem:userMem },
			function(result){
				if (result == 'ok') {
					window.location.href = '/admin/';
				} else {
					loader();
					$('#loginError').html(result);
					$('#loginError').show();
				}
			}
		);
	} else {
		loader();
		$('#loginError').html(loginTxt[0]);
		$('#loginError').show();
	}
}
function doExit() {
	loader();
	$.post('/ajax/adminlogin.php',
			{ logout:'ok' },
			function(result){
				if (result == 'ok') {
					window.location.href = '/';
				} else {
					loader();
				}
			}
		);
}

function loader() {
	if ( !$('#TB_overlay').is(':visible') ) showOverlay();
	else tb_remove();
}
function showOverlay() {
	if (typeof document.body.style.maxHeight === "undefined") {//if IE 6
		$("body","html").css({height: "100%", width: "100%"});
		$("html").css("overflow","hidden");
		if (document.getElementById("TB_HideSelect") === null) {//iframe to hide select elements in ie6
			$("body").append("<iframe id='TB_HideSelect'></iframe><div id='TB_overlay'></div>");
		}
	}else{//all others
		if(document.getElementById("TB_overlay") === null){
			$("body").append("<div id='TB_overlay'></div>");
		}
	}
	if(tb_detectMacXFF()){
		$("#TB_overlay").addClass("TB_overlayMacFFBGHack");//use png overlay so hide flash
	}else{
		$("#TB_overlay").addClass("TB_overlayBG");//use background and opacity
	}
	$("body").append("<div id='TB_load'><p>"+preloadTxt[0]+"</p></div>");//add loader to the page
	$('#TB_load').show();
}
function tb_detectMacXFF() {
  var userAgent = navigator.userAgent.toLowerCase();
  if (userAgent.indexOf('mac') != -1 && userAgent.indexOf('firefox')!=-1) {
    return true;
  }
}
function tb_remove() {
	$("#TB_overlay").fadeOut("fast",function(){$('#TB_window,#TB_overlay,#TB_HideSelect').remove();});
	$("#TB_load").remove();
	if (typeof document.body.style.maxHeight == "undefined") {//if IE 6
		$("body","html").css({height: "auto", width: "auto"});
		$("html").css("overflow","");
	}
	return false;
}

function getCurrentData() {
	var theDate = new Date();
	var curYear = (theDate.getYear() < 2000) ? theDate.getYear()+1900 : theDate.getYear();
	$('#month').val(theDate.getMonth() + 1);
	$('#year').val(curYear);
	$('#date').val(theDate.getDate());
}

function parseQuery ( query ) {
   var Params = {};
   if ( ! query ) {return Params;}// return empty object
   var Pairs = query.split(/[;&]/);
   for ( var i = 0; i < Pairs.length; i++ ) {
      var KeyVal = Pairs[i].split('=');
      if ( ! KeyVal || KeyVal.length != 2 ) {continue;}
      var key = unescape( KeyVal[0] );
      var val = unescape( KeyVal[1] );
      val = val.replace(/\+/g, ' ');
      Params[key] = val;
   }
   return Params;
}

function gup( name ){
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp( regexS );
	var results = regex.exec( window.location.href );
	if( results == null )   return "";
	else return results[1];
}
function showGal(img) {
	$("#bigImage .showen").animate({opacity:'hide'}).removeClass('showen');
	$("#big"+img).animate({opacity:'show'}).addClass('showen');
}
function showMap(img) {
	var mapImageDiv = $('<div id="mapimage" title="Схема расположения">');
	var mapImage = $('<img src="'+img+'" width="721">');
	mapImageDiv.append(mapImage);
	$('body').append(mapImageDiv);

	$("#mapimage").dialog({
    bgiframe: true,
    autoOpen: false,
    height: 600,
    width: 770,
    modal: true,
    draggable: true,
    resizable: false,
    close: function() {
    		$("#mapimage").dialog('destroy');
    		$("#mapimage").remove();
    	},
	buttons: {
	  	'Закрыть': function() {
	  		$("#mapimage").dialog('destroy');
    		$("#mapimage").remove();
	      }
		}
	});
	$("#mapimage").dialog('open');
}
