$(function () {  
			
	/**
	 * CSS Help: Home -> Teaser Boxes (even/odd for margin)
	 */
	 $(".p-home .ce_text:nth-child(odd)").addClass("space");
	 
	 
	/**
	 * Content Slider
	 */	 
	$('#lead .mod_article').cycle({
		fx: 'fade',
		speed:    	cycleSpeed, 
		timeout:  	cycleTimeout,
		pause:		1,
		pager:  '#lead-nav'
	});
	$('#lead-nav a').click(function(){ 
    	$('#lead .mod_article').cycle('pause').stop(); 
        return false;
    });
	
	
	/**
	 * FAQ
	 */		
	$('#faq .ce_text p').hide();
	$('#faq .ce_text h2').click(function() {
		$(this).toggleClass('active');
		$(this).next().slideToggle('1000');		
	});
	// Show/Hide All
	// .:TODO:. Create dynamically
	$('.faq-nav .show-all').click(function(e) {
		$('#faq .ce_text h2').next().slideDown('1000');
		e.preventDefault();
	});
	$('.faq-nav .hide-all').click(function(e) {
		$('#faq .ce_text h2').next().slideUp('1000');
		e.preventDefault();
	});
	
	
	/**
	 * Fancybox
	 */		
	$('a[rel^="lightbox"]').addClass("fancybox").attr("rel", "group");
	$('a.fancybox').fancybox({
		'titleShow': false
	});
	// Inline Content (e.g. Partnerlist)
	$('a.inline-overlay').fancybox({
		'titleShow': false,
		'autoDimensions': false,
		'width': 400,
		'height': 500
	});	


	/**
	 * Region Selection
	 */	
	$('.region ul').hide();
	// Available countries
	var countryGb = '<a href="" id="r-gb">Globale Webseite</a>';
	var countryAt = '<a href="" id="r-at">&Ouml;sterreich</a>';
	var countryDe = '<a href="" id="r-de">Deutschland</a>';
	var countryChfl = '<a href="" id="r-chfl">Schweiz/Liechtenstein</a>';
	
	// Set the region to value in #region-selection
	var activeRegion = $('#active-region').text();
	if (activeRegion == "r-at") {
		$('#region-selected').html(countryAt);
	} else if (activeRegion == "r-de") {
		$('#region-selected').html(countryDe);
	} else if (activeRegion == "r-chfl") {
		$('#region-selected').html(countryChfl);
	} else {
		$('#region-selected').html(countryGb);
	}
	
	$('#region-selected').click(function(e) {
		$('.region ul').toggle();
		e.preventDefault();
	});
	$('.region ul').mouseleave(function() {
		$(this).toggle();
	});
	$('.region ul a').click(function(e) {
			//console.log($(this).html());
		// Define options
		var username = $(this).attr('id'); // Username is always id of <a>
		var password = '12345678'; // Default password
		
		// Set options and submit form
		$('#frm-country-selection #username').attr('value', username);
		$('#frm-country-selection #password').attr('value', password);
		$('#frm-country-selection #autologin').attr('checked', true);
		$('#frm-country-selection').trigger('submit');
		
		e.preventDefault();
	});
	
	/**
	 * Language
	 */
	$('#language-selected').click(function(e) {
	$('#language ul').toggle();
	e.preventDefault();
	});
	$('#language ul').mouseleave(function() {
		$(this).toggle();
	});
	
	/**
	 * Product Navigation
	 */		
	$('#product-nav li:first-child').remove();
	$('#product-nav a').first().addClass("active");
	$('#p-product #normal .mod_article').not('#navigation').addClass('product-content');
	$('#p-product #normal .product-content').hide();
	$('#p-product #normal .product-content:eq(0)').show();
	
	$('#product-nav a').click(function(e) {
		var relAnchor = $(this).attr('href').split('#')[1];
		$('#p-product .product-content').hide();
		$('#p-product #' + relAnchor).show();
		$('#product-nav a').removeClass('active');
		$(this).addClass('active');
		
		e.preventDefault();
	});

	/**
	 * Product Navigation (Tabs)
	 */		
	$('#p-products #product-nav li:eq(0)').remove();
	$('#product-nav a').first().addClass("active");
	$('#p-products #normal #product-nav').parent().nextAll('.mod_article').addClass('product-content');
	$('#p-products #normal .product-content').hide();
	$('#p-products #normal .product-content:eq(0)').show();
	
	$('#product-nav a').click(function(e) {
		var relAnchor = $(this).attr('href').split('#')[1];
		$('#p-products .product-content').hide();
		$('#p-products #' + relAnchor).show();
		$('#product-nav a').removeClass('active');
		$(this).addClass('active');
		
		e.preventDefault();
	});



	//$('#p-product #zubehoer').show();


	/**
	 * Product Tabs
	 */	
	$('#product-tabs').prepend('<ul class="product-tabs">');
	$('#product-tabs .ce_text h1').each(function(index) {
		var content = $(this).text();
		$(this).parent().attr('id', 'tab-'+index);
		$('#product-tabs .product-tabs').append('<li><a href="#" rel="#tab-'+index+'">'+content+'</a></li>');
		$(this).remove();
	});
	$('#product-tabs .ce_text').hide();
	$('#product-tabs .ce_text').first().show();
	$('.product-tabs a').first().addClass('active');
	$('.product-tabs a').click(function(e) {
		$('#product-tabs .ce_text').hide();
		var rel = $(this).attr('rel');
		$('#product-tabs .ce_text#'+rel).show();
		$('.product-tabs a').removeClass('active');
		$(this).addClass('active');
		e.preventDefault();						
	});

	/**
	 * Partnerliste
	 */	
	$('#partnerlist .info').hide();
	$('#partnerlist a.show-info').click(function(e) {
		$(this).parent().parent().parent().find(".info").slideToggle();
		e.preventDefault();
	});
	
	
	/**
	 * PROGRESSIVE ENHANCEMENT: Form - placeholder/value
	 * .:TODO:. Create placeholder instead of value attribute in contao template
	 */		
	$('#frm-search input.text').attr('placeholder', $('#frm-search input.text').val());
	$('#frm-search input.text').val('');
	if (!Modernizr.input.placeholder) {	
		$("input").each(function(){
			if ($(this).val() == "" && $(this).attr("placeholder") != "") {
				$(this).val($(this).attr("placeholder"));
				$(this).focus(function() {
					if ($(this).val() == $(this).attr("placeholder")) $(this).val("");
				});
				$(this).blur(function() {
					if ($(this).val()=="") $(this).val($(this).attr("placeholder"));
				});
			}
			
		});	
	}
	
	
	/**
	 * Dropdown Navigation
	 */		
	$("#nav li.submenu").hover(function() {
		if (!$(this).is('.active, .trail')) {
			$(this).addClass('flyout');	
		}
	}, function() {
		$(this).removeClass('flyout');		
	});
	
//		$("li.submenu").hover(
//			function() {
//				if (!($("li.submenu").parent().hasClass("level_2"))) {
//					if (!($(this).hasClass('active').hasClass("trail"))) {
//						$(this).addClass('flyout');
//					}
//				}
//				$(this).children('.level_2').show();
//			}, 
//			function() {
//				//if (!($("li.submenu").parent().hasClass("level_2"))) {
//					$(this).removeClass('flyout');
//				//}
//			}
//		);
		//console.log("OK");
	



	/**
	 * Homebox
	 */
	$('.ce_homebox').hover(function() {
		$(this).addClass("hover");
	}, function() {
		$(this).removeClass("hover");
	});
	

	

});  
