
/* Determine scroll  */
function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return {scrX: scrOfX, scrY: scrOfY };
}
	
/*  Determine the user's window width and height  */
function width_height() {      
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    w = window.innerWidth;
    h = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    w = document.documentElement.clientWidth;
    h = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    w = document.body.clientWidth;
    h = document.body.clientHeight;
  }
 // w = $(window).width();    /* Ширина экрана на момент загрузки страницы */
 // h = $(window).height();   /* Высота экрана на момент загрузки страницы */
  return {width: w, height: h}; 
}
	
function my_callback_function() {
	$("#close-order, #close-callback").mouseover(function(){
		var url_over = "/" + path + "/images/close_button_over.png";
		$("#close-order img").attr({src: url_over}); 
	}).mouseout(function(){
    var url = "/" + path + "/images/close_button.png";
    $("#close-order img").attr({src: url});
	}).click(function(){
    $("#pop-order, #modal").css({display: 'none'});
    return false;
	});
}
	
//---------------------------------------------------------------------------------


$("body").ready(function() {

	var country = new Array;
	country[0] = "/" + path + "/images/china_flag1.jpg";
	country[1] = "/" + path + "/images/tailand_flag1.jpg";
	country[2] = "/" + path + "/images/emiraty_flag1.jpg";
	country[3] = "/" + path + "/images/croatia_flag1.jpg";
	country[4] = "/" + path + "/images/turkey_flag1.jpg";
	country[5] = "/" + path + "/images/czech_flag1.jpg";
	country[6] = "/" + path + "/images/spain_flag1.jpg";
	country[7] = "/" + path + "/images/egypet_flag1.jpg";
	country[8] = "/" + path + "/images/cyprus_flag1.jpg";
	
	var country_over = new Array;
	country_over[0] = "/" + path + "/images/china_flag2.jpg";
	country_over[1] = "/" + path + "/images/tailand_flag2.jpg";
	country_over[2] = "/" + path + "/images/emiraty_flag2.jpg";
	country_over[3] = "/" + path + "/images/croatia_flag2.jpg";
	country_over[4] = "/" + path + "/images/turkey_flag2.jpg";
	country_over[5] = "/" + path + "/images/czech_flag2.jpg";
	country_over[6] = "/" + path + "/images/spain_flag2.jpg";
	country_over[7] = "/" + path + "/images/egypet_flag2.jpg";
	country_over[8] = "/" + path + "/images/cyprus_flag2.jpg";
	
	
	$(".country-image").mouseover(function(){ 
		var name = $(this).attr('name');
		$(this).attr({src: country_over[name]});
	}).
		mouseout(function(){ 
		var name = $(this).attr('name');
		$(this).attr({src: country[name]});
	});
	
	$(".fun").mouseover(function(){  
		$("#dev").text("куём сайты!").css({marginLeft: '15px' });
		window.setTimeout( "$('#dev').text('разработка сайта ').css({marginLeft: '0px'});", 1500);
		return false;
	});
	
	$("#form-ready").click(function(){
		var country = $("#select-country").attr('value');
		var from = $("#from").attr('value');
		var till = $("#till").attr('value');
		// var stars = $("#select-stars").attr('value');
		// var money = $("#money-limit").attr('value');
		var desires = $("#desires").attr('value');
		var name = $("#name").attr('value');
		var email = $("#email").attr('value');
		var phone = $("#phone").attr('value');
		
    // Вызываем срабатывание цели 'ZakazTuraOtravka' для Яндекс.Метрики
		if ( user_id == 0 ) { 
		  yaCounter1171053.reachGoal('ZakazTuraOtravka');
		}
		
		var url_includes = "/" + path + "/includes/send_order.php";
		
		$("#pop-order").load(url_includes, 
		                    { country: country, 
                  			  from: from, 
                  			  till: till, 
                  			  // stars: stars, 
                  			  // money: money, 
                  			  desires: desires, 
                  			  name: name, 
                  			  email: email, 
                  			  phone: phone, 
                  			  path: path
                			  }, my_callback_function);  
		return false; 
	});
	
	$("#close-order").mouseover(function(){
		var url_over = "/" + path + "/images/close_button_over.png";
		$("#close-order img").attr({src: url_over}); 
	}).
	mouseout(function(){
				var url = "/" + path + "/images/close_button.png";
				$("#close-order img").attr({src: url});
	}).
	click(function(){
				 $("#pop-order, #modal").css({display: 'none'});
				 	return false;
	});

	
	/**
   *  Коррекция абсолютного позиционирования кнопки "ЗАЯВКА НА ТУР"
   */	
	function order_div_abs_pos() {
  	// Получаем ширину окна клиента
  	w = $(window).width();
  	// left -- сдвиг от левого края центрованного контента
  	left = 52;
  	order_div_left = w < 990 ? left : (w - 990)/2 + left;  
  	$('#order').css('left', order_div_left);
	}
	
	/** 
	 * Не корректируем расположение для кнопки на главной странице
	 * Тольно на главной странице location оканчивается на /
	 */
	if ( !location.toString().match(/\/$/)) {
	  order_div_abs_pos();
	}
	
  /**
   *  Коррекция абсолютного позиционирования кнопки "ЗАЯВКА НА ТУР"
   *  после ресайза окна браузера
   *  @see http://api.jquery.com/resize/
   *  
   * Не корректируем расположение для кнопки на главной странице
   * Тольно на главной странице location оканчивается на /
   */	
	if ( !location.toString().match(/\/$/)) {
	  $(window).resize(order_div_abs_pos);
	}
	
  /**
   * Анимация блика на кнопке ЗАЯВКА НА ТУР
   */
	spakr_img = $('#order img');
  if (spakr_img.length == 1) {  
    anim_spark = function () {
      spakr_img
        .animate({"opacity": 1, "right":  20}, 60, "linear")
        .animate({"opacity": 0, "right": 130}, 600,  "linear")
        .animate({"right": 0},   10, "linear");
    };    
    anim_spark();
    setInterval(anim_spark, 2000);
  }

  /**
   * hover кнопки ЗАЯВКА НА ТУР
   */
  order_div = $('#order');
  order_img = $('#order img');
  order_img_src = order_img.attr('src');
  order_img_src_hover = order_img_src.replace(/btn_order_spark.png/, 'btn_order_spark_hover.png');
  
  $('#order a').hover(function() {
    order_div.css('background-position', 'right top');
    order_img.attr('src', order_img_src_hover);
  }, function() {
    order_div.css('background-position', 'left top');
    order_img.attr('src', order_img_src);
  });
  
  // Отслеживание курсора мыши	
  /*
	order_btn_img = $('#order img');
	$(document).mousemove(function(e){
	  //console.log(e.pageX);
	  // console.log(e.pageY);
	  distance = Math.sqrt(Math.pow((e.pageX-190),2) + Math.pow((e.pageY-540),2));
	  // console.log(distance);
	  
	  if (distance >= 900) {
	    opacity = "0.01";
	  } else {
	    opacity = (900 - distance) / 600;
	  }
	  
	  order_btn_img.css('opacity', opacity);
   });
   */

	$("#order").click(function(){
	
		//-----------------------Позиционируем форму и модальное окно
	
		var form_left = 990/2 - 180;
		var form_top =  width_height()['height']/2 -252 + getScrollXY()['scrY'];
		
		var modal_height = width_height()['height'] + getScrollXY()['scrY'];
		
		$("#pop-order").css({top: form_top, left: form_left, display: 'block'});
		$("#modal").css({height: modal_height, display: 'block'});
		
		// Вызываем срабатывание цели 'ZakazTura' для Яндекс.Метрики
		if ( user_id == 0 ) {
		  yaCounter1171053.reachGoal('ZakazTura');
		}
		
		return false; 
	});
	
	//-----------------------Ставим футер на место
	
	var tp_height = parseInt($("#text-place").height());
	
	var footer1_top = tp_height + 286;
		if (footer1_top < 590) footer1_top = 304 + 286;
		//alert (tp_height);
		//alert (footer1_top);
	var main_div_height = tp_height + 286 + 210;  // Высота текстового поля + хэдэр + футер
		if (main_div_height < 800) main_div_height = 304 + 286 + 210;
	
	$("#footer1").css({top: footer1_top});
	$("#maindiv").css({height: main_div_height}); 		
	
});	

