
(function() {

	var nav, featured;

	// Cycle

	var cycle = {
		index: 0,
		max: 5,
		stop: false,
		pause: false,
		timer: null,
		interval: 5000,
		rotate: function() {
			if (!cycle.stop) {
				if (!cycle.pause && cycle.timer) {
					$(nav[cycle.index]).removeClass('active');
					cycle.index = (cycle.index >= cycle.max - 1 ? 0 : ++cycle.index);
					$(nav[cycle.index]).addClass('active');
					featured.stop(true).animate({left: (0 - (cycle.index * 461))});
				}
				cycle.timer = setTimeout(cycle.rotate, cycle.interval);
			}
		}
	};

	// Clock

	var clock = {
		init: function() {
			clock.time = new Date(registry_time * 1000);
			clock.output = $('#time');
			clock.tick();
		},
		tick: function() {
			clock.time.setUTCSeconds(clock.time.getUTCSeconds() + 1);
			clock.output.html('Registry Time: ' + clock.time.toUTCString().replace(/gmt/i, 'UTC'));
			setTimeout(clock.tick, 1000);
		}
	};

	function init() {

		// JS Mode

		$('body').addClass('js');

		// Login form
  $(document).ready(function () {
    setTimeout(function(){
      if ($('#login input.text').val() != '') {
        $('#login input.text').prev().css('display', 'none');
      }
    }, 1000);
  });

		$('#login input.text').each(function(i, field) {
			field = $(field);
			if (field.val().length > 0) {
				field.prev().css('display', 'none');
			}
			field.focus(function() {
				field.prev().css('display', 'none');
			}).blur(function() {
				if (field.val() == '') field.prev().css('display', 'block');
			});
		})

		// Domain search

		$('#search label').click(function() {
			var label = $(this),
				checkbox = label.prev();
			if (checkbox.attr('checked')) {
				checkbox.removeAttr('checked');
				label.addClass('off');
			} else {
				checkbox.attr('checked', 'checked');
				label.removeClass('off');
			}
			return false;
		});

		$('#check').submit(function() {
			var search_window = window.open(
				$(this).attr('action') + '?' + $(this).serialize(),
				'domainsearch',
				'location=0,width=800,height=500,toolbar=0,scrollbars=1,resizable=1,history=0,status=0'
			);
			search_window.focus();
			return false;
		});

		// Featured items

		nav = $('#hls_nav li a');
		featured = $('#hls_items');

		$('#hls_nav li.hls_1 a').addClass('active');

		nav.click(function() {
			if (!$(this).hasClass('active')) {
				nav.removeClass('active');
				featured.stop(true).animate({left: (0 - ($(this).parent().attr('className').replace(/hls_/, '') - 1) * 461)});
				$(this).addClass('active');
			}
			cycle.stop = true;
			return false;
		});

		featured.mouseenter(function() {
			cycle.pause = true;
		}).mouseleave(function() {
			cycle.pause = false;
		});

		// Clock

		clock.init();
	}

	// Lofi

	var keys = [],
		sequence = '38,38,40,40,37,39,37,39,66,65';

	$(document).keydown(function(event) {
		if (event.target.tagName.toLowerCase() != 'input') {
			keys.push(event.keyCode);
			if (keys.toString().indexOf(sequence) >= 0) {
				$('body').addClass('lofi').html('<img src="/images/fab/home/lofi.png" width="960" height="1345" alt="lofi" />')
			}
		}
	}, true);

	$(function() {
		init();
	});

	$(window).load(function() {
		cycle.rotate();
	});

})();
