$(function () {
    var fb = $('#findbox');
    var delay;
    $('#navbar>li.find').mouseenter(function () {
    	clearTimeout(delay);
        if (fb.css('display', 'none')) fb.show();
    }).mouseleave(function () {
    	clearTimeout(delay);
        delay = setTimeout("delayHide()", 1000);
    }).mouseleave();
    $('#navbar>.find>a').click(function () {
        return false;
    });

    $('#email, #image').each(function () {
        this.defaultText = this.value;
    }).focus(function() {
        if (this.value == this.defaultText)
            this.value = '';
        return true;
    }).blur(function () {
        if (this.value == '')
            this.value = this.defaultText;
        return true;
    });

    $('#findbox').submit(function () {
    	if (this.email.value == this.email.defaultText &&
    		this.image.value == this.image.defaultText) {
    		alert(lang.enterCriterion);
    		return false;
    	}
        $('#email, #image').each(function () {
            if (this.value == this.defaultText)
                this.value = '';
        });
        return true;
    });
});

function delayHide() {
	var fb = $('#findbox');
	if (fb.css('display', 'block')) fb.hide();
}


/* Sets the max length of the textareas
   From: quirksmode.org */

function setMaxLength() {
	var x = document.getElementsByTagName('textarea');
	var counter = document.createElement('div');
	counter.className = 'counter';
	for (var i=0;i<x.length;i++) {
		if (x[i].getAttribute('maxlength')) {
			var counterClone = counter.cloneNode(true);
			counterClone.relatedElement = x[i];
			counterClone.innerHTML = '<small><i><span>0</span> ' + lang.of + ' ' +x[i].getAttribute('maxlength')+ ' ' + lang.charactersUsed +  '.</i></small>';
			x[i].parentNode.insertBefore(counterClone,x[i].nextSibling);
			x[i].relatedElement = counterClone.getElementsByTagName('span')[0];
			x[i].onkeyup = x[i].onchange = checkMaxLength;
			x[i].onkeyup();
		}
	}
}

function checkMaxLength() {
	var maxLength = this.getAttribute('maxlength');
	var currentLength = this.value.length;
	if (currentLength > maxLength) {
		this.relatedElement.className = 'toomuch';
		this.value = this.value.substr(0,maxLength);
	    this.relatedElement.firstChild.nodeValue = maxLength;
	} else {
		this.relatedElement.className = '';
	    this.relatedElement.firstChild.nodeValue = currentLength;
    }
}
$(setMaxLength);
