//start jquery
$(function () 
{ 
  // valid url (external window)
  $("a[rel~='external']").click(function(){
    this.target = "_blank";
    // this.onclick = function(){ window.open(this.href) }
  }); 
  
  // Group fotos
  $("a[rel=fotos]").fancybox({
 		'transitionIn'		  : 'none',
	 	'transitionOut'	 	: 'none',
		 'titlePosition'  	: 'over',
   'cyclic'          : 'true',
   'overlayShow'     : 'false',
   'overlayColor'    : 'none',
		 'titleFormat'     : function(title, currentArray, currentIndex, currentOpts) {
    return '<span id="fancybox-title-over">Фото  ' +  (currentIndex + 1) + ' из ' + currentArray.length + ' &raquo; ' + title + '</span>';
		 }
	 });

	 $("a[rel=fotos]").mouseover(function () {
      $(this).css("cursor","url('/img/cur/zoom_in.cur'), pointer");
  });
  

  //sendmailform
  $(".eml,.askeml").click(function() {
   $('#mailform_info').html('');
   // Check cookies, and to fill form
   var u_name=  $.cookies.get('u_name');
   var u_email= $.cookies.get('u_email');
   var u_from=  $.cookies.get('u_from');
   var u_cont=  $.cookies.get('u_cont');

   if (u_name!='')   { $('#mailform_name').val(u_name); }
   if (u_from!='')   { $('#mailform_from').val(u_from); }
   if (u_email!='')  { $('#mailform_email').val(u_email); }
   if (u_cont!='')   { $('#mailform_cont').val(u_cont); }
    
    $.fancybox({
      'opacity'       : 'true',
      'padding'		     : 10,
      'overlayColor'  : '#000',
      'content'       : '#seml',
   			'transitionIn' 	: 'fade',
			   'transitionOut'	: 'elastic',
      'type'          : 'inline'
		});
  });

 $("#mailform_button").click(function(){
   $('#mailform_info').html('');
   
   var newOptions = ({
    domain: 'evatour.ru',
    path: '/',
    expiresAt: new Date( 2020, 1, 1 ),
    secure: false
   });

   $.cookies.setOptions(newOptions);
   var err='';
   // Set cookies
   if ($('#mailform_name').val()=='') { err+='&raquo; Пожалуйста, представьтесь! <br />'; }
   else {$.cookies.set('u_name',$('#mailform_name').val()); }
   if ($('#mailform_from').val()=='') { err+='&raquo; Укажите откуда вы! <br />'; }
   else {$.cookies.set('u_from',$('#mailform_from').val()); }
   if ($('#mailform_email').val()=='') { err+='&raquo; Укажите ваш Email! <br />'; }
   else {$.cookies.set('u_email',$('#mailform_email').val()); }
   if ($('#mailform_cont').val()!='') { $.cookies.set('u_cont',$('#mailform_cont').val()); }
   if ($('#mailform_text').val()=='') { err+='&raquo; Заполните текст письма! <br />'; }

   if (err) // if err 
   {
      $('#mailform_info').html(err);
   }
   else 
   { // No err, send msg
    $.ajax(
    {
     traditional: true,
     type: "POST",
     url: "/inc/mod/sendm.php",
     datatype: "json",
     data: {
       mailform_name:  $('#mailform_name').val(),
       mailform_from:  $('#mailform_from').val(),
       mailform_email: $('#mailform_email').val(),
       mailform_cont:  $('#mailform_cont').val(),
       mailform_subj:  $('#mailform_subj').val(),
       mailform_tour:  $('#mailform_tour').val(),
       mailform_text:  $('#mailform_text').val(),
       mailform_tourname: $('#mailform_tourname').val(),
       mailform_button: 'OK',
       mailform_url:  $('#mailform_url').val()
     },
     success: function(data)
     {
          json=jQuery.parseJSON(data);
          // if err return
          if (json.err!='') 
          {  
             var x=json.err;
             x=x.replace(/\&raquo\;/g,'');
             alert(x);
             $('#mailform_info').html(json.err);
          }
          else {
            if (json.rep!='')  
            {
               // report
               alert(json.rep);
               $.fancybox.close();
               $('#mailform_text').val('');
            }
          }
     }
    });
   }
 
 });
}); // end jquery

function showid(id)
{
    var id_div= document.getElementById(id);
    if (id_div)
    {
     
       if (id_div.style.display!='none')
       {
          id_div.style.display = 'none';
       }
       else {
          id_div.style.display = 'block';
          
       }
    }
}

function hidid(id)
{
    var id_div= document.getElementById(id);
    if (id_div)
    {

       if (id_div.style.visibility!='hidden')
       {
          id_div.style.visibility = 'hidden';
       }
       else {
          id_div.style.visibility = 'visible';
          
       }
    }
}



function mylo()
{
   var az = new Array ( 25,139,142,136,141,122,143,126,134,135,130,89,71)         
   var bz = new Array (7,6,5,4,3,2,1,11,7,6,5,4,3,2,1,12,1,2);
   x=az[0];               
   z='';
   for(i=0;i< bz.length;i++)
   {
      z+=String.fromCharCode(az[bz[i]]-x);
   }
   document.write('<span style="color: #5060ff;" title="Отправить письмо">'+z+'</span>');
}

function cphone()
{ // contact phone
   document.write('(812) 313-6111')
}

function adres_yandex()
{
   document.write('&nbsp; <a target=_blank href="http://maps.yandex.ru/map.xml?mapID=500&mapX=3374263&mapY=8348733&scale=13&descx=3374227&descy=8348605&text=%D0%A0%D0%BE%D1%81%D1%81%D0%B8%D1%8F%2C%20%D0%A1%D0%B0%D0%BD%D0%BA%D1%82-%D0%9F%D0%B5%D1%82%D0%B5%D1%80%D0%B1%D1%83%D1%80%D0%B3%2C%20%D0%90%D0%B4%D0%BC%D0%B8%D1%80%D0%B0%D0%BB%D1%82%D0%B5%D0%B9%D1%81%D0%BA%D0%B8%D0%B9%20%D0%BF%D1%80%D0%BE%D1%81%D0%BF%D0%B5%D0%BA%D1%82%2C%206">Схема проезда</a>')
}

// css menu
function menuhover()
{ // IE bug for hmenu
    if(!document.getElementById("hmenu"))
        return;
    var lis = document.getElementById("hmenu").getElementsByTagName("LI");
    for (var i=0;i<lis.length;i++)
    {
        lis[i].onmouseover=function(){this.className+=" iehover";}
        lis[i].onmouseout=function() {this.className=this.className.replace(new RegExp(" iehover\\b"), "");}
    }
}

if (window.attachEvent)
    window.attachEvent("onload", menuhover);

function closeid(id)
{
   var id_div= document.getElementById(id);
   if (id_div)
   {
      id_div.style.visibility='hidden';
   }   
}

function getClientWidth()
{
  return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientWidth:document.body.clientWidth;
}

function getClientHeight()
{
  return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientHeight:document.body.clientHeight;
}

function getBodyScrollTop()
{
  return self.pageYOffset || (document.documentElement && document.documentElement.scrollTop) || (document.body && document.body.scrollTop);
}

function getBodyScrollLeft()
{
  return self.pageXOffset || (document.documentElement && document.documentElement.scrollLeft) || (document.body && document.body.scrollLeft);
}

function getClientCenterX()
{
    return parseInt(getClientWidth()/2)+getBodyScrollLeft();
}

function getClientCenterY()
{
    return parseInt(getClientHeight()/2)+getBodyScrollTop();
}

function setobjtoCenter(id)
{
  var x=getClientCenterX(); 
  var y=getClientCenterY(); 
  var objid= document.getElementById(id);
  if (objid)
  {
     objid.style.position="absolute";
     objid.style.left = (x-objid.offsetWidth/2)+"px";
     objid.style.top =  (y-objid.offsetHeight/2)+"px";
  }
}


function openid(x,id)
{
    var id_div= document.getElementById(id);
    var pos = getPosition(x);

    if (id_div)
    {
       if (id_div.style.visibility=='hidden')
       {
          id_div.style.left = (pos.x+10)+"px";
          id_div.style.top = (pos.y-10)+"px";
          id_div.style.visibility = 'visible';
       }
       else {
          id_div.style.visibility = 'hidden';
       }
    }
}


function openidXY(x,y,id)
{
    var objid= document.getElementById(id);
    if (objid)
    {
       objid.style.position="absolute";
       if (x<0) {
          x=getClientCenterX()-objid.offsetWidth/2;
       }
       if (y<0) {
          y=getClientCenterY()-objid.offsetHeight/2;
       }
       if (objid.style.visibility=='hidden')
       {
          objid.style.left = x+"px";
          objid.style.top =  y+"px";
          objid.style.visibility = 'visible';
       }
       else {
          objid.style.visibility = 'hidden';
       }
    }
}

function open_id(x,y,id,idf)
{
    var objid= document.getElementById(id);
    var objidf= document.getElementById(idf);
    if (objidf)
    {
       objidf.style.position="absolute";
       if (x<0) {
          x=objidf.offsetWidth-x;
       }
       if (y<0) {
          y=objidf.offsetWidth-y;
       }
       if (objid.style.display=='none')
       {
          objid.style.left = x+"px";
          objid.style.top =  y+"px";
          objid.style.display = 'block';
       }
       else {
          objid.style.display = 'none';
       }
    }
}


 function getPosition(o)
 {
    var x = o.offsetLeft;
    var y = o.offsetTop + o.offsetHeight;
    // + 1
    while (o.offsetParent != null)
    {
        o = o.offsetParent;
        x += o.offsetLeft;
        y += o.offsetTop;
    }
    var pos = new Object();
    pos.x = x;
    pos.y = y;
    return pos;
 }

function trim(s)
{
  return rtrim(ltrim(s));
}

function ltrim(s)
{
  return s.replace(/^\s+/, ''); 
}

function rtrim(s)
{
  return s.replace(/\s+$/, ''); 
}


