var bodyclass = "";	
var frischfilm_navpage=1;
var frischfilm_pages=1;



$(document).ready(function()
{	
	


	// fix for IE to stop the subnavigation "jumping" when you have already paged 
	if (document.attachEvent) {
		$('#subnavigation li').css('visibility','visible');
	}
	
	if (	GetCookie('frischfilm_navpage')!="" 
			&& GetCookie('frischfilm_navpage')!=null
			&& GetCookie('frischfilm_pages')!="" 
			&& GetCookie('frischfilm_pages')!=null
			&& GetCookie('mLeft')!="" 
			&& GetCookie('mLeft')!=null
			)
	{	frischfilm_navpage = GetCookie('frischfilm_navpage');
		frischfilm_pages = GetCookie('frischfilm_pages');
		$('#subnavigationscroller').css('marginLeft',GetCookie('mLeft')+'px');
		frischfilm_calcnav();
		
		SetCookie ("mLeft", '',null,'/');
		SetCookie ("frischfilm_navpage", '',null,'/');
		SetCookie ("frischfilm_pages", '',null,'/');
	}
	

	//only fade in the subnavi if we changed the main category
	//otherwise just set the opacity to 100;
	$("#subnavigation li").css('opacity',100);
	
	if (GetCookie("fadeSubNavi")=="no")
	{	//$("#subnavigation li").css('opacity',100);
		DeleteCookie ("fadeSubNavi");
	}
	else
	{	//$("#subnavigation li").css('opacity',0);
		//$("#subnavigation li").animate(
		//{	opacity: 1
		//}, 1000);
	}
	
	setTimeout("frischfilm_fadeInContentArea('donothing')",1000);
	

	$("#maincontent *").unbind();
	$("#navigation *").unbind();
	$("#subnavigation *").unbind();
	
	
	
	
	var str;
	str = '<div id="controlbar" class="highslide-overlay controlbar">';
	str +='<a href="#" class="previous" onclick="return hs.previous(this)" title="">back</a>';
	str +='<a href="#" class="next" onclick="return hs.next(this)" title="">next</a>';
	str +='<a href="#" class="highslide-move" onclick="return false" title=""></a>';
	str +='<a href="#" class="close" onclick="return hs.close(this)" title="">close</a>';
	str +='</div>';
	
	
	
	$('body').append(str);
	
	// Calculate Nav Paging
	
	mLeft = $('#subnavigationscroller').css('marginLeft');
	mLeft = parseInt(mLeft);
	if (mLeft>=0)
	{	frischfilm_calcnav();
	}
	
	
	// External links - new window
	$(".extern,.reflinks a").mouseover(function ()
	{	$(this).attr('target','_blank');
	
	});
	
	
	// Navi Links
	$("#navigation a").click(function ()
	{	targeturl = this.getAttribute('href');
		
		DeleteCookie ("mLeft",'/');
		DeleteCookie ("frischfilm_navpage",'/');
		DeleteCookie ("frischfilm_pages",'/');
		
		if (this.getAttribute('rel')!=null && this.getAttribute('rel')!="" && this.getAttribute('rel')!="null")
		{	var redirectURL = this.getAttribute('rel');
			redirectURL = redirectURL.replace(/weiterleitung/, "");
			if (redirectURL!="" && redirectURL!="0" && redirectURL!="null" && redirectURL!=null)
			{	targeturl = "/index.php?article_id="+redirectURL;
				
			}
		}
		
		frischfilm_setTitle(targeturl);
				
		frischfilm_fadeOutContentArea();
		
		//setTimeout('frischfilm_fadeInContentArea("'+targeturl+'")',500);
			
		this.parentNode.setAttribute('id','activeLINK');
		$("#navigation li").removeClass();
		$("#activeLINK").addClass('active');
		this.parentNode.setAttribute('id','');	
		this.blur();
	
	
		$("#subnavigation li").animate(
		{	opacity: 0
		}, 500 );
		/*
		$.post(targeturl, { getID: "true", ajax: "true" },
		function(data)
		{	bodyclass = data;
		
			$.post(targeturl, { getNav: "2", ajax: "true" },
			function(data)
			{	$("body").removeClass();
				$("body").addClass(bodyclass);
				$("#subnavigation").html(data);
							
				setTimeout(function() {
					$("#subnavigation li").css('opacity',0);
					
					$("#subnavigation li").removeClass();
					
					$("#subnavigation li").animate(
					{	opacity: 1
					}, 500 );
					
					
					$("a.s"+redirectURL).parent().addClass('active');
				},10);
				
			});
		
		});*/
		
		
		
		//return false;
	});
	
	
	
	// Subnavi Links
	$("#subnavigation a").click(function ()
	{	targeturl = this.getAttribute('href');
		//frischfilm_setTitle(targeturl);
		
		
		this.parentNode.setAttribute('id','activeSUBLINK');
		$("#subnavigation li").removeClass();
		$("#activeSUBLINK").addClass('active');
		this.parentNode.setAttribute('id','');	
		this.blur();
		
		frischfilm_fadeOutContentArea();		


		var expdate = new Date ();
		FixCookieDate (expdate); // Correct for Mac date bug - call only once for given Date object!
		expdate.setTime (expdate.getTime() + (24 * 60 * 60 * 1000)); // 24 hrs from now 
		SetCookie ("fadeSubNavi", "no",expdate);

		mLeft = parseInt($('#subnavigationscroller').css('marginLeft'));
		SetCookie ("mLeft", mLeft,expdate,'/');
		SetCookie ("frischfilm_navpage", frischfilm_navpage,expdate,'/');
		SetCookie ("frischfilm_pages", frischfilm_pages,expdate,'/');

		
		//setTimeout('frischfilm_fadeInContentArea("'+targeturl+'",'+frischfilm_navpage+','+frischfilm_pages+')',500);
		
		//setTimeout(function() {	window.location.href=targeturl;	},	1	);
		//return false;
	});

});



function frischfilm_setTitle(targeturl)
{	$.post(targeturl, { getTitle: "true", ajax: "true" },
	function(data)
	{	if (data!="")
		{	document.title = data;
		}
	});
}

function frischfilm_fadeOutContentArea()
{	// fix flash restart in FF2
	if ($.browser.mozilla && jQuery.browser.version.substr(0,3)=="1.8") {
		$("#maincontent iframe").css('visibility','hidden');
	}


	$("#maincontent").animate(
	{	opacity: 0
	}, 500 );
	setTimeout( function()
		{	$("#content").addClass('ajax');
		},500);
}



function frischfilm_fadeInContentArea(targeturl,navpage,pages)
{	/*
	if($.browser.msie)
	{	x = parseInt($.browser.version);
		if (x<7)
		{	//window.location = targeturl;
			//return true;
		}
	}


	$.ajax
	({	type: "POST",
		url: targeturl,
		data: "getArticle=true&ajax=true",
		dataType: "html",
		success: function(msg)
		{	$("#content").removeClass('ajax');
					
			
			$("#maincontent").html(msg);
			
			
			$("#maincontent").animate(
			{	opacity: 1
			}, 500 );
			
			frischfilm_reloadscripts(navpage,pages);
			
		}
 	});*/
 	
 	x = $('.anfahrt').length;
 	if (x>0 && document.attachEvent)
 	{	var x = document.getElementById('maincontent');
 		
		x.style.filter="";
 		
 		
	}
	else
	{	
		$("#maincontent").css('opacity',0);
	
		// fix flash restart in FF2
		if ($.browser.mozilla && jQuery.browser.version.substr(0,3)=="1.8") {
  			$("#maincontent iframe").css('visibility','hidden');
  			setTimeout(function(){
  				$("#maincontent iframe").css('visibility','visible');
  			},1000);
  			
		}

		
	
		$("#maincontent").animate(
		{	opacity: 1
		}, 1000 );
		
		
		
		
		
	}
	

	
}


function frischfilm_reloadscripts(navpage,page)
{	$("#maincontent *").unbind();
	$("#subnavigation *").unbind();
	$("#navigation *").unbind();
	
	
	
	if (navpage>0 && pages>0)
	{	$.getScript("/layout/js/init.js",function()
		{	frischfilm_navpage = navpage;
			frischfilm_pages = pages;
						
			$.getScript("/layout/js/validate.js");
			
			// remove the registerOverlay call to disable the controlbar
			hs.registerOverlay(
				{
					thumbnailId: null,
					overlayId: 'controlbar',
					position: 'bottom left',
					hideOnMouseOut: false
				}
			);
			
			hs.graphicsDir = 'layout/js/graphics/';
			hs.outlineType = 'rounded-white';
			hs.captionEval = 'this.thumb.title';
			hs.align = 'center';
					
		});
	}
	else
	{	$.getScript("/layout/js/init.js",function()
		{	// remove the registerOverlay call to disable the controlbar
			hs.registerOverlay(
				{
					thumbnailId: null,
					overlayId: 'controlbar',
					position: 'bottom left',
					hideOnMouseOut: false
				}
			);
			
			hs.graphicsDir = 'layout/js/graphics/';
			hs.outlineType = 'rounded-white';
			hs.captionEval = 'this.thumb.title';
			hs.align = 'center';
		});
		$.getScript("/layout/js/validate.js");
		//$.getScript("/layout/js/swfobject.js");
		
	}
}


function frischfilm_calcnav()
{	pageAmount = 16;
	scroller = document.getElementById('subnavigationscroller');

	lis = scroller.getElementsByTagName('li');
	lilength = lis.length;
	
	pLeft = $('ul.ul1').css('paddingLeft');
	pLeft = parseInt(pLeft);


	//figure out how many pages there should actually be if the navi doesn't start at x:0.	
	shift = 0;
	if (pLeft>200)
	{	shift = 4;
	}
	if (pLeft>400)
	{	shift=8;
	}
	if (pLeft>600)
	{	shift=12;
	}
	
	Y = pageAmount - shift;
	
	if (lilength<=Y)
	{	pages = 1;
	}
	else
	{	X = lilength - Y;
		extrapages = 1;

		pages = Math.ceil(X/pageAmount);

		frischfilm_pages=pages+extrapages;
	}
	
	
	//forward button
	if (frischfilm_navpage<frischfilm_pages)
	{	$("#subnavpagerForward").unbind();
		$("#subnavpagerForward").click(function () {frischfilm_slidenav('forward');} );
		$('#subnavpagerForward').addClass('active');
		$("#subnavpagerForward").animate(
		{	opacity: 1
		}, 500 );
	}
	else
	{	$("#subnavpagerForward").unbind();
		//$('#subnavpagerForward').removeClass('active');
		$('#subnavpagerForward').html('');
		$("#subnavpagerForward").animate(
		{	opacity: 0
		}, 500 );		
	}
	
	
	//back button
	if (frischfilm_navpage>1)
	{	$("#subnavpagerBack").unbind();
		$("#subnavpagerBack").click(function () {frischfilm_slidenav('back');} );
		$('#subnavpagerBack').addClass('active');
		$("#subnavpagerBack").animate(
		{	opacity: 1
		}, 500 );		
	}
	else
	{	$("#subnavpagerBack").unbind();
		//$('#subnavpagerBack').removeClass('active');
		$('#subnavpagerBack').html('');
		$("#subnavpagerBack").animate(
		{	opacity: 0
		}, 500 );		
	}
	
}


function frischfilm_slidenav(direction)
{	frischfilm_calcnav();

	//Set Cookies to maintain position upon page change
	var expdate = new Date ();
	FixCookieDate (expdate); // Correct for Mac date bug - call only once for given Date object!
	expdate.setTime (expdate.getTime() + (24 * 60 * 60 * 1000)); // 24 hrs from now 
	

	mLeft = parseInt($('#subnavigationscroller').css('marginLeft'));
	
	if (direction=="forward") 
	{	if (frischfilm_navpage==frischfilm_pages)
		{	alert('no more pages. Page:'+frischfilm_navpage);
		}
		else
		{	$("#subnavigationscroller").animate(
			{	marginLeft:mLeft-896
			}, 1500 );
			frischfilm_navpage++;
			SetCookie ("mLeft", mLeft-896,expdate,'/');
			SetCookie ("frischfilm_navpage", frischfilm_navpage,expdate,'/');
			SetCookie ("frischfilm_pages", frischfilm_pages,expdate,'/');
		}
	}
	
	if (direction=="back")
	{	if (frischfilm_navpage==1)
		{	alert('no more pages. Page:'+frischfilm_navpage);
		}
		else
		{	$("#subnavigationscroller").animate(
			{	marginLeft:mLeft+896
			}, 1500 );
			
			frischfilm_navpage--;
			SetCookie ("mLeft", mLeft+896,expdate,'/');
			SetCookie ("frischfilm_navpage", frischfilm_navpage,expdate,'/');
			SetCookie ("frischfilm_pages", frischfilm_pages,expdate,'/');
		}
	}
	
	
	
	
	frischfilm_calcnav();
}
