jQuery(document).ready(function(){
  

// ================
// = IE cornerify =
// ================
	//$('.subnav2').corner("5px");

  // =============
  // = colourbox =
  // =============
//a.expand,
  $(" .box_ajx").colorbox({title: function(){
      var url = $(this).attr('href');
      return '<a href="'+url+'" target="_blank"></a>';
  }});
  
  $(".colorbox").colorbox();
  
	//window.oncontextmenu = function() { return false }
	//$('.round-corner').corner("7px");
	//$('.round-corner-bottom').corner("bottom", "7px");
	//$('.round-corner-top').corner("top", "7px");
	
	
	// ==================
	// = Home Fachtexte =
	// ==================
	$('.fachtextelink').mouseenter(function(){
    var idsplit = $(this).attr('id').split('-');
    var id = idsplit[1];
    $('.fachtexte').hide();
    $('#fachtext-' + id).show();
  });
  
  
  $('#fachtexte').mouseleave(function(){
    $('.fachtexte').hide();
    $('#fachtext-0').show();  
    });
  
  



  /*
	$(".fachtexte").mouseenter(function() {
		$('#fachtext_beschrieb').find('span').hide();
		var fachtext = $(this).attr('rel');
		$('#fachtext_beschrieb').find('span').html(fachtext).show();
	}).mouseleave(function() {
		var fachtextnormal = $('#fachtext-0').html();
		$('#fachtext_beschrieb').find('span').html(fachtextnormal).show();
		});
*/
	//var autoHeight = $( ".selector" ).accordion( "option", "autoHeight" );
	
	$('#accordion').accordion({collapsible: false, autoHeight: false, navigation: true, header: ".accordion-title"});
	$('.nav_sidebar').accordion({collapsible: false, autoHeight: true});
	
	
	
	
	// ===================
	// = techs more link =
	// ===================
	
	//initial
	$('.tech_togglewrapper').html('<a href="#" class="tech_more-link more">more</a><a href="#" class="tech_less-link" style="display:none">close</a>');
	$('.tech_more').hide();
	
	//open
	$('.tech_more-link').click(function(event){
	  event.preventDefault(); //prevent the default refresh
	  $(this).closest('.tech_togglewrapper').next().children('.tech_more').slideDown('fast'); //slidetoggle the target
	  $(this).hide(); //remove the more link
	  $(this).next('.tech_less-link').show();
	});
	
	//close
	$('.tech_less-link').click(function(event){
	  event.preventDefault(); //prevent the default refresh
	  $(this).closest('.tech_togglewrapper').next().children('.tech_more').slideUp('fast'); //slidetoggle the target
	  $(this).hide(); //remove the more link
	  $(this).prev('.tech_more-link').show();
	});
	
	
	// ====================
	// = Breadcumbs click =
	// ====================
	$('ul.breadcrumbs li').click(function(){
	  $(this).find('.breadcrumbs-sub').slideToggle('fast');
	});

	$(document).click(function(event) {

      if ( $(event.target).closest('.breadcrumbs').get(0) == null ) {
    	  $('.breadcrumbs-sub').slideUp('fast');
      } 
  });
  
	
	
	//ul.breadcrumbs li:hover ul.breadcrumbs-sub{display:block;}
	


	
  

	// ============================
	// = -MAIN- NAVIGATION =
	// ============================
	
	//default values
	ps_id = undefined;
	ul_id_1 = 0;
	ul_id_2 = 0;   
	ansichtid = 5;
	ul_pos_1 = undefined;
	ul_pos_2 = undefined;	
	ul_pos_3 = 0;
	
	
	function getParameterByName(name) {
		name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
		var regexS = "[\\?&]" + name + "=([^&#]*)";
		var regex = new RegExp(regexS);
		var results = regex.exec(window.location.href);
		if(results == null)
		  return "";
		else
		return decodeURIComponent(results[1].replace(/\+/g, " "));
	}

	function shownav(target){
		//ps_id = $(this).attr('id');
		$('#navigation_blacklayer').stop(true,true).show();
		target.stop(true,true).fadeIn('fast');
	}

	function deactivatenav(){
		//reset active elements
		$('.subnav-wrapper').find('.active').removeClass('active');
		$('.subnav-wrapper').find('.inactive').removeClass('inactive');
		$('.info_content').find('span').fadeOut('fast');
		$('.mainnav_info_line, .info-line').fadeOut('fast');
		$('.mainnav_line_1').css({'height' : '0px', 'margin-top' : '0'}).fadeTo(0,0);
		$('.materialien_pdf').fadeOut('fast');

	//	ps_id = 'eingabe';
		ul_id_1 = 0;
		ul_id_2 = 0;   
		ul_id_3 = 5;
		ul_pos_1 = undefined;
		ul_pos_2 = undefined;	
		ul_pos_3 = 0;
	}
	
	function hidenav(target){
		deactivatenav();
		$('.logo_wrapper').css({'margin-left' : -18});
	}
	
	
	$('.mainnav_title').hover(function(){
		$('#navigation_blacklayer').stop(true,true).show();
		
	},function(){
		$('#navigation_blacklayer').stop(true,true).hide();
	});




	//if mouse enters menu item => show nav
	$('.mainnav_title a').mouseenter(function() {
		//get ps_id
		var ps_remove = $(this).attr('id').split('___');
	 	var ps_array = ps_remove[1].split('###');
		ps_id = ps_array[1];
		
		if (ps_id == 'eingabe') {
			$('.logo_wrapper').css({'margin-left' : 531});
		} else {
			$('.logo_wrapper').css({'margin-left' : 692});	
		}
		
		
	});


	//if user clicks outside close the nav (failsave)
	$(document).click(function(event) {

      if ( $(event.target).closest('.subnav2').get(0) == null ) {
			target = $('.subnav-wrapper');
    		hidenav(target);
      } 
  });

	//if mouse leaves area -> hide nav & deactivate nav
	$('.subnav-wrapper').mouseleave(function()	 {
		target = $(this).parent();

		   hidenav();

	});




	//if mouse enters navlink -> get ps_id
	 $('.mainnav_ps').mouseenter(function(){

	   // id="ansicht___3###5"
	   var ps_remove = $(this).attr('id').split('___');
	   var ps_array = ps_remove[1].split('###');
	   ps_id = ps_array[1];

	  });
	   
	//if mouse enters title 2 -> reset inactives
	$('.ul_2').find('.title').hover(function(){
		//hide horizontal lines
		$('.line-r').hide();
		$('.line-l').hide();
	
		//hide vertical lines
		$('.mainnav_line_1').css({'height' : '0px', 'margin-top' : '0'}).hide();
		
		$('.ul_1').find('li').removeClass("inactive");
		$('.ul_2').find('li').removeClass("active");
	  	$(this).addClass("active");
		var ul_pos_2 = 0;
		var ul_id_2 = 0;		
		

	});
  
	//if mouse enters Materialien
	$('.ul_2').find('li').mouseenter(function(){
		
		//hide all infos
			$('.nav_techdetails').hide();
			$('.nav_anforderung').hide();
		
	  if ($(this).hasClass('inactive')) { } else {
  
  
	  //show info lines
	  $('.mainnav_info_line, .info-line, .line_r').fadeIn('fast').fadeTo(0,1);;

	  //get ids and position from its id=""
	  var ul_2_remove = $(this).attr('id').split('___');
	  var ul_2_array = ul_2_remove[1].split('###');
	  ul_id_2 = ul_2_array[1];
	  ul_pos_2	= ul_2_array[0];
	  ansichtid = 7;
  
	  // set ul_1 to inactive if it would not have a result		   
	  var branchen = $(this).find('.has_branchen').html();
	  var branchenarray = branchen.split(', ');
	
		//deacitvate
		$('.ul_1').find('li').not('.title').each(function(){
		  var ul_1_remove = $(this).attr('id').split('___');  
		  var ul_1_array = ul_1_remove[1].split('###');
		  ul_id_1_x = ul_1_array[1];
		  ul_pos_1_x  = ul_1_array[0];
		  inarray = $.inArray(ul_id_1_x, branchenarray);
  
		  if (inarray != -1) {
			$(this).removeClass("inactive");
		  } else {
			$(this).addClass("inactive");
		  };
  
		});
  
		//change Anforderungen info
		$('#'+ps_id+'info_anforderungen___'+ul_id_2).show();
  
		//change techdetails info
	
		$('#'+ps_id+'info_techdetails___'+ul_id_2).show();

	  }// if
  

		//set right line
		if ( (ul_pos_2 > 4)) {
			liheight = $('.ul_2 li:last').height(); 
			lineheight = liheight*ul_pos_2-liheight+4;
		  $('.mainnav_info_line').css({'height' : lineheight});
		} else { 
		  $('.mainnav_info_line').css({'height' : 151});
		};

	});


	//if mouse enters title 1 -> reset inactives
	$('.ul_1').find('.title').mouseenter(function(){
		$('.mainnav_line_1, .line-r, .line-l').hide();
		
		
	  $('.ul_2').find('li').removeClass("inactive");
	  $('.ul_1').find('li').removeClass("active");
	  $(this).addClass("active");
	
		var ul_pos_1  = 0;
		var ul_id_1 = 0; 
	
	});
		  

	// if mouse enters Branchen
	$('.ul_1').find('li').mouseenter(function(){
	  if ($(this).hasClass('inactive')) { } else {
  
	  var ul_1_remove = $(this).attr('id').split('___');
	  var ul_1_array = ul_1_remove[1].split('###');
	  ul_id_1 = ul_1_array[1];
	  ul_pos_1	= ul_1_array[0];
	  ansichtid = 6;
  
	  // set ul_2 to inactive if it would not have a result		   
	  var materialien = $(this).find('.has_materialien').html();
	  var materialienarray = materialien.split(', ');
	
		//deactivate
		$('.ul_2').find('li').not('.title').each(function(){
		  var ul_2_remove = $(this).attr('id').split('___');  
		  var ul_2_array = ul_2_remove[1].split('###');
		  ul_id_2_x = ul_2_array[1];
		  ul_pos_2_x  = ul_2_array[0];
		  inarray = $.inArray(ul_id_2_x, materialienarray);
  
		  if (inarray != -1) {
			$(this).removeClass("inactive");
	  
		  } else {
			$(this).addClass("inactive");
		  };
  
		});
  
		//change Anwendungen info
		$('.nav_anwendung').hide();
		$('#'+ps_id+'info_anwendungen___'+ul_id_1).show();
 
	  } //endif
  
	});


	//prevent clicks on inactive elements
	$('.inactive').find('a').click(function(e) {
		e.preventDefault();
		return false;
	});



	//if mouse enters Materialien or Branchen ->
	$('.ul_1, .ul_2').find('li').mouseenter(function(){
	  if ($(this).hasClass('inactive')) {} else {

  
		//set active and links
		$(this).siblings('.active').removeClass('active');
		$(this).not('.inactive').addClass('active');		 
		$(this).children('a').attr("href", "kategorie.php?kid="+ps_id+"&pid="+ul_id_1+"&matid="+ul_id_2+"&aid="+ansichtid);
	
	  }
	  //set vline
	  v_line();
	}); 





	function v_line(){
	  //only do this function if this is not inactieve
	  if ($(this).hasClass('inactive,title') || $('.title').hasClass('active')) { } else {
  
		//only do this function if both positions are given 
		if ( !(ul_pos_1 == undefined) && !(ul_pos_2 == undefined)) {
	  
		  //show horizontal lines
		  $('.line-r, .line-l').fadeTo('fast',1);  
	
			liheight = $('.ul_1 li:last').height(); //normal
  
		  //Get both full buffers from top
		  ul_pos_2_real = ( ul_pos_2*2)-1;
			ul1_heightline = liheight*ul_pos_1 - liheight;
			ul2_heightline = liheight*ul_pos_2_real-(liheight*2);
	
			//get the difference of buffers for bufferline
			if (ul_pos_1 > ul_pos_2_real) {
			  lineheight = ul1_heightline - ul2_heightline	- liheight;
			  linemargin = ul2_heightline;
	  
			  //componsate center of big button 
			  lineheight = lineheight - 8;
			  linemargin = linemargin + 8;
	  
			} else if (ul_pos_1 < ul_pos_2_real){
			  lineheight = ul2_heightline - ul1_heightline + liheight;
			  linemargin = ul1_heightline  - liheight;			
	  
			  //componsate center of big button 
			  lineheight = lineheight + 8;
	  
			} else if (ul_pos_1 = ul_pos_2_real){ 
			  lineheight = 8;
			  linemargin = ul1_heightline - liheight;	 
			  //console.log('ul_pos_1 == ul_pos_2_real');
			}
  
		  //shift bufferline  a little (title)
		  linemargin = linemargin + 82;
  
		  //bufferline is 1px taller
		  lineheight = lineheight +1;

			$('.mainnav_line_1').css({'height' : lineheight, 'margin-top' : linemargin}).fadeTo('fast',1);

		} else if (ul_pos_1 == undefined || ul_pos_2 == undefined) { 
	
		  //hide horizontal lines
		  $('.line-r').fadeTo(0,0);
		  $('.line-l').fadeTo(0,0);
	
		  //hide vertical lines
		  $('.mainnav_line_1').css({'height' : '0px', 'margin-top' : '0'}).fadeTo(0,0);
	
		}; 
	  } //end if not inactive
	}









	
	
	
	
// ==================
// = TOP NAVIGATION =
// ==================
function hidesubnav () {
	$('#mainnav').mouseleave(function() {
		$('.subnav').hide();
		$(this).find('a').css({color: ''});
		});
	}
	
function showsubnav (subid) {
	var subnavname = '#subnav'+subid;
	var mainnavname = '#nav'+subid;
	
	$('.nav').css({color: ''});
	$('.subnav').hide();
	$(mainnavname).css({color: '#0099b5'});
	$(subnavname).css({display: 'inline', backgroundColor: '#fff', marginRight: '-7px'});
	}

});









// ==============
// = livesearch =
// ==============
	function changeValue() {
		var inputValue = $('#livesearchUebersicht').delay(100).val();
		var linkResult = "livesearch.php?art=uebersicht&q=" + encodeURIComponent(inputValue);

		if((inputValue != '')&&(inputValue != ' ')){
			$('#LSShadowUebersicht').load(linkResult);
			$('#LSResultUebersicht').fadeIn(100);
		} else {
			$('#LSShadowUebersicht').load(linkResult);
			$('#LSResultUebersicht').fadeOut(100);
		}
	}

	function disableEnterKey(e)
	{
	     var key;
	     if(window.event)
	          key = window.event.keyCode;     //IE
	     else
	          key = e.which;     //firefox

	     if(key == 13)
	          return false;
	     else
	          return true;
	}

//close when clicked
jQuery(document).ready(function(){
	$(document).click(function(event) {
	   if ( $(event.target).closest('#LSResultUebersicht').get(0) == null ) {
			target = $('#LSResultUebersicht');
			$('#LSResultUebersicht').fadeOut(100);
	   }	
	});
});


// =========
// = Video =
// =========
jQuery(document).ready(function(){
  $(".videolink").colorbox({title: function(){
      var url = $(this).attr('href');
      return '<a href="'+url+'" target="_blank"></a>';
  }});
});



// ==============
// = Newsletter =
// ==============
jQuery(document).ready(function(){
	$('.newsletter_link').colorbox({width: "550px", top:"100px", href:"inc/newsletter.php"});	
});






// ==========================
// = PDF beforeanfter fixes =
// ==========================
jQuery(document).ready(function(){
	$('.xperto_fliesstext a[href$=".pdf"]').append('<div class="beforepdf">&nbsp;</div>');
	$('.mehr, .more, .download').append('<span class="aftermore">»</span>');
	
});


// ============
// = fancybox =
// ============
$(document).ready(function() {
	$(".fancybox-thumb").fancybox({
		prevEffect	: 'none',
		nextEffect	: 'none',
		helpers	: {
			title	: {
				type: 'outside'
			},
			overlay	: {
				opacity : 0.8,
				css : {
					'background-color' : '#000'
				}
			},
			thumbs	: {
				width	: 50,
				height	: 50
			}
		}
	});
});




// Email von cms
function email(address) {
	var addressDecoded = Base64.decode(address);
	location.href = "mailto:"+addressDecoded;
}

var Base64 = {

	// private property
	_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",

	// public method for encoding
	encode : function (input) {
		var output = "";
		var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
		var i = 0;

		input = Base64._utf8_encode(input);

		while (i < input.length) {

			chr1 = input.charCodeAt(i++);
			chr2 = input.charCodeAt(i++);
			chr3 = input.charCodeAt(i++);

			enc1 = chr1 >> 2;
			enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
			enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
			enc4 = chr3 & 63;

			if (isNaN(chr2)) {
				enc3 = enc4 = 64;
			} else if (isNaN(chr3)) {
				enc4 = 64;
			}

			output = output +
			this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
			this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);

		}

		return output;
	},

	// public method for decoding
	decode : function (input) {
		var output = "";
		var chr1, chr2, chr3;
		var enc1, enc2, enc3, enc4;
		var i = 0;

		input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

		while (i < input.length) {

			enc1 = this._keyStr.indexOf(input.charAt(i++));
			enc2 = this._keyStr.indexOf(input.charAt(i++));
			enc3 = this._keyStr.indexOf(input.charAt(i++));
			enc4 = this._keyStr.indexOf(input.charAt(i++));

			chr1 = (enc1 << 2) | (enc2 >> 4);
			chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
			chr3 = ((enc3 & 3) << 6) | enc4;

			output = output + String.fromCharCode(chr1);

			if (enc3 != 64) {
				output = output + String.fromCharCode(chr2);
			}
			if (enc4 != 64) {
				output = output + String.fromCharCode(chr3);
			}

		}

		output = Base64._utf8_decode(output);

		return output;

	},

	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";

		for (var n = 0; n < string.length; n++) {

			var c = string.charCodeAt(n);

			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}

		}

		return utftext;
	},

	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;

		while ( i < utftext.length ) {

			c = utftext.charCodeAt(i);

			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}

		}

		return string;
	}

}



