jQuery(document).ready(function () {

if ($('.page_header + div').attr('class')==null) $('.page_header + div').addClass('page_teaser_small');
$('.content_middle .tx-indexedsearch input.tx-indexedsearch-searchbox-sword').focus(function(){
		$(this).css({
			'background-position':'0 -22px',
			'color':'#FFF'
		});
});
$('.content_middle .tx-indexedsearch input.tx-indexedsearch-searchbox-sword').focusout(function(){
	$(this).removeAttr('style');
});

// Laufbanner Logo austaschen
$('#c2974 .crm-kompetenz').css({
	'background-image':'url(fileadmin/templates/main/Bilder/cloud_finder_logo_sprite.jpg)',
	'width':'173px'
});

// SPONSOREN ANIMATION BEGIN
	var breite = 0;
	var childnumber1 = 1;
	var childnumber2 = 2;
	
	function breitenberechnung() {
		$('.page_header .wrapper div.csc-default').each(function(){
			breite = $(this).width()+breite+20;
		});
		var breite2 = breite+50;
		$('.page_header .wrapper').css('width', breite2);
		$('.page_header .wrapper').clone().insertAfter('.page_header .sponsors .wrapper').css('left', breite);
	}
	
	function start(childnumber1, childnumber2) {
		var position = Math.abs($('.page_header .sponsors .wrapper:first-child').position().left);
		var zeit = (breite-position)*20;
		//alert('childnumber1: '+childnumber1+', childnumber2: '+childnumber2+', position: '+position+', zeit: '+zeit+'');
		$('.page_header .sponsors .wrapper:nth-child('+childnumber1+')').animate({left:-breite}, zeit, 'linear', function(){
			$(this).css('left', breite);
		});
		$('.page_header .sponsors .wrapper:nth-child('+childnumber2+')').animate({left:"0px"}, zeit, 'linear', function(){
			start(childnumber2, childnumber1);
		});
	}
	
	breitenberechnung();
	start(childnumber1, childnumber2);	
	$('.page_header .sponsors').append('<span class="sponsor_layer_left"></span><span class="sponsor_layer_right"></span>');
	
	$('.page_header .sponsors').hover(function(){
		$('.page_header .sponsors .wrapper').stop();
	}, function(){
		var position = $('.page_header .sponsors .wrapper:first-child').position().left;
		if (position<0) start(childnumber1, childnumber2);
		else start(childnumber2, childnumber1);
	});
	
// SPONSOREN ANIMATOPN END

// HEADER ANIMATION BEGIN
	// ONLOAD
	$('.page_teaser .wrapper:first .csc-default').attr('id', function(index){
		index = index || '';
		index++;
		return "teaser"+index;
	});
	
	$('.page_teaser #teaser1').addClass('active');

// IF STARTSEITE	
if ($('#teaser1').size()=='1') {

	// AUTO BLENDE
	$(document).everyTime(13200, function() {
		// HEADER
		var id = parseInt($('.page_teaser .csc-default.active').attr('id').substr(6));
		var count = $('.page_teaser .wrapper:first .csc-default').size();
		if(count>1) effect(id, count, 'right');
		
		// SPONSOREN RECHTS
		if (starti>=animationcycles) starti=0;
        else starti=starti+1;
		headercycle(starti, 4);
		
		// ADCONTAINER LEFT
		adleftcycle();
	});
	
	function adleftcycle(){
		var count = $('.page_teaser .adcontainer#translate img').size();
		var next = parseInt($('.page_teaser .adcontainer#translate img.active').attr('id').substr(6)) + 1;
		if (next>count) next=1;
		// Animation
		$('.page_teaser .adcontainer#translate img.active').fadeOut(1200, function(){
			// Einblenden
			$('.page_teaser .adcontainer#translate img#adleft'+next).fadeIn(900, "").addClass('active');
			// Bildlink setzen
			var link = $('.page_teaser .adcontainer#translate img#adleft'+next).attr('title');
			// verlinken - ja/nein
			if (link=='') $('a#ad_left_link').removeAttr('href');
			else $('a#ad_left_link').attr('href', link);
			// target - top/blank
			$('a#ad_left_link').attr('target', $('.page_teaser .adcontainer#translate img#adleft'+next).attr('target'));
			//Active entfernen
			$(this).removeClass('active');
		});
	}	
	
	// UEBERBLENDUNGEN
	function effect(id, count, direction) {
		if (direction=='right') var newid = id+1;
		else var newid = id-1;
		if (newid>count) newid=1;
		if (newid<1) newid=count;
		$('.page_teaser #teaser'+id).fadeOut(1200, function(){
			$('.page_teaser #teaser'+newid).fadeIn(900, "").addClass('active');
			$(this).removeClass('active');
		});
	}
}
// else {
// 	$(document).everyTime(8000, function() {		
// 		// SPONSOREN RECHTS
// 		if (starti>=animationcycles) starti=0;
//         else starti=starti+1;
// 		headercycle(starti, 4);
// 	});
// }
// HEADER ANIMATION END


// SPONSOR CYCLE 
	var starti = 0;
	var animationcycles = $('#sponsoranimation .wrapper > div').size() / 5;
    $('#sponsoranimation .wrapper').wrapInner('<div id="animationposition" style="position:absolute" />');
	
	function headercycle(newcontainer, containercount) {
		$('#sponsoranimation .wrapper #animationposition').fadeOut('slow', function(){
						var animationvalue = containercount * 60;
                        animationvalue = (newcontainer*animationvalue)+5;
						animationvalue = '-'+animationvalue+'px';
                        $('#animationposition').css('top', animationvalue);
			$('#sponsoranimation .wrapper #animationposition').fadeIn('slow');
		});
	}

	// BOOK EFFECT
	$('.book_box').hover(function(){
		$(this).addClass('active');
		$(this).find('.sw_img img').fadeIn('1200');
	}, function(){
		$('.book_box.active').find('.sw_img img').fadeOut('1800');
	});
	
	$('.sponsor_logo').hover(function(){
		$(this).addClass('active');
		$(this).find('img').fadeIn('1200');
	}, function(){
		$(this).find('img').fadeOut('1800');
	});
	
	$('.sponsor_logo').click(function(){
		location = $(this).parent('a').attr('href');
	});
	
	$('.s_logo').hover(function(){
		$(this).addClass('active');
		$(this).find('img').fadeIn('1200');
	}, function(){
		$(this).find('img').fadeOut('1800');
	});
	
	$('.s_logo').click(function(){
		location = $(this).parent('a').attr('href');
	});
	
	// TOPIC LIST
	// $('ul#topiclist ul').addClass('hidden');
	// $('ul#topiclist li').click(function(){
	// 	$('ul#topiclist .active').removeClass('active').addClass('hidden');
	// 	$(this).find('ul').addClass('active').removeClass('hidden');
	// });
	
	$('.tx-cetopics-pi1 ul#topiclist li').hover(function(){
		$('.tx-cetopics-pi1 ul#topiclist .active').removeClass('active');
		$(this).addClass('active');
		var content = $(this).find('ul').html();
		if (content==null) content='';
		$('#ie6wrap').html('<ul>'+content+'</div');
	});
	$('.tx-cetopics-pi1 ul#topiclist li:first').addClass('active');
	$('#ie6wrap').html('<ul>'+$('.tx-cetopics-pi1 ul#topiclist li:first ul').html()+'</div');
	//$('#ie6wrap').html($('.tx-cetopics-pi1 ul#topiclist li:first ul'));
	
	
	// TAB-MENU
	$('#tab-container .tab[class!=tab active]').css("display", "none");
	$('#tab-container #tab-menu li').click(function(){
		var id = $(this).find('a').attr('href');
		$('#tab-container .active').removeClass('active');
		$('#tab-container .tab#'+id).addClass('active').css("display", "block");;
		$('#tab-container .tab[class!=tab active]').css("display", "none");
		$(this).addClass('active');
		return false;
	});
	
	// EVEN-ODD-FUNKTION
	function even(selector) {
		var count = $(selector).find('li').size();
		for (i=0; i < (count+1); i++) {
			if (i%2==0) $(selector).find('li:nth-child('+i+')').addClass('odd');
			else $(selector).find('li:nth-child('+i+')').addClass('even');
		}
	}
	
	even('ul.marktuebersicht');
	even('.content_left .tx-cal-controller ul');
	
	// MARKTUEBERSICHT	
	$('ul.marktuebersicht#table1 li:first').before('<li id="legende" style="border-right:1px solid #FFF;"><span class="company">Firma</span><span class="location">Firmensitz</span></li>');
	$('ul.marktuebersicht#table2 li:first').before('<li id="legende"><span class="company">Firma</span><span class="location">Firmensitz</span></li>');
	$('ul.marktuebersicht#table1 li:last').addClass('last');
	$('ul.marktuebersicht#table2 li:last').addClass('last');
	/*
	$('.marktuebersicht li').each(function(){
		var tableid = $(this).parent('table');
		//if ($(this).height()>15) alert ($('.marktuebersicht li').index(this));
		$(this).attr('title', tableid);
		var hoehe = $(this).height();
		if (hoehe>15) {
			$('table.marktuebersicht li:nth-child('+$(this).prev('table').index(this)+')').css('height',  '50px');
			//alert(tableid);
			$(this).attr('alt', $('table#'+tableid+' li').index(this));
		}
	});
	*/
	// SUCHE AUF SUCHSEITE IN SIDEBAR ENTFERNEN
	// var container = $('#removeme').parent().html();
	//alert(container);
	
	// KALENDER LINKS ENTFERNEN
	$('.content_left .tx-cal-controller li > a').each(function(){
		if($(this).attr('href').substr(0, 10)=='ressourcen') {
			$(this).removeAttr('href');
			$(this).click(function(event){
				event.preventDefault();
			});
		}
	});
	// KALENDER TAG WEGNEHMEN
	/*
	$('.tx-cal-controller li').each(function(){
		if($(this).find('span.hidden').html()=='monat') {
			var newdate = $(this).find('em').html().substr(4);
			$(this).find('em').html(newdate);
		}
	});
	*/
	$('.content_left .tx-cal-controller li').append('<div class="clr"></div>');
		
	$('.page_teaser .wrapper .button').click(function(){
		location = $(this).find('a').attr('href');
	});
	
	// WIRTSCHAFT - SPRUCH EINFÜGEN
	$('.content_left ul.IndustriesList[id!=software]').after('<span class="legende"><em>Mit freundlicher Unterst&uuml;tzung von economiesuisse</em><img src="fileadmin/Daten/Bilder/Experten/economiesuisse_middle.jpg"></span>')
	
	// Profil
	$('.content_left .tx-ceprofiles-pi1 li:contains("http://www.")').each(function(){
	});
	
	// MOUSE-OVER button-background-images in sidebar
	function buttonover(object, defaultposX, defaultposY){
		if (defaultposX==undefined) defaultposX='0px';
		if (defaultposY==undefined) defaultposY='0px';
		$(object).hover(function(){
			$(this).css('background-position', '-252px '+defaultposY);
		}, function(){
			$(this).css('background-position', defaultposX+' '+defaultposY);
		});
	}
	buttonover($('.content_middle .tx-cal-controller'));
	buttonover($('.content_middle .tx-indexedsearch'));	
	buttonover($('.content_middle .button.powered'), '0px', '-3px');
	
	// Digital Life Index
	if ($.browser.msie && $.browser.version.substr(0,1)<7) {}
	else $('#toggle_slide').css('height', '280px');
	$('#toggle_slide').stop().hover(function(){
		if ($.browser.msie && $.browser.version.substr(0,1)<7) {}
		else {
			$(this).animate({
				height:'580px'
			}, {queue:false, duration:400});
		}
	}, function(){
		if ($.browser.msie && $.browser.version.substr(0,1)<7) {}
		else {
			$(this).stop().animate({
				height:'280px'
			}, {queue:false, duration:300});
		}
	});
	
});
