var teaserTimeout = 1800;

var main_colors = {
	konzeption: '#236a9c', 
	itservice: '#004da0', 
	print: '#e44b22', 
	lettershop: '#54b888', 
	logistik: '#f4b854', 
	philosophie: '#fde9ca', 
	referenzen: '#fde9ca', 
	kontakt: '#e5efdd', 
	news: '#e5efdd', 
	agb: '#e5efdd', 
	impressum: '#e5efdd', 
	home: '#e5efdd'
};

function toggleSub(elid){
	var el = $(elid);
	var newstyle = (el.getStyle('display') == 'block') ? 'none' : 'block';
	$('subnavigation').getChildren('ul').each(function(sub_el){
		sub_el.setStyle('display', 'none');
	});
	$('mainnavigation').getChildren('li').each(function(sub_el){
		var ael = sub_el.getFirst('a');
		if(ael != null && ael.hasClass('active')) ael.removeClass('active');
	});
	el.setStyle('display', newstyle);
	var add = (newstyle == 'block') ? true : false;
	if(add) $('main_' + elid).addClass('active');
	else $('main_' + elid).removeClass('active');
	return false;
}

function showSub(elid){
	var subs = $('subnavigation').getChildren('ul').each(function(sub_el){
		sub_el.setStyle('display', 'none');
	});
	$('mainnavigation').getChildren('li').each(function(sub_el){
		var ael = sub_el.getFirst('a');
		if(ael != null && ael.hasClass('active')) ael.removeClass('active');
	});
	var el = $(elid);
	if(el != null) el.setStyle('display', 'block');
	$('main_' + elid).addClass('active');
	$('main_' + elid).getParent().getPrevious('li').setStyle('color', main_colors[elid]);
	$('main_' + elid).getParent().getNext('li').setStyle('color', main_colors[elid]);
	prepareSubmenu(elid);
}

function prepareSubmenu(elid){
	if($(elid) != null && $(elid).getChildren('li').length > 0){
		// submenu
		var subs = {};
		var count = 1;
		$(elid).getChildren('li').each(function(el){
			var anker = el.getFirst('a');
			if(anker != null){
				anker.setProperty('id', 'submenu_' + count);
				subs[anker.getProperty('href')] = anker.getProperty('id');
				anker.addEvent('mouseover', function(){
					highlightSub(anker.getProperty('id'), elid);
				});
				anker.addEvent('mouseout', function(){
					unhighlightSub(anker.getProperty('id'));
				});
				count++;
			}
		});
		// thumbnails
		$$('a.thumb_link').each(function(el){
			el.addEvent('mouseover', function(){
				highlightSub(subs[el.getProperty('href')], elid);
			});
			el.addEvent('mouseout', function(){
				unhighlightSub(subs[el.getProperty('href')]);
			});
		});
	}
	highlightActive();
}

function hideSub(elid){
	var el = $(elid);
	el.setStyle('display', 'none');
	$('main_' + elid).removeClass('active');
}

function highlightSub(el, elid){
	var ncolor = main_colors[elid];
	if($(el).getParent().getPrevious('li') != null) $(el).getParent().getPrevious('li').setStyle('color', ncolor);
	if($(el).getParent().getNext('li') != null) $(el).getParent().getNext('li').setStyle('color', ncolor);
	$(el).addClass('sub_active');
	$(el).setStyle('background-image', 'url(../../assets/layout/wand/' + elid + '.gif)');
}

function unhighlightSub(el){
	if($(el).getParent().getPrevious('li') != null) $(el).getParent().getPrevious('li').removeProperty('style');
	if($(el).getParent().getNext('li') != null) $(el).getParent().getNext('li').removeProperty('style');
	$(el).removeClass('sub_active');
	$(el).removeProperty('style');
	highlightActive();
}

function highlightActive(){
	// main menu
	if($('main_' + initShowSub)){
		if($('main_' + initShowSub).getParent().getPrevious('li') != null){
			$('main_' + initShowSub).getParent().getPrevious('li').setStyle('color', main_colors[initShowSub]);
		}
		if($('main_' + initShowSub).getParent().getNext('li') != null){
			$('main_' + initShowSub).getParent().getNext('li').setStyle('color', main_colors[initShowSub]);
		}
		$('main_' + initShowSub).setStyle('color', main_colors[initShowSub]);
		$('main_' + initShowSub).setStyle('background-image', 'url(../../assets/layout/wand/' + initShowSub + '.gif)');
	}
	if($(initShowSub) != null && $(initShowSub).getChildren('li').length > 0){
		// sub menu
		$(initShowSub).getChildren('li').each(function(el){
			var anker = el.getFirst('a');
			if(anker != null){
				if(anker.getProperty('href') == winloc){
					anker.addClass('sub_active');
					highlightSub(anker.getProperty('id'), initShowSub);
				}
			}
		});
		// thumbnails
		$$('a.thumb_link').each(function(el){
			if(el.getProperty('href') == winloc){
				el.addClass('thumb_active');
			}
		});
	}
}

function highlightThumb(id){
	var thumb = $('thumb_' + id);
	if(thumb != null){
		thumb.addClass('thumb_active');
	}
}

function unhighlightThumb(id){
	var thumb = $('thumb_' + id);
	if(thumb != null){
		thumb.removeClass('thumb_active');
	}
}


var cc_changeContent = false;
var cc_orgContent;
var cc_container;
function setTeaser(uri, cc_cont){
	cc_container = $(cc_cont);
	cc_orgContent = cc_container.get('html');
	cc_changeContent = true;
	cc_container.empty();
	cc_container.getParent().setStyle('background-image', 'url(../../assets/layout/teaser/' + uri + ')');
}

function resetTeaser(){
	cc_container.getParent().setStyle('background-image', 'url(../../assets/layout/teaser/' + cc_orgTeaserImage + ')');
	cc_container.set('html', cc_orgContent);
}

var winloc = window.location.href;
winloc = winloc.substring(winloc.lastIndexOf('/')+1);

window.onload = function(){
	if(initShowSub){
		showSub(initShowSub);
	}
	if(cc_changeContent){
		window.setTimeout('resetTeaser();', teaserTimeout);
	}
}

