//var $j = jQuery.noConflict(); 
$(document).ready(function($){
	function linkHandler() {
		var objLinksArr = $('a:not([href^=#])');
		var currentDomain=top.location.hostname;
		var seqURL='';
		for (eachLink in objLinksArr){
			if(objLinksArr[eachLink].href){
				if(objLinksArr[eachLink].href.indexOf('javascript') == -1){
					seqURL = objLinksArr[eachLink].href;
					seqURL = seqURL.replace(/http:\/\//gi,'');
					seqURL = seqURL.replace(/https:\/\//gi,'');
					seqURL = seqURL.split('/');
					if(seqURL[0] != currentDomain){
						$(objLinksArr[eachLink]).click(function() {
							window.open(this.href);
							return false;
						});
					}
				}
			}
		}
	}

	function actFirst (items) {
		$(items[0]).addClass( 'act' );
	}	
	//  carousel-box  -> fce-carousel		switcher -> fce-browser		 white-> act
	var transitionDelay = 900;
	var imgCounter = 0;
	function tabSlide(items) {
		interval = 9000;
		setTimeout(function () {
			if($(items[imgCounter]).hasClass( 'act' ) && !$('.fce-carousel .fce-browser').hasClass('locked')) { 
							$(items).removeClass( 'act' );
							$('#'+$(items[imgCounter]).attr('href').split('#')[1]).fadeOut(transitionDelay);
							if(items[imgCounter+1]) {
								$(items[imgCounter+1]).addClass( 'act' );
								$('#'+$(items[imgCounter+1]).attr('href').split('#')[1]).fadeIn(transitionDelay);
								imgCounter ++;
							} else {
								imgCounter = 0;
								$(items[imgCounter]).addClass( 'act' );
								$('#'+$(items[imgCounter]).attr('href').split('#')[1]).fadeIn(transitionDelay);
							}
			} else if($('.fce-carousel .fce-browser').hasClass('locked')) {
				// 
			} else if(imgCounter < items.length) {
				imgCounter ++; 
			} else {
				imgCounter = 0; 
			}
			tabSlide(items);
		}, interval);
	}
	
	$('.fce-carousel .fce-browser').idTabs(function(id,list,set){
		if($(id).css('display') == 'none') {
			for(i in list) {
				//if(list[i]==id){ imgCounter = i;}
				if($(list[i]).css('display') != 'none'){
					$(list[i]).fadeOut(transitionDelay);
				} else {
					$(list[i]).hide(); 
				}
			}	
			$(id).fadeIn(transitionDelay);
			$('a',set).removeClass('act');
			$('a',set).filter("[href='"+id+"']",set).addClass('act');
			$('a',set).filter("[href='home.html"+id+"']",set).addClass('act');
		}
		if($('.fce-carousel .fce-browser').hasClass('locked')) $('.fce-carousel .fce-browser').removeClass('locked');
		return false;
	});
	
	$('.fce-carousel .next').click(function () {
//alert(imgCounter);
		ind = 0;
		blocks = $('.fce-carousel .fce-browser a');
		while(!ind) {
			if($(blocks[imgCounter]).hasClass( 'act' )) {
							$(blocks).removeClass( 'act' );
							$('#'+$(blocks[imgCounter]).attr('href').split('#')[1]).fadeOut(transitionDelay);
							if(blocks[imgCounter+1]) {
								$(blocks[imgCounter+1]).addClass( 'act' );
								$('#'+$(blocks[imgCounter+1]).attr('href').split('#')[1]).fadeIn(transitionDelay);
								imgCounter ++;
							} else {
								imgCounter = 0;
								$(blocks[imgCounter]).addClass( 'act' );
								$('#'+$(blocks[imgCounter]).attr('href').split('#')[1]).fadeIn(transitionDelay);
							}
							ind = 1;
			} else if(imgCounter < blocks.length) {
				imgCounter ++;
			} else {
				imgCounter = 0;
			}
		}
		if(!$('.fce-carousel .fce-browser').hasClass('locked')) $('.fce-carousel .fce-browser').addClass( 'locked' );
	});	
	
	$('.fce-carousel .prev').click(function () {
		ind = 0;
		blocks = $('.fce-carousel .fce-browser a'); 
		while(!ind) {
			if($(blocks[imgCounter]).hasClass( 'act' )) {
							$(blocks).removeClass( 'act' );
							$('#'+$(blocks[imgCounter]).attr('href').split('#')[1]).fadeOut(transitionDelay);
							if(blocks[imgCounter-1]) {
								$(blocks[imgCounter-1]).addClass( 'act' );
								$('#'+$(blocks[imgCounter-1]).attr('href').split('#')[1]).fadeIn(transitionDelay);
								imgCounter --;
							} else {
								imgCounter = blocks.length-1;
								$(blocks[imgCounter]).addClass( 'act' );
								$('#'+$(blocks[imgCounter]).attr('href').split('#')[1]).fadeIn(transitionDelay);
							}
							ind = 1;
			} else if(imgCounter > 0) {
				imgCounter --;
			} else {
				imgCounter = blocks.length-1;
			}
		}
		if(!$('.fce-carousel .fce-browser').hasClass('locked')) $('.fce-carousel .fce-browser').addClass( 'locked' );
	});			
		
/*	
	$('.fce-carousel .fce-browser').hover(function() {
				$('.fce-carousel.fce-browser').toggleClass('locked');
	},function() {
				$('.fce-carousel .fce-browser').toggleClass('locked');
	});
*/
        // $('.carousel-box .fce-browser a:first').addClass( 'act' );
	if ($('.fce-carousel .fce-browser').length){ 
		actFirst($('.fce-carousel .fce-browser a'));
		tabSlide($('.fce-carousel .fce-browser a'));
	}
	linkHandler();
});
	function getForm(form) {
		var doSubmit = true;
		var errorText = ''; // You must fill in these fields:
		var first_name = $(form).find('#first_name');
		var name = $(form).find('#name');
		var company = $(form).find('#company');
		if(first_name.attr('value') == '' || first_name.attr('value').length < 2) { doSubmit = false; errorText = errorText + 'Vorname\n';}
		if(name.attr('value') == '' || name.attr('value').length < 2) { doSubmit = false; errorText = errorText + 'Name\n';}	
		if(company.attr('value') == '' || company.attr('value').length < 2) { doSubmit = false; errorText = errorText + 'Firma\n';}
		if (doSubmit) {
			$(form).submit();
		} else {
			errorText = 'Sie müssen diese Felder ausfüllen:\n\n' + errorText;
			alert(errorText);
		}
		return false;
	}
/*
		function validate(form_id,email) {
			var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
			var address = document.forms[form_id].elements[email].value;
			if(reg.test(address) == false) {
				alert('Invalid Email Address');
				return false;
			}
		}

*/	