//Höhenberechnungen
function calculateHeight(element) {
	element.each(function(){
		$(this).show().css('height', 'auto');
		
		dataHeight= $(this).height();
		$(this).attr('data-height', dataHeight);
		
		$(this).css('height', 0).hide();
	}); 
};


$(document).ready(function() {

/*	// Startseiten Popup

	if($('body#subBody').length == '') {
		

		$.colorbox({html:'<img src="/ecomaXL/get_blob.php?name=stabile_strom_und_gaspreise.jpg" width="573" height="479" alt="Stabile Strom- und Gaspreise bis mindestens 31. März 2012" class="stromstoerer" style="cursor: pointer;";/>'});
		
		$('img.stromstoerer').click(function() {
		 $('#cboxClose').click();							  
		});
		
		
		var counter = 0;
var interval = setInterval(function() {
    counter++;
    if (counter == 8) {
        $('#cboxClose').click();
    }
}, 1000);
	}*/

	


		$(window).politeWarning(6, 'Vielen Dank f&uuml;r Ihren Besuch. Sie benutzen den Internet Explorer 6. <br />Diese Seite ist f&uuml;r den Internet Explorer ab Version 7 optimiert. <br /><a href="http://windows.microsoft.com/de-DE/internet-explorer/products/ie/home">Hier k&ouml;nnen Sie sich eine neue Version herunterladen.</a>');
		
	// traget:blank ersatz 
		$('a[href^="http://"]').attr('target','_blank');
		
		// traget:blank ersatz	 
		$('a[href^="https://"]').attr('target','_blank');
	
	// Colorbox für die Youtube-Videos

	$(".youtube").colorbox({iframe:true, innerWidth:425, innerHeight:344});
		
	//DROPDOWN
		calculateHeight($('ul#navMain > li > ul'));
		
		//Animaton des Menüs		
		if (!$.browser.msie) {
			$('ul#navMain ul').css('opacity', '0.4');
		}
		
		if (!$.browser.msie) {
			$('ul#navMain li').hover(function() {
				dataHeight = $(this).children('ul').attr('data-height');
				
				$(this).children('ul').show().stop(false, false).animate({
					height: dataHeight,
					opacity: 1
				}, 500, function(){
					$(this).css('display', 'block');
				});
			}, function(){
				$(this).children('ul').stop(false, false).animate({
					height: 0,
					opacity: 0.4
				}, 300, function(){
					$(this).hide();
				});
			});
		} else {
			$('ul#navMain li').hover(function() {
				dataHeight = $(this).children('ul').attr('data-height');
				
				$(this).children('ul').show().stop(false, false).animate({
					height: dataHeight
				}, 500, function(){
					$(this).css('display', 'block');
				});
			}, function(){
				$(this).children('ul').stop(false, false).animate({
					height: 0
				}, 300, function(){
					$(this).hide();
				});
			});
		}
		
		
	if($('div.search').length) {
	
	// replace scrollbar
		if($('div#scrollpane').length) {
		
			jspTrackHovered = false;
			searchHovered = false;
		
			$('li.searchInput div#scrollpane').show().jScrollPane({showArrows: true}).hide();
			$('li.searchInput, li.submit > a').click(function() {
				if (jspTrackHovered == false) {
					$('li.searchInput div#scrollpane').slideToggle();
				}
				if($(this).parent().hasClass('submit')) {
					return false;
				}
			});
			
			$('div.jspTrack').hover(function() {
				jspTrackHovered = true;
			},function(){
				jspTrackHovered = false;
			});
		}
	
	// replace scrollbar 2
		if($('div#sec_scrollpane').length) {
		
			jspTrackHovered = false;
			searchHovered = false;
		
			$('#sec_scrollpane').show().jScrollPane({showArrows: true}).show();
			
			
			
			$('div.jspTrack').hover(function() {
				jspTrackHovered = true;
			},function(){
				jspTrackHovered = false;
			});
		}
	
	// schlagwortsuche li.rename 
		$('div#scrollpane ul li').click(function() {
			if(!$(this).hasClass('first')){
				$('div#scrollpane').prev('span').html($(this).text());
			}		
		});
		
	// schlagwortsuche schließen
		$('div.search').hover(function() {
			searchHovered = true;
		},function(){
			searchHovered = false;
		});


		$('*').bind('click', function() {
			if (searchHovered == false ) {
				$('li.searchInput div#scrollpane').slideUp();
			}
		});
	}
		
	// cycle für die header-pics 
		if($('div.teaserPic').length) {
			// Navigation Erstellen
			$('div.teaserPic').append('<ul id="teaserPicsNav"></ul>');
			$('ul#teaserPicsSlide li').each(function() {
				$('ul#teaserPicsNav').append('<li><a href="#">Teaser-Bild '+($(this).index()+1)+'</a></li>');
			});
	
			$('ul#teaserPicsSlide').cycle({
				timeout: 5000,
				pager: 'ul#teaserPicsNav',
				activePagerClass: 'active',
				pagerAnchorBuilder: function(id) {
						return 'ul#teaserPicsNav li:eq(' + (id) + ') a';
					}
			 });
		 }
	
	// slidereffekt für die reiter
		$('#tabs li:first-child').addClass('active');
							
		$('#tabs li a').click(function(){				
			if (!$(this).parent('li').hasClass('active')){
				$(this).parent('li').addClass('active').siblings('li').removeClass('active');
				
				if ( $(this).attr('href') == '#privat' ) {
					var slidePos = '0px';
				} else if ( $(this).attr('href') == '#geschaefts' ) {
					var slidePos = '-800px';
				} else if ( $(this).attr('href') == '#swdTV' ){
					var slidePos = '-1600px';
				}
				
				$('#products .slide').animate({
					left: slidePos
				},600, 'swing' );
			}
			return false;
		});
		
	// slidereffekt für die veranstaltungen & neuigkeiten
	// Voreinstellungen
		$('div.veranstaltungen div.aktuell > div').css('width', ($('div.veranstaltungen div.aktuell > div').children('div').length * 321)+'px');
		$('div.neuigkeiten div.aktuell > div').css('width', ($('div.neuigkeiten div.aktuell > div').children('div').length * 321)+'px');
		$('div#contentMain div div.switch').show();
		$('div.veranstaltungen div.switch a.zurueck').addClass('disabled');
		$('div.neuigkeiten div.switch a.zurueck').addClass('disabled');
	
		$('div#contentMain div div.switch a.switch').click(function() {
			if(!$(this).hasClass('disabled')) {
				var type = $(this).parent().parent().attr('class');
				if($(this).hasClass('zurueck')) {
					var value = 321;
				} else if($(this).hasClass('vor')) {
					var value = -321;
				}
				moveTopical(type, value);
			}
			return false;
		});
		
		function moveTopical(type, value) {
			var moveCont = $('div.'+type+' div.aktuell > div');
			
			if(!moveCont.is(':animated')) {
				var oldPos = parseInt(moveCont.css('left'));
				var newPos = oldPos+value+'px';
				var maxLength = (($('div.'+type+' div.aktuell > div').children('div').length-1) * -321)+'px';
				
				moveCont.stop().animate({
					left: newPos
				}, 450, 'swing');
				
				if(newPos == '0px') {
					$('div.'+type+' div.switch a.zurueck').addClass('disabled').siblings('a.vor').removeClass('disabled');
				} else if(newPos == maxLength) {
					$('div.'+type+' div.switch a.vor').addClass('disabled').siblings('a.zurueck').removeClass('disabled');
				} else {
					$('div.'+type+' div.switch a').removeClass('disabled');
				}
			}
		}		
		
	//slidereffekt für die entstoerer-dienst box startseite 
		calculateHeight($('p.beschreibung'));
		$('p.beschreibung').hide();
		
		$('h4.entstoerdienst').click(function() {	
			
			var element = $(this).next('div').children('p.beschreibung');				
				
			if(element.is(':hidden')) {
			
				calculateHeight(element);			
				dataHeight = element.attr('data-height');
				
				$(this).addClass('active');
				
				element.show().stop(false, false).animate({
					height: dataHeight
				}, 500);
				
			} else {	

				$(this).removeClass('active');
				
				element.stop(false, false).animate({
					height: 0
				}, 300, function(){
					element.hide();
				});
				
			}			
			
		});
	
	//tarifrechner Hover/Aktiv-Effekt STROM
		$('div.stromtarifRechner ul.nutzer li').click(function() {
			if(!$(this).hasClass('first')) {
				$(this).addClass('active').prevAll().addClass('active');
				$(this).nextAll().removeClass('active');
			} else {
				$(this).nextAll().removeClass('active');
			}
			$('input.kwh').val('');
			$('input[name="personen"]').val($('ul.nutzer li.active').length);
			return false;
		}).hover(function() {
			$(this).css('background-position','left -41px').prevAll().css('background-position','left -41px');
			$(this).nextAll().css('background-position','left top');
		},function() {
			$(this).removeAttr('style').siblings().removeAttr('style');
		});
		$('input.kwh').change(function() {
			$('ul.nutzer li').removeClass('active');
			$('input[name="personen"]').val('');
		});
		
		
		$('div.stromtarifRechner ul.nutzer_natur li').click(function() {
			if(!$(this).hasClass('first')) {
				$(this).addClass('active').prevAll().addClass('active');
				$(this).nextAll().removeClass('active');
			} else {
				$(this).nextAll().removeClass('active');
			}
			$('input.kwh').val('');
			$('input[name="personen_natur"]').val($('ul.nutzer_natur li.active').length);
			return false;
		}).hover(function() {
			$(this).css('background-position','left -41px').prevAll().css('background-position','left -41px');
			$(this).nextAll().css('background-position','left top');
		},function() {
			$(this).removeAttr('style').siblings().removeAttr('style');
		});
		$('input.kwh').change(function() {
			$('ul.nutzer_natur li').removeClass('active');
			$('input[name="personen_natur"]').val('');
		});		
		
		
		//tarifrechner Hover/Aktiv-Effekt ERDGAS
		$('div.erdgastarifRechner ul.nutzer li').click(function() {
			if(!$(this).hasClass('first')) {
				$(this).addClass('active').prevAll().addClass('active');
				$(this).nextAll().removeClass('active');
			} else {
				$(this).nextAll().removeClass('active');
			}
			$('input.kwh').val('');
			$('input[name="personen"]').val($('ul.nutzer li.active').length);
			return false;
		}).hover(function() {
			$(this).css('background-position','left -52px').prevAll().css('background-position','left -52px');
			$(this).nextAll().css('background-position','left top');
		},function() {
			$(this).removeAttr('style').siblings().removeAttr('style');
		});
		$('input.kwh').change(function() {
			$('ul.nutzer li').removeClass('active');
			$('input[name="personen"]').val('');
		});
	
		
		/* // FORM validation
		$('#kontaktformular form').submit(function() {
			var el = $(this);
			$(this).find('label.error').removeClass('error');
			$(this).find('p.error').hide();
			var error = 0;
			var post = '';
			var radio = 0;
			el.find('label').each(function() {
				if(el.find('input[id="'+$(this).attr('for')+'"], textarea[id="'+$(this).attr('for')+'"]').val()  == '' && $(this).hasClass('duty')) {
					error++;
					$(this).addClass('error');
				}
			});
			if(error == 0) {
				$.post('php/send.php', {
					contractReference: 	$(this).find('input[name="vertragsnummer"]').val(),
					firstName: 			$(this).find('input[name="vorname"]').val(),
					name: 				$(this).find('input[name="nachname"]').val(),
					company: 			$(this).find('input[name="firma"]').val(),
					street: 			$(this).find('input[name="straße"]').val(),
					number: 			$(this).find('input[name="nr"]').val(),
					plz: 				$(this).find('input[name="plz"]').val(),
					city: 				$(this).find('input[name="ort"]').val(),
					phone: 				$(this).find('input[name="telefon"]').val(),
					email: 				$(this).find('input[name="eMail"]').val(),
					issue: 				$(this).find('textarea[name="comment"]').val(),
					js:			'1'
					
				}, function(data) {
					el.hide();
					el.after('<p class="affirmed"><strong>Thank you for contacting Fona!</strong><br />Your inquiry has been sent and we should be in touch with you soon.</p>');
				});
			} else {
				$(this).find('p.duty').hide();
				$(this).find('p.error').show();
			}
			return false;
		}); */
			
	
		
	// slidereffekt für die lexikon boxen
		$('.content_lexikon h5').css('background','url(/ecomaXL/pics/bg_top_container_energielexikon.png)').css('height','31');
		calculateHeight($('.container_lexikon_content')) 		
		
		$('.content_lexikon h5').click(function() {
			
			var element = $(this).next('div');			
			
			if(!element.is(':animated')) {				
				
				if(element.is(':hidden')) {
			
					calculateHeight(element);			
					dataHeight = element.attr('data-height');
					
					$(this).css('background','url(/ecomaXL/pics/bg_top_container_energielexikon_offen.png)').css('height','27');
					
					element.children('div').css('height', dataHeight);
					
					element.show().stop(false, false).animate({
						height: dataHeight
					}, 500);
										
					//schließt alle anderen offenen Container
					$(this).parent('div').siblings().each(function() {
						if($(this).children('div').is(':visible')) {
							$(this).children('div').stop(false, false).animate({
								height: 0
							}, 300, function(){
								$(this).hide().prev('h5').css('background','url(/ecomaXL/pics/bg_top_container_energielexikon.png)').css('height','31');
							});
						}
					});						
					
				} else {	
					
					element.stop(false, false).animate({
						height: 0
					}, 300, function(){
						element.hide().children('div').removeAttr('style');
						$(this).siblings('h5').css('background','url(/ecomaXL/pics/bg_top_container_energielexikon.png)').css('height','31');
					});
				
				}		
				
			}
			
			return false;
		});
		
			$('.content_lexikon h5').hover(function() {
				$(this).css({
					'cursor':'pointer', 
					'color':'#00a2b2'
				});
			}, function() {
				$(this).css({
					'cursor':'pointer', 
					'color':'#666'
			});
		});
	// Font-Resize
		$('ul.fontResize li a').click(function(){
			if($(this).parent('li').hasClass('copyNormal')) {			
				$('body').removeClass('copyMedium').removeClass('copyLarge');		
				var the_cookie = '';
			} else if($(this).parent('li').hasClass('copyMedium')) {				
				$('body').removeClass('copyLarge').addClass($(this).parent('li').attr('class'));	
				var the_cookie = 'copyMedium';
			} else if($(this).parent('li').hasClass('copyLarge')) {				
				$('body').removeClass('copyMedium').addClass($(this).parent('li').attr('class'));		
				var the_cookie = 'copyLarge';
			}
			
			$.cookie('fontSize',  the_cookie, { expires: 7, path: '/', domain: 'mag.em.lan' });
			
			
			calculateHeight($('ul#navMain > li > ul'));			
			return false;
		});
		

	
	// Galerien
	if($('div.gallery').length) {
		// Presets
		$('div.gallery ul.galleryImages li').css({
			'position': 'absolute',
			'left': '0px',
			'top': '0px',
			'display': 'none',
			'z-index': '1'
		});
		$('div.gallery ul.galleryImages li:first-child').show();
		
		$('div.gallery').prepend('<ul class="galleryNav"><li class="galleryPrevImages"><a href="#">Vorherige Bilder</a></li><li class="galleryImagePreview"><ul></ul></li><li class="galleryNextImages"><a href="#">N&auml;chste Bilder</a></li></ul>');
		$('div.gallery ul.galleryNav li.galleryImagePreview ul').attr('pos', '0');
		
		$('div.gallery').each(function() {
			var countPics = $(this).find('ul.galleryImages li').length;
			$(this).find('li.galleryImagePreview ul').css('width', countPics*143+'px');
			
			$(this).find('ul.galleryNav li.galleryPrevImages a').addClass('disabled');
			if(countPics <= 4) {
				$(this).find('ul.galleryNav li.galleryNextImages a').addClass('disabled');
			}
			
			$(this).find('ul.galleryImages li').each(function() {
				var arr = $(this).children('img').attr('src').split('.')
				var pic = arr[arr.length-2];
				pic = pic + '_th';
				arr[arr.length-2] = pic;
				var picThumb = arr.join('.')
				$(this).parents('div.gallery').find('li.galleryImagePreview ul').append('<li><img src="' + picThumb + '" alt=""></img></li>');
				$(this).parents('div.gallery').find('li.galleryImagePreview ul > li').fadeTo(0, .5);
				$(this).parents('div.gallery').find('li.galleryImagePreview ul > li:first-child').addClass('active').fadeTo(0, 1);
			});
		});
		
		// Change Images
		$('div.gallery ul.galleryNav li.galleryImagePreview ul > li').live('mouseenter', function() {
			if(!$(this).hasClass('active')) {
				$(this).fadeTo(200, 1);
			}
		}).live('mouseleave', function() {
			if(!$(this).hasClass('active')) {
				$(this).fadeTo(200, .5);
			}
		}).live('click', function() {
			if(!$(this).hasClass('active')) {
				var picIndex = $(this).index();
				var newPic = $(this).parents('div.gallery').find('ul.galleryImages li').eq(picIndex);
				newPic.css('z-index', '2').fadeIn(600).siblings('li').css('z-index', '1').fadeOut(600);
				$(this).addClass('active').fadeTo(200, 1).siblings('li').removeClass('active').fadeTo(200, .5);
			}
		});
		
		// Switch Thumbnails
		// Prev
		$('div.gallery ul.galleryNav li.galleryPrevImages a').live('click', function() {
			if(!$(this).hasClass('disabled')) {
				var newPos = parseInt($(this).parents('ul.galleryNav').find('li.galleryImagePreview > ul').attr('pos'))-1;
				$(this).parents('ul.galleryNav').find('li.galleryImagePreview > ul').attr('pos', newPos);
					
				$('div.gallery ul.galleryNav li.galleryNextImages a').removeClass('disabled');
				if(newPos == 0) {
					$(this).addClass('disabled');
				}
					
				$(this).parent().siblings('li.galleryImagePreview').children('ul').animate({
					left: -1*newPos*(parseInt($(this).parent().siblings('li.galleryImagePreview').width())+15)
				}, 600, 'swing', function() {
				});
			}
			return false;
		});
		
		// Next
		$('div.gallery ul.galleryNav li.galleryNextImages a').live('click', function() {
			if(!$(this).hasClass('disabled')) {
				var newPos = parseInt($(this).parents('ul.galleryNav').find('li.galleryImagePreview > ul').attr('pos'))+1;
				var maxPos = Math.ceil($(this).parents('ul.galleryNav').find('li.galleryImagePreview > ul li').length/4)-1;
				$(this).parents('ul.galleryNav').find('li.galleryImagePreview > ul').attr('pos', newPos);
				
				$('div.gallery ul.galleryNav li.galleryPrevImages a').removeClass('disabled');
				if(newPos == maxPos) {
					$(this).addClass('disabled');
				}
				
				$(this).parent().siblings('li.galleryImagePreview').children('ul').animate({
					left: -1*newPos*(parseInt($(this).parent().siblings('li.galleryImagePreview').width())+15)
				}, 600, 'swing', function() {
				});
			}
			return false;
		});
	}
		
	// googlemaps
	if($('div#gMap').length){
	  function initializeMap() {
		var myLatlng = new google.maps.LatLng(50.793222, 6.489632);
		var myOptions = {
			zoom: 15,
			center: myLatlng,
			mapTypeId: google.maps.MapTypeId.ROADMAP
			};
		var map = new google.maps.Map(document.getElementById("gMap"), myOptions);
	
		var marker = new google.maps.Marker({
				position: myLatlng, 
				map: map, 
				title:"Hallenbad Jesuitenhof"
		 });
		 }
	
		if('#map_canvas') {
			initializeMap();
		}
	}
	
	
	// Energiewelt Flash
	if($('div#colLeft div#energiewelt').length) {
		if(swfobject.hasFlashPlayerVersion("9.0.0")) {
			var flashvars = {};
			var params = { wmode:'transparent' };
			var attributes = {};
			swfobject.embedSWF('/ecomaXL/swf/navi_welt_small.swf', 'energiewelt', '194', '117', '9.0.0', 'expressInstall.swf', flashvars, params, attributes);
		} else {
			$('div#energiewelt').parent().parent().hide();
		}
	}
	
	// Webcam Auto-Refresh
	
	if($('img.webcam').length){
		webcamImages = new Array();
		countWebcamImages = 0;
		
		$('img.webcam').each(function(){
			
			webcamImages[countWebcamImages] = $(this);
			refreshWebcam(webcamImages[countWebcamImages]);
			countWebcamImages++;
		});
	};

	// Add Tracking function to external Link 'Energiewelt'
	
	$('#navMain li ul li a').each(function() {
		if($(this).attr('href') == 'http://energiewelt.stadtwerke-dueren.de') {
				$(this).attr('onclick', "clickTracker('Energiewelt')");
		}
	});
	
});

// Tracking function

var toSleep = true; function stopTimer(){toSleep=false;}
function clickTracker(LinkName){var secureID="u592SV";
var redirectUrl="http://www.etracker.de/skin/de.def/img/1px.gif";
etrackerSink=new Image(1,1);toSleep=true;
etrackerSink.src="http://www.etracker.de/lnkcnt.php?et="+secureID+"&url="+
redirectUrl+"&lnkname="+LinkName;etrackerSink.onload=stopTimer;if(toSleep==true)
{maxDelay(500);}return true;}function maxDelay(gap){var then,now;then=new
Date().getTime();now=then; while( toSleep==true&&(now-then)<gap){now=new Date().
getTime();}}

// Funktion zur Aktualisierung der Webcam-Bilder
function refreshWebcam(element) {
	setInterval(function() {
		imageSrc = element.attr('src') + '?' + (new Date()).getTime();
		element.attr('src', imageSrc);
	}, 10000);
}


// Abfangen des Energiewelt Links
function mininavifunction() {
	window.location = 'http://energiewelt.stadtwerke-dueren.de/';
}


/* if($(this).siblings('.container_lexikon_content').is(':hidden')) {
					$(this).css('background','url(/ecomaXL/pics/bg_top_container_energielexikon_offen.png)').css('height','27');
				} */		



/* $(this).next('div').slideToggle('slow', function() {
					if($(this).is(':hidden')) {
						$(this).siblings('h5').css('background','url(/ecomaXL/pics/bg_top_container_energielexikon.png)').css('height','31');
					}	
				}); */



