window.addEvent('domready', windowLoad);

var backgroundImageUrl = '/heeldice/extension/heeldice/design/ezwebin/images/bg/';

function windowLoad() {
	setInputToggles();
	setBackgroundImage();
	setupZoom();
}

function setInputToggles()
{
	
	var relevantForms = $ES('form').filterByClass('hide-inputs');
	
	
	var inputFields = new Array();
	var textAreas = new Array();
	
	
	relevantForms.each(function(element){		
		inputFields.merge(element.getElements('input').filterByAttribute('type', '=', 'text'));
	    
		textAreas.merge(element.getElements('textarea'));
	});
    
	
	inputFields.each(function(element){toggleTextFields(element)});
	textAreas.each(function(element){toggleTextArea(element)});

}

function toggleTextArea(element)
{
   element.addEvent('focus', function(){
        element.originalValue = element.innerHTML;
        element.innerHTML = '';
       }
    );
    
   element.addEvent('blur', function(){
        if(element.value == '') {
            element.innerHTML = element.originalValue;
        }
       }
    );
}

function toggleTextFields(element)
{
	if(element.hasClass('password')) {return};
	
	element.addEvent('focus', function(){
		element.originalValue = element.value;
		element.setProperties({value: ''});
	   }
	);
	
   element.addEvent('blur', function(){
		if(element.value == '') {
			element.value = element.originalValue;
		}
       }
    );
}

function togglePasswordFields(element) 
{
	element.originalValue = element.value;
    
	element.addEvent('focus', function(){ 
            element.setProperties({
				type: 'password'
			});
	        element.value = '';
	   }
    );
   
   element.addEvent('blur', function(){
		if(element.value == '') {
			element.setProperties({
                type: 'text'
            });
            element.value = element.originalValue;
        }
       }
    );
}

function initFaqToggles()
{
    var faqQuestions = $ES('a').filterByClass('faq-question');
    var faqAnswers = $ES('p').filterByClass('faq-answer');
    
    faqAnswers.each(function(element) {
        element.setStyles({display: 'none'});
    });
    
    faqQuestions.each(function(element) {
        element.addEvent('click', toggleAnswer.bindWithEvent(element, new Array(element)));
    });
}

function toggleAnswer(event, element) 
{
    event.stop();
    var questionID = element.id.split('-')[1];
    var answer = $('answer-'+questionID);
        
    if (answer.style.display == 'none') {
        answer.style.display = 'block';
		//$('footer').setStyles({'bottom': '0px'}); // IE!!!!
    } else {
        answer.style.display = 'none';
		//$('footer').setStyles({'bottom': '1px'}); // IE!!!!
    }
	
	if ($('footer').style.bottom == '0px') {
		$('footer').setStyles({'bottom': '1px'});
	} else {
		$('footer').setStyles({'bottom': '0'});
	}
	
}

function setBackgroundImage()
{		
	document.body.style.backgroundImage = "url(https://www.heeldice.com/extension/heeldice/design/ezwebin/images/bgs/bg-3.jpg)";
}

/* Setzt den Sprachencode als Cookie
 * 
 * @param {Object} el
 */
function setLangCookie(el) 
{
	//var myCookie = Cookie.write('heeldice_lang', el);
	var elString = el.toString();	
	var elParts = elString.split('/');
	
	//Sprachencode ermitteln
	var countryCode = elParts[(elParts.length-1)];
	
	deleteCookie('heeldice_lang');
	setCookie('heeldice_lang', countryCode, 50);
}

function setCookie(cookieName,cookieValue,nDays) {
	 var today = new Date();
	 var expire = new Date();
	 if (nDays==null || nDays==0) nDays=1;
	 expire.setTime(today.getTime() + 3600000*24*nDays);
	 document.cookie = cookieName+"="+escape(cookieValue)
	                 + ";expires="+expire.toGMTString()
					 + ";path=/";
}

function deleteCookie(name){
    if(getCookie(name)){
        setCookie(name, '', -30);
    }
} 

function getCookie(name) {
    var sPos = document.cookie.indexOf(name + "=");
    var len = sPos + name.length + 1;
    if((!sPos) && (name != document.cookie.substring(0, name.length))){
        return null;
    }
    if(sPos == -1){
        return null;
    }
    var ePos = document.cookie.indexOf(';', len);
    if(ePos == -1) ePos = document.cookie.length;
    return unescape(document.cookie.substring(len, ePos));
} 
