// JavaScript Document

onresize = function() {

	$("#sideslideback").css("height",$(".container").height());

};

var timer;
var counter; //somehow on loading the page a hover event is thrown if on the menu which is quite anoying ths should fix the hover

$(document).ready(function(){
	
	$(".container").css({height: ($(".container").height() + $(".publicationlinks ul").height() + $(".questions ul").height() + $(".backgroundinformation ul").height())+'px'});
	
    $("#q").bind("keyup", function() {
        toggleButton();
    });

	$("#sideslideback").css("height",$(".container").height());

	$(".field").focus(function () {
	    $(this).addClass("fieldfocus");
	});
	$(".field").blur(function () {
	    $(this).removeClass("fieldfocus");
	});

	setTimeout(function() {
		
		$("#side").hover(function() {
			clearTimeout(timer);
			timer = setTimeout(function() {
			$("#sideslideback").css("height",$(".container").height());																										 
			$("#sideslideback").animate({marginLeft: "-643px" }, { "duration": 600, queue: false });
			$("#sideslidelinks").animate({marginLeft: "-643px", width: "685px" }, { "duration": 600, queue: false });
			$("#sideslidelinks .links").animate({marginLeft: "0", width: "685px" }, { "duration": 600, queue: false });
			},400);
		}, function() {
			clearTimeout(timer);
			timer = setTimeout(function() {
			$("#sideslideback").animate({marginLeft: "0"}, { "duration": 400, queue: false });
			$("#sideslidelinks").animate({marginLeft: "0", width: "42px"}, { "duration": 400, queue: false });
			$("#sideslidelinks .links").animate({marginLeft: "-743px", width: "643px"  }, { "duration": 400, queue: false });
			},200);
		});
	
	},700);
	
	$(".menu").hover(function() {
	}, function() {
		resetFlyoutmenu();
		$(".menu").css({backgroundPosition: '0px 0px'});
		$(".selectedhome").css({backgroundPosition: '0px -100px'});
		$(".selectedorganisation").css({backgroundPosition: '0px -200px'});
		$(".selectedreactions").css({backgroundPosition: '0px -300px'});
		$(".selectednewsletter").css({backgroundPosition: '0px -400px'});
		$(".selectedlogin").css({backgroundPosition: '0px -500px'});
		$(".selectedcontact").css({backgroundPosition: '0px -600px'});
	});

	function resetFlyoutmenu() {
		$("div.flyoutmenu").stop().hide();
		$("div.flyoutmenu").height("120px");
	}

	$(".menu .home").hover(function() {
		$(".menu").css({backgroundPosition: '0px -100px'});
		resetFlyoutmenu();
	}, function() {
	});

	$(".menu .organisation").hover(function() {
		$(".menu").css({backgroundPosition: '0px -200px'});
		resetFlyoutmenu();
	}, function() {
	});

	$(".menu .reactions").hover(function() {
		$(".menu").css({backgroundPosition: '0px -300px'});
		resetFlyoutmenu();
	}, function() {
	});

	$(".menu .newsletter").hover(function() {
		$(".menu").css({backgroundPosition: '0px -400px'});
		resetFlyoutmenu();
	}, function() {
	});

	$(".menu .login").hover(function() {
		$(".menu").css({backgroundPosition: '0px -500px'});
		resetFlyoutmenu();
	}, function() {
	});
	
	$(".menu .contact").hover(function() {
		$(".menu").css({backgroundPosition: '0px -600px'});
		resetFlyoutmenu();
	}, function() {
	});

    $("#backgroundinformation").hover(function() {
        $("#backgroundinformation").css({cursor: "pointer"});
        $("#backgroundinformation").addClass("expanded");
    }, function() {
        $("#backgroundinformation").removeClass("expanded");
    })
    
    $("#backgroundinformation").click(function() {
        var linksHeight = $(".backgroundinformation ul").height();
        var currentHeight = $(".backgroundinformation").height();
        if (linksHeight == currentHeight) {
            $(".container").css({height: $(".container").height()+'px'});
            $(".backgroundinformation").stop().animate({ height: 1 }, { duration: 250 });
        } else {
            $(".backgroundinformation").stop().animate({ height: linksHeight }, { duration: 250 });
        }
    })
	
    $("#links").hover(function() {
        $("#links").css({cursor: "pointer"});
        $("#links").addClass("expanded");
    }, function() {
        $("#links").removeClass("expanded");
    })
    
    $("#links").click(function() {
        var linksHeight = $(".publicationlinks ul").height();
        var currentHeight = $(".publicationlinks").height();
        if (linksHeight == currentHeight) {
            $(".container").css({height: $(".container").height()+'px'});
            $(".publicationlinks").stop().animate({ height: 1 }, { duration: 250 });
        } else {
            $(".publicationlinks").stop().animate({ height: linksHeight }, { duration: 250 });
        }
    })
    
    $("#qna").hover(function() {
        $("#qna").css({cursor: "pointer"});
        $("#qna").addClass("expanded");
    }, function() {
        $("#qna").removeClass("expanded");
    })
    
    $("#qna").click(function() {
        var questionsHeight = $(".questions ul").height();
        var currentHeight = $(".questions").height();
        if (questionsHeight == currentHeight) {
            $(".container").css({height: $(".container").height()+'px'});
            $(".questions").stop().animate({ height: 1 }, { duration: 250 });
        } else {
            $(".questions").stop().animate({ height: questionsHeight }, { duration: 250 });
        }
    })
});

jQuery.voormedia = {};

// SEARCH BAR TEXT SHOW/HIDE

jQuery.voormedia.inputfieldwithlabel = {
	initialize: function(options) {
		this.options = jQuery.extend({}, this.options, options);
		var container = this;
		
		$(container).val(container.options.text);
		
		$(container).focus(function(){
			container.clearbox();
		});
		$(container).blur(function(){
			container.resetbox();
		});
		
	},
	clearbox: function() {
		var container = this;
		if ($(container).val() == container.options.text) {
			$(container).val("");
		}
	},
	resetbox: function() {
		var container = this;
		if ($(container).val() == "") {
			$(container).val(container.options.text);
		}
	}
};

// PREVENT BUTTON FROM SUBMITTING

jQuery.voormedia.noclick = {
	initialize: function(options) {
		this.options = jQuery.extend({}, this.options, options);
		var container = this;
		
		$(container).click(function(){
			return container.checkvalues();
		});

	},
	checkvalues: function() {
		var container = this;
		var inputField = $(container).parent().find(container.options.field);
		if (inputField.val() == container.options.text) {
			inputField.focus();
			return false;
		}
	}
};

// FLY OUT MENUS

jQuery.voormedia.flyoutmenu = {
	options: {
		showDelay: 1,
		offMenuDelay: 200,
		tabs: new Array("organisation"),
		submenu: "div.flyoutmenu",
		speed: 150
	}, 
	initialize: function(options)
	{
		this.options = jQuery.extend({}, this.options, options);
		this.tabs = this.options.tabs; // Not really necessary, but because container.tabs is used so much, this is more readable.
		showTimer = null;
		offMenuTimer = null;
		overMenu = false;
		var container = this;

		$(this).children("a").click(function(){
			if (container.isTab()) {
				container.showMenu();
			}
		});

		$(this).hover(function(){
			if (container.isTab()) {
				container.delayShow();
			}
		}, function(){
			if (container.isTab()) {
				container.delayHide();
			}
		});
		
	},
	isTab: function() {
		var container = this;
		for (key in container.tabs) {
			if (container.hasClass(container.tabs[key])) {
				return true;
			}
		}
		return false;
	},
	showMenu: function() {
		var container = this;
		var flyoutMenu = $(container).children(container.options.submenu);
		var currentHeight = flyoutMenu.height();
		flyoutMenu.height("0");
		flyoutMenu.stop().animate({height: currentHeight+"px"}, container.options.speed);
	},
	hideMenu: function() {
		var container = this;
		$(container).children(container.options.submenu).hide();
	},
	delayShow: function() {
		var container = this;
		if (overMenu) {
			container.showMenu();
			clearTimeout(offMenuTimer);
		} else {
			showTimer = setTimeout(function(){
				overMenu = true;
				container.showMenu();
				}, container.options.showDelay);				
		}
	},
	delayHide: function() {
		var container = this;
		container.hideMenu();
		clearTimeout(showTimer);
		offMenuTimer = setTimeout(function(){
			overMenu = false;
			}, container.options.offMenuDelay);
	}
};

// ICONS IN FRONT OF LINKS
// This can be done with a background-image in CSS, but if the link linewraps, the CSS breaks.

jQuery.voormedia.leadingicons = {
	options: {
		imagefolder: "/images/icons/"
	},
	initialize: function(options)
	{
		this.options = jQuery.extend({}, this.options, options);
		href = $(this).attr("href");
		alt = $(this).val();
		$("<a href=\""+href+"\"><img class=\"linkicon\" src=\""+this.options.imagefolder+this.options.image+"\" alt=\""+alt+"\"></a>").insertBefore(this);
	}
};

jQuery(function() {
	jQuery("input.menusearchbox").each(function() {
		$(this).extend(jQuery.voormedia.inputfieldwithlabel).initialize({text: "zoek..."});
	});
	jQuery("div.menusearch input.button").each(function() {
		$(this).extend(jQuery.voormedia.noclick).initialize({text: "zoek...", field: "input.menusearchbox"});
	});	
	jQuery("div#mainmenu ul li").each(function() {
		$(this).extend(jQuery.voormedia.flyoutmenu).initialize({});
	});
	jQuery("a.pdf").each(function() {
		$(this).extend(jQuery.voormedia.leadingicons).initialize({image: "acrobat-small.gif"});
	});
	jQuery("a.doc").each(function() {
		$(this).extend(jQuery.voormedia.leadingicons).initialize({image: "word-small.gif"});
	});	
});

var downloadURL = "";

function showDownload(url) {
	downloadURL = url;
	$('#overlay').animate({'opacity': 0.77}, 800);
	document.getElementById("overlay").style.display = "block";
	document.getElementById("dlwindow").style.display = "block";
}

function hideDownload() {
	document.getElementById("overlay").style.display = "none";
	document.getElementById("dlwindow").style.display = "none";
	downloadURL = "";
	document.getElementById("overlay").style.opacity = "0";
}

function submitEmailAddress() {
    if (document.getElementById("emailaddress").value != "") {
        window.open(downloadURL+"&emailaddress="+document.getElementById("emailaddress").value);
        document.getElementById("overlay").style.display = "none";
    	document.getElementById("dlwindow").style.display = "none";
    } else {
        alert("U heeft geen e-mail adres opgegeven.");
    }
}

function skipEmailSubmittal() {
    downloadPublication(downloadURL);
    document.getElementById("overlay").style.display = "none";
	document.getElementById("dlwindow").style.display = "none";
}

function downloadPublication(url) {
	window.open(url);
}

function toggleButton() {
	if (document.getElementById("searchbutton")) {
		var value = document.getElementById("q").value;
		if (value == "") {
			document.getElementById("searchbutton").style.display = "none";
		} else {
			document.getElementById("searchbutton").style.display = "inline";
		}
	}
}