$(function(){
	ieHover(".menu .holder, .menu .drop-box .item");
	initNavigation();
	setImpressionLightbox();
});


function setImpressionLightbox() {
	$(function() {
		$('#impressies a').lightBox();
	});

}


// hover for IE
function ieHover(_list) {
	if ($.browser.msie && $.browser.version < 7) {
		$(_list).hover(function() {
			$(this).addClass('hover');
		}, function() {
			$(this).removeClass('hover');
		});
	}
}

function initNavigation() {
	var _stayTime = 400;
	var _slideSpeed = 300;
	var _hoverClass = 'hover';

	$('div.menu').each(function(){
		var _opener = $(this);
		var _slideHolder = _opener.find('div.drop');
		var _slider = _slideHolder.find('div.drop-box');
		var _sliderHeight = 0;
		var _timer;

		_slideHolder.show();
		_sliderHeight = _slider.outerHeight(true);
		_slider.css({marginTop:-_sliderHeight});

		_opener.hover(function(){
			if(_timer) clearTimeout(_timer);			
			_opener.addClass(_hoverClass);
			_slider.animate({marginTop:0},{duration:_slideSpeed,queue:false});
		},function(){
			_timer = setTimeout(function(){
				_opener.removeClass(_hoverClass);
				_slider.animate({marginTop:-_sliderHeight},{duration:_slideSpeed,queue:false});
			},_stayTime);
		});
	});
}


/**
 *	Changes the URL of the browser, with the selected option as parameter
 **/
function changeProjectFilter(element) {
	try {
		var selectedValue = element.options[element.selectedIndex].value;
		var url = window.location.toString();
		var newUrl = url.split('?')[0];
		if(selectedValue.length > 0) 
		{
			newUrl += "?filter=" + selectedValue;
		}

		window.location = newUrl;
	}
	catch(e) { }
}


/**
 *	Submits a form
 *	Makes it possible to submit a form with something else than the submit-button, like a textlink
 *	@param string id The ID of the form that should be submitted
 **/
function submitForm(id) {
	try {
		document.getElementById(id).submit();
	}
	catch(e) {}
}


/**
* Add a URL parameter (or changing it if it already exists)
* @param {search} string  this is typically document.location.search
* @param {key}    string  the key to set
* @param {val}    string  value 
* Source: http://stackoverflow.com/questions/486896/adding-a-parameter-to-the-url-with-javascript
*/
var addUrlParam = function(search, key, val){
  var newParam = key + '=' + val,
      params = '?' + newParam;

  // If the "search" string exists, then build params from it
  if (search) {
    // Try to replace an existance instance
    params = search.replace(new RegExp('[\?&]' + key + '[^&]*'), '&' + newParam);

    // If nothing was replaced, then add the new param to the end
    if (params === search) {
      params += '&' + newParam;
    }
  }

  return params;
};