//BLUR LINKS ==============================================================

function BlurLinks(){

// Text- u. Grafik-Links
lnks=document.getElementsByTagName('a');
for(i=0;i<lnks.length;i++){
lnks[i].onfocus=new Function("this.blur()");
}

// ImageMaps
lnks=document.getElementsByTagName('area');
for(i=0;i<lnks.length;i++){
lnks[i].onfocus=new Function("this.blur()");
}
}

onload=BlurLinks;

// This function calls makeMap() if function is present.
// Function comes from rsgooglemap extension.
checkAndMakeMap = function() {
	if( typeof( window.makeMap ) == "function" ) {
		makeMap();
	}
}

//JQUERY SECTION ==============================================================

/* outer html function */
jQuery.fn.outerHTML = function(s) {
	return (s)
		? this.before(s).remove()
		: jQuery("<p>").append(this.eq(0).clone()).html();
}


$(document).ready(function(){

	// MAIN MENU HIDE

	if( $("#loggedInHeader").length > 0 ) {
		$("#hoverNav").css({
			"top": parseInt( $("#hoverNav").css( "top" ) ) + parseInt( $("#loggedInHeader").height() ) + "px"
		});
	}
	
	if( !$('.tx-sremailsubscribe-pi1 .tx-sremailsubscribe-pi1-error' ).length && !$('.tx-sremailsubscribe-pi1 .success-subject' ).length ) {
		$("#hoverNav").hide();
	}

	
	// LAYER TABS - CHANGE TAB  

	$("#pagelayerWrapper ul.tabBar").idTabs(function(id,list){
		for( i in list ) {
			$(list[i]).hide();
		}
		
		$(id).fadeIn(1500);
		
		return false;
	});  

	//Information Files
	$('#showInformationFiles').click(function(){
		$('.packagingInformationFiles').modal()
	})

	// SLIDE IN FX - MAIN MENU

	var t;
	$("#navigationFirstlevel").hover(function () {
	t = setTimeout(function () {
	if ($("#hoverNav").is(":hidden")) {
		$("#hoverNav").show('slide', { direction: "up" });
	  } else {
		$("#hoverNav").show();
	  }
	  if( $( '#tx-sremailsubscribe-pi1-email' ).is( '.hasFocus' ) ) {
		$( '#tx-sremailsubscribe-pi1-email' ).focus();
	  } else {
		$( '#tx-sremailsubscribe-pi1-email' ).blur();
	  }
	}, 300)
	}, function () {clearTimeout(t)});
		

	// SLIDE OUT FX - MAIN MENU
	$("#hoverNav").mouseleave(
		  function () {
			  $("#hoverNav").slideUp();
		  }
	); 
	
	// HORIZONTAL ACCORDION
	$('.sections').hAccordion( { selected: '.sectionRight' } );
	
	
	// FAQ simple accordion

	$('.accordion').accordion({
		active: false,
		header: 'h2',
		autoHeight: false,
		collapsible: true,
		animated: 'easeslide'
	});
	
	$('.accordion h2').focus( function() {
		this.blur();
		return false;
	});
	
	$('.accordion h2').wrapInner( '<span class="accordion-arrow"></span>' );

	
	// MODAL / LIGHTBOX
	$('.modalLightboxBox').hide();
	
	$('a.lightboxLink').click( function ( ) {
		
		if( $(this).attr('href').match('#') ) {
			
			if( $('a#'+$(this).attr('href').split('#')[1]).next('div.modalLightboxBox').length == 0 ) {
				
				// check next element because referenced inserted records in typo3 insert two anchor links
				if( $('a#'+$(this).attr('href').split('#')[1]).next().next('div.modalLightboxBox').length > 0 ) {
					var modalContentElement = $('a#'+$(this).attr('href').split('#')[1]).next().next('div.modalLightboxBox');
				}
				
			} else {
				var modalContentElement = $('a#'+$(this).attr('href').split('#')[1]).next('div.modalLightboxBox');
			}
			
			if( !modalContentElement ) {
				return false;
			}
			
			if( modalContentElement.hasClass('modalLightboxFrameless') ) {
				var modalWidth = 780;
				var baseHeight = ($(window).height()-50);
				var baseHeightInner = ($(window).height()-100);
			} else {
				var modalWidth = 870;
				var baseHeight = ($(window).height()-120);
				var baseHeightInner = ($(window).height()-250);
			}
			
			modalContentElement.find('script').remove();
			
			$.modal( modalContentElement, {
				minWidth: modalWidth,
				maxWidth: modalWidth,
				minHeight: baseHeight,
				maxHeight: baseHeight,
				position: [100,],
				overlay: 50,
				opacity: 80,
				overlayCss: { backgroundColor: "#3a2e5b" },
				overlayClose: true,
				persist: true,
				onOpen: function( dialog ) {
					dialog.overlay.slideDown( 'fast', function() {
						dialog.data.hide();
						dialog.container.fadeIn( 'fast', function() {
							dialog.data.fadeIn( 'fast' );
						});
					});
				},
				onClose: function( dialog ) {
					dialog.data.fadeOut( 'fast', function() {
						dialog.container.fadeOut( 'fast', function() {
							dialog.overlay.slideUp( 'fast', function() {
								$.modal.close();
							});
						});
					});
				}
			});
			
			$('#simplemodal-container .modalLightboxContent').css({
				"height":  baseHeightInner + "px"
			});
		  
		}
		
		return false;
	} );
	
	// Search field default text and inactive color
	if( $('#navigationMeta ul li input#searchField').val() == '' ) {
		$('#navigationMeta ul li input#searchField').val('Search');
		$('#navigationMeta ul li input#searchField').css('color', '#BBBBBB');
	}
	$('#navigationMeta ul li input#searchField').focus( function(e) {
		if( $(this).val() == 'Search' ) {
			$(this).val('').css('color', '');
		}
	} ).blur( function(e) {
		if( $(this).val() == '' ) {
			$(this).val('Search').css('color', '#BBBBBB');
		}
	} );
	
	
	searchFieldTimer = false;
	
	// Search field enlargement in meta navigation
	$('#navigationMeta ul li form#indexedsearch').hover(
		function( e ) { // over
			clearTimeout( searchFieldTimer );
			$(this).closest('li').stop(true, true).removeClass('wide').addClass('widePrepared').addClass('wide', 300);
		},
		function( e ) { // out
			searchFieldTimer = setTimeout( function() {
				$('#navigationMeta ul li form#indexedsearch').closest('li').stop(true, true).removeClass('wide', 300).removeClass('widePrepared');
			}, 500 );
		}
	);
	
	// newsletter registration field text and inactive color
	$('#tx-sremailsubscribe-pi1-email').focus( function(e) {
		$(this).addClass( 'hasFocus' );
		if( $(this).val() == 'Enter Email Address' ) {
			$(this).val('').css('color', '');
		}
	} ).blur( function(e) {
		$(this).removeClass( 'hasFocus' );
		if( $(this).val() == '' ) {
			$(this).val('Enter Email Address').css('color', '#BBBBBB');
		}
	} );
	
	$('#hoverNav .tx-sremailsubscribe-pi1-error').click( function(e) {
		$(this).hide();
	} ).css( 'cursor', 'pointer' );
	
	var slides = $('#homepageSlideshow .slideshow').children();
		if(slides.length > 0) {
		$('#homepageSlideshow').append('<div class="slideshowControl"><a class="button prev"></a><a class="button pause"></a><a class="button next"></a></div>');
		$('#homepageSlideshow .slideshow').cycle({
			timeoutFn: calculateTimeout,
			prev: '#homepageSlideshow .slideshowControl .prev',
			next: '#homepageSlideshow .slideshowControl .next'
		});
		$('#homepageSlideshow .slideshowControl .pause').click( function(e) {
			$('#homepageSlideshow .slideshow').cycle('toggle');
		});
		var navlink = 0;
		for(var slide=0; slide < slides.length; slide++) {
			if($(slides[slide]).find('.addnavlink').text() == '1') {
				$('<a class="button goto" rel="' + (slide) + '">' + (++navlink) + '</a>').appendTo($('#homepageSlideshow .slideshowControl')).click( function() {
					$('#homepageSlideshow .slideshow').cycle(parseInt($(this).attr('rel')));
				});
				if(slide == 0) {
					$('#homepageSlideshow .slideshowControl a.goto').addClass('active');
				}
			}
		}
	}
	
	//DOWNLOAD BOX
	$('.downloadBox').click(function(){
		$.modal('<iframe src="' + $(this).attr('href') + '" height="432" width="768" style="border:0">',{
			containerCss:{
				backgroundColor:"#fff",
				borderColor:"#fff",
				height:432,
				padding:0,
				width:768,
				close: false,
				escClose: false,
				overlayClose: false,
				persist: true,
				modal: false,
			}
		});
		return false;
	});
});

function calculateTimeout(currElement, nextElement, opts, isForward) { 
	var seconds = 1;
	if($(currElement).find('.duration').text()) {
		seconds = $(currElement).find('.duration').text();
	}
	$('#homepageSlideshow .slideshowControl a.goto').removeClass('active');
	if($(currElement).find('.addnavlink').text() == '1') {
		$('#homepageSlideshow .slideshowControl a.goto[rel=' + opts.currSlide + ']').addClass('active');
	}
	
	return seconds * 1000;
} 
