$(function(){
	$(window).resize(onResize);
	
	if($(".gal-item").length>0) v=new Galery();
	//$(".gal-item").wrapAll("<div class=\"gallery\"/>");
	onResize();
	$(window).load(function(){
		bg();
		$(window).resize(function(){
			bg();
			onResize();
		});
	});
	if($(".slide img").length>1 ) new Slider(".slide",".slide img",5000,5000);
	try{
	mapsload(mapsLoader[0],mapsLoader[1]); 
	r=true;}catch(e){};
});

function mapsload(locations,locationsAll) 
{
	$(".content").append($("<div id=\"maps\"/>").css({height: 300,width: 564}));
	var div=document.getElementById("maps");
	if (GBrowserIsCompatible())
  {
	var map = new GMap2(div);
    map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl());
    var point = new GLatLng(locationsAll[0],locationsAll[1]);
    map.setCenter(point, locationsAll[2],G_NORMAL_MAP);//G_HYBRID_MAP
    click1=new Array(locations.length);
    for(i=0;i<locations.length;i++) map.addOverlay(createMarker(i,locations[i],locationsAll[2],map));
  }
}

function createMarker(nr,locArray,zoomDec,map)
{
	var point = new GLatLng(locArray[0],locArray[1]);
	var marker = new GMarker(point,{title: locArray[3]});
	click1[nr]=0;
	GEvent.addListener(marker, "click",function(a) {
		zoom=(click1[nr]==0)?locArray[2]:zoomDec;
		map.setCenter(point,zoom,G_NORMAL_MAP);//G_HYBRID_MAP
		click1[nr]++;
		click1[nr]=(click1[nr]==2)?0:click1[nr];
	});	
	return marker;
}

function bg()
{
	$(".bgMain").height("auto");
	if($(".bgMain").height()<$(window).height()-130) $(".bgMain").height($(window).height()-130);
	//$(".shadowBottom").height($(".mainBG").height()-563);
}

function onResize()
{
	var w=$(window).width();
	if(w<1238)
	{
		var x=Math.round((1238-w)/2);
		var width="auto";
		var css={};
		css.position='relative';
		var width=1238;
		if(x>127)  x=127;
		css.width=1238-x;
		css.overflowX='hidden';
		css.left=-x;
		$(".bgDiv").css(css);
	}
}

Galery=function(options)
{
	var defaults={
		OVERLAY_ID: "overLayMask"
	}
	
	this.setup = $.extend({}, defaults, options); 
	
	this.list=new Array();
	this.Biglist=new Array();
	
	
	this.show=function(ind,dif){
		var t=this;
		this.curInd=ind;
		if(!this.overlay.isOpened()) this.overlay.show(this.createNav());
		var cur=this.preload(ind);
		
		var x=$("#showGalItem");
		x.css({height: x.height(),width: x.width()}).empty();
		this.updateNav();
		imgOnLoad(cur,function(cached){
			var d=getDimension(cur);
			if(cached!==true) cur.hide();
			x.empty().css({height: d.h,width: d.w}).append(cur.fadeIn("fast"));
			t.overlay.repos();
		});
		
		if(this.curInd+1<this.list.length && this.curInd-1>=0) this.preload(ind+dif);
	}
	
	this.preload=function(ind)
	{
		if(this.Biglist[ind]===false) this.Biglist[ind]=$("<img src=\""+this.list[ind].children("span").text()+"\" />").load(function(){$(this).data("loaded",true);});
		return $(this.Biglist[ind]);
	}
	
	this.createNav=function(){
		var t=this;
		return $("<table/>").addClass("galNav").append(
			$("<tr />").append(
				$('<td colspan="3"></td>').append($(".content h1").clone())
			),
			$('<tr height="100px"/>').append(
				$('<td colspan="3" id="showGalItem"></td>')
			),
			$("<tr id=\"galNav\"/>").append(
				$('<td class="before">&laquo;&nbsp;zurück</td>').click(function(){t.show(t.curInd-1,-1)}),
				$('<td class="close">schließen</td>').click(function(){t.overlay.close()}).css({'text-align': 'center'}),
				$('<td class="after">weiter&nbsp;&raquo;</td>').click(function(){t.show(t.curInd+1,+1)}).css({'text-align': 'right'})
			)
		);
	}
	
	this.updateNav=function()
	{
		$('td.before').css({'visibility':this.curInd-1>=0?"visible":"hidden"});
		$('td.after').css({'visibility':this.curInd+1<this.list.length?"visible":"hidden"});
	}
	
	this.init=function(){
		var t=this;
		new ImageLoader({IMGS: $(".gal-item img")});
		$(".gal-item").each(function(ind,ite){
			var id=$(ite).prop("id").substr(11);
			$(this).click(function(e){t.show(ind);});
			t.Biglist.add(false);
			t.list.add($("#bigGalImg"+id));
		});
		this.overlay=new Overlay();
	}
	
	this.init();
}

ImageLoader=function(options)
{
	var defaults={
		WRAPPER_CLASS: "none",
		IMGS: false,
		LOADER_GIF: 
		{
			src: $("base").prop("href")+"assets/tpl/load.gif",
			width: 32,
			height: 32,
			alt: "Lädt...",
			className: "loadInfo"
		}
	}
	
	this.setup = $.extend({}, defaults, options); 
	
	this.init=function()
	{
		var t=this;
		var loader = $("<img class=\""+t.setup.LOADER_GIF.className+"\" src=\""+t.setup.LOADER_GIF.src+"\" alt=\""+t.setup.LOADER_GIF.alt+"\" />");
		
		if(t.setup.IMGS!==false) $.each(t.setup.IMGS,function(ind,ite){
			$(ite).wrap("<div class=\""+t.setup.WRAPPER_CLASS+"\" />");
			$('.'+t.setup.WRAPPER_CLASS).css({position: "relative"});
			loader.css({
				top: Math.round(($(ite).height()-t.setup.LOADER_GIF.height)/2),
				left: Math.round(($(ite).width()-t.setup.LOADER_GIF.width)/2),
				position: "absolute",
				width: t.setup.LOADER_GIF.width,
				height: t.setup.LOADER_GIF.height
			});
		});
		
		$('.'+t.setup.WRAPPER_CLASS).append(loader);
		$('.'+t.setup.WRAPPER_CLASS+' img').each(function(ind,ite){
			if($(this).hasClass(t.setup.LOADER_GIF.className)) return;
			$(this).hide();
			imgOnLoad(this,function(){t.loaded(ite)});
		});
	}
	
	this.loaded=function(ite)
	{
		$(ite).parent().children('.'+this.setup.LOADER_GIF.className).hide();//.fadeOut('fast');
		$(ite).fadeIn('slow');
	}
	
	this.init();
}

var imgOnLoad=function(img,fnc)
{
	if($(img)[0].complete || $(img)[0].readyState === 4) {fnc(true);return true;}
	else {$(img).load(function(){fnc(false)});return false;}
}

var getDimension=function(div)
{
	div=$(div);
	var d;
	var g=function(a){
		var z={
			wow:div.outerWidth(),
			hoh:div.outerHeight(),
			wiw:div.innerWidth(),
			hih:div.innerHeight(),
			w:div.width(),
			h:div.height(),
			append: a
		}
		return z;
	}
	if(div.parent().length==0)
	{
		var s=$("<div/>").css({height: 0,width: 0,overflow: "hidden"}).append(div);
		$("body").append(s);
		d=g(true);
		s.detach();
		return d;
	}
	else return g(false);
}

var Overlay=function(options)
{
	var defaults={
		maskCSS: {
			position: 'fixed',
			top: 0,left: 0,
			width: '100%',
			height: '100%',
			'background-color': '#000',
			opacity:0.5,
			'z-index': 10000
		},
		overlayCSS: {
			position: 'absolute',
			top: 0,left: 0,
			'z-index': 10001,
			overflow: 'hidden'
		},
		oclass: "overlayInput",
		closeButton: false,
		closeButtonClass: "overlayClose",
		minWidth: 600,
		minHeight: 400
	}
	this.setup = $.extend({}, defaults, options); 
	
	this.isOpened=function()
	{
		return this.isOpen;
	}
	
	this.show=function(div)
	{
		this.innerDIV=div;
		this.repos();
		if(this.isOpened()) this.overlay.empty();
		else 
		{
			this.isOpen=true;
			this.overlayMask.fadeIn("fast");
			$("body").append(this.overlayMask.fadeIn("fast"),this.overlay.fadeIn("slow"));
		}
		var t=this;
		if(this.setup.closeButton===true) this.overlay.append($("<span class=\""+this.setup.closeButtonClass+"\"/>").text("X").click(function(e){e.stopPropagation();t.close()}));
		this.overlay.append(this.innerDIV);
		this.repos();
		this.isOpen=true;
	}
	
	this.repos=function()
	{
		var dd=getDimension(this.innerDIV);
		var w=$(window);
		if(this.innerDIV.parent().length===0) this.overlay.append(this.innerDIV);
		this.overlay.css({top: Math.round((w.height()-dd.hoh)/2)+w.scrollTop(),left: Math.round((w.width()-dd.wow)/2)+w.scrollLeft(),'min-width': this.setup.minWidth,'min-height': this.setup.minHeight});
	}
	
	this.close=function()
	{
		if(!this.isOpened()) return;
		this.overlay.fadeOut("slow",function(){$(t.innerDIV).detach();});
		this.overlayMask.fadeOut("fast",function(){$(t.innerDIV).detach();});
		this.isOpen=false;
	}
	
	this.overlayMask=$("<div/>").css(this.setup.maskCSS).hide();
	this.overlay=$("<div/>").css(this.setup.overlayCSS).addClass(this.setup.oclass).hide();
	this.isOpen=false;
	var t=this;
}

var Slider=function(wrapper,els,pause,fadeSpeed)
{
	this.wrapper=wrapper;
	this.els=els;
	this.pause=pause;
	this.fadeSpeed=fadeSpeed;
	
	this.start=function()
	{
		var t=new Array(this,this.pause,this.fadeSpeed);
		$(this.els).last().fadeOut(t[2],function(){
			var p=$(this).parent();
			var el=$(this).detach();
			p.prepend(el);
			el.show();
			t[0].start.mdelay(t[1],t[0]);
		});
	}
	
	//$(this.wrapper).css({position: "relative"});
	$(this.els).css({position: "absolute"});
	$(window).load(jQuery.proxy(function(){this.start.mdelay(this.pause,this);},this));
}

Function.prototype.mdelay=function(t,bind,d){setTimeout(jQuery.proxy(this,bind),t,d);};
Array.prototype.add=function(x){this[this.length]=x};
