$(document).ready(function(){
	var screenWidth = $(window).width();
	var screenHeight = $(window).height();

		$(function () {
	        $('.bubbleInfo').each(function () {
	            var distance = 15;
	            var time = 150;
	            var hideDelay = 150;
	
	            var hideDelayTimer = null;
	
	            var beingShown = false;
	            var shown = false;
	            var trigger = $('.trigger', this);
	            var info = $('.popup', this).css('opacity', 0);
				var bubbleWidth = 200;
				var triggerWidth = trigger.width();
				var bubblepos = (bubbleWidth / 2) - (triggerWidth / 2); 
	
	            $([trigger.get(0), info.get(0)]).mouseenter(function () {
	                if (hideDelayTimer) clearTimeout(hideDelayTimer);
	                if (beingShown || shown) {
	                    // don't trigger the animation again
	                    return;
	                } else {
	                    // reset position of info box
	                    beingShown = true;
	
	                    info.css({
	                        top: -170,
	                        left: -bubblepos,
	                        display: 'block'
	                    }).animate({
	                        top: '-=' + distance + 'px',
	                        opacity: 1
	                    }, time, 'swing', function() {
	                        beingShown = false;
	                        shown = true;
	                    });
	                }
	
	                return false;
	            }).mouseleave(function () {
	                if (hideDelayTimer) clearTimeout(hideDelayTimer);
	                hideDelayTimer = setTimeout(function () {
	                    hideDelayTimer = null;
	                    info.animate({
	                        top: '-=' + distance + 'px',
	                        opacity: 0
	                    }, time, 'swing', function () {
	                        shown = false;
	                        info.css('display', 'none');
	                    });
	
	                }, hideDelay);
	
	                return false;
	            });
	        });
	    });

		// initialize navigation glow
		if($('li','.nav > ul').hasClass('open')){
			$act = $('li.open','.nav > ul');		
			var overpos = $act.offset();
			var versatz = (($act.width()-60)/2)-($('.glow').width()/2);
			$('.glow').css({ 'left': (overpos.left + versatz) },{duration:200});
		}

		// navigations
		$('.nav > ul > li').click(function(){
			var overpos = $(this).position();
			var versatz = ($(this).outerWidth()/2)-($('.glow').width()/2);
			$('.glow').animate({ 'left': (overpos.left + versatz) },{duration:200});
		});	
		
		if($('.nav > ul > li > ul > li').hasClass('open')){
			$subact = $('.nav > ul > li > ul > li.open');
					
			var overpos = $subact.offset();
			$('.arrow').css({ 'left': (overpos.left-65) },{duration:200});
		}		
		$('.nav > ul > li > ul > li').click(function(){
			var overpos = $(this).position();
			$('.arrow').animate({ 'left': (overpos.left) },{duration:200});

			if ($("ul.srcategories").length > 0) {
				var searchId = $(this).attr('id').substr(0, $(this).attr('id').length - 3);
				$('ul.srcategories > li:not(#' + searchId + ')').hide();
				$('ul.srcategories > li#' + searchId).show();
			}
		});

		// initialize showroom
		var showroomHeight 	= screenHeight - ( $('#pagehead').height() + 30 + 40 );
		
		var showroomWidth 	= screenWidth;
		$("div.showroom").height(showroomHeight);
		
	//-----------------------------------------------------------------
	// Products 
	//-----------------------------------------------------------------
		if ($("ul.srcategories").length > 0) {	
			
			//$('.nav > ul > li > ul > li > a').removeAttr('href');

			// subnavigation over
			$('.nav > ul > li > ul > li > img').mouseenter(function(){
				this.src = this.src.replace("_off","_on");
				
				$(this).click(function(){
					if( $('.nav > ul > li > ul > li > img').hasClass("open") ){
						var pfad = $('.nav > ul > li > ul > li > img.open').attr('src');
						$('.nav > ul > li > ul > li > img.open').attr({ src: pfad.replace("_on", "_off") });
						$('.nav > ul > li > ul > li > img.open').removeClass("open");
					}		
					$(this).addClass('open');
					this.src = this.src.replace("_off","_on");
				});
			});
			
			$('.nav > ul > li > ul > li > img').mouseleave(function(){
				if ( $(this).hasClass("open") ) {} else {
					this.src = this.src.replace("_on", "_off");
				}
			});		
			
			// initialize catlists
			$catlist = $('ul.srcategories');
			$catlistchild = $('ul.srcategories > li');
			
			var elementWidth 		= 300; // elementbreite	inkl. padding	
			var mouseBereichOffsetX = 200; // X Bereich in dem die Maus nicht reagieren soll
			var mouseBereichOffsetY = 150; // Y Bereich in dem die Maus nicht reagieren soll
			var anzEleScreen 		= 6; // elemente in präsentationsfläche
			var showListPadding		= 65;
			var showListWidth 		= (elementWidth * anzEleScreen) + mouseBereichOffsetX; // berechnung der präsentationsflächen-breite
			var showListHeight;		// wird später gesetzt
			var mfXadd				= 0.5; // verändert den Mousemove X so das mehr platz zum screen rand ist
			
			$catlist.width(showListWidth);
			$('ul.srcategories > li > div:nth-child('+ (anzEleScreen + 2) +')').after("<br clear='all' />");

			showListHeight = $catlist.outerHeight() + mouseBereichOffsetY;		
			$catlist.height(showListHeight);
			
			// scrolling funcion - abfragen wann gescrollt werden muss (wenn screen > list dann nicht)
			var mfX = showroomWidth / Math.abs(showroomWidth - showListWidth);
			var mfY = showroomHeight / Math.abs(showroomHeight - showListHeight);

			
			$("body").mousemove(function(move){
				moveleft = -((move.clientX - mouseBereichOffsetX) / (mfX - 0.5));
				movetop =  -((move.clientY - mouseBereichOffsetY) / mfY);
				
				if (move.clientX > mouseBereichOffsetX) {
					$catlist.css({
						'left': moveleft
					});
				}				
				if (move.clientY > mouseBereichOffsetY) {
					$catlist.css({
						'top': movetop
					});
				}
			});
			
			
			/*$catlist.parent().mouseleave(function(){
				$catlist.stop().animate({
					left: 0,
					top: 0
				},{duration:500});
			});*/
			
			// mouseover client
			if($.support.opacity == false) $('div.client > img').css({opacity: 0.2});
			$catlistchild.children('div').mouseenter(function(){
				$(this).children('span').css('visibility','visible');
				$(this).children('a').children('img').fadeTo('fast',1.0);
			});
			$catlistchild.children('div').mouseleave(function(){
				$(this).children('span').css('visibility','hidden');
				$(this).children('a').children('img').fadeTo('fast',0.4);
			});
		}
	//-----------------------------------------------------------------
	// Detailview 	
	//-----------------------------------------------------------------
		if ($("div.detail").length > 0) {
			var showroomHeight 	= screenHeight - 120 ;
			if (showroomHeight <= 580) showroomHeight = 580;
			
			$("div.showroom").height(showroomHeight);	
			 
			// in detail view show main navigation and a small logo 
			$('.logo').attr('src', $('.logo').attr('src').replace("_big", "_small"))
				.css('padding-top', '65px')
				.removeAttr('height').removeAttr('width');
			$('div.nav ul').css('padding-top', '67px');
			$('#pagehead').css('margin-top', '-54px');
			
			//$('.nav > ul > li > ul > li > a')
			//	.removeAttr('href')
			//	.removeAttr('onmouseover')
			//	.css('cursor','default')
			//	.parent().css('cursor','default');
			
			// description-text layer and cover
			$('.foldhead').add('.cover').add('.srclsbimg').add('.pinfo').click(function(){		
				if ($('.foldbox > .cntholder').text() != "") {
					
					//alert($('.foldbox').height());
					
					$('.mbg').height(
						$('.foldbox').height() + 20
					);
					if ($('.cover').hasClass('open')) {
						$('.cover').fadeOut();
						$('.foldbox').fadeOut();
						$('.cover').add('.foldbox').removeClass('open');
					}
					else {
						$('.cover').css({
							display: 'block',
							opacity: 0.0
						}).fadeTo('fast', 0.8);
						$('.foldbox').fadeIn();
						$('.cover').add('.foldbox').addClass('open');
					}
				}
			});
			
			// adjust showbox on monitor
			var showboxHeight = 500;
			var showboxPosition = (showroomHeight/2) - (showboxHeight/2) + 20; 
			$('.srclshowbox').css('top',showboxPosition);

			// thumblist init
			var liWidth = 130;
			
			$thumblist = $('.srclprojects > ul');
			$thumblist.prepend('<li class="spacer">&nbsp;</li>');
			
			var liCount = $thumblist.children('li').length;
			var ulWidth = (liCount * liWidth);
			
			$thumblist.width(ulWidth);
			
			// build Thumbnails
			var verkleinerung = 5.25;
			$('.srclprojects > ul > li > img.thumb').each(function(intIndex){
				$(this).wrap('<div class="thumbwrap"></div>');
				$(this).before('<div class="preload">&nbsp;</div>');
				
				var newHeight = $(this).height() / verkleinerung;
				var newWidth = $(this).width() / verkleinerung;
				
				$(this).attr({ height:newHeight });	
				$(this).attr({ width:newWidth });	
				
				$_tmpTop = -1 * (($(this).height() / 2) - (90/2));
				$_tmpLeft = -1 * (($(this).width() / 2) - (120/2));
				$(this).css({ 
					position: 'absolute',
					top: $_tmpTop,
					left: $_tmpLeft
				});		
			});
			
			// Preloade images
			var preloaded = 0;
			$('img.thumb').preload({
				onRequest: function(data){			
					if(preloaded == 0){
						var imgSrc = $(".srclprojects > ul > li:nth-child("+ (data.index+2) +") > .thumbwrap > img.thumb").attr('src');
						var imgAlt = $(".srclprojects > ul > li:nth-child("+ (data.index+2) +") > .thumbwrap > img.thumb").attr('alt');
						$(".srclprojects > ul > li:nth-child("+ (data.index+2) +")").addClass('open');
						$('.srclsbimg').parent().prepend('<div class="next">&nbsp;</div><div class="back">&nbsp;</div>');
						$('.srclsbimg').prepend('<img id="t'+ (data.index+2) +'" src="'+ imgSrc +'" alt="'+ imgAlt +'" />');
						
						$('.foldbox > .cntholder').html( $('.srclprojects > ul > li.open > .thumbdesc').html() );
						
						preloaded = 1;
					}
					$(".srclprojects > ul > li:nth-child("+ (data.index+2) +") > .thumbwrap > img.thumb").attr('id',(data.index+2));
					$(".srclprojects > ul > li:nth-child("+ (data.index+2) +") > .thumbwrap > img.thumb").prev('div.preload').remove();
				}
			});
		
			//swap function
			$('.srclprojects > ul > li > .thumbwrap').click(function(){	
				$('.srclprojects > ul > li').removeClass('open');
				$(this).parent().addClass('open');
				
				var imgSrc = $(this).children('img.thumb').attr('src');
				var imgAlt = $(this).children('img.thumb').attr('alt');
				var imgId  = $(this).children('img.thumb').attr('id');
			
				$('.srclsbimg > img').fadeOut('slow');
				setTimeout(function(){
					$('.srclsbimg > img').attr('src',imgSrc);
					$('.srclsbimg > img').attr('alt',imgAlt);
					$('.srclsbimg > img').attr('id','t' + imgId);
					$('.srclsbimg > img').fadeIn('slow');				
				},500);
				
				$('.foldbox > .cntholder').html( $('.srclprojects > ul > li.open > .thumbdesc').html() );
			});	
		
			// mousemove but just if list bigger than screen
			if (ulWidth > screenWidth) {		
				if ((screenWidth - ulWidth) < 0) {
					var mf = screenWidth / Math.abs(screenWidth - ulWidth);
				} else {
					var mf = screenWidth / screenWidth - ulWidth;
				}
				
				$thumblist.parent().mousemove(function(move){
					$thumblist.css({
						'left': -(move.clientX / mf) + 'px'
					});
				});
			}
			
			// arrow navigation in srclsbimg
			$('div.srclshowbox').mouseenter(function(){				
				$('.back').add('.next').css({display:'block',opacity: 0.0}).animate({opacity:0.2},'fast');
								
				$('div.next').click(function(){
				
					$('.foldbox > .cntholder').html("");
					
					var currPicId = $('div.srclsbimg > img').attr('id').substr(1,$('div.srclsbimg > img').attr('id').length);
					var nextPicId = parseInt(currPicId) + 1;
									
					if ($thumblist.children('li').children('div.thumbwrap').children('img#' + nextPicId).length > 0) {
						$('.srclprojects > ul > li').removeClass('open');
						$thumblist.children('li').children('div.thumbwrap').children('img#' + nextPicId).parent().parent().addClass('open');
					
						var imgSrc = $thumblist.children('li').children('div.thumbwrap').children('img#' + nextPicId).attr('src');
						var imgAlt = $thumblist.children('li').children('div.thumbwrap').children('img#' + nextPicId).attr('alt');
						var imgId = $thumblist.children('li').children('div.thumbwrap').children('img#' + nextPicId).attr('id');
						
						$('.srclsbimg > img').fadeOut('slow');
						setTimeout(function(){
							$('.srclsbimg > img').attr('src', imgSrc);
							$('.srclsbimg > img').attr('alt', imgAlt);
							$('.srclsbimg > img').attr('id', 't' + imgId);
							$('.srclsbimg > img').fadeIn('slow');
						}, 500);
										
						$('.foldbox > .cntholder').html( $('.srclprojects > ul > li.open > .thumbdesc').html() );
					}		
				});
				$('div.back').click(function(){	
					var currPicId = $('div.srclsbimg > img').attr('id').substr(1,$('div.srclsbimg > img').attr('id').length);
					var backPicId = parseInt(currPicId) - 1;
										
					if ($thumblist.children('li').children('div.thumbwrap').children('img#' + backPicId).length > 0) {
						$('.srclprojects > ul > li').removeClass('open');
						$thumblist.children('li').children('div.thumbwrap').children('img#' + backPicId).parent().parent().addClass('open');
						
						var imgSrc = $thumblist.children('li').children('div.thumbwrap').children('img#' + backPicId).attr('src');
						var imgAlt = $thumblist.children('li').children('div.thumbwrap').children('img#' + backPicId).attr('alt');
						var imgId = $thumblist.children('li').children('div.thumbwrap').children('img#' + backPicId).attr('id');
						
						$('.srclsbimg > img').fadeOut('slow');
						setTimeout(function(){
							$('.srclsbimg > img').attr('src', imgSrc);
							$('.srclsbimg > img').attr('alt', imgAlt);
							$('.srclsbimg > img').attr('id', 't' + imgId);
							$('.srclsbimg > img').fadeIn('slow');
						}, 500);
						
						$('.foldbox > .cntholder').html( $('.srclprojects > ul > li.open > .thumbdesc').html() );
					}
				});
				
				$('.back').mouseenter(function(){
					if(!$('.srclprojects > ul > li:eq(1)').hasClass('open'))
						$(this).animate({opacity:1},'fast');
				});
				$('.next').mouseenter(function(){
					if(!$('.srclprojects > ul > li:last').hasClass('open'))
						$(this).animate({opacity:1},'fast');
				});
				$('.back').add('.next').mouseleave(function(){
					$(this).animate({opacity:0.2},'fast');
				});
				
			});
			$('div.srclshowbox').mouseleave(function(){
				$('.back').add('.next').animate({opacity:0.0});
			});
			
			// slide list up and down
			$thumblist.parent().toggle(function(){
					$(this).animate({ top: 0 });
				},function(){
					$(this).animate({ top: -110 });
			});	

			// toggle opacity on element
			$thumblist.children('li').mouseenter(function(){
				
				if($.support.opacity == false) $(this).children('div.thumbwrap').children('img').fadeTo('fast',1.0);
				else $(this).children('div.thumbwrap').fadeTo('fast',1.0);
				
			});
			$thumblist.children('li').mouseleave(function(){
				
				if($.support.opacity == false) $(this).children('div.thumbwrap').children('img').fadeTo('fast',0.4);
				else $(this).children('div.thumbwrap').fadeTo('fast',0.4);
				
			});
		};
	//-----------------------------------------------------------------
	// Team page 	
	//-----------------------------------------------------------------
	if ($("div.teamboxwrap").length > 0) {
		list = $('ul.teambox');
		back = $('div.backsmall');
		next = $('div.nextsmall');
	
		if(list.position().left == 0)
			back.hide();
			
		if(list.children('li').length == 1)
			next.hide();
		
		var listwidth = (968 * list.children('li').length) + 10;
		var maxleft = listwidth - list.children('li').outerWidth() - 10;
				
		list.width( listwidth );	
			
		next.click(function(){
			back.show();				
			if (Math.abs(list.position().left - list.children('li').outerWidth()) == maxleft) 
				next.hide();

			list.animate({
				left: list.position().left - list.children('li').outerWidth()
			});
		});
		back.click(function(){
			next.show();					
			if ( Math.abs( list.position().left + list.children('li').outerWidth() ) == 0) 
				back.hide();

			list.animate({
				left: list.position().left + list.children('li').outerWidth()
			});
		});
	}

	$(".troi").html('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="78" height="160" id="090928_troi_minded_btn" align="middle"><param name="allowScriptAccess" value="sameDomain" /><param name="allowFullScreen" value="false" /><param name="movie" value="fileadmin/templates/img/090928_troi_minded_btn.swf" /><param name="quality" value="high" /><param name="bgcolor" value="#ffffff" /><embed src="fileadmin/templates/img/090928_troi_minded_btn.swf" quality="high" bgcolor="#ffffff" width="78" height="160" name="090928_troi_minded_btn" align="middle" allowScriptAccess="sameDomain" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.adobe.com/go/getflashplayer_de" /></object>');

	$(function() {
		$( ".datepicker" ).datepicker();
	});

	function doneSubmit(){

		$(".buglisting").load('index.php?id=47 .buglisting', function() {
			
			$("#addform").dialog( "close" );	

			$(".newtask > a").bind("click",function(e) {
				e.preventDefault();  
				$("#addform").dialog("open");
		        return false;
		    });
			
		});						
	}

	if($(".newtask").size() > 0){
		// load the addform
		var url = $(".newtask > a").attr("href");
		var dialog = $('<div id="addform" style="display:none;"></div>').appendTo('body');	
		
		//var dialog = $('<iframe id="addform" style="padding: 0px; width:620px;" />').appendTo('body');	
		
		dialog.load(url+' #c47', {}, function(){
			dialog.dialog({
				height: 520,
				width: 620,
				resizable: false,
				modal: true,
				autoOpen: false
			});
			
			$(".datepicker").datepicker();
			$(".submit-it-addform").button();
			
			$('#bt-addform').submit(function(e) {
				e.preventDefault();
				
				SubmitDone = false;
				var options = { 
				    success:    function() { 
						doneSubmit();
					} 
				}; 				
		        $(this).ajaxSubmit(options);
	
		        return false;
		    }); 					
		});				
		// opener
		$(".newtask > a").bind("click",function(e) { 
			e.preventDefault(); 
			$("#addform").dialog("open");
	        return false;
	    });		
	}

	//col over 
	$("table.buglisting > tbody > tr").bind("mouseenter mouseleave", function(e){
		if(e.type == "mouseenter") $("td",this).css({"background-color":"#505050"});
		if(e.type == "mouseleave") $("td",this).css({"background-color":"transparent"});
	});	
});	
