jQuery.noConflict()(function($){
	/**
	 * Core
	 */
	var EGZ = EGZ || {
		main: function(){
			$('body').addClass('js');
			
			// Preisliste
			$('#main_sub .tariff').egzPreisliste();
			$('#preisliste1').egzPreisliste2({activeColStart: 4, selectableCols: [3,4,5]});
			
			// eFAQ
			$('#main_sub dl.faq').efaq();
			
			// Teaser Gallery
			$("#erdgasTeaserGallery").slideViewerPro({
				easeTime: 1800,		// the time it takes a slide to move to its position 
				autoslide: true,
				asTimer: 6000	 	// if autoslide is true, this is the interval between each slide 
			});
			//erst jetzt wird die gallery eingeblendet, damit das flackern beim laden nicht sichtbar ist
			$(".mainTeaser").css('visibility','visible');
			
			// Taeser
			$('#main_sub .tx-jppageteaser-pi1 .teaser').egzTeaser();
			
			// Search field behaviour
			var searchField = $('#indexedsearch');
			var fieldValue = searchField.attr('value');
			searchField.attr('defaultValue',fieldValue);
			searchField.focus(function() {
				if (this.value == this.defaultValue) {
					this.value = '';
				}
			});
			searchField.blur(function() {
				if (this.value == '') {
					this.value = this.defaultValue;
				}
				var i = 0;
			});

			// IE6 PNG FIX
			$('body').supersleight({shim: 'http://www.erdgaszuerich.ch/fileadmin/templates/main/img/transparent.gif'});
		},

		last: function(){}
	};

	/**
	 * Plugins
	 */

	$.fn.efaq = function() {
		$(this).each(function() {
			var $that = $(this);
			$that.find("dd").hide();
			$that.find("dt").toggle( function(){
					$(this).addClass('faq-question-open');
					$(this).next('dd').show();
				}, function(){
					$(this).removeClass('faq-question-open');
					$(this).next('dd').hide();
				}
			);
		});
	};

	$.fn.egzTeaser = function() {
		$(this).each(function() {
			var $teaser = $(this);
			var $link = $teaser.find(".teaser-link a");
			if ($link.length) {
				$teaser.click(function (e) {
					window.location = $('head base').attr('href') + $link.attr('href');
				});
			}
		});
	};

	$.fn.egzPreisliste = function() {
		$(this).each(function() {
			var $tblBody = $(this).children('tbody');
			var $hiRows  = $tblBody.children('.tariff-hi');
			var $hiHeads = $hiRows.children('th');
			var $navDivs = $hiHeads.children('.tariff-hi-nav');
			
			// Hide "Hi" Rows (tr.tariff-hi)
			$hiRows.addClass('hidden');
			
			// Copy HTML of nav divs
			var navHtmlArr = [];
			navHtmlArr.push('<th>');
			$navDivs.each(function(i){
				navHtmlArr.push($('<div></div>').append($(this).clone(false)).html());
			});
			navHtmlArr.push('</th>');
			
			// Replace Hi Heads
			$hiHeads.each(function(i){
				$head = $(this);
				$head.replaceWith(navHtmlArr.join(''));
			});
			// Re-reference Heads
			$hiHeads = $hiRows.children('th');
			
			// Add hovering to nav divs
			$hiHeads.each(function(rowIndex){
				var $head = $(this);
				var $headDivs = $head.children('.tariff-hi-nav');
				$headDivs.each(function(divIndex){
					var $rowDiv = $(this);
					
					// Set active element on each if matching Div index == Row index
					if (divIndex == rowIndex) {
						$rowDiv.addClass('tariffHiNavActive');
					}
					else {
						$rowDiv.hover(	// Hover Over
						function(){
							// Hide All
							$hiRows.addClass('hidden');
							$hiRows.removeClass('tariff-active');
							
							// Show identical index
							// Target Row
							$trow = $($hiRows.get(divIndex));
							$trow.addClass('tariff-active');
							$trow.removeClass('hidden');
						}, // Hover Out
						function(){
						
						});
					}
				});
			});
			
			// Show active rows
			$tblBody.children('.tariff-active').removeClass('hidden');
		});
	};

	$.fn.egzPreisliste2 = function(options) {
		options = $.extend({
			activeColStart:	4,
			selectableCols: [3,4,5],
			activeClass:	'colHover'
		}, options);
		
		return $(this).each(function() {
			// Private vars
			var	activeColIndex = options.activeColStart,
				$table = $(this),
				$pointersRow = null,
				$rows = null
				;
			
			var prepareTable = function () {
				var $currRow = null,
					$currCol = null;
				
				$rows.each(function (i) {
					$currRow = jQuery(this);
					
					if ($currRow.hasClass('.nx-ignore')) {
						return true;
					}
					
					$currRow.addClass('row-' + i);
					
					$currRow.children().each(function (j) {
						$currCol = jQuery(this);
						$currCol.addClass('td-' + i + j).addClass('col-' + j);
					});
					
				});
					
			};
			
			var getColDetails = function (classAttribute) {
				var pattern = null,
					reg = null,
					result = [];
				
				// pattern checks for "col-##". Valid values are col-1, col-01, col-100000000.	
				pattern = /(col-(\d+))/g;
				reg = new RegExp(pattern);
				result = reg.exec(classAttribute);
				
				// if result is null, return -1 for all array elements
				if (!result) {
					result = [-1, -1, -1];
				}
				
				return {
					'cssClass' : result[0],
					'colNumber' : parseInt(result[2])
				};
			};
			
			var onTableHover = function () {
				var $self = null,
					colDetails = null;
				
				// Todo: apply DRY
				jQuery('td,th').hover(function () {
					if (activeColIndex !== -1) {
						deactivateActiveCol(activeColIndex);
						activeColIndex = -1;
					}
					
					$self = jQuery(this);
					
					if ($self.hasClass(options.activeClass)) {
						return false;
					}
					
					colDetails = getColDetails($self.attr('class'));
					
					if (colDetails.colNumber === -1 || $.inArray(colDetails.colNumber, options.selectableCols) === -1) {
						return false;
					}
					
					jQuery('.' + colDetails.cssClass).addClass(options.activeClass);
					jQuery($pointersRow.get(colDetails.colNumber)).addClass('show');
					
				}, function () {
					$self = jQuery(this);
					
					if (!$self.hasClass(options.activeClass)) {
						return false;
					}
					
					colDetails = getColDetails($self.attr('class'));
					
					if (colDetails.colNumber === -1) {
						return false;
					}
					
					jQuery('.' + colDetails.cssClass).removeClass(options.activeClass);
					jQuery($pointersRow.get(colDetails.colNumber)).removeClass('show');
					
				});
			
			};
			
			var setActiveCol = function (index) {
				jQuery('.col-' + index).addClass(options.activeClass);
				jQuery($pointersRow.get(index)).addClass('show');
			};
			
			var deactivateActiveCol = function (index) {
				jQuery('.col-' + index).removeClass(options.activeClass);
				jQuery($pointersRow.get(index)).removeClass('show');
			};
			
			var init = function () {
				$pointersRow = $table.find('.pointers .pointer');
				$rows = $table.find('tr');
				
				// order is important
				prepareTable();
				onTableHover();
				setActiveCol(activeColIndex);
			}(); // run
		});
	};

	$(document).ready(function(){
		EGZ.main();
	});

});


//// OLD STUFF ////
{
	// Startzeit für den Countdown
	var start = new Date();
	start = Date.parse(start) / 1000;
	
	// CountDownFunktion
	function CountDown(args){
		var now = new Date();
		now = Date.parse(now) / 1000;
		
		delay = CountDown.arguments[0];
		var x = parseInt(delay - (now - start), 10);
		if (document.form1) {
			document.form1.clock.value = x;
		}
		if (x > 0) {
			setTimeout("CountDown(delay)", 100);
		}
	}
	
	// nur wenn CountDown alleine verwendet wird
	//var delay=0;
	//delay=3;
	//window.setTimeout('CountDown(delay)', 100);
	
	// Hauptfunktion
	
	function Start(URL, WIDTH, HEIGHT, WINNAME, BLNPOPUP, CLOSETIME){
	
		if (BLNPOPUP) {
			windowprops = '';
			windowprops += 'left=50,'; // Position - X
			windowprops += 'top=50'; // Position - Y
			if (WIDTH != 0) 
				windowprops += ', width=' + WIDTH; // Weite
			if (HEIGHT != 0) 
				windowprops += ', height=' + HEIGHT; // Höhe
			windowprops += ', location=yes';
			windowprops += ', menubar=yes';
			windowprops += ', resizable=yes';
			windowprops += ', scrollbars=yes';
			windowprops += ', status=yes';
			windowprops += ', toolbar=yes';
			
			preview = window.open(URL, WINNAME, windowprops);
			
		}
		else {
			location.href = URL;
		}
		
		if (CLOSETIME) 
			setTimeout("preview.close();", CLOSETIME * 1000);
	}
	
	function doPopup(args){
	
		// Aufruf:
		// doPopup(url, blnCountDown, blnPopup, width, height, winname, delay, closetime)
		// doPopup('http://www.erdgas.ch/601.html',1,1,0,0,'',3,0)  oder
		// doPopup('http://www.erdgas.ch/601.html')
		
		blnCountDown = 1; // Soll der CountDown ausgeführt werden?
		if (doPopup.arguments.length > 1) 
			blnCountDown = doPopup.arguments[1];
		
		blnPopup = 1; // Soll die neue Seite als Popup oder als Redirect geöffnet werden
		if (doPopup.arguments.length > 2) 
			blnPopup = doPopup.arguments[2];
		
		//url = "http://www.erdgas.ch/601.html";	// Zieladresse
		url = doPopup.arguments[0];
		
		//width = 267;  // Fensterbreite (nur Popup)
		width = 0; // 0 = keine Breitenangabe
		if (doPopup.arguments.length > 3) 
			width = doPopup.arguments[3];
		
		//height = 103; // Fensterhöhe (nur Popup)
		height = 0; // 0 = keine Höhenangabe
		if (doPopup.arguments.length > 4) 
			height = doPopup.arguments[4];
		
		//winname = 'preview';	// Fenstername wird benötigt zum Schliessen des Fensters
		winname = ''; // keine Angabe erzeugt default name
		if (doPopup.arguments.length > 5) 
			winname = doPopup.arguments[5];
		
		
		// Zeit in Sekunden bevor das Popup öffnet
		delay = 3;
		if (doPopup.arguments.length > 6) 
			delay = doPopup.arguments[6];
		
		// Zeit in Sekunden bis das Fenster wieder geschlossen wird
		// 0 = nicht schliessen
		closetime = 0;
		if (doPopup.arguments.length > 7) 
			closetime = doPopup.arguments[7];
		
		
		if (blnCountDown) 
			timerCountDown = setTimeout("CountDown(delay)", 100);
		
		timerStart = setTimeout("Start(url, width, height, winname, blnPopup, closetime)", delay * 1000);
		
	}
}