var lia = '';

$().ready(function()
{
	$('body').addClass('js');
	
	lia = new lia_init('synflex.de', '$Rev: 47597 $');

	lia.dummy_links();
	lia.external_links();
	lia.ie6_noflickr();
	
// DEBUG ...................................................................< */
	$('ul#navigation li').each(function(){
		//alert($(this).height());
	});
	
	$("a[href*=.pdf]").each(function() {
		lia.console($(this).attr('href').replace(/http:\/\/devel\/~rose\/synflex\/download\/produkte\/de\//,''));
	});
	
	
// FIXED MENU ............................................................... */
	header_height = 117;
	scroll_delay = 250;
	fixed_item_height = 30;
	scroll_after_px_invisible = fixed_item_height;
	
	$('body').attr('onscroll', 'fixedMenu()');
	if ($(window).scrollTop() > header_height)
	{
		fixedMenu();
	}
	//$('body').load(doAttachEvent);
	
// JAVASCRIPT-FEATURES ...................................................... */

	elems_to_show = [
		'#navigation_product_quick_dropdown',
		'body#template_support_ansprechpartner #navigation_product_groups_wrapper',
		'h3.navigation_product_quick_title',
		'a.navigation_welcome_open'
	];
	
	elems_to_hide = [
		'#navigation_product_groups_persons',
		'ul.jobs_list',
		'div.footer_jobs',
		'#navigation_product_groups_wrapper'
	];
	
	$.each(elems_to_show, function(i, elem) {
		//console.log($(elem));
		if ($(elem).length >= 1) {
			$(elem).show();
		}
	});
	$.each(elems_to_hide, function(i, elem) {
		//console.log($(elem));
		if ($(elem).length == 1) {
			$(elem).removeClass('open');
		}
		else {
			$(elem).hide();
		}
	});
	
// PNG-Fix .................................................................. */
	if ($.browser.msie && jQuery.browser.version == '6.0') {
		elems_to_fix = '.png';
		elems_to_fix_obj = $(elems_to_fix);
		DD_belatedPNG.fix(elems_to_fix);
	}
	
	//alert($('div#navigation_top').outerHeight());
	//alert($('ul#navigation li').outerHeight());
	
// Navigation ............................................................... */

	var sf_options = {
	    hoverClass:    'hovered',          // the class applied to hovered list items 
	    pathClass:     'overideThisToUse', // the class you have applied to list items that lead to the current page 
	    pathLevels:    1,                  // the number of levels of submenus that remain open or are restored using pathClass 
	    delay:         800,                // the delay in milliseconds that the mouse can remain outside a submenu without it closing 
	    animation:     {opacity:'show'},   // an object equivalent to first parameter of jQuery’s .animate() method 
	    speed:         'normal',           // speed of the animation. Equivalent to second parameter of jQuery’s .animate() method 
	    autoArrows:    false,               // if true, arrow mark-up generated automatically = cleaner source code at expense of initialisation performance 
	    dropShadows:   false,               // completely disable drop shadows by setting this to false 
	    disableHI:     false,              // set to true to disable hoverIntent detection 
	    onInit:        function(){},       // callback function fires once Superfish is initialised – 'this' is the containing ul 
	    onBeforeShow:  function(){
			//if (this).parent('li').
			if (this.parent('li').hasClass("selected") === true) {
			}
			
		}, // callback function fires just before reveal animation begins – 'this' is the ul about to open 
	    onShow:        function(){},       // callback function fires once reveal animation completed – 'this' is the opened ul 
	    onHide:        function(){}        // callback function fires after a sub-menu has closed – 'this' is the ul that just closed 
	};
	//$("ul#navigation").superfish(sf_options);
	
// Ansprechpartner-Bilder ................................................... */
	$('div.contact_person tr.contact_person_img img').each(function(){
		if ($(this).attr('src') != 'images/img_ansprechpartner_dummy.jpg')
		{
			$(this).hover(
				function()
				{
					$(this).attr('src', $(this).attr('src').replace(/\.jpg/, '_f.jpg'));
				},
				function()
				{
					$(this).attr('src', $(this).attr('src').replace(/\_f.jpg/, '.jpg'));
				}
			);
		}
	});
	
// Kupfer-Kurse ............................................................. */
	$('a#navigation_top_open_courses').click(function(){
		$('div#navigation_top').toggleClass("open");
		return false;
	});
	
// Feste Breiten für die Navigations-Elemente unten links ................... */
	$('ul#navigation_extra li:not(.divider, .selected)').each(function() {
		$(this).find('a').addClass('selected');
		var new_width = $(this).width();
		$(this).find('a').removeClass('selected');
		$(this).css('width', new_width);
	});
	
// STARTSEITE ............................................................... */
if ($('body#template_welcome').length == 1) {
	
	// Boxen ................................................................ */
	boxes = $('div.navigation_welcome_subdata');
	lia.equal_height(boxes);
	
	$('a.navigation_welcome_open').toggle(
		function () {
			boxes.fadeIn('normal',function(){
				$('div#wrapper_navigation_welcome').addClass('open');
			});
		},
		function () {
			boxes.fadeOut('normal',function(){
				$('div#wrapper_navigation_welcome').removeClass('open');
			});
		}
	);

	// Flash-Intro .......................................................... */
	if ( $('#header_image.flash').length == 1)
	{
		var swf = base+'swf/intro';
		swf += '.swf';
		
		var flashvars = {};
		var params = {};
		var attributes = {};
		attributes.id = 'header_image';
		params.wmode = 'transparent';
		swfobject.embedSWF(
			swf,
			"header_image",
			"950",
			"525",
			"9.0.0",
			false,
			flashvars,
			params,
			attributes
		);
	}
		
	// Reflektion ..........................................................< */
	$("#navigation_product_quick img").reflect({height:0.22});
		
	// Produkt-Galerie/Schnelleinstieg .....................................< */
	$('div#wrapper_navigation_product_quick').addClass('js');
		
	$('ul#navigation_product_quick').cycle({
	    fx:     'scrollHorz', 
	    speed:   1000, 
	    timeout: 3000, 
	    next:   'a#navigation_product_quick_next', 
	    prev:   'a#navigation_product_quick_prev',
	    pause:   1
	});
	
	$('ul#navigation_letzteBox').cycle({
	    fx:     'fade', 
	    speed:   1000, 
	    timeout: 8000, 
	    next:   '', 
	    prev:   '',
	    pause:   1
	});
		
}

// MEHRERE HEADER-BILDER .................................................... */
	
	// Wechsel der möglichen Header-Bilder
	if ($('#header_image.multiple_header').length == 1 && $('#header_image').cycle != undefined) {
		/*$('div#header_image').css('cursor','pointer').toggle(
			function () {
				$(this).css('background-image','url(http://devel/~rose/synflex/images/bg_header_image_support_branche_2.jpg)');
			},
			function () {
				$(this).css('background-image','url(http://devel/~rose/synflex/images/bg_header_image_support_branche.jpg)');
			}
		);*/
		$('#header_image').cycle({
		    fx:     'fade', 
		    speed:   1500, 
		    timeout: 4000, 
		    next:   'a#navigation_product_quick_next', 
		    prev:   'a#navigation_product_quick_prev'
		});
	}
	
// Produkt-Schnelleinstieg .................................................< */
	$("#product_quick_dropdown, .navigation_product_groups_dropdown").change(function() {
		var sel = $(this).val();
		//console.log(sel);
		var newurl = sel;
		//console.log(newurl);

		if ($('body#template_support_ansprechpartner').length) {
			//console.log('foo');
			sel = sel.split('#');
			//lia.console($('.contact_person_box.open').length);
			if ($('.contact_person_box.open').length == 1) {
				$('.contact_person_box.open').fadeOut('normal', function() {
					$('.contact_person_box_'+sel[1]).fadeIn('slow').addClass('open');
				}).removeClass('open');
			}
			else {
				$('.contact_person_box_'+sel[1]).fadeIn('slow').addClass('open');
			}
		}
		else if ($('body#template_karriere_offene-stellen').length && sel.indexOf('produkte-leistungen') < 1) {
			
			$('#navigation_product_groups_wrapper').addClass('open');
			sel = sel.split('#');
			elem_id = 'jobs_'+sel[1];
			if (sel[1])
			{
				location.hash = sel[1];
				
				//lia.console(sel);
				lia.console(elem_id);
				//lia.console($('.jobs_.open').length);
				if ($('.jobs_.open').length) {
					$('.jobs_.open').fadeOut('normal', function(){
						$('#' + elem_id).fadeIn('slow').addClass('open');
					}).removeClass('open');
				}
				else {
					// jobs_list_schueler-schulabgaenger
					//lia.console($('.'+class_name));
					// .jobs_list_schueler-schulabgaenger
					$('#' + elem_id).fadeIn('slow').addClass('open');
				}
				
				$('div.footer_offene-stellen').show();
			}
		}
		else {
			if (newurl != '#') {
				location.href = newurl;
			}
		}
	});
	
// KARRIERE ................................................................. */
if (($('body.karriere').length == 1) || ($('body.fachlexikon').length == 1)) {
		
	$('.jobs_txt').hide();
	
	$('#wrapper_jobs h3').css('cursor', 'pointer');
	
	$('#wrapper_jobs h3').click(function()
	{
		$('#wrapper_jobs').removeClass('kaufmaennisch').addClass('technisch');
	});
	
	$('h3#jobs_kaufmaennisch_h3').click(function()
	{
		$('#wrapper_jobs').removeClass('technisch').addClass('kaufmaennisch');
	});
	
	$('div.jobs_ h4').css('cursor', 'pointer').click(
		function () {
			if ($(this).siblings('.jobs_txt').is('.open'))
			{
				$(this).siblings('.jobs_txt').fadeOut(
					'slow',
					function(){
						$(this).removeClass('open');
						//$.scrollTo($(this).parent());
					}
				);
			}
			else
			{
				$(this).siblings('.jobs_txt').addClass('open').fadeIn(
					'slow',
					function() {
						$.scrollTo($(this).parent());
					}
				);					
			}
			
			return false;
		}
	);
	
	// URL-Hash aktiv? (z.B. 'offene_stellen/#schueler-schulabgaenger')
	if (location.hash)
	{
		// Auf der Seite 'Offene Stellen' öffnet sich beim Zugriff
		// über den URL-Hash die jeweilige Rubrik
		/*if ($('body#template_karriere_offene-stellen').length)
		{
			if (location.hash)
			{
				hash = location.href;
				//lia.console(hash);
				target_given = hash.split('~~~');
				//lia.console(target_given.length);
				
				if (target_given.length === 2)
				{
					hash = target_given[0];
					
					target = '#' + target_given[1];
					//lia.console(target);
					
					if ($(target).length)
					{
						$(target).click();
					}
					
					delete(target_given);
				}
				
				$(".navigation_product_groups_dropdown").val(hash);
				//lia.console($(".navigation_product_groups_dropdown").val());
				delete(hash);
				
				//lia.console($(".navigation_product_groups_dropdown").val());
				$(".navigation_product_groups_dropdown").change();
			}
		}
		else if (location.hash.search(/jobs/) > -1)
		{
			$(location.hash + '_h3').click();
		}*/
	}
}

// SUPPORT > Ansprechpartner ................................................ */
if (($('body#template_support_ansprechpartner').length == 1))
{
	// URL-Hash aktiv? (z.B. 'support/ansprechpartner.html#anwendungstechnik')
	if (location.hash)
	{
		hash = location.href;
		//lia.console(hash);
		
		$(".navigation_product_groups_dropdown").val(hash);
		//lia.console($(".navigation_product_groups_dropdown").val());
		delete (hash);
		
		//lia.console($(".navigation_product_groups_dropdown").val());
		$(".navigation_product_groups_dropdown").change();
	}
	
	
}

	// Link auf Ansprechpartner-Seite?
	ap_href = $("#content a[href*='ansprechpartner.html']");
	lia.console(ap_href.length);
	
	if ( ap_href.length === 1)
	{
		body_id = $('body').attr('id');
		if ( body_id === 'template_produkte-leistungen_technologische-beratung'
		|| body_id === 'template_produkte-leistungen_oekologische-verantwortung'
		|| body_id === 'template_unternehmen_produktion-konfektion')
		{
			target = 'anwendungstechnik';
		}
		
		if ( typeof(target) !== 'undefined' )
		{
			ap_href.attr('href', ap_href.attr('href') + '#' + target);
		}
	}
	
// PRODUKTSEITEN ............................................................ */
	// Wrappen der Spalte 'Typ' in der Übersichtstabelle unterbinden
	if ($('.table_produkt_overview').length == 1)
	{
		$('.table_produkt_overview td.table_produkt_overview_th_1').attr('nowrap','nowrap');
	}
	
// Produkt-Ansprechpartner .................................................< */
	if ($('#navigation_product_groups_wrapper #a_product_contact').length == 1
	&& $('#navigation_product_groups_wrapper div.contact_person').length >= 1)
	{
		lia.console($('#navigation_product_groups_wrapper #a_product_contact').length);
		$('#navigation_product_groups_wrapper #a_product_contact').click(function()
		{
			$('li#navigation_product_groups_persons').toggleClass('open');
			return false;
		});
	}
	
//  Kartenmodul ............................................................. */
if ($('#flash_standorte').length)
{
	
	// Flash ................................................................ */
	flashvars = {};
	params = {};
	attributes = {};
	attributes.id = 'flash_standorte';
	attributes.name = 'flash_standorte';
	params.wmode = 'transparent';
	params.swliveconnect = 'true';
	params.menu = 'false';
	params.allowscriptaccess = 'always';
	swfobject.embedSWF(
		base+"swf/karte.swf",
		"flash_standorte",
		"682",
		"816",
		"9.0.0",
		false,
		flashvars,
		params,
		attributes
	);

	$('.affiliate_data img.affiliate_data_close').click(function(){
		$(this).parent('.affiliate_data').removeClass('open').fadeOut('fast');
	});
}

	//Produktsuche
	$("select#suche_produktgruppe").change(function()
	{
		$("div.suche").fadeOut();
		gruppe = $("#suche_produktgruppe option[@selected]").attr('value');

		if (gruppe != "null")
		{		
			$("div#suchkriterien").fadeIn();
			
			if (gruppe != "trafoklemmen") {
	        	$("div#suche_waermeklasse").fadeIn();
	        }
			if (gruppe != "trafoklemmen" && gruppe != "impraegnierstoffe") {
	        	$("div#suche_ul").fadeIn();
	        }
			if (gruppe == "flaechenisolierstoffe") {
	        	$("div#suche_material_flaechen").fadeIn();
	        }
			if (gruppe == "wickeldraehte") {
				$("div#suche_normen").fadeIn();
			}

			if (gruppe == "klebebaender") {
				$("div#suche_traeger").fadeIn();
			}

			if (gruppe == "trafoklemmen") {
				//$("div#suche_bemessung").fadeIn();
				$("div#suche_strombelastung").fadeIn();
			}
			if (gruppe == "impraegnierstoffe") {
				$("div#suche_eigenschaft").fadeIn();
			}
			
		}
		else
		{
			$("div#suchkriterien").css("display","none");
		}       
	});
});

// Popup-Funktion für Kartenmodul ........................................... */
function show_popup(country) {
	//alert('Flash, ahaaaaaaaaaaaaaaaaa!');
	country_div = $('div#affiliate_data_'+country);
	if (country_div.length) {
		$('.affiliate_data.open').hide();
		country_div.addClass('open');
		country_div.addClass('open').fadeIn('slow');
	}
}

function flash_standorte_DoFSCommand(command, args) {
	//alert('Flash DoFSCommand, ahaaaaaaaaaaaaaaaaa!');
	if (command == 'show_popup') {
		show_popup(args);
	}
}

// hebt Elemente hervor, wenn diese über Anker angesprochen werden
// z.B. #kontakt_mti-america
function hash_highlight() {
	if (location.hash && $(location.hash).length)
	{
		bg_old = $(location.hash).css('backgroundColor');
		
		$(location.hash).fadeOut('normal').fadeIn('normal', function()
		{
			$(location.hash).css('backgroundColor', '#FFFF99');
			$(location.hash).mouseover(function(){
				$(location.hash).css('backgroundColor', bg_old);
			});
		});
	}
}

function fixedMenu()
{
	scrolled = $(window).scrollTop();

	var fix_when_scrolled_more_than = (header_height + scroll_after_px_invisible);
	fix_when_scrolled_more_than = header_height;

	if (scrolled > fix_when_scrolled_more_than) {
		lia.console("Menu is now fixed");
		$('div#wrapper_navigation_and_logo').addClass('fixed');
	}
	else {
		if (scrolled < header_height) {
			lia.console("Menu is now unfixed");
			$('div#wrapper_navigation_and_logo').removeClass('fixed');
			$('div#left').removeClass('fixed');
		}
	}
	return false;
}

function doAttachEvent()
{
	try
	{
		if ($.browser.msie)
		{
			window.attachEvent("onscroll", fixedMenu);
		}
	}
	catch(err) {}
}

function neuesFenster(formul){
	var alles = document.getElementById(formul);
	var uebstr = '';
	for(var i=0;i<alles.length;i++){
		if(alles.elements[i].type != "hidden" && alles.elements[i].type != "submit"){		
			uebstr += alles.elements[i].name;
			uebstr += "="+alles.elements[i].value+"&";
		}
	}
	uebstr = uebstr.substring(0,uebstr.lastIndexOf("&"));	
	window.open(alles.url.value+"?"+uebstr);
	return false;
}
