jQuery(document).ready(function () {
	
	// backgroundStyle
	eos.initPage();
	
	// make cols equal
	// eos.equalize('#content .content .contentBox, #content .content .contentSidebar');
	eos.equalize('div.container2cols > div.indexBox > div.inner');
	eos.equalize('div.container3cols > div.teaser');
	eos.equalize('#footer ul ul');
	
	// just for footer
	eos.equalizeFooterHorizontal();
	
	// remove last border
	jQuery('#footer ul ul').last().css('border-right', 0);
	
	// init image replacement
	eos.initFlir();
	
	// init faq
	eos.initFaq('div.tx-irfaq-pi1');
	
	// init auto empty
	eos.initForm();
	
	// init event tracking
	eosEventTracker.trackDownloads();
	
});

var eosEventTracker = {
	
	// Tracks clicks on downloadlinks with default class "download"
	trackDownloads : function () {
		
		if(typeof ET_Event.download != 'function')
			return;
		
		jQuery('a.download').bind('mousedown', function(e) {
			// var trackString = encodeURI(jQuery(this).text() + ' (' + this.href + ')');
			var trackString = encodeURI(this.href);
			ET_Event.download(trackString, '');	
		});
	} 
	
}

var eos = {
	
	// set bg style by content elements
	initPage : function () {
		
		// body class
		var bodyClass = (jQuery('#content .contentSidebar').length > 0) ? 'contentBgExtra' : 'contentBgDefault';
		jQuery('#content .content').addClass(bodyClass);
		
		// menu width
		jQuery('#topNav > li').each(function () {
			var w		= jQuery(this).width();
			var childW	= jQuery(this).children('div').width();
			jQuery(this).width(w);
				
			// to prevent inheritance of width
			if (childW > w) {
				jQuery(this).children('div').width(childW);
			} else {
				jQuery(this).children('div').width(w -2);
			}
		});
		
		// navigation visual aid
		jQuery('#navigation a.lvl1.current').parent().each(function() {
			if (jQuery(this).children('ul').length < 1) jQuery(this).addClass('borderBottom');
		});
		
		// parameter settings for newsletter
		var emailAddress = jQuery.getUrlVar('emailAddress');
		if (typeof(emailAddress) != 'undefined') {
			emailAddress = decodeURIComponent(emailAddress);
			jQuery('#tx-sremailsubscribe-pi1-email').val(emailAddress);
		}
		
	},
	
	// rebuilds faq extension output
	initFaq : function (selector) {
		
		// build new
		var elements = '';
		
		jQuery(selector).find('.tx-irfaq-answer').each(function() {
			
			var headline	= jQuery(this).children('h4').text();
			var content		= jQuery(this).children('p').html();
			var additional	= jQuery(this).children('div.additional-info').html();
			
			elements		+= '<li><h4>' + headline + '</h4><div class="slide"><p>' + content + '</p>' + additional + '</div>';
		});
		
		elements = '<ul class="eosfaq">' + elements + '</ul>';
		
		// replace static content
		jQuery(selector).html(elements);
		
		jQuery(selector).find('li').each(function () {
			var li	= jQuery(this);
			var div = jQuery(this).find('div.slide');
			
			div.hide();
			
			jQuery(this).click(function (e) {
				div.slideToggle('fast');
				li.toggleClass('open');
			})
		});

	},

	initForm : function () {
		

		var autoEmpty = jQuery('input.autoEmpty');

		autoEmpty.focus(function () {
			if (jQuery(this).val() == this.title) {
				jQuery(this).val('');
				jQuery(this).removeClass('textDefault');
			}
		});
		
		
		autoEmpty.blur(function () {

			if (jQuery(this).val() == '' || jQuery(this).val() == this.title) {
				jQuery(this).val(this.title);
				jQuery(this).addClass('textDefault');
			} 
		});
		
		autoEmpty.blur();
		
		
		jQuery('#paymentNewsletterSubmit').click(function (e) {
			var field = jQuery('#emailAddress');
			e.preventDefault();
			
			if (field.val() != '' && field.val() != field.attr('title')) {
				document.forms.paymentNewsletter.submit();
			} else {
				field.addClass('error');
			}
		
		});		
			
	},


	initFlir : function () {

		FLIR.init( { path: '/fileadmin/extras/rev10/facelift-2.0b3-B/' } );
		jQuery('#content .indexTeaser h2, #content .content h1').each(function()	{FLIR.replace(this, new FLIRStyle({mode:'wrap'}) ); });
	
	},
	
	equalizeFooterHorizontal : function () {
		var footerElements	= jQuery('#footer > ul > li');
		var avgWidth		= Math.floor(footerElements.parent().width() / footerElements.length) - parseInt(footerElements.css('margin-right')); 
	
		footerElements.width(avgWidth);
	},
	
	
	equalize : function(selector) {
		var elements = jQuery(selector).toArray();
		var variables = { indexStart: false, indexLast: false, height: false, y: false };
		
		for(index in elements) {
			if(variables.indexStart === false) {
				variables.indexStart = index;
			}
			
			element = jQuery(elements[index]);
			
			if(variables.height === false || element.height() > variables.height) {
				variables.height = element.height();
			}
			
			if(variables.y === false) {
				variables.y = element.position().top;
			}
			
			if(element.position().top > variables.y) {
				for(i = variables.indexStart; i <= index - 1; i++) {
					element2 = jQuery(elements[i]);
					
					element2.height(variables.height);
				}
				
				variables = { indexStart: index, indexLast: false, height: element.height(), y: element.position().top };
			}
			
			variables.indexLast = index;
		}
		
		if(variables.indexStart !== false) {
			for(i = variables.indexStart; i <= variables.indexLast; i++) {
				element = jQuery(elements[i]);
						
				element.height(variables.height);
			}
		}
	}
}

jQuery.extend({
  getUrlVars: function(){
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
      hash = hashes[i].split('=');
      vars.push(hash[0]);
      vars[hash[0]] = hash[1];
    }
    return vars;
  },
  getUrlVar: function(name){
    return jQuery.getUrlVars()[name];
  }
});


sfHover = function() {
	var sfEls = document.getElementById("topNav").getElementsByTagName("li");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}

if (window.attachEvent) window.attachEvent("onload", sfHover);

