// ************************************************************************************************
//
// Javascript-Ergänzungen für www.jb-electronics.de
//
// [x] einstellbare Sprache (durch ?lang=de bzw. ?lang=en)
// [x] einstellbare Ansicht (durch ?view=print)
// [x] Speicherung der Auswahl (Normal- oder Printlayout) durch Cookie (7 Tage)
// [x] verschlüsselte Email-Adresse
//
// Copyright www.jb-electronics.de
//
// last modified 11:54 19.03.2010
//
//
// ************************************************************************************************


// ************************************************************************************************
// Globale Variablen
var LANG, VIEW, MODUS;
var DE = 1, EN = 2, NORMAL = 1, PRINT = 2, ONLINE = 1, OFFLINE = 2;
var titel_de, titel_en, URL;

// ************************************************************************************************
// Home-Pfade
var HOME_ONLINE		= "http://www.jb-electronics.de/";
var HOME_OFFLINE	= "file:///D:/Homepage/HVP/_server/";

// ************************************************************************************************
// Online- / Offline-Modus bestimmen
MODUS = getModus();

// ************************************************************************************************
// ermittelt, ob das Skript on- oder offline aufgerufen wird
function getModus () {

	if (window.location.protocol == "file:") {
		return OFFLINE;
	} else {
		return ONLINE;
	}

}

// ************************************************************************************************
// wird zu Beginn aufgerufen
function startup () {

	// speicher die URL
	URL = window.location.pathname;

	// Speicher die Titel (ermittle den englischen Titel aus der Überschrift)
	titel_de = window.top.document.title;	
	titel_en = document.getElementById("Englisch").getElementsByTagName("h1")[0].firstChild.data + ' - www.jb-electronics.de';

	// Hole Anzeigeinfos aus der URL und wende sie an
	analyseURL(window.location.href);

}

// ************************************************************************************************
// analysiere die URL auf Steuerparameter
function analyseURL (myURL) {

	// Variablen
	var i, Zeichen, tmp = "";

	// Voreinstellungen
	LANG = getDefaultLanguage();
	VIEW = getDefaultView();

	// URL parsen
	for (i = 0; i <= myURL.length; i++) {

		// aktuelles Zeichen ermitteln
		Zeichen = myURL.charAt(i);

		// Trennen
		if ((Zeichen == "?") || (Zeichen == "&") || (Zeichen == "#") || (i == myURL.length)) {

			// welcher Parameter? lang oder view?
			if (tmp.substr(0, 4) == "lang") {
				tmp = tmp.substr(tmp.lastIndexOf("=") + 1, tmp.length - tmp.lastIndexOf("="));
				LANG = getLanguage(tmp);
			} else if (tmp.substr(0, 4) == "view") {
				tmp = tmp.substr(tmp.lastIndexOf("=") + 1, tmp.length - tmp.lastIndexOf("="));
				VIEW = getViewType(tmp);
			}
			tmp = "";


		} else {
			tmp = tmp + Zeichen;
		}

	}

	// Druck-Icon anpassen
	if (VIEW == PRINT) {	
		document.getElementById("print1").src = createPath() + "images/allgemein/print_no.gif";
		document.getElementById("print2").src = createPath() + "images/allgemein/print_no.gif";
		document.getElementById("print1").alt = "Drucklayout verlassen";
		document.getElementById("print2").alt = "leave print layout";
		selectStylesheet (createPath() + "css/print.css");

	} else {
		document.getElementById("print1").src = createPath() + "images/allgemein/print.gif";
		document.getElementById("print2").src = createPath() + "images/allgemein/print.gif";
		document.getElementById("print1").alt = "Drucklayout anzeigen";
		document.getElementById("print2").alt = "show print layout";
		selectStylesheet (createPath() + "css/style.css");
	}

	// Sichtbarkeit der Seite einstellen
	if (LANG == DE) {
		window.top.document.title = titel_de;
		document.getElementById('Deutsch').style.display = "block";
		document.getElementById('Englisch').style.display = "none";
	} else {
		window.top.document.title = titel_en;
		document.getElementById('Deutsch').style.display = "none";
		document.getElementById('Englisch').style.display = "block";
	}

}

// ************************************************************************************************
// Standardspracheinstellung ist deutsch (d.h. keine Angabe des "lang"-Parameters => deutsche Seite)
function getDefaultLanguage () {

	return DE;
	
}

// ************************************************************************************************
// liefert den ausgewählten Displaymodus zurück
function getDefaultView () {

	// Wurden Cookies gesetzt?
	if (document.cookie) {
		if (document.cookie.substr(14, 1) == "p") {
			return PRINT;
		}
	}
	return NORMAL;

}

// ************************************************************************************************
// Sprache aus Parameter ermitteln
function getLanguage (Ausdruck) {

	Ausdruck = Ausdruck.toLowerCase();
	if ((Ausdruck == "de") || (Ausdruck == "deutsch") || (Ausdruck == "german")) {
		return DE;
		
	} else if ((Ausdruck == "en") || (Ausdruck == "englisch") || (Ausdruck == "english")) {
		return EN;
	
	} else {
		return getDefaultLanguage();
	}

}

// ************************************************************************************************
// View-Modus aus Parameter ermitteln
function getViewType (Ausdruck) {

	switch (Ausdruck.toLowerCase()) {

		case "print":
			return PRINT;

		default:
			return NORMAL;

	}

}

// ************************************************************************************************
// Stylesheet dynamisch wechseln für Print-Ansicht
function selectStylesheet (Dateiname) {

	// Stylesheet-Eintrag entfernen
	var child = document.getElementsByTagName("head")[0].lastChild;
	//dummy = document.getElementsByTagName("head")[0].removeChild(child);

	// neues <link> erzeugen
	var myLink = document.createElement("link");

	// Attribute erstellen
	// <link rel="" href="" type="" media="">

	var myLink_rel	       = document.createAttribute("rel");
	myLink_rel.nodeValue   = "stylesheet";

	var myLink_type        = document.createAttribute("type");
	myLink_type.nodeValue  = "text/css";

	var myLink_href        = document.createAttribute("href");
	myLink_href.nodeValue  = Dateiname;

	var myLink_media       = document.createAttribute("media");
	myLink_media.nodeValue = "all";
	
	// Attribute hinzufügen	
	myLink.setAttributeNode(myLink_rel);
	myLink.setAttributeNode(myLink_type);
	myLink.setAttributeNode(myLink_href);
	myLink.setAttributeNode(myLink_media);

	// Das Element wieder hinzufügen
	document.getElementsByTagName("head")[0].appendChild(myLink);

}

// ************************************************************************************************
// Den Pfad zu den Stylesheets ermitteln
function createPath () {

	// Gebe im ONLINE-Modus einfach den absoluten Pfad an
	if (MODUS == ONLINE) {
		return HOME_ONLINE;
	
	// OFFLINE-MODUS
	} else {
		return HOME_OFFLINE;
		
	}

}

// ************************************************************************************************
// speichert die aktuellen Einstellungen im Cookie
function updateCookies () {

	// Verfallsadatum der Cookies: 1 Woche
	var expire = new Date();	
	expire = new Date(expire.getTime() + 1000*60*60*24*7);

	// Sprache
	if (LANG == DE) {
		document.cookie = "lang=de; expires=" + expire.toGMTString() + "; path=/;";
	} else {
		document.cookie = "lang=en; expires=" + expire.toGMTString() + "; path=/;";
	}

	// Ansicht (normal oder Drucklayout?)
	if (VIEW == NORMAL) {
		document.cookie = "view=n; expires=" + expire.toGMTString() + "; path=/;";
	} else {
		document.cookie = "view=p; expires=" + expire.toGMTString() + "; path=/;";
	}

}

// ************************************************************************************************
// Sprache auf Deutsch stellen und URL updaten
// Zudem: Wahl in Cookie abspeichern
function setGerman () {

	// Variable setzen
	LANG = DE;
	
	// in Cookie speichern
	updateCookies ();
	
	// URL updaten
	updateURL ();

}

// ************************************************************************************************
// Sprache auf Englisch stellen und URL updaten
// Zudem: Wahl in Cookie abspeichern
function setEnglish () {

	// Variable setzen
	LANG = EN;
	
	// in Cookie speichern
	updateCookies ();
	
	// URL updaten
	updateURL ();
	
}

// ************************************************************************************************
// Zwischen Normal- und Druckansicht umschalten und URL updaten
function toggleView () {

	// Ansicht toggeln
	if (VIEW == NORMAL) {
		VIEW = PRINT;
	} else {
		VIEW = NORMAL;
	}

	updateCookies ();
	updateURL ();

}

// ************************************************************************************************
// erstellt die entsprechende URL; die Daten werden dann beim nächsten Laden von analyseURL() ausgewertet
function updateURL () {

	if ((LANG == DE) && (VIEW == NORMAL)) {
		window.location.href = URL;
	} else if ((LANG == DE) && (VIEW == PRINT)) {
		window.location.href = URL + "?lang=de&view=print";
	} else if ((LANG == EN) && (VIEW == NORMAL)) {
		window.location.href = URL + "?lang=en";
	} else if ((LANG == EN) && (VIEW == PRINT)) {
		window.location.href = URL + "?lang=en&view=print";
	}

}

// ************************************************************************************************
// entschlüsselt die Email, die in Array() angegeben ist, Anti-Spam-Methode
function DecryptEmail() {

	// Variablen
	var data = new Array("%77","%65","%62","%6D","%61","%73","%74","%65","%72","%40","%6A","%62","%2D","%65","%6C","%65","%63","%74","%72","%6F","%6E","%69","%63","%73","%2E","%64","%65");
	var a = "mailto:"
	var i = 0;

	// Email zusammensetzen
	for (i = 0; i < 27; i++) {
		a = a + unescape(data[i]);
	}

	// und Email-Fenster öffnen
	window.location.href = a;

}

// ************************************************************************************************
// sucht nach einer Nixie-Röhre in der Sammlung
function SucheNixie (id) {

	var address = ""; 
	var counter;
	for (counter = 0; counter < window.location.href.length; counter++) {
		if (window.location.href.charAt(counter) != '#') {
			address = address + address.charAt(counter);						
		}
	}
	window.location.href = address + "#" + document.getElementById(id).value.toUpperCase();

}
