// pings (wc)
function imstillhere() {
  $.get(SITEROOT + '/ajax/omghy');
}
setInterval("imstillhere()", 60000);

// temperatuura
function howhotisitinhere() {
  $.ajax({
    type: "GET",
    url: SITEROOT + '/ajax/issithot',
    success: function(res){
      $('#temperature').html(res)
    }
  });
}
setInterval("howhotisitinhere()", 180000);

// wc lodziņš
function open_wc() {
  window.open(SITEROOT + '/webcam/','webcam','width=361,height=310');
}
// WC
function refresh_wc(size) {
  // size - 168x137, 352x288
  if (typeof(size) == "undefined" || !in_array(size, ['168x137', '352x288'])) {
    size = '352x288';
  }
  $('#wc').attr("src", SITEROOT + '/webcam/wc-' + size + '.jpg?' + Math.random());
}

// kontaktforma
function print_msg(msg) {
  $('#msg').remove();
  if (msg != "") {
    msg = '<div id="msg">' + msg + '</div>';
    $('#contacts').prepend(msg);
  }
}
function sayhellotomylittlefriend() {
  var error = false;
  var msg = "";
  var cname = $('#cname');
  var cemail = $('#cemail');
  var ctext = $('#ctext');
  
  if (cname.val() == "") {
    msg = contacts_txt_err_name;
    cname.focus();
    error = true;
  }

  if (!error && ctext.val() == "") {
    msg = contacts_txt_err_text;
    ctext.focus();
    error = true;
  }
  
  var filter = /^[a-z0-9\._-]+@([a-z0-9_-]+\.)+[a-z]{2,6}$/i;
  if (!error && !filter.test(cemail.val())) {
    msg = contacts_txt_err_email;
    cemail.focus();
    error = true;
  }
  
  if (!error) {
    $.ajax({
      type: "POST",
      url: SITEROOT + '/ajax/contacts',
      data: "cname=" + cname.val() + "&cemail=" + cemail.val() + "&ctext=" + ctext.val(),
      async: false,
      success: function(res) {
        if (res == "ok") {
          $('#contacts').empty();
          msg = contacts_txt_ok;
        } else {
          msg = res;
        }
        print_msg(msg);
      }
    });
  }
  
  print_msg(msg);
}

function unemployedinsummertime() {
  var error = false;
  var msg = "";
  var cname = $('#cname');
  var cemail = $('#cemail');
  var ctext = $('#ctext');
  // var vacancy = $('#vacancy option:selected');
  
  if (cname.val() == "") {
    msg = contacts_txt_err_name;
    cname.focus();
    error = true;
  }

  if (!error && ctext.val() == "") {
    msg = contacts_txt_err_text;
    ctext.focus();
    error = true;
  }
  
  // if (!error && vacancy.val() == "") {
  //   msg = vacancies_txt_err_vacancy;
  //   vacancy.focus();
  //   error = true;
  // }
  
  var filter = /^[a-z0-9\._-]+@([a-z0-9_-]+\.)+[a-z]{2,6}$/i;
  if (!error && !filter.test(cemail.val())) {
    msg = contacts_txt_err_email;
    cemail.focus();
    error = true;
  }
  
  if (!error) {
    $.ajax({
      type: "POST",
      url: SITEROOT + '/ajax/contacts',
      data: "cname=" + cname.val() + "&cemail=" + cemail.val() + "&ctext=" + ctext.val() /*+ "&vacancy=" + vacancy.text()*/,
      async: false,
      success: function(res) {
        if (res == "ok") {
          $('#contacts').empty();
          msg = contacts_txt_ok;
        } else {
          msg = res;
        }
        print_msg(msg);
      }
    });
  }
  
  print_msg(msg);
}

//-----------------------------------------------
// asociatīvo masīvu simulācija
function arrCount(array) {
  var i = 0;
  if (typeof(array) == "object") {
    for (var tmp in array) {
      i++;
    }
  }
  return i;
}

function arrGetNextKey(array, currentKey) {
  var i = 0;
  if (typeof(array) == "object") {
    for (var tmp in array) {
      if (tmp == currentKey) {
        break;
      }
      i++;
    }
    var j = 0;
    for (var tmp in array) {
      if (j == i+1) {
        return tmp;
        break;
      }
      j++;
    }
  }
}
function arrGetPrevKey(array, currentKey) {
  var i = 0;
  if (typeof(array) == "object") {
    for (var tmp in array) {
      if (tmp == currentKey) {
        break;
      }
      i++;
    }
    var j = 0;
    for (var tmp in array) {
      if (j == i-1) {
        return tmp;
        break;
      }
      j++;
    }
  }
}

function arrGetValue(array, key) {
  if (typeof(array) == "object") {
    for (var tmp in array) {
      if (tmp == key) {
        return array[tmp];
      }
    }
  }
}

function arrGetCurrentPos(array, currentKey) {
  var i = 0;
  if (typeof(array) == "object") {
    for (var tmp in array) {
      if (tmp == currentKey) {
        return i;
      }
      i++;
    }
  }
}
//-----------------------------------------------
function in_array(el, arr) {
  for (var i = 0; i < arr.length; i++) {
  	if (arr[i] == el) {
			return true;
		}
  }
  return false;
}

function ucfirst(str) {
  // http://kevin.vanzonneveld.net
  var f = str.charAt(0).toUpperCase();
  return f + str.substr(1, str.length-1);
}

function trim(str, charlist) {
    // http://kevin.vanzonneveld.net
 
    var whitespace;
    
    if (!charlist){
      whitespace = ' \n\r\t\f\x0b\xa0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000';
    } else {
      whitespace = charlist.replace(/([\[\]\(\)\.\?\/\*\{\}\+\$\^\:])/g, '\$1');
    }
  
  for (var i = 0; i < str.length; i++) {
    if (whitespace.indexOf(str.charAt(i)) === -1) {
      str = str.substring(i);
      break;
    }
  }
  for (i = str.length - 1; i >= 0; i--) {
    if (whitespace.indexOf(str.charAt(i)) === -1) {
      str = str.substring(0, i + 1);
      break;
    }
  }
  return whitespace.indexOf(str.charAt(0)) === -1 ? str : '';
}

function str_ireplace(search, replace, subject) {
    // http://kevin.vanzonneveld.net
    
    var i;
    
    if(!(replace instanceof Array)){
        replace=new Array(replace);
        if(search instanceof Array){//If search    is an array and replace    is a string, then this replacement string is used for every value of search
            while(search.length>replace.length){
                replace[replace.length]=replace[0];
            }
        }
    }
 
    if(!(search instanceof Array))search=new Array(search);
    while(search.length>replace.length){//If replace    has fewer values than search , then an empty string is used for the rest of replacement values
      replace[replace.length]='';
    }
 
    if (subject instanceof Array) {//If subject is an array, then the search and replace is performed with every entry of subject , and the return value is an array as well.
      for(k in subject){
          subject[k]=str_replace(search,replace,subject[k]);
      }
      return subject;
    }
 
    for (var k=0; k<search.length; k++){
      reg = new RegExp(search[k], 'gi');
      subject = subject.replace(reg, replace[k]);
    }
    return subject;
}
//-----------------------------------------------

$(document).ready(function(){
  $('tr td:first-child, tr th:first-child').addClass('col-first');
  $('tr td:last-child, tr th:last-child').addClass('col-last');

  if (typeof(loadLightBox) != "undefined" && loadLightBox) {
    $("a.lightbox").lightBox({
      imageLoading: SITEROOT + "/js/lightbox/images/lightbox-ico-loading.gif",
      imageBtnPrev: SITEROOT + "/js/lightbox/images/lightbox-btn-prev.gif",
      imageBtnNext: SITEROOT + "/js/lightbox/images/lightbox-btn-next.gif",
      imageBtnClose: SITEROOT + "/js/lightbox/images/lightbox-btn-close.gif",
      imageBlank: SITEROOT + "/js/lightbox/images/lightbox-blank.gif"
    });
  }
});

function ga_trackLink(linkname) {
  try {
    pageTracker._trackPageview(linkname);    
  } catch (e) {
    // alert("ga_trackLink: " + e);
  }
}
