//Grafiken, z.B. für Hauptmenü preloaden
function preload(path, preload_images) {
	for(var i = 0; i < preload_images.length; i++) {
		var index = g_preload.length;
		g_preload[index] = new Image();
		g_preload[index].src = path + preload_images[i];
	}
}


//Target _blank mit Script
function changeTarget() {
	var lnks = document.getElementsByTagName('a');
	for(var i = 0; i < lnks.length; i++) {
		if(lnks[i].getAttribute('rel') == 'external') {
			lnks[i].onclick = new Function('window.open(this.href); return false');
		}
	}
}
onload = changeTarget;


//id = id des divs, das eingeblendet werden soll
//img = falls andere grafiken für die buttons als die default grafiken 'minus.gif' und 'plus.gif' verwendet werden sollen.
function show_hide(id, img) {
	if(img == null) img = '';
	with(document.getElementById(id).style) {
		if(display=="none") {
			display="block";
			eval('document.images.' + id + '_but.src = "' + g_tpl_path + 'img/links/' + img + 'minus.gif"');
		} else {
			display="none";
			eval('document.images.' + id + '_but.src = "' + g_tpl_path + 'img/links/' + img + 'plus.gif"');
		}
	}
}
//Funktion für eingeklappte Bereiche umgekehrt, da sonst 2X geklickt werden mußte!?
function hide_show(id) {
	with(document.getElementById(id).style) {
		if(display=="block") {
			display="none";
			eval('document.images.' + id + '_but.src = "' + g_tpl_path + 'img/links/plus.gif"');
		} else {
			display="block";
			eval('document.images.' + id + '_but.src = "' + g_tpl_path + 'img/links/minus.gif"');
		}
	}
}
//Funktion die eingeklappte Bereiche nur einblendet
function only_show(id) {
	with(document.getElementById(id).style) {
		if(display=="none") {
			display="block";
			eval('document.images.' + id + '_but.src = "' + g_tpl_path + 'img/links/minus.gif"');
		}
	}
}


//Warenkorb Box ein-/ausblenden
function hide_cart(elm_cart) {
	with(document.getElementById(elm_cart).style) display = "none";
}
function show_cart(elm_cart) {
	with(document.getElementById(elm_cart).style) display = "block";
}



//Folgende Variablen und Funktionen für Zurück-Button oben links auf Seite
var g_last_hash_value = false;
var g_history_length = 1;
try {
	window.clearInterval(g_timer_active);
} catch (e) {
	//alert(e)
}
var g_timer_active = false;

//Für alle Browser außer Opera und IE ab 5.5, um aktuellen Hash Wert zu ermitteln, falls User Back im Browser geklickt hat.
function get_window_hash() {
	if(g_last_hash_value != window.location.hash) {
		//User hat im Browser Back geklickt
		g_history_length--;
		g_last_hash_value = window.location.hash;
	}
}

//Um verschiedene Anker anzuspringen. Wird von Opera und IE ab 5.5 immer benutzt. Von Firefox und anderen nur, wenn nicht nur backToTop geklickt wurde.
function goto_anchor(achor_url) {
	if(g_is_opera) {
		//Opera nimmt jeden Klick (auch mehrmalige Klicks auf die gleiche Ankermarke z.B. #backToTop) in history.length auf.
		//Auch scheint nicht wie beim Firefox die Länge von history.length beschränkt zu sein. Deshalb hier so (auch weil Version 8 und 7 mit Code im else Zweig nicht zurecht kamen)
		if(!g_last_hash_value) {
			g_history_length = history.length;
			g_last_hash_value = true;
		}
		return true;
	} else if(g_is_ie5_5up) {
		//Name des Ankerpunkts aus kompletten Pfad
		var anchor_id_position = achor_url.lastIndexOf("#") + 1;
		var anchor_id = achor_url.substr(anchor_id_position);
		
		//g_host_path_shop wird in includes/header.php gesetzt
		top.anchors_ie.location.href = g_host_path_shop + 'anchors_ie.php?hash=' + anchor_id;
		window.location.href = achor_url;
		
		if(g_last_hash_value != anchor_id) g_history_length++;
		g_last_hash_value = anchor_id;
		
		return false;
	} else {
		//Firefox scheint history.length auf 50 Einträge zu beschränken. Deshalb dieser Weg.
		//Name des Ankerpunkts aus kompletten Pfad (hier mit #)
		//Getestet für Firefox und Safari. IE 5 verhält sich in diesem Fall wie Firefox.
		var anchor_id_position = achor_url.lastIndexOf("#");
		var anchor_id = achor_url.substr(anchor_id_position);
		
		//Nicht durch Script, sondern einfach den href Teil ausführen lassen, indem unten true zurück gegeben wird.
		//window.location.href = achor_url;
		
		//Falls schon auf backToTop geklickt wurde
		if(window.location.hash == '#backToTop' && g_timer_active == false) {
			g_history_length++;
			g_last_hash_value = '#backToTop';
		}
		
		if(anchor_id != g_last_hash_value) {
			g_history_length++;
			g_last_hash_value = anchor_id;
		}
		
		//Falls Intervall zum pollen des Window Hash Werts noch nicht gestartet wurde.
		if(g_timer_active == false) {
			//Wert 100 = 10 Mal pro Sekunde. Bei Wert 1000 = 1 Mal pro Sekunde hat es bei schnellem Back im Firefox nicht geklappt.
			g_timer_active = window.setInterval("get_window_hash()", 100);
		}
		return true;
	}
}

//Für Links backToTop
function goto_top(backtotop_url) {
	//Opera und IE ab 5.5 immer mit goto_anchor(). Bei Firefox Timer bzw. setInterval() nicht bentuzen, wenn nur Links mit backToTop geklickt wurden.
	if(g_is_opera || g_is_ie5_5up || g_timer_active != false) return goto_anchor(backtotop_url);
	else return true;
}

//Um History für IE ab 5.5 ermitteln zu können.
function write_history_iframe_ie() {
	if(g_is_ie5_5up) {
		document.write('<iframe src="anchors_ie.php?hash="' + false + ' width="0" height="0" name="anchors_ie"><\/iframe>');
	}
}

//Funktion für Zurück-Button links oben
function go_back() {
	//alert(g_timer_active);
	
	if(g_is_opera && g_last_hash_value) g_history_length = history.length - g_history_length + 1;
	
	if(g_history_length > 1) {
		//alert(g_history_length + ' Schritte L zurück');
		history.go(-g_history_length);
	} else if(window.location.hash == '#backToTop' && !g_is_ie5_5up && !g_is_opera) {
		//alert('Firefox und Safari nur backToTop Links ohne gestarteten Timer');
		history.go(-2);
	} else {
		//alert('1 Schritt zurück');
		history.back();
	}
}


//Popup öffnen
function popWindow(url, popup_width, popup_height, scroll_value) {
	popup_values = { width:popup_width, height:popup_height, left:0, top:0, scroll:scroll_value, resize:(scroll_value == "yes" ? "yes" : "no") };
	
	try {
		this.popup_win.close(); //Versuchen eventuelle offenes Fenster wieder zu schliessen
	} catch(e) {
		//alert(e);
	}
	
	if(!g_is_opera && screen.availHeight && typeof(screen.availHeight) == 'number') {
		//IE, FF, Safari, Chrome
		if(screen.availWidth < popup_values.width) popup_values.width = screen.availWidth;
		else popup_values.left = Math.round((screen.availWidth - popup_values.width) / 2);
		
		if(screen.availHeight < popup_values.height) popup_values.height = screen.availHeight;
		else popup_values.top = Math.round((screen.availHeight - popup_values.height) / 2);
	} else if(window.innerHeight && typeof(window.innerHeight) == 'number') {
		//Opera
		if(window.innerWidth < popup_values.width) popup_values.width = window.innerWidth;
		else popup_values.left = Math.round((window.innerWidth - popup_values.width) / 2);
		
		if(window.innerHeight < popup_values.height) popup_values.height = window.innerHeight;
		else popup_values.top = Math.round((window.innerHeight - popup_values.height) / 2);
	}
		
	settings = 'height='+popup_values.height+',width='+popup_values.width+',top='+popup_values.top+',left='+popup_values.left+',resizable='+popup_values.resize+',scrollbars='+popup_values.scroll;
	try {
		this.popup_win = window.open(url,'',settings);
	} catch (e) {
		return true;
	}
	
	try {
		this.popup_win.focus();
	} catch (e) {
		//alert(e);
	}
	return false;
}
