$(document).ready(function() {

  $("#attention>ul").liScroll({travelocity: 0.05});


  $("#photo-slider").nivoSlider({
    effect:"random",
    slices:15,
    boxCols:8,
    boxRows:4,
    animSpeed:500,
    pauseTime:5000,
    startSlide:0,
    directionNav:true,
    directionNavHide:true,
    controlNav:true,
    controlNavThumbs:false,
    controlNavThumbsFromRel:true,
    keyboardNav:true,
    pauseOnHover:true,
    manualAdvance:false
  });

  $('#news-carusel .bx').bxSlider({autoStart:true, auto:true, pause:30000 });

  $('#jparallax').show();
  jQuery('.parallax_layer').parallax(
    { mouseport:$('#media,#middle-bg') },
    { xparallax:-0.2, yparallax:0 },
    { xparallax:-0.15, yparallax:0 },
    { xparallax:-0.1,  yparallax:0 }

  );

  /*Site Map scroller*/
  var slide = false;
  var height = $('#site-map').height();
  var isfb = 0;
  $('#site-map-button').click(function() {
    var docHeight = $(document).height();
    var windowHeight = $(window).height();
    var scrollPos = docHeight - windowHeight + height;
    if (!isfb) {
      $('#facebookContainer').append('<iframe scrolling="no" frameborder="0" allowTransparency="true" id="facebookFrame"></iframe>');
      $('#facebookFrame').attr('src', $('#facebookUrl').text());
      isfb = 1;
    }
    $('#site-map').animate({ height: "toggle"}, 1000);
    if (!slide) {
      if ($.browser.opera) { //Fix opera double scroll bug by targeting only HTML.
        $('html').animate({scrollTop: scrollPos + 'px'}, 1000);
      } else {
        $('html, body').animate({scrollTop: scrollPos + 'px'}, 1000);
      }
      slide = true;
    } else {
      slide = false;
    }
  });

  $(window).bind('load resize', function() {
    var ww = $(window).width();
    /*if (ww>=1100) $('#palm-leaves,#pbg1,#pbg3').show(); else $('#palm-leaves,#pbg1,#pbg3').hide();*/
    if (ww>=1100) $('#pbg1,#pbg3').show(); else $('#pbg1,#pbg3').hide();


  });

  $('#usr').tezEnter(doLogin);
  $('#pwd').tezEnter(doLogin);
  $('#loginbtn').click(doLogin);
  $(document).keyup(function(e) { if (e.keyCode == 27) { if (!$('#translate').is(':hidden')) $('#translate').modalClose(); else $('#translate').modal('#trans', 0); }});
  $('#recaptcha,#captcha').click(function() { $('#captcha').attr('src', $('#captcha').attr('src') + '?');});
  $('textarea#message').tezMaxLength(function(cnt) { $('#charsleft').text(cnt);});

});

function doLogin() {

  if ($.trim($('#usr').val()) == '') {
    $('#usr').focus();
    return;
  }
  if ($.trim($('#pwd').val()) == '') {
    $('#pwd').focus();
    return;
  }
  $('#loginfrm').tezSubmit(function(msg) {
    if (msg.ok == 1) {
      top.location.href = msg.url;
      return;
    }
    showError($('#usr'), msg.info, null, 'topLeft');
    $('#usr').focus();
  }, null, $('#loginbtn'), 'input-button-dis');
}

function doSendSms() {

  var frm = $('#frmSendSms');
  if ($.trim(frm.find('input[name=num]').val()) == '') {
    frm.find('input[name=num]').focus();
    return;
  }
  if ($.trim(frm.find('textarea[name=msg]').val()) == '') {
    frm.find('textarea[name=msg]').focus();
    return;
  }
  if ($.trim(frm.find('input[name=code]').val()) == '') {
    frm.find('input[name=code]').focus();
    return;
  }

  frm.tezSubmit(function(msg) {

    if (msg.ok == 1) {

      frm.find('textarea[name=msg]').val('');
      frm.find('input[name=code]').val('');
      $('#captcha').click();
      frm.find('textarea[name=msg]').focus();
      showSuccess($('#smsNumLabel'), msg.info);
    }
    else {
      showError(frm.find(msg.fld), msg.info);
    }
  }, null, $('#btnSendsms'), 'input-button-dis');
}

var rates = null;
function doRates() {

  if (rates) {
    showRates();
    return;
  }

  $.ajax({
    type: 'POST',
    url: '/rates/get',
    dataType: 'json',
    success: function(msg) {
      rates = msg;
      showRates();
    }
  });
}

function showRates() {

  var id = $('#country').val();
  if (id) {
    $('#rates_inc').text(rates[id].inc);
    $('#rates_out').text(rates[id].out);
    $('#rates_sms').text(rates[id].sms);
    $('#rates_gprs').text(rates[id].gprs);
  }
  else {
    $('#rates_inc').text('');
    $('#rates_out').text('');
    $('#rates_sms').text('');
    $('#rates_gprs').text('');
  }
}

function showError(el, text, color, target) {

  if (!color) color = 'red';
  if (!target) target = 'topRight';
  el.qtip({
    content: text,
    show: { when: { event: 'none'}, ready: true },
    hide: { when: { event: 'unfocus' }, delay: 0 },
    position: {
      corner: {
        target: target,
        tooltip: 'bottomLeft'
      }
    },
    style: {
      tip: true,
      name: color
    }
  });
}

function showSuccess(el, text) {

  showError(el, text, 'green');
}

function submitForm(btn) {

  var frm = $(btn.form);

  frm.tezSubmit(function(msg) {

    if (msg.func) window[msg.func](msg);
    if (msg.ok == 1) {
      if (msg.url) top.location.href=msg.url;
      else if(msg.next) $('.next').click();
      else if (msg.reload) top.location.reload();
    }
    else {
      showError(frm.find(msg.fld), msg.info);
      frm.find(msg.fld).focus();
    }
  }, null, $(btn), 'input-button-dis');
}

function addCities(val, el, lurl) {

  $.ajax({
    type: 'POST',
    url: lurl+'/buy/cities/'+val,
    dataType: 'json',
    success: function(opt) {
      el.find('option').remove();
      for(var k in opt) el.append(new Option(opt[k], k));
    }
  });
}

function recaptcha() {

  $('#captcha').click();
  $('#code').val('');
}
