var Sweepstake = function() {
	var yd = YAHOO.util.Dom;
	var ye = YAHOO.util.Event;
	var isSending = false;
	
	var enterCodeBtn, sendCodeBtn, sendFormBtn;
	
	initializeForms();
	
	YAHOO.util.History.register("page", "default", onPageChange);
	
	YAHOO.util.History.onReady(function () {
        var currentPage = YAHOO.util.History.getCurrentState("page");
		if(currentPage == "terms")
		{
			showTerms();
			redrawIe6();
		}
    });

	
	try {
        YAHOO.util.History.initialize("yui-history-field", "yui-history-iframe");
    } catch (e) {
    }
	
	function hideTerms()
	{
		yd.setStyle("sweepstake-home", "display", "block");
		yd.setStyle("teasers", "display", "block");
        yd.setStyle("sweepstake-participation-start", "display", "block");
        yd.setStyle("sweepstake-participation-terms", "display", "none");
	}
	
    function initializeForms()
	{
		sendFormBtn = yd.get("send-form-btn");
		if(sendFormBtn)
        {
           initParticipationForm();
        }
	}
	
	function initParticipationForm()
	{
		ye.on(sendFormBtn, "click", onSendForm);
           
        var inputs = yd.getElementsByClassName("input");
        var defaultVal = "", eventArgs;
		
        for(var i = 0; i < inputs.length; i++)
        {
	          defaultVal = inputs[i].attributes["defaultvalue"] ? inputs[i].attributes["defaultvalue"].nodeValue : null;
			  
	          if(defaultVal == null)
	          {
	            continue;
	          }
	          inputs[i].value = defaultVal;
	           
	          eventArgs = {input : inputs[i], defaultVal : defaultVal};
	              
	          ye.on(inputs[i], "focus", function(e, ea) {
		          if(ea.input.value == ea.defaultVal)
		          {
		              ea.input.value = "";
		          }
		        }, eventArgs);
	              
	          ye.on(inputs[i], "blur", function(e, ea) {
		          if(ea.input.value == "")
		          {
		            ea.input.value = ea.defaultVal;
		          }
	          }, eventArgs);
        }
        
		
        ye.on("sweepstake-terms-link", "click", function(e) {
			ye.stopEvent(ye.getEvent(e));
			
			YAHOO.util.History.navigate("page", "terms");
		});
		
		ye.on("sweepstake-participation-terms-back", "click", function(e) {
		    ye.stopEvent(ye.getEvent(e));
			
			YAHOO.util.History.navigate("page", "default");
	    });
	}
	
	
	ye.on(window, "load", correctDialogT);
	
	function correctDialogT() {
		yd.setStyle("sweepstake-dialog-t", "display", "none");
        yd.setStyle("sweepstake-dialog-t", "display", "block");
		
		redrawIe6();
		
		
	}
	
	function onCodeSended(ea)
    {
        isSending = false;
        
        if(ea.success)
        {
            yd.setStyle("sweepstake-send-code-start", "display", "none");
            yd.setStyle("sweepstake-send-code-thank-you", "display", "block");
			redrawIe6();
        }
    }
	
	function onEnterCode(e)
	{
		e = ye.getEvent(e);
		ye.stopEvent(e);
		
		if(isSending)
		{
			return;
		}
		
		isSending = true;
		
		Application.sendForm(enterCodeForm, "sweepstake-enter-code-form", "/index.php?ajax=1&ext=sweepstake&action=CheckCode", onEnterCodeSended, yd.getLastChild(enterCodeBtn));
		
	}
	
	function onEnterCodeSended(ea)
	{
		isSending = false;
		
		if(ea.success)
		{
		  var form = yd.get("sweepstake-enter-code-form");
	       document.location = "/aktuell/code-gewinnspiel-formular.html?code=" + form.code.value;
		}
	}
	
	function onPageChange(pageName)
    {
        Application.hideErrorTooltip();
        
        if(pageName == "terms")
        {
            showTerms();
        }
		else if(pageName == "catsan-terms")
		{
			showCatsanTerms();
		}
        else
        {
            hideTerms();
        }
        
        redrawIe6();
    } 
	
	function onSendCode(e)
	{
		e = ye.getEvent(e);
        ye.stopEvent(e);
		
		if(isSending)
        {
            return;
        }
		
		isSending = true;
		
		Application.sendForm(sendCodeForm, "sweepstake-send-code-form", "/index.php?ajax=1&ext=sweepstake&action=SendCode", onCodeSended, yd.getLastChild(sendCodeBtn));
	}
	
	
	
	function onSendForm(e)
    {
        e = ye.getEvent(e);
        ye.stopEvent(e);
        
		if(isSending)
        {
            return;
        }
        
        isSending = true;
		
		var form = yd.get("sweepstake-participation-form-frm");
		
		form.birthdate.value = form.birthyear.value + "-" + form.birthmonth.value + "-" + form.birthday.value;
		
        Application.sendForm(formCfg, form, "/index.php?ajax=1&ext=sweepstake&action=CheckUserForm", onFormSended, yd.getLastChild(sendFormBtn));
    }
	
	function onFormSended(ea) {
		
		isSending = false;
		
		if(ea.success)
		{
			yd.setStyle("sweepstake-participation-start", "display", "none");
			yd.get("sweepstake-participation-text").innerHTML = ea.htmlText;
            yd.setStyle("sweepstake-participation-thank-you", "display", "block");
			
			
			if(ea.jsCode)
			{
				eval(ea.jsCode);
			}
			
			var iframe = yd.get("dblclick-iframe");
			if(iframe && !ea.exists)
			{
				var axel = Math.random() + "";
                var a = axel * 10000000000000;
				iframe.src = "http://fls.doubleclick.net/activityi;src=2084134;type=whiskasc;cat=whi_c096;qty=1;cost=1.00;ord=" + ea.userId + "?";
			}
			
			redrawIe6();
		}
	}
	
	function redrawIe6()
	{
		if(YAHOO.env.ua.ie == 6)
        {
            setTimeout(function() {
                var div = document.createElement("div");
				div.style.height = "500px";
                document.body.appendChild(div);				
                document.body.removeChild(div);				
            }, 100);
        }
	}
	
	function showTerms()
    {
        yd.setStyle("sweepstake-home", "display", "none");
		yd.setStyle("teasers", "display", "none");
        yd.setStyle("sweepstake-participation-start", "display", "none");
        yd.setStyle("sweepstake-participation-terms", "display", "block");
    }
	
	function showCatsanTerms()
    {
        yd.setStyle("sweepstake-home", "display", "none");
        yd.setStyle("teasers", "display", "none");
        yd.setStyle("sweepstake-participation-start", "display", "none");
        yd.setStyle("sweepstake-catsan-terms", "display", "block");
    }
    
	
	return {
		
	};
}();
