/**
* project customfuncs
*
* @author		Frank Schnappenberger
* @copyright	triple-i new media system design gmbh
* @version		1.0.0
*/

var gBaseval = '';
var gButton = null;
var gSubSubOpen = null;

function ToggleDropdown(Link)
{
	var sub = Link.parentNode.getElementsByTagName('ul');
	
	if(sub[0]){
		Effect.toggle(sub[0],'blind',{ duration: 0.2 });
		if(Link.blur) Link.blur();
	}
	
	return false;
}

function SendForm (Button)
{	
	gButton = Button;
	
	Element.extend(Button);
	
	if(!Button.hasClassName('wait'))  {
		gBaseval = Button.innerHTML;
		
		Button.innerHTML.value='';
		Button.addClassName('wait');
	}
	
	Button.blur();
	
	var i = 0;
	var pars = '';
	
	if($('f-salutation-mr')){
		if($('f-salutation-mr').checked){
			pars += 'salutation='+$('f-salutation-mr').value+'&';
		}else{
			pars += 'salutation='+$('f-salutation-mrs').value+'&';
		}
	}
	
	while($('f-contact'+i)){
		pars += 'contact'+i+'='+$('f-contact'+i).value+'&';
		i++;
	}

	pars += 'street='+$('f-street').value;
	pars += '&zip='+$('f-zip').value;
	pars += '&city='+$('f-city').value;
	pars += '&phone='+$('f-phone').value;
	pars += '&mail='+$('f-mail').value;
	pars += '&message='+$('f-message').value;

	if($('f-type')) {
		pars += '&type='+$('f-type').value;
	}

	$('f-beratung').checked ? pars += '&beratung=Ja' : pars += '&beratung=-';
	$('f-anruf').checked ? pars += '&anruf=Ja' : pars += '&anruf=-';
	$('f-info').checked ? pars += '&info=Ja' : pars += '&info=-';

	$('fulltext').onclick = closenotification;
	
	var myAjax = new Ajax.Updater(
		'result',
		'/html/sendform.php',
		{
			method: 'post',
			parameters: pars,
			onComplete: FormComplete
		});
	return false;
}

function closenotification() {
	if(Element.visible('result')){
		new Effect.SlideUp('result', { duration: 0.3 });
	}
}

function FormComplete()
{
	// loeschen der fehlermarkierungen

	var allfields = $$('form li.error');
	allfields.each(
		function (elem,index) {

			if(elem.hasClassName('error')) {
				elem.removeClassName('error');
			}
		}
	);

	new Effect.SlideDown('result', { duration: 0.5 });

	var result_inner = $('result').getElementsByTagName('div');

	Element.extend(result_inner[0]);
	
	if(result_inner[0].hasClassName('error')){
		var errorfields = $$('#formerrorlist li');

		errorfields.each(
			function (li,index) {
				var elem = $('field-' + li.innerHTML);

				if(elem) {
					elem.addClassName('error');
				}
			}
		);
		gButton.innerHTML = gBaseval;
	}else{
		gButton.innerHTML = '';
	}
	
	if( gButton.hasClassName('wait')) {
		gButton.removeClassName('wait');
	}
}

/**
* store the movie pos
*/
function ufoCom_DoFSCommand(command, args)
{
	if(command=='StoreImage'){
		var name = args.split('|');
		CreateCookie('img_'+name[1],name[0]);
	}

	if(command=='Init'){
		InitFlash(args);
	}

	if(command=='trace'){
		alert(args);
	}
}

/**
* init function after page load
*/
function InitFlash(Gallery)
{
	if(document.getElementById("ufoCom")){
		var img = ReadCookie('img_'+Gallery);

		if(img!=null){
			document.getElementById("ufoCom").SetVariable("cookie_image", img);
		}else{
			document.getElementById("ufoCom").SetVariable("cookie_image", 0);
		}
	}
}

/**
* init function after page load
*/
function InitNav(SubSubInit)
{
	var subsubs = $('nav').getElementsByClassName('subsub');
	for(var i=0;i<subsubs.length;i++){
		var subsubul = subsubs[i].getElementsByTagName('ul');

		for(var j=0;j<subsubul.length;j++){
			var a = subsubul[j].getElementsByTagName('a');

			for(var k=0;k<a.length;k++){
				a[k].onclick = function(e){
					document.location.href = this.href;
				}
			}
		}
	}

	if(SubSubInit!=''){
		var ul = $(SubSubInit).getElementsByTagName('ul');
		ul[0].style.display = 'block';
		gSubSubOpen = ul[0];
	}

	// init nav
	var nav = $('nav').getElementsByTagName('li');
	for(var i=0;i<nav.length;i++){
		nav[i].onclick = function(ev)
		{
			if(this.className=='sub' || this.className=='sub active'){
				var subul = this.getElementsByTagName('ul');

				if(document.all){
					subul[0].style.marginLeft = ((Element.getWidth(this)+15)*-1)+'px';
				}

				if(!Element.visible(subul[0])){
					new Effect.Appear(subul[0],{ duration: 0.25, beforeStart:HideOthers, from: 0, to: 0.9 });
				}else{
					new Effect.Fade(subul[0],{ duration: 0.25, from: 0.9, to: 0 });
				}
			}

			if(this.className=='subsub'){
				var subul = this.getElementsByTagName('ul');

				if(!Element.visible(subul[0])){
					if(gSubSubOpen!=null && gSubSubOpen!=subul[0]){
						new Effect.BlindUp(gSubSubOpen,{ duration: 0.25 });
					}
					new Effect.BlindDown(subul[0],{ duration: 0.25, afterFinish:SetOpen });
				}else{
					new Effect.BlindUp(subul[0],{ duration: 0.25 });
				}

				if(!ev){
					Event.stop(window.event);
				}else{
					ev.stop();
				}
			}
		}
	}
}

function SetOpen(El)
{
	gSubSubOpen = El.element;
}

function HideOthers(El)
{
var other = $('nav').getElementsByTagName('ul');
	for(var i=0;i<other.length;i++){
		if(other[i].className!='subsubul' && other[i]!=El.element){
			new Effect.Fade(other[i], {duration: 0.25, from: 0.9, to: 0 });
		}
	}
}

/**
* open accessible popup
*
* @param	string		url
* @return	boolean		always false
*/
function Popup (url) {
	var newwindow	= window.open(url, "popup","width=400,height=550,menubar=no,location=no,status=no,scrollbars=yes,toolbar=yes,resizable=yes");
	newwindow.focus();
	return false;
}

/**
* simulates target blank in an accessible way
*
* use links: <a href="http://www.triple-i.de" onclick="return TargetBlank(this.href)">Link</a>
*
* @param	string		url
* @return	boolean		always false
*/
function TargetBlank (url) {
	var newwindow	= window.open(url, "newwindow"+Math.random(),"menubar=yes,location=yes,status=yes,scrollbars=yes,toolbar=yes,resizable=yes");
	newwindow.focus();
	return false;
}

/*
* Creates a new browser window.
*
* @param	string		url to load in new window
* @param	string		name of new window
* @param	string		width of new window (optional, uses default size if omitted)
* @param	string		height of new window (optional, uses default size if omitted)
* @param	string		string containg attribs the new window should have (optional,
						default is everything on, use an empty string to turn everything,
						off, certain attribute keywords to activate these attributes)
* @returns	object		window object of created window
*/
function CreateWindow(url,name,w,h,attribs) {
	var menubar=0;
	var toolbar=0;
	var locationbar=0;
	var personalbar=0;
	var statusbar=0;
	var scrollbars=0;
	var resizable=0;
	if(typeof(w)=='undefined') { var w=0; }
	if(typeof(h)=='undefined') { var h=0; }
	if(typeof(attribs)=='undefined'||attribs=='none') { var attribs=''; }
	else if(attribs.match('all')||attribs.match('every')) {
		menubar=1;
		toolbar=1;
		locationbar=1;
		personalbar=1;
		statusbar=1;
		scrollbars=1;
		resizable=1;
	} else {
		if(attribs.match('menu')) { menubar=1; }
		if(attribs.match('tool')) { toolbar=1; }
		if(attribs.match('loc')) { locationbar=1; }
		if(attribs.match('pers')) { personalbar=1; }
		if(attribs.match('stat')) { statusbar=1; }
		if(attribs.match('scroll')) { scrollbars=1; }
		if(attribs.match('resiz')) { resizable=1; }
	}
	attribs='';
	if(w>0) { attribs+='width='+w; }
	if(h>0) {
		if(attribs!='') { attribs+=','; }
		attribs+='height='+h;
	}
	if(attribs!='') { attribs+=','; }
	attribs+='menubar='+menubar+',toolbar='+toolbar+',location='+locationbar;
	attribs+=',personalbar='+personalbar+',status='+statusbar+',scrollbars='+scrollbars;
	attribs+=',resizable='+resizable;
	return window.open(url,name,attribs);
}

/*
* Close a window and optionally focus on opener.
*
* @param	boolean 	wether or not to focus on window opener
*/
function CloseWindow(focusopener) {
	if(typeof(focusopener)=="undefined") { var focusopener=false; }
	if(focusopener&&window.opener) { window.opener.focus(); }
	window.close();
}

/**
* center a popup window on screen
*
* @param    object  window object (optional)
*/
function CenterPopup(win) {
	if(typeof(win)=='undefined'||win=='') { var win=window; }

	win_width	= GetDocumentWidth(win);
	win_height	= GetDocumentHeight(win);

	MoveWindowTo(screen.width/2-win_width/2,screen.height/2-win_height/2,win);
}

/*
* Move a window to a certain position on screen.
*
* @param	int			x coordinate
* @param	int			y coordinate
* @param	object		window object to use (optional)
*/
function MoveWindowTo(x,y,win) {
	if(typeof(win)=='undefined') { var win=window; }
	win.moveTo(x,y);
}


/**
* Creates a cookie
*
* @param	string		name of the cookie
* @param	string		value
* @param	integer		number of days till expires
*/
function CreateCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

/**
* read a cookie by name
*
* @param	string		name of cookie
* @return	string		get value of cookie
*/
function ReadCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

/**
* erase a cookie by name
*
* @param	string		name of cookie
*/
function EraseCookie(name) {
	createCookie(name,"",-1);
}


