var Perwoll = function() {
	
	var Dom = YAHOO.util.Dom;
	var Event = YAHOO.util.Event;
	var Anim = YAHOO.util.Anim;
	var Net = YAHOO.util.Connect;
	
    
    // disabled "Gewinnen" click - start
	/*
    var navigation = document.getElementById('menu-item-gewinnen');
    var child = navigation.firstChild;
    
    child.style.cursor = 'default';
    
    Event.on(child, 'click', function(e) { 
        e =  Event.getEvent(e); 
        Event.stopEvent(e); 
        return false; 
    });
	*/
    // disabled "Gewinnen" click - end
    
    
	
	var tooltip = null;
	var hideTimeoutId = 0;
    
	// Setting input default values
	function getInputsDefault(el)
	{
		if(el.attributes["default"])
		{
			el.value = el.attributes["default"].nodeValue;
			Event.on(el, "focus", onInputFocus, {element : el, defaultValue : el.attributes["default"].nodeValue});
			Event.on(el, "blur", onInputBlur, {element : el, defaultValue : el.attributes["default"].nodeValue});
			
			
			return true;
		}
		
		return false;
	}
	
	function initTooltip()
	{
		tooltip = document.createElement("div");
		tooltip.id = "error-tooltip";
		tooltip.className = "tooltip";
		tooltip.innerHTML = [
		  '<div class="hd"></div>',
		  '<div class="bd"><div class="bd-wrap"><div></div></div></div>',
		  '<div class="ft"></div>',
		  '<a href="#close" class="close"></a>'
		].join("");
		
		Event.on(tooltip.lastChild, "click", Perwoll.hideErrorTooltip);
		
		Dom.get("page").appendChild(tooltip);
		
		
	}

	
	function onInputBlur(e, ea)
	{
		if(ea.element.value == "")
		{
			ea.element.value = ea.defaultValue;
		}
	}
	
	function onInputFocus(e, ea)
	{
		if(ea.element.value == ea.defaultValue)
		{
			ea.element.value = "";
		}
	}
	
	
	
	Dom.getElementsBy(getInputsDefault, "input", "");
	
	
	
	return  {
	hideErrorTooltip : function(e) {
		clearTimeout(hideTimeoutId)
		
		if(!tooltip)
		{
			return;
		}
		
		
		try
		{
		    e =  Event.getEvent(e);
		    Event.stopEvent(e);
		}
		catch(e) {}
		
		var bd = tooltip.firstChild.nextSibling.firstChild;
        bd.firstChild.innerHTML = "";
		
		Dom.setStyle(tooltip, "visibility", "hidden");
		
	},
	sendForm : function(formCheckCfg, formId, url, callbackFn) {
		var form = Dom.get(formId);
		
		this.hideErrorTooltip();;
		
		var formCheck = new FormCheck(formCheckCfg);
		var formVars = formCheck.getFormVars(formId);
		for(var field in formVars)
		{
			if(form[field] && !form[field].length && form[field].nodeName.toLowerCase() == "input" && form[field].type == "text")
			{
				var defaultVal = form[field].attributes["default"] ? form[field].attributes["default"].nodeValue : null;
				if(defaultVal == form[field].value)
				{
				   formVars[field] = "";
				}
			}
		}
		
		formCheck.check(formVars);
		
		
		var result = {
			success : false
		}
		
		if(formCheck.hasErrors())
		{
			var errors = formCheck.getErrorTextsAssoc();
			
			var field = errors[0].field;
			var message = errors[0].message;
		
			var fieldEl = (field == "birthdate") ? form.birthday : form[field];
			
			this.showErrorTooltip(message, fieldEl);
			
			
            callbackFn.call(this, result);					
		}
		else
		{
			var callback = {
				success : function(o) {
					result = null;
					try
					{
					  eval("var result=" + o.responseText);
					}
					catch(e)
					{
						alert("Leider konnte Deine Anfrage nicht Ã¼bertragen werden! Bitte versuche es spÃ¤ter erneut!");
					}
					
					if(!result.success)
					{
						for(var field in result.errors)
						{
							if(field == "captcha")
							{
								Dom.get("captcha-code").src = "/images/captcha.php?t=" + Math.round(Math.random() * 1000000);
							}
							
							Perwoll.showErrorTooltip(result.errors[field], form[field]);    
						}
					}
					
					
					callbackFn.call(this, result);
				}
			};
			
			Net.setForm(form);
			
			Net.asyncRequest("post", url, callback);
		}
		
	},
	showErrorTooltip : function(text, element) {
		if(tooltip === null)
		{
			initTooltip();
		}
		
		if(element.nodeName && element.nodeName.toLowerCase() == "input")
		{
			setTimeout(function() {
                element.focus();
            }, 500);
		}

		
		if(element.length)
		{
			element = element[0];
		}
		
		var elementPos = Dom.getXY(element);
		elementPos[0] += (element.offsetWidth - tooltip.offsetWidth) / 2;
		elementPos[1] -= 20;
		
		Dom.setXY(tooltip, elementPos);
		Dom.setStyle(tooltip, "visibility", "visible");
		
		var bd = tooltip.firstChild.nextSibling.firstChild;
		Dom.setStyle(bd, "height", 0);
		
		bd.firstChild.innerHTML = text;
		var height = bd.firstChild.offsetHeight;
		
		var newTop = parseInt(Dom.getStyle(tooltip, "top").replace("", ""), 10) - height;
		
		var expandBdAnim = new Anim(bd, {height: {to: height}}, 1, YAHOO.util.Easing.elasticOut);
		expandBdAnim.animate();
		
		var fadeIn = new Anim(bd.firstChild, {opacity : {from: 0, to: 1}}, .5, YAHOO.util.Easing.easeOut);
        fadeIn.animate();
		
		var expandAnim = new Anim(tooltip, {top: {to: newTop}}, 1, YAHOO.util.Easing.elasticOut);
		expandAnim.animate();
		
		hideTimeoutId = setTimeout(Perwoll.hideErrorTooltip, 4500);
	}
};
	
}();
