(function($) {
  $.fn.typewriter = function() {
    this.each(function() {
      var $ele = $(this), str = $ele.text(), progress = 0;
      var waitloop = 50;
      $ele.text('');
      var sp = setTimeout(function() {
        $ele.css('display', 'block');
        var timer = setInterval(function() {
          if (str.substring(progress -2, progress) == '. ' && waitloop-- > 0) {
            // do nothing
          } else {
            waitloop = 20;
            $ele.text(str.substring(0, progress++) + '_');
          }
          if (progress >= str.length) {
            clearInterval(timer);
            $ele.text(str.substring(0, progress));
          }
        }, 40);
      }, 2000);
    });
    return this;
  };
})(jQuery);

function uti() {
  try {
    var days = parseInt(document.getElementById('cd_days').innerHTML, 10);
    var hours = parseInt(document.getElementById('cd_hours').innerHTML, 10);
    var mins = parseInt(document.getElementById('cd_mins').innerHTML, 10);
    var secs = parseInt(document.getElementById('cd_secs').innerHTML, 10);

    if (mins == 0 && hours == 0 && days == 0 && secs == 0) {
      return;
    }

    secs--;
    if(secs < 0) {
      if(mins > 0 || hours > 0 || days > 0) {
        secs = 59;
        mins--;
      }
    }

    if(mins < 0) {
      if(hours > 0 || days > 0) {
        mins = 59;
        hours--;
      }
    }

    if(hours < 0) {
      hours = 23;
      days--;
    }

    document.getElementById('cd_days').innerHTML = (days < 10) ? '0' + days : days;
    document.getElementById('cd_hours').innerHTML = (hours < 10) ? '0' + hours : hours;
    document.getElementById('cd_mins').innerHTML = (mins < 10) ? '0' + mins : mins;
    document.getElementById('cd_secs').innerHTML = (secs < 10) ? '0' + secs : secs;
  } catch (exception) { }
}

function checkLogin(login) {
  if (login.length < 3) {
    alert(error_login_short);
    document.forms['newuserform'].login.focus();
    return;
  }
  ro.open('get', 'ajax/logincheck.php?u=' + encodeURIComponent(login));
  ro.onreadystatechange = handleCheckLogin;
  ro.send(null);
}

function handleCheckLogin() {
  if(ro.readyState == 4) {
    var response = ro.responseText;
    if(response == 'in_use') {
      alert(error_login_inuse);
      document.forms['newuserform'].login.focus();
      document.forms['newuserform'].login.select();
    } else {
      alert(alert_login_ok);
    }
  }
}

window.onload = function() {
  var test = setInterval('uti()', 1000);

  if(!NiftyCheck()) return;
  RoundedTop('div#container_title', '#373737', '#6c0');
  RoundedBottom('div#container_title', '#414141', '#96DD4D');

  RoundedTop('a.mc_menu_item', '#434343', '#99cbfd');
  RoundedBottom('a.mc_menu_item', '#535353', '#83a9d1');

  RoundedTop('div#mc_login', '#434343', '#970000');
  RoundedBottom('div#mc_login', '#535353', '#6d0000');

  try {
    document.getElementById('mc_container_featured');
    RoundedTop('div#mc_container_featured', '#555555', '#000');
    RoundedBottom('div#mc_container_featured', '#666', '#000');

    RoundedTop('div#mc_container_body', '#666', '#333');
    RoundedTop('div#mc_container_right', '#666', '#69c');

    $("#feat_text").typewriter();
  } catch (exception) {
    RoundedTop('div#mc_container_body', '#545454', '#333');
    RoundedTop('div#mc_container_right', '#545454', '#69c');
  }

  try {
    setCitiesAndFlag(document.forms['newuserform'].mc_country.value);
    if(document.forms['newuserform'].city.value == 'Anonymous') {
      noPersonal();
    }
  } catch (exception) { }
}

if(window != top) top.location.href=location.href;

var ro = createRequestObject();