//Place ID's of all required fields here.
function requiredFields() {
	switch(jQuery('#themen').val()) {
		case 'Produkttechnik':		return ['produkte',		'mitteilung',	'vorname',	'nachname',				'email',	'strasse',	'plz',	'stadt',	'land',	'telefon']; break;
		case 'Vertrieb':			return ['verkauf',		'mitteilung',	'vorname',	'nachname',				'email',				'plz',	'stadt',	'land']; break;
		case 'Marketing':			return [				'mitteilung',				'nachname',				'email',	'strasse',	'plz',	'stadt',	'land']; break;
		case 'Sponsoring':			return [				'mitteilung',	'vorname',	'nachname',	'firma',	'email',	'strasse',	'plz',	'stadt',	'land',	'telefon']; break;
		case 'Reklamation':			return [				'mitteilung',	'vorname',	'nachname',				'email',	'strasse',	'plz',	'stadt',	'land',	'telefon']; break;
		case 'Personal':			return [				'mitteilung',	'vorname',	'nachname',				'email']; break;
		case 'AnregungundKritik':	return [				'mitteilung',				'nachname',				'email']; break;
		case 'AllgemeineAnfrage':	return [				'mitteilung',				'nachname',				'email']; break;
		default:					return [				'mitteilung',				'nachname',				'email'];
	}
}

$(document).ready( function(){
	/*searchhover();*/
	validate_contactform();
	initMenu();
	elements_css();
	gleiche_hoehe();
	ie6_fix();
	sechser_padding_left();
	table_style();
	downloads_imagecheck();
	news_pagebrowser_icons();
	tablesort();
	//wenn hier tablesort gestartet wird funtionier accordion nicht mehr???
	related_links_box_hoehe();
	change_search_field();
	einer_box_design();

	/* Fancybox für Produktbilder */
	if(!$('a.image1').length == 0){
		$("a.image1").fancybox({
			'speedIn'		:	1000,
			'speedOut'		:	200
		});
	}

	$("#cat_filter").change(function(){
		$("#suchfeld").val('');
	});

	var searchInputElement = $('#tx-indexedsearch-searchbox-sword');
	var searchInputElementBackgroundActive = '/fileadmin/templates/image/menu_such_feld.png';
	var searchInputElementBackgroundInactive = '/fileadmin/templates/image/menu_such_btn.png';

	searchInputElement.focus(function() {
		$(this).css({
			'background': 'url(' + searchInputElementBackgroundActive + ') no-repeat',
			'margin-left': '49px',
			'margin-top': '-6px',
			'padding': '10px 31px 10px 10px',
			'width': '119px'
		});
	});
	searchInputElement.blur(function() {
		$(this).css({
			'background': 'url(' + searchInputElementBackgroundInactive + ') no-repeat',
			'margin-left': '134px',
			'margin-top': '-1px',
			'padding': '5px 28px 15px 8px',
			'width': '34px'
		});
	});

	searchInputElement.blur();
});

function einer_box_design(){
	$('.box_zweier.lonely').each(function(intIndex) {
		var counting = intIndex % 2;
		if (counting === 0) {
			$(this).addClass('left height_one'+intIndex);
		}
		else {
			test = intIndex-1;
			$(this).addClass('right height_two'+test);
			$(this).after('<div class="clearer"></div>');
		}
	});

	$('.box_zweier.lonely:last').after('<div class="clearer"></div>');

	//jeden zweiten gleich Höhe //MUSS NOCH DYNAMISCH GEMACHT WERDEN
	h_one = '';
	h_two = '';
	h_one = $('.height_one0').height();
	h_two = $('.height_two0').height();
	if(h_one > h_two){
		$('.height_two0').css({'height': h_one});
	}
	if(h_two > h_one){
		$('.height_one0').css({'height': h_two});
	}
}

function change_search_field(){
	$('.stw_field').show();
	$('.artnr_field').hide();
	var $radios = $('input:radio[name=stichw_artnr]');
	 $radios.filter('[value=stw]').attr('checked', true);

	$("#stw").click(function() {
		$('.stw_field').show();
		$('#cat_filter').show();
		$('.stichwort_such_btn').show();
		$('.artnr_field').hide();
		$('.stichwort_artnr_btn').hide();
	});
	$("#artnr").click(function() {
		$('.stw_field').hide();
		$('#cat_filter').hide();
		$('.stichwort_such_btn').hide();
		$('.artnr_field').show();
		$('.stichwort_artnr_btn').show();
	});
}

function related_links_box_hoehe(){
	$h_one = $('.interessieren_liste.one').height();
	$h_two = $('.interessieren_liste.two').height();
	if($h_one > $h_two){
		$('.interessieren_liste.two').css({'height': $h_one});
	}
	if($h_two > $h_one){
		$('.interessieren_liste.one').css({'height': $h_two});
	}
}


function news_pagebrowser_icons(){
	//DESIGN FÜR BUTTON  'VOR ANFANG ENDE ZURÜCK' IM PAGEBROWSER
	$('.page_browse a').each(function(){
		bval = $(this).html();
		if( bval == '&gt;' || bval == '&gt;|' || bval == '&lt;' || bval == '|&lt;')
		$(this).addClass('next_last');
	});
}

function downloads_imagecheck(){
	//PDFs Vorschauen werden von Typo generiert
	//Checke src für PDF Vorschaubilder in Download, bei Problem setze Default Bild
	$('.half_div div img').each(function(){
		if($(this).attr("src") == '') {
			$(this).hide();
			$(this).parent('a').html('<img src="/fileadmin/templates/image/dummy72.png" alt="NGK Download" />');
		}
	});
		//.attr("src");
		//
}

//Suchformular Texte (texte in ts/setup.ts)
function searchhover(){
	lang_search_val = $('#searchform .searchstring').val();
	$("#searchform .searchstring").focus(function(){
	    if($(this).attr("value") == lang_search_val) $(this).attr("value", "");
		});
	$("#searchform .searchstring").blur(function(){
		if($(this).attr("value") == "") $(this).attr("value", lang_search_val);
		});

}

//Kontaktformular validieren
function validate_contactform(){
	// If using an ID other than #email or #error then replace it here
	email = $("#email");
	emailw = $("#emailw");
	errornotice = $("#error");
	// The text to show up within a field when it is incorrect
	emptyerror = "";
	emailerror = "";

	$("#kontakt_form").submit(function(){
		//Place ID's of all required fields here.
		required= requiredFields();

		//Validate required fields
		if(
				(email.val() != emailw.val()) ||
				(email.val() == '' && emailw.val() == '')
		) {
			$('#email_error').addClass("needsfilled");
			$('#email_error').removeClass("green");
			$('#email').addClass("needsfilled");
			$('#emailw').addClass("needsfilled");
		}
		else{
			$('#email_error').removeClass("needsfilled").addClass("green");
		}

		for (i= 0; i< required.length; i++) {
			var input= jQuery('#'+ required[i]);
			var ckval= input.val();

			if(input.hasClass('radiogroup')) {				//	input.get(0).tagName.toLowerCase()== 'span')				ckval= input.find('input:checked').val();	//	'[name="' + required[i] + '"]:checked');
//				alert(ckval);
			}
			if ((ckval == '') || (ckval == null) || (ckval == undefined) || (ckval == emptyerror)) {
				input.addClass("needsfilled");
				input.val(emptyerror);
				errornotice.fadeIn(750);
			} else {				input.removeClass("needsfilled");
			}
		}
		// Validate the e-mail.
		if (!/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(email.val())) {
			email.addClass("needsfilled");
			email.val(emailerror);
		}

		//if any inputs on the page have the class 'needsfilled' the form will not submit
		if ($(":input").hasClass("needsfilled")) {
			return false;
		} else {
			errornotice.hide();

			formaction=$("#kontakt_form").attr("action");
			$("#kontakt_form").attr("action", formaction + "?checked=1");

			return true;
		}
	});

	// Clears any fields in the form when the user clicks on them
	$(":input").add($(":textarea")).focus(function(){
	   if ($(this).hasClass("needsfilled") ) {
			$(this).val("");
			$(this).removeClass("needsfilled");
	   }
	});
}

function initMenu(){

	//Jeden zweiten ul in ul vom Hauptmenü

	$('#navi ul li div.outer').each(function(){
		//alert('div');
		$(this).find('div').each(function(intIndex) {
			var counting = intIndex % 2;
			if (counting === 1) {
				$(this).addClass('zwei');
				$(this).after('<div class="spcclear">&nbsp;</div>');
			}
		});
	});

	//MENÜ OBEN HINTERGRUND HALTEN
	$("#navi ul li div.outer").hover(function() {
		$(this).parent("li").find("a:first").addClass('akt_hovi');
	},
		function(){
			$(this).parent("li").find("a:first").removeClass('akt_hovi');
		}
	);

}

function elements_css(){

	$("#footer a:first").addClass('first');

	//Sechser Unten hover
	$("#content .sechser a").hover(function() {
		$(this).parent("div").addClass('sechser_hover');
	},
		function(){
		$(this).parent("div").removeClass('sechser_hover');
		}
	);

	//Startseite Dreier hover
	$("#content .start_dreier").hover(function() {
		$(this).addClass('hover');
	},
		function(){
		$(this).removeClass('hover');
		}
	);

	//Startseite Dreier hover
	$("#content .start_dreier_two").hover(function() {
		$(this).addClass('hover');
	},
		function(){
		$(this).removeClass('hover');
		}
	);

	//Startseite Dreier hover
	$("#content .box_zweier").hover(function() {
		$(this).addClass('hover');
	},
		function(){
		$(this).removeClass('hover');
		}
	);

	//Startseite Dreier p-Tag ohne a
	$("#content .start_dreier p a").parent('p').addClass('has_link');

	//Abstand für Bild ja/nein
	$('.content_rechts .yellow_bottomline').each(function(i){
		if(!$(this).find('img').attr("src")){
			$(this).find('div.float_left a').html('<img width="156" height="98" border="0" title="NGK News" alt="NGK News" src="fileadmin/templates/image/dummies/news_dummy_313.png">');
		}
	});

	$('#content .content_rechts .bild_text .float_left').each(function(i){
		if(!$(this).find('img').attr("src")){
			$(this).hide();
		}
	});

	$('.content_rechts .yellow_bottomline:last').addClass('last');


	//Design für Buttons .std_btn
	$("#content .std_btn").after('<div class="std_btn_end"></div>');

	//Downloads jedes zweite div Float right
	$('#content .content_rechts .half_div').each(function(intIndex) {
		var counting = intIndex % 2;
		if (counting === 1) {
			$(this).addClass('zweiter')
		}
	});

	//Clearer nach jedem zweiten DIV in Downloads
	$('.half_div.zweiter').after('<div class="clearer">&nbsp;</div>');

	//Suchergbnis letzter Block ohne gelbe Linie
	$(".tx-indexedsearch-res .tx-indexedsearch-res:last").addClass('last');

	//Suchergebnis Pagination Design < > Vor Zurück Button
	$('#content ul.browsebox li a').each(function(intIndex) {
		text = $(this).html();
		if( text == '&gt;' || text == '&lt;')
		$(this).addClass('next_last');
	});

	$(".lang_choose").hover(function(){
		$(this).find('ul.second').show();
    }, function(){
    	$(this).find('ul.second').hide();
    });


	$('.pfinder_right_outer .pfinder_half').each(function(intIndex) {
		var counting = intIndex % 2;
		if (counting === 1) {
			$(this).addClass('right');
			$(this).after('<div class="clearer"></div>');
		}
		else
			$(this).addClass('left');
	});

	//Alle DIV gleiche Höhe
	var HalfTallest = 0;
	$('.pfinder_right_outer .pfinder_half .imagebox').each(function(){
		if ($(this).height() > HalfTallest) { HalfTallest = $(this).height(); }
	});
	$('.pfinder_right_outer .pfinder_half .imagebox').css({'min-height': HalfTallest});
	if ($.browser.msie && $.browser.version == 6.0) { $('.pfinder_right_outer .pfinder_half .imagebox').css({'height': HalfTallest}); }

	var H6HolderTallest = 0;
	$('.pfinder_right_outer .pfinder_half .h6_holder').each(function(){
		if ($(this).height() > H6HolderTallest) { H6HolderTallest = $(this).height(); }
	});
	$('.pfinder_right_outer .pfinder_half .h6_holder').css({'height': H6HolderTallest});

	var H6Padding = 0;
	$('.pfinder_right_outer .pfinder_half .h6_holder').each(function(){
		h6Obj = $(this).find('h6');
		h6height = $(this).find('h6').height();

		//Max 2 Zeilen
		if (H6HolderTallest == 34) {
			if (h6height < 18)
				h6Obj.css({
					'padding-top': 17
				});
		}
		//Max 3 Zeilen
		if (H6HolderTallest == 51) {
			if (h6height == 17)
				h6Obj.css({
					'padding-top': 34
				});
			if (h6height == 34)
				h6Obj.css({
					'padding-top': 17
				});
		}

	});

}

function table_style(){
	$('#content .content_rechts table').each(function(){
	//alert('found table');
	var $table = $(this);
		first = 0;
		$('tr', $table).each(function(row) {
			var $row = $(this);
				//erste td fett
				td=0;
				$('td', $row).each(function(td) {
					if(td==0)$(this).addClass('fett');
					td = 1;
				});
			//erste tr
			if (row == 0) $row.addClass('topper');

			if(first==1){
				//alert('ddd');
				var counting = row % 2;
				if (counting === 1) $row.addClass('first');
				else $row.addClass('second');
			}

			/*
			$row.hover(function() {
					$row.addClass('row_hover');
				},
					function(){
					$row.removeClass('row_hover');
					}
			);
			*/

			//erster Durchlauf beendet
			first = 1;
		});
	});
}

function gleiche_hoehe(){
	//Start Dreier gleiche Höhe
	var dreier_tallest = 0;
	$('#content .start_dreier').each(function(j){
		if ($(this).height() > dreier_tallest) { dreier_tallest = $(this).height(); }
	});
	//dreier_tallest = dreier_tallest + 5;
	$('#content .start_dreier').css({'height': dreier_tallest});
	if (typeof document.body.style.maxHeight === "undefined") { $('#content .start_dreier').css({'height': dreier_tallest}); }
	$('#content .start_dreier').css({'min-height': dreier_tallest});


	var dreier_tallest_two = 0;
	$('#content .start_dreier_two').each(function(j){
		if ($(this).height() > dreier_tallest_two) { dreier_tallest_two = $(this).height(); }
	});
	//dreier_tallest_two = dreier_tallest_two + 5;
	$('#content .start_dreier_two').css({'height': dreier_tallest_two});
	if (typeof document.body.style.maxHeight === "undefined") { $('#content .start_dreier_two').css({'height': dreier_tallest_two}); }
	$('#content .start_dreier_two').css({'min-height': dreier_tallest_two});


	zweier_tallest = 0;
	$('#content .zweier_aussen').each(function(k){
		zweier_tallest = 0;
		$(this).find('.box_zweier').each(function(j){
				if ($(this).height() > zweier_tallest) { zweier_tallest = $(this).height(); }
		});

		$(this).find('.box_zweier').css({'height': zweier_tallest});
	});
}

//Abstand links für Sechser Boxen Unten abhängig von der Anzahl
function sechser_padding_left(){
	$('#content #bottom_links .sechser').each(function(i){
		i++;
		switch(i)
        {
            case 1: x=410;
            break;
            case 2: x=334;
            break;
            case 3: x=252;
            break;
            case 4: x=162;
            break;
            case 5: x=82;
            break;
            case 6: x=0;
            break;
            default: x =0;
        }
		$('#content #bottom_links').css({'padding-left': x});
	});
}

//IE6 FIXING
function ie6_fix(){
	if(typeof document.body.style.maxHeight === "undefined") {
		$("#content .sechser").addClass('ie6');
		$("#content .sechser:first").addClass('ie6_first');

		//MENÜ OBEN
		$("#navi ul li").hover(function() {
			$(this).find("div.outer").addClass('ie_hover');
		},
			function(){
			$(this).find("div.outer").removeClass('ie_hover');
			}
		);

		//Sprach Dropdown
		$(".lang_choose").hover(function(){
			$(this).find('ul.second').show();
	    }, function(){
	    	$(this).find('ul.second').hide();
	    });
	}
	//TOPMENÜ
}

function tablesort(){
		$('table.pfinder tr th:last-child').addClass('last');
		$('table.pfinder tr td:last-child').addClass('last');

		$('table.pfinder tr:first').addClass('first_tr');

		$("table.pfinder tr.val").hover(function() {
			if(!($(this).hasClass(".theader"))) $(this).addClass('tr_hover');
		},
			function(){
			$(this).removeClass('tr_hover');
		});
}

function setElement(eID,funct,wert){
	if (document.getElementById){
		var newElement = document.getElementById(eID).style;

		newElement[funct] = wert;
	}
}

function m2uri(that) {
	that.href= that.href
		.replace(/###HOST###/g, escape(document.location.host))
		.replace(/###URI###/g, escape(document.location.href))
	;
	return true;
}


