$(document).ready(function(){
	// IE6: Einblenden der Subnav, da kein li:hover unterstuetzt wird
	if ($.browser.msie && $.browser.version=="6.0") {
		$("div#nav-main ul li, div#nav-sub ul li, div#rubriken ul li, div#produktauswahl ul li").hover(
			function () {
				$(this).addClass("aktiv");
				$(this).children("ul").addClass("subnavaktiv");
			},
			function () {
				$(this).removeClass("aktiv");
				$(this).children("ul").removeClass("subnavaktiv");
			}
		);
	}
	
	// Startseite
	if ($("body").hasClass("home")) {
		set_motiv(); // Wechselbild
		set_movies(); // Filme
	}
	
	// Marketing Support
	if (document.getElementById('ru')) {		
		prod_graben = $("option#graben").text();
		prod_spezial = $("option#spezial").text();
		
		langname_ru = $("option#ru").text();
		langname_tu = $("option#tu").text();
		
		brochure_lang();
		brochure_prod();
	}
	
	if (document.getElementById('Produktgruppe_r')) { setLanguage(); }
	if (document.getElementById('Werbemittel_r')) { setImageWM(); }
	
	if ( $(".pressebuttons").length > 0) {
		backbutton();
	}
	
});


/* =========
Startseite
========= */

// Download Kataloge

function get_catalogue (katalog) {
	if (katalog != "") {
		if ($.browser.msie) {
			window.location = katalog;
		}
		else {
			window.open(katalog,'download','toolbar=0,location=0,directories=0,statusbar=0,menubar=0,scrollbars=no,resizable=yes');
		}
	}
}

// Wechselbild

function set_motiv () {
	var bild_anzahl = 14;
	var bild_index = Math.round( Math.random()*(bild_anzahl-1) ); // Zufallszahl 0 bis 13
	
	// Zeigt das neue Bild an
	var motiv = "media/images/home/seitenbild-" + bild_index + ".jpg";
	$("div#seitenbild").prepend('<img src="' + motiv + '" width="953" height="408" alt="Willkommen" />');
}

// Filme

function set_movies () {
	// setup overlay actions to buttons
	$("a[rel]").overlay({

		// use the Apple effect for overlay
		effect: 'apple',
		expose: '#001125',
		top: '30%',
		
		onLoad: function(content) {
			// find the player contained inside this overlay and load it
			this.getOverlay().find("a.player").flowplayer(0).load();
		},
		
		onClose: function(content) {
			$f().unload();
		}
	});				
	
	// install flowplayers
	$("a.player").flowplayer("media/include/flowplayer-3.2.5.swf"); 
}


// Seite in neuem Fenster oeffnen

function openWinCenter(url,name,breite,hoehe,x,y){
  var MyWindow =0;
  MyWindow = window.open(url,name,'toolbar=0,location=0,directories=0,statusbar=0,menubar=0,scrollbars=no,resizable=no,width='+breite+',height='+hoehe);
  if (MyWindow){
    MyWindow.moveTo(screen.width/2-(breite/2+x),screen.height/2-(hoehe/2+y));
    MyWindow.focus();
  }
}


/* ==============
Marketing Support
============== */

var download_lang = '';
var langname = "";
var prodname = "";

// Initialisierung mit Sprache
function setLanguage () {
	if (document.getElementById('Sprache_r')) {
		download_lang = document.getElementById('Sprache_r').value;
	}
	if (document.getElementById('Produktgruppe_r')) {
		setImage();
	}
}

// Initialisierung ohne Sprache
function setImageWM() {
	var product_index = document.getElementById('Werbemittel_r').selectedIndex;
	if (document ['vorschaubild']) {
		document ['vorschaubild'].src = download_items_kat[product_index];
	}
	if (document.getElementById('Produktbeschreibung')) {
		document.getElementById('Produktbeschreibung').innerHTML = werbemittel_descr[product_index];
	}
}

function setImage (index) {
	var selected = document.getElementById('Produktgruppe_r').selectedIndex;
	if (index) { selected = index; }
	changeImage (selected);
}

function changeLanguage (lang_object) {
	var img_index = document.getElementById('Produktgruppe_r').selectedIndex;
	var lang_index = lang_object.selectedIndex;
	
	// Workaround: Index muss bei tuerkisch erhoeht werden
	if( (img_index == 0) && (document.getElementById('Produktgruppe_r').value == "Berg_Tunnelbau")) {
		img_index++;
	}
	
	download_lang = lang_object.value;		
	setImage (img_index);
	
	brochure_lang();
}

function changeLanguageKat (lang_object) {
	var lang_index = lang_object.selectedIndex;
	
	if (document ['vorschaubild']) {
		document ['vorschaubild'].src = download_items_kat[lang_index];
	}
}

function changeDescr (product_object) {
	var product_index = product_object.selectedIndex;
	
	if (document ['vorschaubild']) {
		document ['vorschaubild'].src = download_items_kat[product_index];
	}
	
	if (document.getElementById('Produktbeschreibung')) {
		document.getElementById('Produktbeschreibung').innerHTML = werbemittel_descr[product_index];
	}
}

function changeProduct (download_obj) {
	var img_index = download_obj.selectedIndex;
	changeImage (img_index);
	
	brochure_prod();
}

function changeImage (img_index) {
	if (document ['vorschaubild']) {
		if (download_lang == 'de') {			
			document ['vorschaubild'].src = download_items_de[img_index];
		}
		if (download_lang == 'en') {
			document ['vorschaubild'].src = download_items_en[img_index];
		}
		if (download_lang == 'es') {			
			document ['vorschaubild'].src = download_items_es[img_index];
		}
		if (download_lang == 'fr') {
			document ['vorschaubild'].src = download_items_fr[img_index];
		}
		if (download_lang == 'ru') {
			document ['vorschaubild'].src = download_items_ru[img_index];
		}
		if (download_lang == 'tu') {
			document ['vorschaubild'].src = download_items_tu[img_index];
		}
	}
}


// ================================
// Anpassung Broschueren / Kataloge
// ================================

function brochure_lang () {
	var index = document.getElementById('Produktgruppe_r').selectedIndex;
	//alert(index);
	
	// RU: Grabenfraesen nicht verfuegbar
	if (document.getElementById('Sprache_r').value == "ru") {
		$("option#graben").replaceWith('<span id="graben_ru"></span>');
	}
	else {
		$("span#graben_ru").replaceWith('<option id="graben" value="Grabenfraesen" >'+prod_graben+'</option>');
	}
	
	// TU: Nur Bergbau verfuegbar
	if (document.getElementById('Sprache_r').value == "tu") {
		$("option#graben").replaceWith('<span id="graben_tu"></span>');
		$("option#spezial").replaceWith('<span id="spezial_tu"></span>');
	}
	else {
		$("span#graben_tu").replaceWith('<option id="graben" value="Grabenfraesen" >'+prod_graben+'</option>');
		$("span#spezial_tu").replaceWith('<option id="spezial" value="Spezialtiefbau" >'+prod_spezial+'</option>');
	}
	
	document.getElementById('Produktgruppe_r').value.selectedIndex = index;
}

function brochure_prod () {
	var index = document.getElementById('Sprache_r').selectedIndex;
	
	// RU: Grabenfraesen nicht verfuegbar
	if (document.getElementById('Produktgruppe_r').value == "Grabenfraesen") {
		$("option#ru").replaceWith('<span id="lang_ru"></span>');
	}
	else {
		$("span#lang_ru").replaceWith('<option id="ru" value="ru" >'+langname_ru+'</option>');
	}
	
	// TU: Nur Bergbau verfuegbar
	if (document.getElementById('Produktgruppe_r').value != "Berg_Tunnelbau") {
		$("option#tu").replaceWith('<span id="lang_tu"></span>');
	}
	else {
		$("span#lang_tu").replaceWith('<option id="tu" value="tu" >'+langname_tu+'</option>');
	}
	
	document.getElementById('Sprache_r').value.selectedIndex = index;
}


// Agarar-Seiten Backbutton für News
function backbutton() {
	var found = false;
	var ref = document.referrer;
	if(ref.search(/news/) == -1 && ref.search(/nouveautes/) == -1 && ref.search(/novedades/) == -1) {
		var buttontext = 'back';
		if(ref.search(/\/de\//) != -1) buttontext = 'zur&uuml;ck';
		$('.pressebuttons').prepend('<div class="button backlink"><a href="javascript://" onclick="history.back();"><span>'+buttontext+'</span></a></div></div>');
	}
}

