if (navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPad/i)) {
var viewportmeta = document.querySelectorAll('meta[name="viewport"]')[0];
if (viewportmeta) {
viewportmeta.content = 'width=device-width, minimum-scale=1.0, maximum-scale=1.0';
document.body.addEventListener('gesturestart', function() {
viewportmeta.content = 'width=device-width, minimum-scale=0.25, maximum-scale=1.6';
}, false);
}
}


Cufon.replace('#menu-main > ul > li > ul > li > a, .slogan, #menu-top a, h1, h2', {
	hover: true
});

Cufon.replace('#menu-main > ul > li > a, #right h3, h3.green, .emphasis-container .title', {
	textShadow: '1px 1px #353535',
	hover: true
});

/**
 * Global functions
 ***/

/*
 * Handle redirects
 * redir:
 * $param url - the given url
 * 
 * runDelayer:
 * $param url - the given url
 * $param delay - time when redirect occurs
 * */
function redir(url){
	window.location = url;
}

function runDelayer(url, delay)
{
	if(delay)
		delay = delay;
	else
		delay = 5000;
	if(url) setTimeout('redir("'+url+'")', delay );
}

/*
 * Handle scrolltos
 * scrollToElement:
 * $param element - element to scroll to
 * $param timer - time the scrollto takes
 * */
function scrollToElement(element, timer){
	if(timer)
		timer = timer;
	else
		timer = 0;
	
	if(element)
		$.scrollTo(element,timer);
}

function handleTwitterClick(){
	$('h3.twitter').click(function(){
    	window.open('http://www.twitter.com/warandeloop',
    			 'open_window',
    			 'menubar, toolbar, location, directories, status, scrollbars, resizable, dependent, width=800, height=600, left=0, top=0')
    });
	
}

/*
 * Tweet loader
 * */

function loadtweets(){
	$(".tweet").tweet({
		avatar_size: 32,
		count: 8,
		username: "warandeloop",
		template: function(i){return i["text"]}
	});
}

function animateTweets()
{
   var current = $('.tweet ul li:nth-child(1)');
   var container = $('.tweet ul');
   if (container){
	  slideheight = container.find('li').outerHeight();
      container.animate({'margin-top' : -slideheight}, 800, null, function(){
         current.hide();
         container.css({'margin-top' : 0});
         current.appendTo('.tweet ul');
         current.show();
      });
   }
}


/**
 * Functions
 ***/

// Submenu animation
function handleSubMenu(){
	$('#menu-sub > ul > li:not(.active)').hoverIntent({
		timeout: 100,
		over: function(){
			$(this).addClass('over');
			$(this).children('ul').slideDown();
		},
		out: function (){
			$(this).removeClass('over');
			$(this).children('ul').slideUp();
		}
	});
	
}

//Main menu animation
function handleMainMenu(){
	$('#menu-main > ul > li').hoverIntent({
		timeout: 100,
		over: function(){
			$(this).addClass('over');
			$(this).children('ul').slideDown();
		},
		out: function (){
			$(this).removeClass('over');
			$(this).children('ul').slideUp();
		}
	});
	
}

// Run the slider on home
var amount = $('.emphasis li').length;

var nextSlide = 2;
function slideSwitchAni()
{
	if (nextSlide > amount)
	{
		$('.emphasis-container').scrollTo($('.emphasis-container ul .slide-1'), 1500, { easing:'easeInOutExpo' });
		nextSlide = 2;
	
	}else{
		$('.emphasis-container').scrollTo($('.emphasis-container ul .slide-'+nextSlide), 1500, { easing:'easeInOutExpo' });
		nextSlide = nextSlide+1;
	}
}


function handleSlider(){
	$('.emphasis-container').scrollTo($('.emphasis-container ul .slide-1'), 0);
	amount = $('.emphasis-container ul li').length;
	interval = setInterval('slideSwitchAni()', 6000 );
	
	$('.arrow').mouseenter(function(){
		$(this).find('img').animate({'opacity': 0.8});
	}).mouseleave(function(){
		$(this).find('img').animate({'opacity': 1});
	});
	
	$('.arrow.right').click(function(){
		clearInterval(interval);
		slideSwitchAni();
		interval = setInterval('slideSwitchAni()', 6000 );
		return false;
	});
	
	$('.arrow.left').click(function(){
		clearInterval(interval);
		if (nextSlide == 1 || nextSlide == 2)
			nextSlide = amount;
		else
			nextSlide = nextSlide-2;
		slideSwitchAni();
		interval = setInterval('slideSwitchAni()', 6000 );
		return false;
	});
}

var newsAmount = 5;
var canScroll = true;
function handleAjaxNews()
{
	if(newsAmount){
		$("#load-more").click(function(){
			doAjaxNieuwsRequest();
			canScroll = false;
			return false;
		});
	}
}
// Ajax loader for news
function doAjaxNieuwsRequest()
{
	if(canScroll){
		$('.ajax-loader').fadeIn();
		//ajax request with id to fetch form
		$.ajax({
			type: "POST",
			url: 'nl/news/ajax',
			data: {'newsAmount' : newsAmount},
			success: function(newElement) {    
				//place result on stage
				$('.ajax-loader').fadeOut(400, function(){
					$("#menu-news ul").append(newElement);
					if(newElement == '')
					{
						canScroll = false;
						$('#load-more').html('Er zijn geen nieuws-items beschikbaar');
					}
					else 
						canScroll = true;
				});
				if(newsAmount)
					newsAmount = newsAmount+5;
				
				
			}
		});
	}
}

