// JavaScript Document
var timeout = false;
var runningfade = false;
var running = false;
var opened = false;
var currentImg = "";
var originalText;
var speed = 250;
var fadeInnerSpeed = 0;
var openerHeight = 0;


$(function(){
	openerHeight = parseInt(($("#projectsmenu li").length)/4)*33;
	$("#submenu").hover(function(){
			if(timeout){clearTimeout(timeout);}
			$(this).find("ul").slideDown("fast");			   
	},	function(){
			if(timeout){clearTimeout(timeout);}
			timeout = setTimeout(function(){$("#submenu ul").slideUp("fast");	},300);	
	});	
	$("#projectsopener").hover(function(){
			if(timeout){clearTimeout(timeout);}
			$("#projectsmenu").slideDown("fast");
			$("#projectsopener").css("color","#FFF");
			$("#projectsopener").css("height","auto");
	},	function(){});
	$("#projectsopener").css("height",openerHeight);
	$("#projectsmenu").hover(function(){},	function(){
			if(timeout){clearTimeout(timeout);}
			timeout = setTimeout(function(){$("#projectsmenu").slideUp("fast", function(){
				$("#projectsopener").css("color","#7a7a7e");
				$("#projectsopener").css("height",openerHeight);
			}); 		},300);	
	});	
	if(window.name=="CoellnFullscreen") $("#fullscreenbutton img").attr("src","/images/arrange.gif");
});


/***************************     Standard Functions    ********************************/
function back(){
	
		if(window.location.href.indexOf("#")>0){
			window.history.go(-2);
		}else{
			
			//alert(window.history.length);
			//alert(window.history[52].href);
			window.history.go(-1);
			//if (window.history.length = 0){
			//	window.location.href ="http://www.coellncompany.de/"
			//}
		}
		
}

function hoverInInnerImg(obj, txt){
	if (runningfade==false && opened == true )	{
			var src = $(obj).attr("href");
			if (src != currentImg){
				runningfade=true;
				var time = speed;
				var i = new Image();
				i.onload = function(){
					var w = this.width;
					var h = this.height;
					if(txt=="")txt=originalText;
					$("#subtitle").html(txt);
					$("#frontimage img").attr("src",src);
					$("#frontimage").add("#frontimage img").fadeIn(fadeInnerSpeed,function(){runningfade=false; currentImg=src; });
					$("#frontimage img").css({width: w, height:h, marginLeft: ((-0.5)*w)-1,marginTop: ((-0.5)*h)-1, display:"block"});
				}
				if ($("#frontimage img").is(":visible") && fadeInnerSpeed>0){
					$("#frontimage img").fadeOut(fadeInnerSpeed, function(){								  
						i.src=src;	
					});	
				}else {
					i.src=src;	
				}
			}
		}	
}

function hoveroutInnerImgs(txt){
	//if (runningfade==true)	$("#frontimage").add("#frontimage img").stop();
	setTimeout(function(){
		runningfade=true;
		$("#subtitle").html(txt);
		$("#frontimage").add("#frontimage img").fadeOut(fadeInnerSpeed,function(){runningfade=false; currentImg="";});
	},speed*0.5);
}

function switchFS(){
	if(window.name=="CoellnFullscreen"){
		window.opener.document.location.href = document.location.href;
		self.close();
	}else {
		if ($.browser.msie) {
			window.open(document.location.href,"CoellnFullscreen","fullscreen=yes");
		} else {
			window.open(document.location.href,"CoellnFullscreen","resizable=yes, location=no, menubar=no, toolbar=no, width="+screen.width+", height="+screen.height);
		}
	}
}

$.fx.step.scrollBoth = function(fx){
    if (!fx.state) {
        var o = fx.options;
		fx.innerElem = $("#center");
		fx.outerElem = $("#centerblock");
        fx.start = 157;
		fx.end = fx.options.curAnim.scrollBoth*157;
		fx.top = (fx.options.curAnim.scrollBoth==1) ? 211 : 54;
		fx.innerTop = (fx.options.curAnim.scrollBoth==1) ? (-157) : 0;
    }
	var step = parseInt((fx.pos*fx.end));
	fx.outerElem.css("marginTop",fx.top-step);
	fx.innerElem.css("marginTop",(fx.innerTop+step));
};

/***********************     BBCarousel by Beyerdesign˛    ****************************/
/***********************  a lightweight content carousel   ****************************/

$.fn.extend({
	bbCarousel: function(options){
		
		var me = this;
		var o = $.extend({}, $.fn.bbCarousel.options, options);
		var now = new Date();
		var items;
		var tooltipdiv;
		
		
		init();
		preload();
		
		function init(){
			me.content = $(me);
			me.pageX = 0;
			me.pageY = 0;
	
			me.margin=-400;
			items = me.content.find(o.items);
			me.titles = new Array();
			items.each(function(){
				me.titles[me.titles.length] = me.content.find("a").attr("title");				
			});
			var sliderx = o.itemSize*(items.length);
			me.slidersize = {left:((-1)*sliderx), right:(-1)*sliderx};
			me.content.css({width:(2*sliderx)});
			me.content.append(me.content.html());
			items = me.content.find(o.items);
			me.step=0;
			me.pauser = false;
			me.easer = false;
			me.eventOffset = parseInt(($("body").width()-800)/2)
			me.easestep=0;
			me.x = 0;
			for(var a=0; a<items.length; a++){
				items.eq(a).attr("carouselID",a)
			}
			me.tooltippos = false;
			
		}
		
		me.titlepic = false;
		
		
		me.starttimertowait = 200;
		
		function preload(){
			
			var timeToWait = 0;
			
			if(getPreloadStatus()==false || window.name==""){
				
				if (window.name==""){
					
					me.titlepic = true; 
					
					me.content.parent().prepend("<div id='picoverlay' style='position:absolute; width:798px; height:133px; border:1px #7c7c7a solid; border-width:1px 0px 1px 0px; background:#fff; text-align:center; padding-top:0px; z-index:500 '><img src='/pics/title.jpg' style='display:block; cursor:pointer' alt='' /></div>");
					$("#picoverlay img").click(function(){ me.titlepic = false; me.hideTitle();});
					window.name	="coelln_company";	
					me.titlepic = true;
					me.starttimertowait = 7000;
					
				} else {
					me.content.parent().prepend("<div id='picoverlay' style='position:absolute; width:798px; height:73px; border:1px #7c7c7a solid; border-width:1px 0px 1px 0px; background:#fff; text-align:center; padding-top:60px; z-index:500 '><div style='width:240px; margin:0 auto; text-align:center; text-transform:lowercase; color:#9c9c9a;'>Bilder werden geladen...<img src='/images/ajax-loader.gif' style='display:block;' alt='' /></div></div>");
				}
				startIfReady();
			} else {
				setPos(o.startpos);
				startIntervals();
			}
			
		}
		
		
		function getPreloadStatus(){
			var status = true;
			for (i = 0; i < document.images.length; ++i) {
				if (document.images[i].complete == false) status = false;
			}
			return (status);
		}
	
		
		me.starttimer = false;
		
		
		function startIfReady(){
			if (getPreloadStatus()==true){
				var timeToWait = (me.titlepic==true) ? Math.max(1000,7500-(new Date()-now)) : me.starttimertowait;
				setPos(o.startpos);
				me.starttimer = setTimeout(startFadeOut,timeToWait) ;
			} else {
				setTimeout(	startIfReady, me.starttimertowait);
			}
		
		}
	
	
		function startFadeOut(){
			$('#picoverlay').fadeOut("slow",function(){
				$('#picoverlay').remove();
				startIntervals();
			}); 	 
					
		}
		
		function startIntervals(){
				var delay = setTimeout(	function(){ 
					var interval = setInterval(margin,o.speed);
					if (me.step==0)	me.step=o.autodirection;	
				},o.autostartdelay) ;
		}
		
		this.pause = function(){
			me.step = 0;
		}
		this.restart = function(){
			restart();
		}
		
		this.hideTitle = function(){
		 	me.titlepic = false;
			me.starttimertowait = 50;
			clearTimeout(me.starttimer);
			startIfReady();
		}
		
		
		
		this.getPos = function(){
			var pos= 0;
			pos = (-1)*Math.round((me.margin - 400) / o.itemSize);
			return pos;
		}
		this.setPos = function(pos){
			setPos(pos);
		}
		function setPos(pos){
			me.margin = (-1)*((pos *o.itemSize)-400)-70;
			me.content.css({marginLeft:me.margin});
			margin();
		}
		
		
		$(this).parent().hover(function(){
			clearTimeout(me.easer);		
			clearTimeout(me.pauser);		
		},function(){
			clearTimeout(me.pauser);
			me.easestep=Math.round(me.step);
			me.easer = setTimeout(ease,20);
		});
				
		$(this).parent().mousemove(function(e){
			var x = e.pageX - me.eventOffset ;
			me.pageX = x;
			x=((x-404)/34);
			
			me.x = (x>0) ? Math.max(0,x-2): Math.min(0,x+2);
			clearTimeout(me.pauser);							  
			me.step=-me.x;
		});
		
		function ease(){
			if(me.easestep!=0){
				(me.easestep>0) ? me.easestep-=1 : me.easestep+=1;
				me.step=me.easestep;
				setTimeout(ease,20);
			} else {
				restart();
			}
		}
		
		function restart(){
			clearTimeout(me.pauser);
			me.step=0;
			me.pauser = setTimeout(function(){me.step=o.autodirection;},1000)
		}
				
		var constantForMargin1 = me.pageX + (o.itemSize*0.5);
		var constantForMargin2 = items.length*0.5;
		
		function margin(){
			me.margin += me.step;
			me.margin = (me.margin>=0) ? me.margin + me.slidersize.left : me.margin % me.slidersize.left;
			me.content.css({marginLeft:Math.round(me.margin)});
			var tooltipper = (-1)*Math.round((me.margin - constantForMargin1) / o.itemSize) % constantForMargin2;
			if (me.tooltippos!=tooltipper) {$("#tooltip h3").html(me.titles[tooltipper]);me.tooltippos=tooltipper;}
		}
		return (this);
	}
})
$.fn.bbCarousel.options={
	speed: 20, // timeout interval	
	itemSize:145,
	items : "li",
	autodirection:(-0.5),
	autostartdelay : 500,
	startpos:0
};

