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

var gToolOpen = null;
var gActiveTab = null;

function InitPage()
{
	document.onmouseup = function(e){
		if(gToolOpen!=null){
			new Effect.Fade(gToolOpen, {duration:0.5});
			gToolOpen = null;
		}
	}

	if(document.all){
		// nav fallback ie
		var nav = $('sitenav').getElementsByTagName('li');

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

			if(a.length>0){
				if(a[0].className=='sub'){
					nav[i].onmouseover = function(){
						var sub = this.getElementsByTagName('ul');

						if(sub.length>0){
							sub[0].style.marginLeft = (Element.getWidth(this)*-1)+'px';
						}

						this.className = 'open';
					}

					nav[i].onmouseout = function(){
						this.className = '';
					}
				}
			}
		}

		// gallery?
		var gallery = document.getElementsByClassName('gallery');
		for(i=0;i<gallery.length;i++){
			gallery[i].onmouseover = function(){
				this.className += ' open';
			}

			gallery[i].onmouseout = function(){
				this.className = this.className.replace(' open','');
			}
		}
	}

	return true;
}

function ShowGliderSub(Num,Link)
{
	Link.blur();

	var dd = $('scrollarea').getElementsByTagName('dd');
	var dt = $('scrollarea').getElementsByTagName('dt');

	for(var i=0;i<dd.length;i++){
		dd[i].className = 'nodisplay';
	}

	dd[Num].style.left = (dt[Num].offsetLeft-dt[Num].parentNode.offsetLeft+9)+'px';
	dd[Num].style.display = 'none';

	dd[Num].className = '';
	Effect.Appear(dd[Num], { duration: 0.5 });

	return false;
}

function HideGliderSub()
{
	var dd = $('scrollarea').getElementsByTagName('dd');

	for(var i=0;i<dd.length;i++){
		dd[i].className = 'nodisplay';
	}
}

function OpenToolbox(Which)
{
	if(Which=='login'){
		var open = $('login').getElementsByTagName('form')[0];
		var close = $('cart').getElementsByTagName('ul')[0];
	}else{
		var close = $('login').getElementsByTagName('form')[0];
		var open = $('cart').getElementsByTagName('ul')[0];
	}

	if(close){
		new Effect.Fade(close, {duration:0.5});
	}

	if(open){
		new Effect.Appear(open, {duration:0.5, afterFinish:SetOpened});
	}

	return false;
}

function SetOpened(obj)
{
	gToolOpen = obj.element;

	gToolOpen.onmouseup = function(e){
		if(e) e.stop();
	}
}

function NoAccess()
{
	new Effect.Appear('noaccess',{duration:0.5});

	return false;
}

function Highlight(Slot)
{
	$('beerslider').className = 'nodisplay';
	$('beercloud').className = 'nodisplay';

	$('beerslider-tab').className = 'inactive';
	$('beercloud-tab').className = 'inactive';

	$(Slot).className = 'tabbox-inner';
	$(Slot+'-tab').className = '';
}

function ToggleBottle(Link,LinkShop)
{
	var li = $('gebinde').getElementsByTagName('li');
	for(var i=0;i<li.length;i++){
		li[i].className = '';
	}

	Link.parentNode.className = 'active';

	var bottles = document.getElementsByClassName('bottle');

	if(bottles.length==1){
		bottles[0].className = 'hide';
	}

	$('z'+Link.id.substr(1)).className = 'bottle';
	var link_item = $('z'+Link.id.substr(1)).getElementsByTagName('a');
	if(link_item.length>0){
		if(!LinkShop){
			gLinkBuffer = link_item[0].href;
			link_item[0].href = '#';
			link_item[0].title = '';
		}else{
			if(gLinkBuffer){
				link_item[0].href = gLinkBuffer;
				link_item[0].title = 'Dieses Glas im Shop zeigen';
			}
		}
	}

	return false;
}

function TabNav(Click)
{
	var section = Click.href.split('#');

	if($(section[1])!=gActiveTab){
		$(gActiveTab.id.replace("file-","tab-")).parentNode.className = '';
		gActiveTab.style.display = 'none';
		gActiveTab = $(section[1]);
		Click.parentNode.className = 'active';
		Click.blur();
		gActiveTab.style.display = 'block';
	}

	return false;
}

function Download(Url)
{
	TargetBlank(Url);
	document.location.href = '/presse/downloads.htm';
	return false;
}

/**
* 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"+String(Math.round(Math.random()*10)),"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);
}

/* * * Javascript Funktionen zur Darstellung des Bundesehrenpreis * * */

/**
 * Fügt an alle container, welche die klasse "hideme" besitzen die klasse "nodisplay" hinzu.
 * Bei Übergabe einer ContainerId (spareme) wird der entsprechende Container übersprungen
 */
function hideAll(spareMe) {
	if(typeof(spareMe) == "undefined") {
		spareMe = '';
	}
	var divsToHide = $$('.hideme');

	for (var index = 0; index < divsToHide.length; ++index) {
		if(divsToHide[index].id != spareMe) {
			divsToHide[index].addClassName('nodisplay');
		}
	}
}
/**
 * Sichtbar / Unsichtbarmachen von Containern, laden von SWF Video / Audioplayer
 */
function toggleDiv(DivId,Movie,MediaType) {
	hideAll(DivId);
	if($(DivId).hasClassName('nodisplay')) {
		$(DivId).removeClassName('nodisplay');
	} else {
		$(DivId).addClassName('nodisplay');
	}
	if(typeof(MediaType) != "undefined") {
		if(MediaType == 'video') {
			var playerId = DivId + '_player';
			var mv = { movie:"/video.swf?m=" + Movie, width:"259", height:"246", majorversion:"8", build:"0" };
			UFO.create(mv, playerId);
		} else if(MediaType == 'audio') {
			var playerId = DivId + '_player';
			var mv = { movie:"/audio.swf?m=" + Movie, width:"259", height:"36", majorversion:"8", build:"0" };
			UFO.create(mv, playerId);
		}
	}
	return false;
}
/**
 * Klasse "hover" zu Container DivId hinzufuegen
 */
function hover(DivId) {
	$(DivId).addClassName('hover');
}
/**
 * Klasse "hover" zu Container DivId entfernen
 */
function unhover(DivId) {
	$(DivId).removeClassName('hover');
}
/**
 * Fuer Container Klasse "active" hinzufuegen oder loeschen
 */
function toggleactive(DivId) {
	var deactivate = $$('.teaser');
	for (var index = 0; index < deactivate.length; ++index) {
		if(deactivate[index].id != DivId)
		deactivate[index].removeClassName('active');
	}
	if($(DivId).hasClassName('active')) {
		$(DivId).removeClassName('active')
	} else {
		$(DivId).addClassName('active')
	}
}

/**
 * Div ein und ausblenden
 *
 * @param	string		ID des Containers
 * @param	string		anzeigen? true/false
 */
 
function ToggleById(Id,Show) {
	elem = $(Id);
	if(elem) {
		if(Show && elem.hasClassName('nodisplay')) {
			elem.removeClassName('nodisplay');
		} else if(!elem.hasClassName('nodisplay')) {
			elem.addClassName('nodisplay');
		}
	}
}

