var newsCntDif;

$(function(){
	
		newsInit();
		
		$('.wochenkarte').click(function(){
			 self.location.href = '?page_id=65';
		});
		
		$('#logo').click(function(){
			 self.location.href = 'http://www.schwanen-neckartailfingen.de/';
		});
		
		$('.map').click(function(){
			window.open("http://maps.google.de/maps?f=q&source=s_q&hl=de&q=schwanen&sll=48.613534,9.267139&sspn=0.007831,0.022724&ie=UTF8&split=1&rq=1&ev=zo&radius=0.62&hq=schwanen&hnear=&ll=48.612839,9.264736&spn=0.007831,0.022724&z=16&iwloc=A");	
		});
});

function init()
{
	$(highlightMainMenu);
	$(posOpeningHours);
}

function posOpeningHours() {
	var openHours = $('div.oeffnungszeiten');
	var winWidth = pageWidth();
	var oldMargin = openHours.css('left');

	if (winWidth < 1164) {
		openHours.mouseenter(function(){
			$(this).animate({
				'left':'0px'
			});
		}).mouseleave(function(){
			$(this).animate({
				'left':oldMargin
			});
		});
	}
}

// Browser Window Size and Position
// copyright Stephen Chapman, 3rd Jan 2005, 8th Dec 2005
// you may copy these functions but please keep the copyright notice as well
function pageWidth() {return window.innerWidth != null? window.innerWidth : document.documentElement && document.documentElement.clientWidth ?       document.documentElement.clientWidth : document.body != null ? document.body.clientWidth : null;} function pageHeight() {return  window.innerHeight != null? window.innerHeight : document.documentElement && document.documentElement.clientHeight ?  document.documentElement.clientHeight : document.body != null? document.body.clientHeight : null;} function posLeft() {return typeof window.pageXOffset != 'undefined' ? window.pageXOffset :document.documentElement && document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft ? document.body.scrollLeft : 0;} function posTop() {return typeof window.pageYOffset != 'undefined' ?  window.pageYOffset : document.documentElement && document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop ? document.body.scrollTop : 0;} function posRight() {return posLeft()+pageWidth();} function posBottom() {return posTop()+pageHeight();}
                    

function newsInit()
{
	if ($('#view-start')) {
		getNewsCntDif();
		setNewsNavigatorActions();
	}
}

function highlightMainMenu()
{
	$('#main-navigation').find('li').each(function() {
		$(this).mouseover(function() {
			$(this).addClass('active');
			$('ul:hidden',$(this)).show();
		});
		$(this).mouseout(function() {
			$(this).removeClass('active');
			$('ul',$(this)).hide();
		});
	});
}

/* News on start site */

function getNewsCntDif()
{
	var newsHeight=getNewsHeight();
	newsCntDif=newsHeight-$('#newsContent').height();
}

function setNewsNavigatorActions()
{
	showNewsNavigators(1, 0);
	
	
	var moveUp;
	$('a.news-navigator-up').mousedown(function() {
		moveUp=window.setInterval("moveNewsCntUp()",20);			
	});
	$('a.news-navigator-up').mouseup(function() {
		window.clearInterval(moveUp);
	});
	$('a.news-navigator-up').mouseover(function() {
		$(this).addClass('navigator-up-active');
	});
	$('a.news-navigator-up').mouseout(function() {
		$(this).removeClass('navigator-up-active');
	});
	$('a.news-navigator-up').focus(function() {
		$(this).blur();	
	});


	
	var moveDown;
	$('a.news-navigator-down').mousedown(function() {
		moveDown=window.setInterval("moveNewsCntDown()",20);			
	});
	$('a.news-navigator-down').mouseup(function() {
		window.clearInterval(moveDown);
	});
	$('a.news-navigator-down').mouseover(function() {
		$(this).addClass('navigator-down-active');
	});
	$('a.news-navigator-down').mouseout(function() {
		$(this).removeClass('navigator-down-active');
	});
	$('a.news-navigator-down').focus(function() {
		$(this).blur();	
	});
}

function getNewsMarginTop(elId,multi)
{
	var offsetTop=$('#'+elId+'').css('marginTop');
	
	offsetTop=offsetTop.split('px')[0];
	
	offsetTop=offsetTop * multi;
	
	return offsetTop;
}

function moveNewsCntUp()
{
	var marginTop=getNewsMarginTop('all-posts',1);
	if (marginTop < 0)
	{
		var offsetTopTmp=parseInt(marginTop)+parseInt(2);
		$('#all-posts').css('marginTop',''+offsetTopTmp+'px');
	}
}

function moveNewsCntDown()
{
	var newsHeight=getNewsHeight();
	var max=newsHeight-$('#newsContent').height()+40;
	
	var marginTop=getNewsMarginTop('all-posts', 1);
	var marginTopMinus=getNewsMarginTop('all-posts', -1);
	
	if (marginTopMinus < max) {
		$('#all-posts').css('marginTop',''+(marginTop-2)+'px');
	}
}

function showNewsNavigators(visible,fade)
{
	if (newsCntDif > 0) 
	{
		if (visible==1 && fade==1) 
		{
			$('#news-navigators').fadeIn(200);
		} 
		else if (visible==1 && fade==0) 
		{
			$('#news-navigators').css('display','');
		}
		else if (visible==0 && fade==1) 
		{
			$('#news-navigators').fadeOut(200);
		}
		else if (visible==0 && fade==0)
		{
			$('#news-navigators').css('display','none');
		}
	} 
	else
	{
		$('#news-navigators').css('display','none');
	}
}

function getNewsHeight()
{
	var newsHeight=0;
	$('.post').each(function() {
		newsHeight+=$(this).outerHeight();
		newsHeight+=parseInt($(this).css('marginBottom'));
	});
	
	return newsHeight;
}
