var map;
var pointer;
var mark;
var zoomMap;
var centerMap;
var distance;
var points = [];
var icons = {};
var pointAdress = [];
var pointsMarker = {};
var optionsLogon = {
   beforeSubmit:  showRequestLogon,
   success: showResponseLogon
};

var dw = 'Откуда';
var dwr = 'Куда';
var searchText = 'Поиск';
var is_ie = (/msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent));
function GID(id) { return (is_ie) ? document.all[id] : document.getElementById(id); }

$(document).ready(function() {
   $( 'body' ).mousemove(function(){ setCookie('tecEverest'); })
   $(document).pngFix();
   checkFlash();
   if ($("#gallery a[rel=fancy]").length) $("#gallery a[rel=fancy]").fancybox({ 'hideOnContentClick': true, 'overlayShow': true, 'overlayOpacity': 0.5, 'speedIn': 1000, 'speedOut': 600, 'titlePosition': 'inside', 'transitionIn': 'elastic', 'transitionOut': 'elastic' });
   if ($(".callback").length) $(".callback").fancybox({ 'titleShow': false });
   if ($(".login").length) $(".login").fancybox({ 'titleShow': false });
   if ($(".zvonok").length) $(".zvonok").fancybox({ 'titleShow': false });
   $('#logonPopupForm').submit(function() {
      $(this).ajaxSubmit(optionsLogon);
      return false;
   });
   $.fn.alignCenter = function() {
      var marginLeft = - $(this).width()/2 + 'px';
      var marginTop = - $(this).height()/2 + 'px';
      return $(this).css({'margin-left': marginLeft, 'margin-top': marginTop});
   };
   $.fn.togglePopup = function(){
      if ($('#popup').hasClass('hidden')) {
         if ($.browser.msie) {
            $('#opaco').height($(document).height()).toggleClass('hidden').click(function() { $(this).togglePopup(); } );
            $('#popup').css({'position': 'absolute', 'left': ($(document).width() / 2), 'top': ($(document).height() / 2)});
         }
         else {
            $('#opaco').height($(document).height()).toggleClass('hidden').fadeTo('slow', 0.7).click(function() { $(this).togglePopup(); } );
            $('#popup').css({'position': 'fixed', 'left': '50%', 'top': '50%'});
         }
         $('#popup').alignCenter().toggleClass('hidden');
      }
      else {
         $('#opaco').toggleClass('hidden').removeAttr('style').unbind('click');
         $('#popup').toggleClass('hidden');
      }
   };
});

function setCookie(str)
{
    var NowDate = new Date();
    var day = (NowDate.getDate() < 10 ? '0'+NowDate.getDate() : NowDate.getDate());
    var month = ((NowDate.getMonth() + 1) < 10 ? '0'+(NowDate.getMonth() + 1) : (NowDate.getMonth() + 1));
    var fullDate = day+'.'+month+'.'+NowDate.getFullYear();
    var cookieName = $().crypt({method:"md5",source:str+fullDate});
    var cookieVal = $().crypt({method:"md5",source:str+fullDate+'tecEverest'});
    if (!$.cookie(cookieName)) {
		var CookieOption = {path: '/'};
		$.cookie(cookieName, cookieVal, CookieOption);
    }
}

function initVideo(path, image, auto, div, width, height) {
   if (div) {
      var w = (width ? width : 640);
      var h = (height ? height : 480);
      var fv = { file: path, backcolor: 'black', frontcolor: 'white', lightcolor: 'white', screencolor: 'black', repeat: 'always', autostart: auto, stretching: 'fill' };
      if (image) fv['image'] = image;
      htmlOptions = { src: "image/player.swf", width: w, height: h, allowfullscreen: true, flashvars: fv, replace: '<table class="noFlash"><tr><td align="center">Не установлен Adobe® Flash® Player<br /><a href="http://www.adobe.com/ru/products/flashplayer/" target="_blank">Установить</a></td></tr></table>' };
      $('#'+div).flash(htmlOptions, pluginOptions);
   }
}

function showWindow() {
   $('#popup').togglePopup();
   $.ajax({
    type: "POST",
    async: false,
    url: "setsession.php",
    data: 'set=1',
    success: function(response) {  }
   });
}

// Google Maps
function hashIsEmpty(hash) {
    for (var k in hash) return false;
    return true;
}

function hashLength(hash) {
    var ret = 0;
    for (var k in hash) ret = ret + 1;
    return ret;
}

onresize = bodyReSize;

function bodyReSize(){
   if($('body').width() < 1024) $('#tablemain').css('width', 950);
   else $('#tablemain').css('width', '100%');
}

function bodySize() {
   if ($.browser.msie && (document.documentElement.clientWidth || document.body.clientWidth)) {
      $("#tablemain").css('width', ((document.documentElement.clientWidth || document.body.clientWidth) > 1230) ? 1230 : "100%");
   }
}

function fnCheckSearchForm(f) {
   if (!f.search.value.match(/^.+$/) || f.search.value == searchText) {
      alert("Укажите поисковый запрос!");
      f.search.focus();
      return false;
   }
   return true;
}

function fnCheckSubscribeForm(f) {
   if (!f.email.value.match(/^[0-9A-Za-z\._-]+@([0-9a-z\._-]+\.)+[a-z]{2,4}$/)) {
      alert("Укажите правильно Ваш e-mail!");
      f.email.focus();
      return false;
   }
   return true;
}

function fnCheckRegistrationForm(f) {
   if (!f.fio.value.match(/(.+)/)) {
      alert("Укажите ФИО!");
      f.fio.focus();
      return false;
   }
   if (!f.email.value.match(/^[0-9A-Za-z._-]+@([0-9a-z_-]+\.)+[a-z]{2,4}$/)) {
      alert("Укажите правильный E-mail!");
      f.email.focus();
      return false;
   }
   if (!f.password.value.match(/(.+)/)) {
      alert("Укажите пароль!");
      f.password.focus();
      return false;
   }
   if (f.password.value != f.re_password.value) {
      alert("Подтверждение пароля не совпадает с основным паролем!");
      f.re_password.focus();
      return false;
   }
   return true;
}

function showRequestLogon(formData, jqForm, options) {
   for (var i = 0; i < formData.length; i++) {
      if (formData[i].name == 'logon_login' && !formData[i].value.match(/^[a-zA-Z0-9._-]+\@[^\s:,]+\.[^\s:,]+$/i)) {
         jqForm[0].logon_login.value = '';
         jqForm[0].logon_login.focus();
         alert('Укажите правильный логин!');
         return false;
      }
      else if (formData[i].name == 'logon_passwd' && !formData[i].value.match(/^[0-9a-zA-Z_]+$/)) {
         jqForm[0].logon_passwd.value = '';
         jqForm[0].logon_passwd.focus();
         alert('Укажите пароль!');
         return false;
      }
   }
   return true;
}

function showResponseLogon(text) {
   if (text.match(/^http:\/\//)) {
      document.location.href = text;
   }
   else if (text == 'yes') {
      document.location.href = 'cabinetbonus_main.htm';
   }
   else {
      alert(text);
   }
   return true;
}

function showbOrderForm() {
   $(".bOrder").show();
}

function checkBOrderForm(f) {
   if (!f.number.value.match(/(.+)/)) {
      alert("Укажите номер заявки!");
      f.number.focus();
      return false;
   }
   if (!f.idate.value.match(/^([0-9]{2})\.([0-9]{2})\.([0-9]{4})$/)) {
      alert("Укажите дату заявки в формате ДД.ММ.ГГГГ!");
      f.idate.focus();
      return false;
   }
   return true;
}
