var aktivID;
var GET;
var IEold;
var lastID;
var Aheight;
var winh;
var winw;
var ImgRatio;
var cur;
var moreLinkDone = false;

function scrollspeed(mythis){
	var h = jQuery(mythis).height();
	var speed = h*25;
	return speed;
}

function checksize(){

	winh = jQuery(window).height();
	winw = jQuery(window).width();
	var oldfontsize = jQuery("li.page_item a").css("font-size");
	if(!oldfontsize){oldfontsize="48px";}
	var pxstart = oldfontsize.search(/p.+/);
	oldfontsize = oldfontsize.substr(0,pxstart);

	var newfontsize = Math.round((winw*44)/900);
	Aheight = jQuery("#top h2.feed a").height();
	
	var itemlenght = jQuery("li.page_item a").length;
	
	if(newfontsize<14){newfontsize=14;var newlineheight=10;}else if (newfontsize>48){newfontsize=48;newlineheight=63;}else{newlineheight = Math.round((newfontsize*63)/48);}
	
	
	for(num=0;num<itemlenght;num++){
		var ele = jQuery("li.page_item:eq("+num+") a:first");
		var oldwidth = jQuery("li.page_item:eq("+num+") a:first").width();
		ele.width((oldwidth*newfontsize)/oldfontsize);
	}
	
	jQuery("#text").width((jQuery("#content ul li.page-item-"+aktivID+" a").width())+40);
	jQuery("li.page_item a").css("font-size",newfontsize);
	jQuery("li.page_item a").css("line-height",newlineheight+"px");
	
	var mar = newlineheight/10;
	
	jQuery("li.page_item").css("min-height",newlineheight+mar+"px");
	jQuery("#text").css("margin-bottom",mar+"px");
	
	bgsize();
	stylenavi();

	jQuery("#top h2.feed").css("height",Aheight+100);
}
function bgsize(){

	if(IEold){
		var winhIE = jQuery(window).height();
		jQuery("#bg").css("height",""+winhIE+"");
	}else{winhIE = winh;}
	
	var WinRatio = winh/winw;

	if(WinRatio>=ImgRatio){

		jQuery("#bg .bild #dasbild img").height(winhIE);jQuery("#bg .bild #dasbild img").width("auto");
		}else{
		jQuery("#bg .bild #dasbild img").height("auto");jQuery("#bg .bild #dasbild img").width(winw);
		}
}

function stylenavi(){
	jQuery("#content #text .col ul.links li a").css("font-size",15).css("filter","none").css("background","#FFF").css("line-height","18px");
	jQuery("#content #text .col a").css("font-size",12).css("filter","none").css("background","#FFF").css("line-height","18px");
	jQuery("li.page_item div#text .wp-pagenavi a").css("padding","0 4px 0 4px").css("filter","none").css("background","#FFF").css("color","#000");
}

function pagenavi(GET){

	//var myHref = jQuery("a.more-link:first).attr("href");

	var moreemlenght = jQuery("a.more-link").length;
		for(num=0;num<moreemlenght;num++){

			var moreLink = jQuery("a.more-link:eq("+num+")").attr("href");
			jQuery("a.more-link:eq("+num+")").attr("id",""+moreLink+"");
			jQuery("a.more-link:eq("+num+")").attr("href","javascript:void(0);");

	}
				jQuery("a.more-link").mouseup(function(){
					cur = jQuery("span.current").text();
					moreLinkDone = true;
		 			doload(jQuery(this).attr("id"));
				});
	
	var naviitemlenght = jQuery(".wp-pagenavi a").length;
		for(num=0;num<naviitemlenght;num++){
				var elenavi = jQuery(".wp-pagenavi a:eq("+num+")");
				var x = elenavi.attr("href");
				
			//	alert(x);
				
					if(x.indexOf("&paged=")==-1){
						elenavi.attr("id",""+x+"&paged=0");
					}else{
						elenavi.attr("id",""+x+"");
					}
					
				elenavi.attr("href","javascript:void(0);");
		}
				stylenavi();
				
				jQuery("#text div.col div.wp-pagenavi a").mouseup(function(){
		 			doload(jQuery(this).attr("id"));
				});
				
			//zurckbutton
			
			if(moreLinkDone){
			
			var a = lastID.indexOf("?");
			var e = lastID.indexOf("&");	
			e!=-1 ? (backURL = lastID.substring(a,e)) : (backURL = lastID.substring(a));	
			
			jQuery("#text div.col").append("<a href=\"javascript:void(0);\" onclick=\"doload('/"+backURL+"&paged="+cur+"#back');\">zur&uuml;ck zur &Uuml;bersicht</a>");
			moreLinkDone = false;
			cur = false;
			}
			
			
			//interne verlinkung in den posts
			
		var interenlinklenght = jQuery(".entry p a").length;
		for(num=0;num<interenlinklenght;num++){
				var elelink = jQuery(".entry p a:eq("+num+")");
				var x = elelink.attr("href");
				
			
			if((x.indexOf("http://www.mailath.at/?")!=-1)||(x.indexOf("http://mailath.at/?")!=-1)){
			
					// alert(x);
					//if(x.indexOf("&paged=")==-1){
					//	elenavi.attr("id",""+x+"&paged=0");
					//}else{
						elelink.attr("id",""+x+"#back");
					//}
				
				elelink.attr("href","javascript:void(0);");
			}
		}
			
			jQuery("#text div.col div.entry p a").mouseup(function(){
					if(!cur){
						cur = jQuery("span.current").text();
					//alert(cur);
						moreLinkDone = true;}
		 			doload(jQuery(this).attr("id"));
				});
		
		
			//	stylenavi();
				
			//	jQuery("#text div.col div.entry p a").mouseup(function(){
		 	//		doload(jQuery(this).attr("id"));
			//	});
			
}

function start(){
	jQuery("#top h1 a").attr("id","?s=0");
	jQuery("#top h1 a").attr("href","javascript:void(0);");
	
	if(IEold){
		jQuery("#top h1 a img").attr("src","/wp-content/themes/mailath/lib/leer.gif");
	}
	
	var itemlenght = jQuery("li.page_item a").length;
	var winw = jQuery(window).width();
	
	for(num=0;num<itemlenght;num++){
		var ele = jQuery("li.page_item:eq("+num+") a:first");
		var x = ele.attr("href");
			ele.attr("id",""+x+"");
			ele.attr("href","javascript:void(0);");
			ele.append("<img src=\"http://www.mailath.at/wp-content/themes/mailath/pic/ajax-loader.gif\" alt=\"\" id=\"loaderimg\" />");

			
			var realwidth = ele.width();
			var widthRatio = winw/realwidth;
			
	
			if(realwidth <= winw*0.65){
				ele.width(realwidth+(realwidth*(widthRatio/3)))
			}	
			if(ele.width()>winw*0.75){ele.width(winw*0.75)}
		}
					
	ImgRatio = jQuery("#bg .bild #dasbild img").height()/jQuery("#bg .bild #dasbild img").width();
	
		checksize();
		jQuery("#load").fadeOut("slow");
}

function NachOben(GET) {
  var y = jQuery(".page-item-"+GET).offset();
  window.scrollTo(0,y.top-10)
}


function loadpage(myID){

	//if(IEold){
	//	window.scrollTo(0,0)
	//}

	var a = myID.indexOf("?");
	var e = myID.indexOf("&");	
	e!=-1 ? (GET = myID.substring(a,e)) : (GET = myID.substring(a));	
	var a = GET.indexOf("=");
	var e = GET.indexOf("&");
	e!=-1 ? (GET = GET.substring(a+1,e)) : (GET = GET.substring(a+1));
	
	lastID = myID;
	aktivID = GET;
	
	
			var MSIEget = Math.random()*999;
			myID = myID+"&msieget="+MSIEget;
		
		
			if(IEold){
				jQuery("li.page-item-"+GET+" a:first").attr("id","?s=0").css("filter","none").css("background","#FFF");
			}else{
				jQuery("li.page-item-"+GET+" a:first").attr("id","?s=0").css("background","#FFF");
			}
			jQuery("li.page-item-"+GET+" a:first img#loaderimg").css("display","inline");

			jQuery("#text").load(""+myID+" #text .col","",function(){	

			 	jQuery("#alt").clone(true).attr("id","neu").css("display","none").appendTo("#bg").insertAfter("#alt");
			 
				jQuery("#neu").load(""+myID+" #bg #alt #dasbild",function(){
					bgsize();
						jQuery("#neu").fadeIn("slow", function(){
							jQuery("#alt").remove();
							jQuery("#neu").attr("id","alt");
						});
				});
			
				if(jQuery("#text object").length > 0){
					jQuery("#text object").removeAttr("width").removeAttr("height");
					jQuery("#text embed").removeAttr("width").removeAttr("height");
					jQuery("#text object:first").wrap("<div id='vid'></div>");
					jQuery("#text object:gt(0)").wrap("<div class='vidfollow'></div>");
					jQuery("div.vidfollow").appendTo("#vid");
				}else{jQuery("#text .col").css("padding-bottom","10px");}
			
				jQuery("#text").appendTo("#content ul li.page-item-"+GET);
				
				if(IEold){
					jQuery("#text").width((jQuery("#content ul li.page-item-"+GET+" a").width())+60);
				}else{
					jQuery("#text").width((jQuery("#content ul li.page-item-"+GET+" a").width())+40);
				}
				
					var formaction = jQuery("form.wpcf7-form").attr("action");
					jQuery("form.wpcf7-form").attr("action","javascript:dopost('"+formaction+"')");
				
				shutterOnload();
				pagenavi(GET);
				stylenavi();
			
				if(jQuery("#text div.col ul.links li")){
					jQuery("ul.links").prependTo("#text div.col p:first");
				}
				jQuery("#text").slideDown(""+scrollspeed(jQuery(this))+"",function(){NachOben(GET);
					jQuery("#vid").fadeIn("slow");
					jQuery("li.page-item-"+GET+" a:first img#loaderimg").css("display","none")
				});
			
		});
		
}

function dopost(URL){
	jQuery.post(""+URL+"",jQuery("form.wpcf7-form").serialize(), function(daten){
	var datastring = new String(daten);
	if(datastring.indexOf("<form")!=-1){
		var formbeginn = datastring.indexOf("<form");
		var formende = datastring.indexOf("</form>");
		var formcode = datastring.substring(formbeginn, formende+7);
		jQuery("div .wpcf7").empty();
		jQuery("div .wpcf7").append(formcode);
		var formaction = jQuery("form.wpcf7-form").attr("action");
		jQuery("form.wpcf7-form").attr("action","javascript:dopost('"+formaction+"')");
	}
},"html");

}

function doload(myID){
	// Projektseite
	if(myID.indexOf("?page_id=693") != -1) {
		window.open('projekte');
		return;
	}

	if((aktivID)&&(myID.indexOf("?s=0")==-1)&&(myID.indexOf("&paged=")==-1)&&(myID.indexOf("#more-")==-1)&&(myID.indexOf("#back")==-1)&&(myID.indexOf("#wpcf7")==-1)){
		var altID = aktivID;
		
		jQuery("li.page-item-"+altID+" a").attr("id",""+lastID+"");
		jQuery("div.vid").fadeOut("slow");
			jQuery("#text").slideUp(""+scrollspeed(jQuery(this))+"", function(){
					
				
				if(IEold){
					jQuery("li.page-item-"+altID+" a").css("filter","progid:DXImageTransform.Microsoft.Alpha(opacity='70', style='0');");
					}else{
					jQuery("li.page-item-"+altID+" a").css("background","url('wp-content/themes/mailath/pic/navi_bg.png') top left transparent");
				}
					loadpage(myID);
				});

	}else if((myID.indexOf("?s=0")!=-1)&&(myID.indexOf("&paged=")==-1)&&(myID.indexOf("#more-")==-1)&&(myID.indexOf("#back")==-1)&&(myID.indexOf("#wpcf7")==-1)){

			var altID = aktivID;
			jQuery("li.page-item-"+altID+" a:first").attr("id",""+lastID+"");
			jQuery("div.vid").fadeOut("slow");
			jQuery("#text").slideUp(""+scrollspeed(jQuery(this))+"", function(){
				if(IEold){
					window.scrollTo(0,0)
					jQuery("li.page-item-"+altID+" a").css("filter","progid:DXImageTransform.Microsoft.Alpha(opacity='70', style='0');");
					}else{
					jQuery("li.page-item-"+altID+" a").css("background","url('wp-content/themes/mailath/pic/navi_bg.png') top left transparent");
					
				}bgsize();
								});

	}else if(
				(
					(myID.indexOf("&paged=")!=-1)||(myID.indexOf("#more-")!=-1)||(myID.indexOf("#back")!=-1)
				)
				&&(myID.indexOf("#wpcf7")==-1)
			){	
				jQuery("#text").load(""+myID+" #text div.col", function(){pagenavi();});
	}else if(myID.indexOf("#wpcf7")!=-1){
		//alert(myID);
		dopost(myID);//return false;
	}else{
			loadpage(myID);
	}	
}


