$(function(){
	
	// // hackish ... better way? -> hackisch isch bescht
	currentLang = window.location.href.split('/')[4];
	
	//dim unrelated pages

	if(!$.browser.msie) {
		$('.links .unrelated').css({opacity:.7})
		$('.links').hover(function(){
			$('.unrelated',this).stop().animate({opacity:1},200,'easeInOutQuad');
		},function(){
			$('.unrelated',this).stop().animate({opacity:.7},600,'easeInOutQuad');
		});
	}
	
/*	Version vom 18.11.2010  //background resizing
	$('#backgroundImage img').load(resizeBg);
	$(window).resize(resizeBg).trigger('resize');*/
	
	//background resizing
	$('#backgroundImage img').load(function(){
			
	//------neu:		
 		resizeBg();
	});
	
	$(window).resize(resizeBg);

	
	
	//image slideshow
	if($('.images ul li').length > 1){
		enableImageSlideshow();
	}
	
	if($('.videos ul').length) {
		enableVideoPlayers();
	}
	
	//list hover effect
	/*$('.listing .item,.tx-indexedsearch-res .tx-indexedsearch-res').hover(function(){
		$(this).stop().animate({backgroundPosition:'500px 10px'},200,'easeOutExpo');
	},function(){
		$(this).stop().animate({backgroundPosition:'600px 10px'},500,'easeInOutExpo');
	}).css({backgroundPosition:'600px 10px',cursor:'pointer'}).click(function(){
		// .trigger('click');
		window.location.href = $('a',$(this)).eq(0).attr('href');
	});*/
		
	//list hover effect neu:
	
	$(".item").find('.abstract').each(function() {
		var target = $('a',$('.abstract').parent().children()).eq(0).attr('href');		
        var a = $('<a/>').attr('href', target);
        $(this).wrap(a);
   	});
	
	
	$('.listing .item,.tx-indexedsearch-res .tx-indexedsearch-res .abstract').hover(function(){
		$(this).stop().animate({backgroundPosition:'500px 10px'},200,'easeOutExpo');
	},function(){
		$(this).stop().animate({backgroundPosition:'600px 10px'},500,'easeInOutExpo');
	}).css({backgroundPosition:'600px 10px',cursor:'pointer'})
	$('.item').click(function(){
		// .trigger('click');
		window.location.href = target;
	});

	// console.log('_')
	
	//searchbox
	$('input[type="text"]','#header').example(function() {
	     return 'Suchen';
	    });
	
	
	//search result colors
	if($('.tx-indexedsearch').length){
		
		$('.tx-indexedsearch-title a[href*=produkte]').css({color:'#99cc00'});
		$('.tx-indexedsearch-title a[href*=unternehmen]').css({color:'#0072bc'});
		$('.tx-indexedsearch-title a[href*=leistungen]').css({color:'#cc3300'});
		$('.tx-indexedsearch-title a[href*=fallstudien]').css({color:'#0d5172'});
	}
	
	//proposal form
	if($('#proposalForm')){
		initProposalForm();
	}
	
	$('.home #teaserContainer > a img[height=166]').css({opacity:1}).hover(function(){
		$(this).animate({opacity:.5},300,'easeInOutExpo');
	},function(){
		$(this).animate({opacity:1},600,'easeInOutExpo');
	});
	
	// repair anchor links for alpha nav (broken cause we use base url
	
	$("a[href^='\#']").click(function(e){
		e.preventDefault();
		document.location.hash=this.href.substr(this.href.indexOf('#')+1);
	});
	
	
	//limit unrelated links to 8
	limitRelatedLinks();
	
	
	
	//hide content button
	//if(!$('#content.home').length){
	//if(!$('#content.home').length){
		chbg = (backgrounds.length > 2) ? '<a href="javascript:void();" id="changebg" title="nächstes bild" class="hidden"/>' : '';
		$('#container').append('<a href="javascript:void();" id="hidecontent" title="Hintergrund anzeigen"></a>'+chbg);
//	}
	
	
	$('#hidecontent').toggle(function(){
			// if()
			$('#content,#mainnaviContainer,#langpick').stop().animate({opacity:0},400);
			$('#fakegraphwrap,#graphwrap').hide();

	$('#changebg').fadeIn('slow');
			$(this).toggleClass('contenthidden',400);
		},function(){
			$('#content,#mainnaviContainer,#langpick').stop().animate({opacity:1},200);
			$('#fakegraphwrap,#graphwrap').show();

			$('#changebg').fadeOut(10);
			$(this).toggleClass('contenthidden',400);
		});
	
	$('#changebg').click(function(e){
		e.preventDefault();
		c = $('#backgroundImage img').attr('src');
		path = c.split('/');
		c = path[path.length-1];
		bgs = window.backgrounds;
		n = -1;
		for (var i=0; i < bgs.length; i++) {
			if(bgs[i] == c) {
				n = (i+1 < bgs.length) ? i+1 : 0;
			}
		};
		
	
	
		if(n > -1) {
			path[path.length-1] = backgrounds[n];
			$('#backgroundImage img').stop().css({opacity:0}).attr('src',path.join('/')).load(function(){
				$(this).stop().animate({opacity:1},1000,'easeInOutQuad');
			});
		} else {
			$(this).fadeOut(10);
		}
	})
	
	
	if($.isFunction($.fn['pngFix']) && !$('#singleView').length){
		$(document).pngFix();
	}
	
	
	cLh = $('#contentLeft').height();
	cRh = $('#contentRight').height();
	
	if(cRh > cLh) {
		$('#contentLeft').css({minHeight:cRh+'px'});
	}
	
	
	
	// glossary functions
	$('#glossary #alphaNav li a').bind('click', function() {
	 	filterGlossary($(this).attr("href").substr(-1));
	});
	//deaktiviert dar®da 18.12.10 ------->> 
	//filterGlossary();
	
	// Graphfunktion-Schalter deaktiviert 22.11.10 da®da
	/*if($('.graphwrap').length && $('.related').length) {
		$('#contentRight').append($('<a/>').attr('href','#').attr('id','switcher').text('connected view').click(reloadGraph))
	}*/
	
	setupHashWatch();
});


	
	


// Animationsfunktion für Minigraph rechts
// deaktiviert 22.11.10 da®da
/*graphLoaded = false;
function reloadGraph() {

	if(!graphLoaded) {
		$('.linkwrap').animate({
			left:'-300px'
		},{
			duration:400,
			complete:function(){
				$(this).hide();
				$('.graphwrap').show().css({left:'-300px',height:'400px'}).animate({left:0},400);
			}
		}); 
		$('#switcher').text('static view');
		graphLoaded = true;
	} else {
		$('.graphwrap').animate({
			left:'-300px'
		},{
			duration:400,
			complete:function(){
				$(this).hide();
				$('.linkwrap').show().css({left:'-300px'}).animate({left:0},400);
			}
		}); 
		$('#switcher').text('connected view');
		graphLoaded = false;
	}
	
	return false;
}*/


function limitRelatedLinks() {
	

	//limit unrelated links to 8
	window.limit = -1;
	$('#contentRight .links').each(function(i,e){
		total = $('.unrelated a',$(e)).length;
		if(total > limit+1){
			$('.unrelated a',$(e)).each(function(i2,e2){
				if(i2 > limit){
					$(e2).addClass('hiddenlink').addClass('ex');
				}
			});
			
			//alert(currentLang);
			
			if (currentLang == "en") {
				$('.unrelated',$(e)).show().append('<a href="javascript:void();" class="linkexpand closed">'+(total-limit)+' total ..</a>');
			} else {
				$('.unrelated',$(e)).show().append('<a href="javascript:void();" class="linkexpand closed">'+(total-limit)+' insgesamt ..</a>');
			}
			
			
			$('.linkexpand',$(e)).click(function(){
				
				$(this).css({opacity:0});
				
				$(this).parent().find("a.ex").css({opacity:1}).each(function(i,e){
					// $(e).animate({opacity:1},i*30,'linear',function(){
						
						if($(this).is('.hiddenlink')){
							$(e).css({opacity:0}).removeClass('hiddenlink').animate({opacity:1},300,'easeInOutExpo');
						} else {
							$(e).animate({opacity:0},300,'easeInOutQuad',function(){
								$(this).addClass('hiddenlink');
							});
							// $(e).css({opacity:0}).addClass('hidden');
						}
					// });
				});
				
				$(this).animate({opacity:0},600,'easeInOutExpo',function(){
					$(this).animate({opacity:1},500,'easeInOutQuad');
				})
				
				
				if($(this).is('.closed')){
					if(!$(this).data('ctext')) {
						$(this).data('ctext',$(this).text());
					}
					$(this).text('weniger').toggleClass('closed');
				} else {
					$(this).text($(this).data('ctext')).toggleClass('closed');
				}
				
				
				return false
			})
		}

	})
}


// GLOSSARY FILTER
// ---------------------------------------------------------------------------------------------------

function filterGlossary($letter){
	if (!$letter) {
		if (window.location.hash) {
			$letter	= (window.location.hash.substr(-1,1));
		} else {
			$letter = 'a';
		}
	}
	$('#glossary .item').hide();
    $('#glossary .item.'+$letter).show(1000);
  //--> da®da 19.12.10-->hack für IE glossary Funktionalität
    $('#glossary .item.'+$letter.substr(17,18)).show(1000);
  	//alert(window.location.hash.substr(0));
	// alert($letter.substr(17,18));
}


// IMAGE SLIDESHOW
// ---------------------------------------------------------------------------------------------------

function enableImageSlideshow(){
	//setup visibility
	$('.images ul').css({position:'relative',overflow:'hidden',height:'250px'}).find('li').css({position:'absolute',top:0,left:0,opacity:0,zIndex:300}); //.eq(0).css({opacity:1}).addClass('active');
	//add behaviour -> show next on click
	$('.images ul').click(nextImage).attr('title','nächstes Bild').css({cursor:'pointer'});
	//add interface
	$('.images').append('<div id="imagenav"><div id="info"></div><div id="nav"><a class="prev" href="javascript:;" onclick="prevImage();" title="ein Bild zurück">Zurück</a><a title="ein Bild vor" class="next" href="javascript:;" onclick="nextImage();">Vor</a></div></div>')
	//show first image
	showImage($('.images ul li').eq(0));
}

function nextImage(){
	n = $('li.active + li','.images');
	if(!n.length) n = $('li','.images').eq(0);
	return showImage(n);
}

function prevImage(){
	n = $('li.active','.images').prev();
	if(!n.length) n = $('li:last','.images');
	return showImage(n);
}

function showImage(n){
	total = $('.images ul li');
	$('.images #info').text('Bild '+(total.index(n)+1)+' von '+(total.length));
	$('li.active').removeClass('active').stop().animate({opacity:0},400,'easeInOutQuad');
	n.addClass('active').stop().animate({opacity:1},400,'easeInOutQuad');
	return false;
}


// VIDEO PLAYER
// ---------------------------------------------------------------------------------------------------

function enableVideoPlayers(){
	$('.videos ul li').each(function(i,e){
		e = $(e)
		e.css({height:'300px'});
		e.attr('id','video'+i);
		var flashvars = {};
		flashvars.file = $('base').attr('href')+e.text();
		e.text('');
		// flashvars.image = $('img',e).eq(0).attr('src');
		flashvars.controlbar = 'over'; 
		flashvars.autostart = false;
		flashvars.autoload = true;
		flashvars.fullscreen = true;
		flashvars.skin = $('base').attr('href')+'fileadmin/swf/videoplayer/skin.swf';
		e.append('<div id="playback'+i+'"/>');
		var params = {};
		params.scale = "noscale";
		params.wmode = "transparent";
		params.align = 'middle'
		params.allowfullscreen = "true";
		params.bgcolor = '#000000';
		params.allowscriptaccess = 'always';
		var attributes = {'class':'videoplayer',align:'left'};
		swfobject.embedSWF($('base').attr('href')+'fileadmin/swf/videoplayer/player.swf','playback'+i,'100%','300px','9',true,flashvars,params,attributes);
	});
}


//---------------BackgroundResize neu
function resizeBg() {
   $("#backgroundImage").resizeImage();
};

//---------------BackgroundResize neu


//PROPOSAL FORM
//---------------------------------------------------------------------------------------------------

function initProposalForm(){
	var $default = "Bitte ausfüllen!";
	$('form#proposalForm').submit(function(){
		if ($("form#proposalForm textarea:proposal").val() && !($("form#proposalForm textarea:proposal").val() == $default)) {
			var $mail = decryptString('uftuAuzqp4.uvupsjbmt/psh',-1);
			location.href="mailto:"+$mail+"?subject=Deltapage Vorschlagen&body=Hallo Deltacontrol,%0d%0a%0d%0aBitte folgende Seite anlegen:%0d%0a%0d%0a"+$("form#proposalForm textarea:proposal").val()+"%0d%0a%0d%0aVielen Dank";
		} else {
			$("form#proposalForm textarea:proposal").val($default);
		}
		return false;
	});
}







// Ajax Loading
//---------------------------------------------------------------------------------------------------

function setupHashWatch(){
	if($('#content').length) {
		window.h = self.location.hash;
		window.hw = setInterval(window.checkHash,100);
	}
}

function checkHash() {
	if(h != self.location.hash) {
		
		if($('object#graph').length) {
			try {
				getFlash('graph').centerByUrl(self.location.hash);
			} catch(e) {
				alert('unable to call centerByUrl');
			}
			
		}
	}
	
	window.h = self.location.hash;
}
function onNodeCentered(u){
	
	// alert('on node centered');
	// return;
	
	justUrl = self.location.href
	if(justUrl.indexOf('#') != -1) {
		justUrl = self.location.href.substr(0,justUrl.indexOf('#'));
	}
	
	if(justUrl.indexOf(u)!=-1) {
		return;
	}
	
	if(!$('#content').length || !$('#graphwrap.home').length) {
		return;
	}
	
	if(window.isloading == u) {
		return
	}
	

	
	// $('#content').load(u+' #content',cLoaded).addClass('ajax');
	if(!($.browser.msie && $.browser.version < 7)) {
		window.isloading = u;
		self.location.hash = u;
		$('#content').load(u+' #content',cLoaded).addClass('ajax');
		$('#contentLeft').addClass('loading');
	} else {

	
		// var go = 'http://'+window.location.host+'/cms/'+u;
		// window.location.href = go;

	}
	
	
}

function cLoaded(response,text,xhr) {

		
		
		limitRelatedLinks();
		$('.loading').removeClass('loading'); 
		//image slideshow
		if($('.images ul li').length > 1){
			enableImageSlideshow();
		}

		if($('.videos ul').length) {
			enableVideoPlayers();
		}

		if($('object#graph').length) {
			$('#content .links a').click(function(e){
				e.preventDefault();
				getFlash('graph').centerByUrl($(this).attr('href'));
			})
		}
		
		loadedBgImgSrc = $(response).filter('#backgroundImage').find('img').attr('src'); 
		$('#backgroundImage img').attr('src',loadedBgImgSrc)
}

function getFlash(movieName) {
         // if (!$.browser.msie) {
         //     return window[movieName];
         // } else {
             return document[movieName];
         // }
     }




//fakegraph:
//---------------------------------------------------------------------------------------------------

/*	$(document).ready(function(){	
		$('#graphwrap').slideDown(1000);

		window.setTimeout(function() {
			$('#fakegraphwrap').animate({"height": "30px"},1000);
			var test = window.location.href;
			//if( test != "http://s15357998.onlinehome-server.info/cms/de/produkte-systeme.html" && test != "http://s15357998.onlinehome-server.info/cms/de/leistungen.html" && test != "http://s15357998.onlinehome-server.info/cms/de/glossar.html" && test != "http://s15357998.onlinehome-server.info/cms/de/projekte.html" && test != "http://s15357998.onlinehome-server.info/cms/de/kontakt.html" && test != "http://s15357998.onlinehome-server.info/cms/de/unternehmen.html") {
			if(test != "http://s15357998.onlinehome-server.info/cms/de/home.html") {
			//test = test.replace("http://s15357998.onlinehome-server.info/cms/noCache","http://s15357998.onlinehome-server.info/cms#");
			test = test.substring(44,test.length);
			test = test.replace(test, "http://s15357998.onlinehome-server.info/cms/#"+test);
			$('#fakegraphwrap a').eq(0).attr("href",test);
					//	alert(test);
			}

			//}
		}, 1200);
	}); */

	$(document).ready(function(){	
		// return
		$('#graphwrap').slideDown(800);

		var test = window.location.href;
			
			//if(test != "http://www.deltacontrol.de/de/home.html")
			if(test != "http://www.deltacontrol.de/") {
			//alt:
			test = test.substring(27,test.length);
			test = test.replace(test, "http://www.deltacontrol.de/#"+test);
			/*$('#fakegraphwrap a').eq(0).attr("href",test);*/
			$('#opener a').eq(0).attr("href",test);
			
			//neu:
			//test = test.split("no_cache/")[0] +"#no_cache/"+test.split("no_cache/")[1];
			//$('#opener a').eq(0).attr("href",test);
			//alert(test);
			}
		var curUrl = window.location.href;
		
/*			if( curUrl == "http://s15357998.onlinehome-server.info/cms/de/produkte-systeme.html" || curUrl == "http://s15357998.onlinehome-server.info/cms/de/leistungen.html" || curUrl == "http://s15357998.onlinehome-server.info/cms/de/glossar.html" || curUrl == "http://s15357998.onlinehome-server.info/cms/de/projekte.html" || curUrl == "http://s15357998.onlinehome-server.info/cms/de/kontakt.html" || curUrl == "http://s15357998.onlinehome-server.info/cms/de/unternehmen.html") {
		window.setTimeout(function() {
//			$('#fakegraphwrap').animate({"height": "10px"},800);
				$('#fakegraphwrap').css("height","10px");
	
		}, 800);
			} else {
				$('#fakegraphwrap').css("height", "10px");
				}*/
	});
