/* Input Placeholder */
input_placeholder = function()
{
	if(!!('placeholder' in document.createElement('input')))
	{
		$$('input[placeholder]').each(function(element)
		{
			element.value="";
		});
		return;
	}
	$$('input[placeholder]').each(function(element)
	{
		element.addClassName("empty");
		if(element.getAttribute("type")=="password")
		{
			element.addClassName("password");
			changeInputType(element,"text");
		}
	});
	$$('input[placeholder]').each(function(element)
	{
		element.observe('focus',function(e)
		{
			if(element.value==element.getAttribute("placeholder"))
			{
				element.value="";
				element.removeClassName("empty");
				if(element.className.search(/password/)!=-1)
				{
					changeInputType(element,"password");
				}
			}
		});
		element.observe('blur',function(e)
		{
			if(element.value=="")
			{
				element.value=element.getAttribute("placeholder");
				element.addClassName("empty");
				if(element.className.search(/password/)!=-1)
				{
					changeInputType(element,"text");
				}
			}
		});
	});
}

/* Global */
readCookie = function(name)
{
	a = document.cookie;
	res = '';
	while(a != ''){
		while(a.substr(0,1) == ' '){
			a = a.substr(1,a.length);
		}
		cookiename = a.substring(0,a.indexOf('='));
		if(a.indexOf(';') != -1){
			cookiewert = a.substring(a.indexOf('=')+1,a.indexOf(';'));
		}else{
			cookiewert = a.substr(a.indexOf('=')+1,a.length);
		}
		if(name == cookiename){
			res = cookiewert;
		}
		i = a.indexOf(';')+1;
		if(i == 0){
			i = a.length;
		}
		a = a.substring(i,a.length);
	}
	return(unescape(res));
}

setCookie = function(name, value)
{
	document.cookie = name+'='+escape( value ) + ';path=/';
}


function getScrollY() {
    var scrOfX = 0, scrOfY = 0;
 
    if( typeof( window.pageYOffset ) == 'number' ) {
        scrOfY = window.pageYOffset;
    } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
        //DOM compliant
        scrOfY = document.body.scrollTop;
    } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
        //IE6 standards compliant mode
        scrOfY = document.documentElement.scrollTop;
    }
    return scrOfY;
}


changeInputType = function(element,type){
	try
	{
		element.type = type;
	}
	catch(e)
	{
	}
}

disable_inputs = function(id,disabled){
	if(disabled == true){
		$(id).style.display = "none";
	}else{
		$(id).style.display = "block";
	}
	
	$$('#'+id+'>input').each(function(obj){
		obj.disabled = disabled;
	});
}

check_all = function(id,checked){
	$$('#'+id+' input').each(function(obj){
		obj.checked = checked;
	});
}

addBookmark = function(){
	try
	{
		window.external.AddFavorite('http://industrierat.de/', 'Industrierat.de')
	}
	catch(e)
	{
	}
	try
	{
		window.external.addPanel( 'Industrierat.de','http://industrierat.de/','')
	}
	catch(e)
	{
	}
}

clearQueue = function(scope){
	var queue = Effect.Queues.get(scope);
	queue.each(function(effect){effect.cancel();});
}

hideChildDivs = function(id){
	$$('#'+id+'>div').each(function(obj){						
		obj.style.display = "none";
	});
}

fadeChildDivs = function(id){
	$$('#'+id+'>div').each(function(obj){						
		if(obj.style.display != "none"){
			obj.fade({duration:0.2,queue:'end'});
		}
	});
}

observe_article_service = function(){
	$$("#article_service>div>h2").each(function(obj){
		obj.observe('click',function(evt){
			$$("#article_service>div").each(function(div){
				if(div!=obj.up()){
					div.className="inactive";
				}
			});
			if(obj.up().className=="inactive"){
				obj.up().className="active";
			}else{
				obj.up().className="inactive";
			}
		});
	});
}

navigation_bg = function(){
	if(!$$("ul.navigation.main li.active").length){
		active_nav_left = "0";
		$('bg_navi').style.left = active_nav_left+'px';
	}else{
		active_nav_left = 	$$("ul.navigation.main")[0].offsetLeft 
						+ $$("ul.navigation.main li.active a")[0].offsetLeft 
						+ $$("ul.navigation.main li.active a")[0].clientWidth/2-30;
		$('bg_navi').style.left = active_nav_left+"px";
	}
	
	$$("ul.navigation.main li a").each(function(obj){
		obj.observe('mouseover',function(e){
			hover_nav_left = 	$$("ul.navigation.main")[0].offsetLeft 
								+ Event.element(e).offsetLeft 
								+ Event.element(e).clientWidth/2-30;
			
			clearQueue('navigation');
			new Effect.Move('bg_navi', {
				x:hover_nav_left,
				y:0,
				mode: 'absolute',
				duration: 0.4,
				queue: {position:'end', scope:'navigation'},
				transition: Effect.Transitions.sinoidal
			});
			
		});
	});
	$$('ul.navigation.main')[0].observe('mouseout',function(e){
		clearQueue('navigation');
		new Effect.Move('bg_navi', {
			x:active_nav_left,
			y:0,
			mode: 'absolute',
			duration: 0.3,
			queue: {position:'end', scope:'navigation'},
			transition: Effect.Transitions.sinoidal
		});
	});
}

text_switch = function(id){
	var blocks = $$('#'+id+'>div');
	count = blocks.length;
	if(count == 1){
		$('text_switch_links').style.display= 'none';
	}
	blocks.each(function(obj,x){
		obj.style.display = "none";
		obj.setAttribute('id','text_'+x);
		node = Builder.node('a',{'id':'link_text_'+x,'onclick':'text_switch_displayOneOf("'+x+'");'});
		$('text_switch_links').appendChild(node);
	});
	text_switch_displayOneOf(0);
}

text_switch_displayOneOf = function(switchid){
	switchid_global = switchid;
	fadeChildDivs('text_switch');
	$('text_'+switchid).appear({duration:0.2,queue:'end'});
	
	$$('#text_switch_links>*').each(function(obj){						
		obj.setAttribute('class','inactive');
	});
	
	$('link_text_'+switchid).setAttribute('class','active');
	
	if(switchid<count-1){
		switchid++;
	}
	else{
		switchid=0;
	}
	
	if(typeof text_switch_timeout != 'undefined'){
		window.clearTimeout(text_switch_timeout);
	}
	text_switch_timeout = window.setTimeout('text_switch_displayOneOf('+switchid+')',10000);
}

/* Kontaktformular */
function checkValues(form,form_id,message_id)
{
	var error=false;
	
	var toCheck=new Array();
	
	toCheck[0]='name';
	toCheck[1]='vorname';
	toCheck[2]='strasse_nr';
	toCheck[3]='plz';
	toCheck[4]='ort';
	toCheck[5]='telefon';
	toCheck[6]='email';
							
	for(x=0;x<toCheck.length;x++){
		if($(toCheck[x]).value==''||$(toCheck[x]).value==' '){
			$(toCheck[x]).style.borderColor="#e10000";
			error=true;
		}
		else{
			$(toCheck[x]).style.borderColor="#00cc00";
		}
	}
							
	if(error==false)
	{
		$('mailer_message_errortext').style.display='none';
		var currPost='&ajax=true';
		
		for(x=0;x<form.length;x++)
		{
			if (form[x].value!=''){
				if (form[x].type=='checkbox'){
					if (form[x].checked==true){
						currPost=currPost+'&'+form[x].name+'='+form[x].value;
					}
				}else{
					currPost=currPost+'&'+form[x].name+'='+form[x].value;
				}
			}else{
				if (form[x].tagName=='INPUT' || form[x].tagName=='TEXTAREA'){
					if (form[x].type=='checkbox'){
						if (form[x].checked==true){
							currPost=currPost+'&'+form[x].name+'='+form[x].value;
						}
					}else{
						currPost=currPost+'&'+form[x].name+'='+form[x].value;
					}
				}else{
					currPost=currPost+'&'+form[x].name+'='+form[x].options[form[x].selectedIndex].value;
					if(form[x].selectedIndex==0){
						currPost=currPost+'&'+form[x].name+'=Herr';
					}else{
						currPost=currPost+'&'+form[x].name+'=Frau';
					}
				}
			}
		}
		//alert(currPost);
		$(message_id).style.display='';
		$(form_id).style.display='none';
		
	new Ajax.Updater('mailer_message_text','../scripts/forms/mailer.php', { asynchronous:true, postBody:currPost });
	}else{
		$('mailer_message_errortext').style.display='';
	}

	return false;
}

/* Registrierung */
function checkValuesRegister(form)
{
	var error=false;
	
	var toCheck=new Array();
	

	toCheck[0]='username';
	toCheck[1]='email';
	toCheck[2]='email_repeat';
	toCheck[3]='password';
	toCheck[4]='password_repeat';
	toCheck[5]='vorname';
	toCheck[6]='name';
	toCheck[7]='plz';
	toCheck[8]='ort';

							
	for(x=0;x<toCheck.length;x++){
		if($(toCheck[x]).value==''){
			$(toCheck[x]).className = "invalid";
			error=true;
		}
		else{
			$(toCheck[x]).className = "valid";
		}
	}

	
	if($$('input[id=password]')[0].value!=$('password_repeat').value){
		error=true;
		$$('input[id=password]')[0].className = 'invalid';
		$('password_repeat').className = 'invalid';
		Effect.Appear('mailer_pwrepeat_errortext',{duration:0.5});
	}
	else{
		Effect.Fade('mailer_pwrepeat_errortext',{duration:0.5});
	}
	
	if($('email').value!=$('email_repeat').value){
		error=true;
		$('email').className = 'invalid';
		$('email_repeat').className = 'invalid';
		Effect.Appear('mailer_emailrepeat_errortext',{duration:0.5});
	}
	else{
		Effect.Fade('mailer_emailrepeat_errortext',{duration:0.5});
	}			
				
				
	if(error==false)
	{
		var currPost='&ajax=true';
		for(x=0;x<form.length;x++)
		{
			if (form[x].value!=''){
				if (form[x].type=='checkbox'){
					if (form[x].checked==true){
						currPost=currPost+'&'+form[x].name+'='+form[x].value;
					}
				}else{
					currPost=currPost+'&'+form[x].name+'='+form[x].value;
				}
			}else{
				if (form[x].tagName=='INPUT' || form[x].tagName=='TEXTAREA'){
					if (form[x].type=='checkbox'){
						if (form[x].checked==true){
							currPost=currPost+'&'+form[x].name+'='+form[x].value;
						}
					}else{
						currPost=currPost+'&'+form[x].name+'='+form[x].value;
					}
				}else{
					currPost=currPost+'&'+form[x].name+'='+form[x].options[form[x].selectedIndex].value;
					if(form[x].selectedIndex==0){
						currPost=currPost+'&'+form[x].name+'=Herr';
					}else{
						currPost=currPost+'&'+form[x].name+'=Frau';
					}
				}
			}
		}
		
		Effect.Fade('mailer_message_errortext',{duration:0.5});
		new Ajax.Request('../scripts/security/account.php', { asynchronous:true, postBody:currPost, onComplete:checkValuesRegister_done });
	}
	else{
		if(getScrollY() > 550){
			Effect.ScrollTo('register_form',{duration:0.5});
		}
		Effect.Appear('mailer_message_errortext',{duration:0.5});
	}
	
	return false;
	
}

checkValuesRegister_done = function(req,json){
	var message = '';
	if(json){
		if(json.success){
			$('register_success').style.display = '';
			$('register_form').style.display='none';
			return;
		}
		else{
			for(var i=0;i<json.messages.length;i++){
				if(json.messages[i] == 'mail_mismatch'){
					message += 'Die E-Mail Adressen m&uuml;ssen<br />';
				}
				if(json.messages[i] == 'password_mismatch'){
					message += 'Die Passw&ouml;rter m&uuml;ssen<br />';
				}
				if(json.messages[i] == 'double_username'){
					message += 'Der Benutzername existiert schon<br />';
				}
				if(json.messages[i] == 'double_email'){
					message += 'Die E-Mail-Adresse existiert schon<br />';
				}
			}
		}
	}
	else{
		message = 'unbekannter Fehler';
		return false;
	}
	$('register_message').style.display = '';
	$('register_message').innerHTML = message;
}

/* Standorte */
switchStandort = function(id){
	hideChildDivs('standorte');
	div = $('standort_'+id);
	div.style.display='block';
	
	adresse = div.down('.teaser').innerHTML;

	adr_array = adresse.toLowerCase().split("<br>");
	
	$$('#standort_navigation a').each(function(obj){
		obj.className = "";
	});
	
	$('standort_link_'+id).className = "active";

	$('google_map_link').href = "http://maps.google.de/maps?q="+encodeURI(adr_array[2])+","+encodeURI(adr_array[3]);
	$('google_map').src = "http://maps.google.com/maps/api/staticmap?zoom=14&size=367x208&maptype=roadmap&markers=icon:http://www.industrie-rat.de/images/icon_16.png|"+encodeURI(adr_array[2])+","+encodeURI(adr_array[3])+"&sensor=false";
}

document.observe("dom:loaded", function(){
	input_placeholder();
	observe_article_service();
	
	search_back = window.location.search.replace(/^.*search_back=(.*)$/,'$1');
	if(search_back=="1"){
		$('search_back').style.display = "block";
	}
	
});
