// JavaScript Document
loadOperations = {
	pendingOps: 0,
	data: false,
	trigger: function(incomingData){
			if(incomingData!=false){
				this.data = incomingData;
			}else{
			}
			this.pendingOps --;
			if(this.pendingOps <= 0 && this.data!=false){
				this.redrawPage();
				this.data=false;
			}
	},
	redrawPage: function(){
		data = this.data;
		$("#menuWrap").html($(data).find("#menuWrap").html());
		$(".textWrap .t").html($(data).find(".t").html()).hide();
		
		initContent();
		var faders = $(".tab > a,.tab-act .imageWrap img:first,.tab-act .tab-text").hide();
		$(".t").show();
		window.setTimeout('$(".tab > a,.tab-act .imageWrap img:first,.tab-act .tab-text").fadeIn("slow");',0);
		
	/*
		$(".tab-act .imageWrap img:first,.tab-act .tab-text").hide();
		$(".textWrap .t,").fadeIn("slow",function(){$(".tab-act .imageWrap img:first,.tab-act .tab-text").fadeIn("slow");});
	*/

	}
}

// Loads links dynamical via ajax to give a lite sugar!
$.fn.linkLoader = function(){
	var href = $(this).blur().attr("href");
	// check for external link cause theis links should not handled with ajax
	if (href=='index.php?id=91' || href=='https://www.youtube.com/user/RichterMediaGroup' || href=='https://www.xing.com/companies/richtermediagroupgmbh' || href=='http://de-de.facebook.com/RichterMediaGroup'){document.location(href);return true;}
	$("#nav-box").remove();
	loadOperations.pendingOps = 2;
	$(".imageWrap a img").css({position:"absolute",top:0,left:0});
	$(".tab-act .imageWrap").css({width:"510px",height:"370px"});
	$(".tab-link, .tab-text, .imageWrap *").fadeOut("slow");
	window.setTimeout("loadOperations.trigger(false)",600);//600=slow ,400=normal 200=fast
	//$("#footerWrap").after("<p>Loading!</p>");
	$.get(href,{ajax:1},function(data){loadOperations.trigger(data)});

	
}

$.fn.rmgslide = function(direction){
	var list = $(".tab-act .imageWrap img").get();
	var cur = -1;
	for(i=0;i<list.length;i++){
		if($(list[i]).css("display")=="block"){
			cur = i;
			break;
		}
	}
	// sanity checks 

		
	if(direction=="next" && cur+1<list.length){
		$(list[cur]).fadeOut("slow",function(){$(list[cur+1]).fadeIn("slow");});
		$('.nav-prev').show();
		if(cur+2==list.length){
			$('.nav-next').hide();
		}
	}
	if(direction=="prev" && cur>0){
		$(list[cur]).fadeOut("slow",function(){$(list[cur-1]).fadeIn("slow");});
		$('.nav-next').show();
		if(cur-1==0){
			$('.nav-prev').hide();
		}
	}
}


selectTab = function(tabObject){
	$(".tab-text:visible, .imageWrap img:visible").fadeOut("slow",function(){
		$(".tab").removeClass("tab-act");
		$(tabObject).parent().addClass("tab-act");
		$(".tab-act .tab-text,.tab-act .imageWrap img:first").fadeIn("slow",function(){
			initBoxNav();
		});
		
	});
	
	
}


// Setting up Content Events should be executed after loading of page
initContent = function(){
	$("#menuWrap a, #footerWrap a").unbind().click(function(){$(this).linkLoader();this.blur();return false;});
	$(".textWrap .tab:first").addClass("tab-act");

	$(".textWrap .tab").css('width',(100.0/$(".textWrap .tab").length)+"%");
	

	$(".imageWrap").each(function(){
		$(this).find('a[rel=lightbox-gal]').lightBox({
								fixedNavigation:true,
								overlayBgColor: '#000',
								overlayOpacity: 0.7,
								imageLoading: 'http://www.richtermediagroup.com/fileadmin/template7/images/loading.gif',
								imageBtnClose: 'http://www.richtermediagroup.com/fileadmin/template7/images/lightbox-btn-close.gif',
								imageBtnPrev: 'http://www.richtermediagroup.com/fileadmin/template7/img/left-big.gif',
								imageBtnNext: 'http://www.richtermediagroup.com/fileadmin/template7/img/right-big.gif',
								containerResizeSpeed: 300,
								txtImage: 'Bild',
								txtOf: 'von'
							 });
		$(this).find('a[rel=lightbox-gal]') //.attr('title','link-title')
			.children('img').attr('title','Click to Enlarge')
			//.attr('alt','img-alt');
	});
	
	initBoxNav();
	
	$(".tab-link").each(function(){
		$(this).click(function(){selectTab(this); return false;});
	});
	$(".tab-text,.imageWrap img").hide();
	$(".tab-text:first, .imageWrap img:first").show();
	ieFix();
}

initBoxNav = function(){
	$('#nav-box').remove();
	if($(".imageWrap img").length >1){
		$('.block').append('<div id="nav-box"><a href="#" class="nav-prev"></a><a href="#" class="nav-next"></a></div>');
		$('#nav-box a').css({opacity:0.5}).hover(function(){$(this).animate({opacity:"1.0"},"fast")},function(){$(this).animate({opacity:"0.5"},"fast")});
		$('.nav-next').click(function(){$(this).rmgslide("next");this.blur();return false;});
		$('.nav-prev').click(function(){$(this).rmgslide("prev");this.blur();return false;}).hide();
		if($(".tab-act .imageWrap img").length < 2){
			$('.nav-next').hide();
		}
	}
}

ieFix = function (){
	if (jQuery.browser.msie && parseInt(jQuery.browser.version) < 7) {
		$("#menuWrap").css({width:"900px",margin:"80px 0 30px 0"});
		$("#menuWrap ul").css({margin:0});
		$(".textWrap .tab-text").css({overflow:"hidden"});
	}
}

$(document).ready(function(){
	initContent();
	//ieFix();
});

