addEventListener("load",function(){setTimeout(hideAddressBar,0);});
addEventListener("scroll",function(){setTimeout("if(0===window.pageYOffset){hideAddressBar();}",4000);});

function hideAddressBar(){
	window.scrollTo(0,1);
}


function loadCarousel(width, height){
	$("#nav").jFlow({
		slides: "#slides",
		width: width, //width van 1 item in carousel
		height: height //height van 1 item in carousel
	});
}


function doSearch() {
    window.location.href = "/search/" + escape($("#searchQuery").val()) + ".html";
}

function postVote() {
	$.post("/poll/pag/vote.jsp", $("#pollForm").serialize(),
		function(data) {
			$("#postVoteButton").hide();
			$("#poll ul").empty().append(data);
		}
	);
}

function authenticateUser() {
    $.post("/auth/pag/authenticate_user.jsp", $("#loginForm").serialize(),
        function(data) {
            $("#loginSlashReaction").empty().append(data);
        }
    );
}

function postReaction() {
    // These 2 values are set in article_detail.jsp
    $("#navigationId").val(navigationId);
    $("#articleId").val(articleId);

    $.post("/auth/pag/post_comment.jsp", $("#postReactionForm").serialize(),
        function(data) {
            $("#loginSlashReaction").empty().append(data);
        }
    );
}

$(function(){
	// check if openend standalone
	if (window.navigator.standalone) {
		$("#switch").css("margin-top","20px");  
	}

	//swipe
	if ($("#slides").length > 0){
		$("#slides").touchwipe({
			wipeLeft: function() {
                if ($("#carouselCntr").length > 0){
					_gaq.push(['_trackEvent', 'Navigeren_' + portalName, portalName + ': Swipe Next in caroussel']);
				}
				$(".jFlowNext").click();
            },
            wipeRight: function() {
                if ($("#carouselCntr").length > 0){
					_gaq.push(['_trackEvent', 'Navigeren_' + portalName, portalName + ': Swipe Prev in caroussel']);
				}
				$(".jFlowPrev").click();
            },
			min_move_x: 20,
			min_move_y: 20,
			preventDefaultEvents: false
		});
	}

	// autoslide photoalbum
	var startAutoslide = 0;

	$(".playpause").click(function(){
		$(".playpause").toggle();		
	});
	
	$('#play').click(function(){
		startAutoslide = setInterval('$(".jFlowNext").click()', 3000);
		$("#header, #slides p").fadeOut("slow", "linear");
		$(".overlayIcon").fadeIn("slow", "linear");
		hideAddressBar();

	});
	$('#pause').click(function(){
		clearInterval(startAutoslide);
		$("#header, #slides p").fadeIn("slow", "linear");
		$(".overlayIcon").fadeOut("slow", "linear");
	});


	// toggle search home
	$("#searchBtn, #annuleren").click(function(){
		$("#searchBtn, #annuleren, #weatherType").toggle();
		$("#search").fadeToggle();
	});
	
	$("#searchBtn").click(function(){
		$("#searchQuery").focus();
	});
	
	// scroll to top
    $('a[href=#top]').click(function(){
        $('html, body').animate({scrollTop:0}, 'fast');
        return false;
	});
	
});


function setImgDimensions(){
	$(".slide").css({
		"width": screen.width + "px",
		"height": "auto"	
	}) 
	hideAddressBar();
}

// GET + SET COOKIE
function getCookie(NameOfCookie){
    if (document.cookie.length > 0) {              
    begin = document.cookie.indexOf(NameOfCookie+"=");       
    if (begin != -1) {           
      begin += NameOfCookie.length+1;       
      end = document.cookie.indexOf(";", begin);
      if (end == -1) end = document.cookie.length;
        return unescape(document.cookie.substring(begin, end));
    } 
  }
  return null;
}

function setCookie(NameOfCookie, value, expiredays, domainName) {
var ExpireDate = new Date ();
ExpireDate.setTime(ExpireDate.getTime() + (expiredays * 24 * 3600 * 1000));
 
  document.cookie = NameOfCookie + "=" + escape(value) + 
  ((expiredays == null) ? "" : "; expires=" + ExpireDate.toGMTString()) + "; path=/;domain=" + domainName ;

}

