$(document).ready(function() {

	$("#photoalbum .photorow a").fancybox();

	$('ul.toprow li, ul.bottom li').empty().css({ 'background' : 'url(../img/ajax-loader.gif) no-repeat center center' });
	Cufon.replace('h1, h1 strong, h2, h3 strong, #nav-meta li a, #nav-main li a.cufon, #nav-footer-meta li a, .cta-simple h3, .cta-simple a.readmore, .cta-list h3, .cta-list a.readmore', { hover: true });
	Cufon.replace('#adv-search h3, .frm fieldset a span.btn, .frm fieldset a span.num', { hover: true });
	Cufon.replace('.frm h3, .frm fieldset a span.btn, .frm fieldset a span.num, #sitemap .titel', { hover: true });
	
	function updateMenu() {
		setTimeout(function() {	Cufon.replace('#nav-main li a.cufon', {}, false); }, 5); 
	}
	
	$('#nav-main li').hover(updateMenu, updateMenu);
	
	$('#nav-main li ul li').hover(
		function() {
			$(this).addClass('hover');
		},
		function() {
			$(this).removeClass('hover');
		}
	).click(entireElmClick);
	
	
	$("a.btn_envelope").fancybox({
		'transitionIn'	:	'elastic',
		'transitionOut'	:	'elastic',
		'speedIn'		:	600, 
		'speedOut'		:	200, 
		'width'			:	600,
		'height'		:	470,
		'overlayShow'	:	true,
		'type'				: 'iframe'

	});
	
	
	/* lifestyle header */
	$('.content-container a.headerbtn').removeClass('active')
	
	if(!$('#life-style-header').hasClass('closed')) {
		$('#content-container a.headerbtn.up').addClass('active');
	} else {
		$('#content-container a.headerbtn.down').addClass('active');
	}
	
	$('#content-container a.headerbtn.down').click(function() { return headerDown(); });
	$('#content-container a.headerbtn.up').click(function() { return headerUp(); });
	
	$('.frm fieldset a').hover(
		function() {
			$(this).addClass('hover');
		},
		function() {
			$(this).removeClass('hover');	
		}
	)

	init_header();
	
	/**
	 * Autocomplete
	 */
	$("#frm-search .autocomplete").autoCompleteSearch();
	
	
	/**
	 * Googlemaps
	 */	 	
	if (GBrowserIsCompatible() && jQuery('#googlemap').length > 0) {
		map = new GMap2(document.getElementById('googlemap'));
		map.removeMapType(G_HYBRID_MAP);
		var mapControl = new GMapTypeControl();
		map.addControl(mapControl);
		map.setCenter(new GLatLng(51.588187,4.775829),13);
		map.addControl(new GLargeMapControl3D());
		
		load_bebreda_kml();
	}
	if (GBrowserIsCompatible() && jQuery('#smallgooglemap').length > 0) {
		map = new GMap2(document.getElementById('smallgooglemap'));
		map.removeMapType(G_HYBRID_MAP);
		var mapControl = new GMapTypeControl();
		map.addControl(mapControl);
		map.setCenter(new GLatLng(51.588187,4.775829),13);
		map.addControl(new GLargeMapControl3D());
		
		load_bebreda_kml();
	}
	
	
});

function load_bebreda_kml() {
	var gm_options_url = '';
	if (gm_options.length > 0) {
		gm_options_url = '?';
		gm_options_url += gm_options.join('&');
	}

  	KMLoverlay = new GGeoXml('http://www.bebreda.nl/xml/locaties.kml.php'+gm_options_url ,function() { show_KmlOverlay() } );
}

function show_KmlOverlay() {
 	if (KMLoverlay.loadedCorrectly()) {
 		map.addOverlay(KMLoverlay);
		map.setZoom(map.getBoundsZoomLevel(KMLoverlay.getDefaultBounds(), map.getSize())); 
		map.panTo(KMLoverlay.getDefaultCenter()); 
	}  else {
		show_KmlOverlay();
	}
}


function headerUp() {
	$('#imagerows li').unbind();
	$('#content-container a.headerbtn').addClass('active');
	$('#content-container a.headerbtn.up').removeClass('active');
	$('#flash-outer-container').animate({ height: '128px' },500)
	$('#life-style-header').addClass('closed');
	if($('.largeimage img').length > 0) {
		$('.largeimage img').animate({ width: '150px', height: '100px' },100, function() { 
			$(this).parent().empty().hide().removeClass('selected')
		});
	}
	$('#imagerows .bottom').fadeOut('fast');
	$('#bredatalk-container').animate({ height: '0px', marginBottom: '0'},300);
	return false; 
}

function headerDown() {
	$('#bredatalk-container').animate({ height: '120px', marginBottom: '20'},300, function () { 	$('#imagerows .bottom').fadeIn(); });
	$('#content-container a.headerbtn').addClass('active');
	$('#content-container a.headerbtn.down').removeClass('active');
	$('#flash-outer-container').animate({ height: '230px' },500);
	$('#life-style-header').removeClass('closed');
	init_header_click();
	return false; 
}

function entireElmClick () {
	if($(this).find("a").attr("target") == '_blank'){
		window.open($(this).find("a").attr("href"));
	}else{
		window.location=$(this).find("a").attr("href");
	}
	return false;
}
/*
* FANCY HEADER FUNCTIONS 
*/
var currentPosition = 0;
var positions = new Object();
positions[2] ={ largeDiv : '#largeimage-a'};
positions[2].css =  {  position: 'absolute', left: '0px' };
positions[3] ={ largeDiv : '#largeimage-a'};
positions[3].css =  {  position: 'absolute', right: '0px' }
positions[4] ={ largeDiv : '#largeimage-b'};
positions[4].css =  {  position: 'absolute', left: '0px' }
positions[5] ={ largeDiv : '#largeimage-b'};
positions[5].css =  {  position: 'absolute', right: '0px' }
positions[6] ={ largeDiv : '#largeimage-c'};
positions[6].css =  {  position: 'absolute', left: '0px' }
positions[7] ={ largeDiv : '#largeimage-c'};
positions[7].css =  {  position: 'absolute', right: '0px' }

function init_header_click() {
	$('#flash-inner-container .largeimage').click(function() { $('img',this).animate({ width: '150px', height: '100px' }, 500, function() { $(this).parent().hide();})});
	$('#imagetoprow li').click( function() {
	 	i = $("#imagetoprow li").index(this);
	 	$(''+positions[i].largeDiv).empty().show();
		$('img',this).clone().appendTo(positions[i].largeDiv).css(positions[i].css).css({top: '0px'}).animate({ width: '304px', height: '204px' }, 500 );
	
	}); 
	$('#imagebottomrow li').click( function() {
		i = $("#imagebottomrow li").index(this);
	 	$(''+positions[i].largeDiv).empty().show();
		$('img',this).clone().appendTo(positions[i].largeDiv).css(positions[i].css).css({bottom: '0px'}).animate({ width: '304px', height: '204px' }, 500 );
	});	
}

function init_header() {
	if($('#life-style-header').hasClass('closed')) {
		$('#imagebottomrow').hide();
	} else {
		init_header_click();
	}
	
	$.getJSON('/ajax/next_header_photos.php?p=start', function(data) {
		for ( i=0; i < data.bottom.length; i ++ ) {
			img = $('<img />').load( function() { $(this).fadeIn('1000'); }).appendTo($('ul.toprow li:nth-child('+ (i +1) +')')).attr({ 'src': data.bottom[i].source, 'width' : '150', 'height': '100', 'alt': data.bottom[i].source});
			if (!$.browser.msie) img.css({ 'display' : 'none'}) ;
		}
		for ( i=0; i < data.toprow.length; i ++ ) {
			img = $('<img />').load( function() { $(this).fadeIn('1000'); }).appendTo($('ul.bottom li:nth-child('+ (i +1) +')')).attr({ 'src': data.toprow[i].source, 'width' : '150', 'height': '100', 'alt': data.bottom[i].source });
			if (!$.browser.msie) img.css({ 'display' : 'none'}) ;
		}
	});
	
	$("#hdr-nav-next").click( function(){
		if(!$(this).hasClass('disabled')) {
			$("#hdr-nav-next, #hdr-nav-prev").addClass('disabled');	
			$('#imagerows li').unbind();
			
			if($('.largeimage img').length > 0) {
				$('.largeimage img').animate({ width: '150px', height: '100px' },200, function() { 
					$(this).parent().empty().hide().removeClass('selected')
				});
			}
			loadNext();
		}
	}).hover( function() { $('img',this).css({ 'margin' : '-36px 0 0 0' }); }, function() { $('img',this).css({ 'margin' : '0' }); } );
	
	$("#hdr-nav-prev").click( function(){	
		if(!$(this).hasClass('disabled')) {
			$("#hdr-nav-next, #hdr-nav-prev").addClass('disabled');	
			$('#imagerows li').unbind();
			if($('.largeimage img').length > 0) {
				$('.largeimage img').animate({ width: '150px', height: '100px' },200, function() { 
					$(this).parent().empty().hide().removeClass('selected')
				});
			}
			loadPrev();
		}
	}).hover( function() { $('img',this).css({ 'margin' : '-36px 0 0 0' }); }, function() { $('img',this).css({ 'margin' : '0' }); } );

}

function loadNext() {
	currentPosition ++;	
	oldleft = parseInt($('#imagerows').css('left'));
	$('#imagerows').animate({ left: oldleft - 154}, function() { 
		$('#imagetoprow li:first, #imagebottomrow li:first').remove();
		$('<li>').appendTo('#imagetoprow , #imagebottomrow');
		$.getJSON('/ajax/next_header_photos.php?p='+currentPosition, function(data) { 
			for ( i=0; i < data.bottom.length; i ++ ) {
				$('<img />').appendTo($('#imagetoprow li:last')).attr({ 'src': data.bottom[i].source, 'width' : '150', 'height': '100' });
			}
			for ( i=0; i < data.toprow.length; i ++ ) {
				$('<img />').appendTo($('#imagebottomrow li:last')).attr({ 'src': data.toprow[i].source, 'width' : '150', 'height': '100' });
			}
			$("#hdr-nav-next, #hdr-nav-prev").removeClass('disabled');
		});
	
		$('#imagerows').animate({ left: oldleft+'px'}, 0);
		if(!$('#life-style-header').hasClass('closed')) {
			init_header_click();
		}
	});
}

function loadPrev() {
	currentPosition --;	
	oldleft = parseInt($('#imagerows').css('left'));
	$('#imagerows').animate({ left: oldleft + 154}, function() { 
		$('#imagetoprow li:last, #imagebottomrow li:last').remove();
		$('<li>').prependTo('#imagetoprow , #imagebottomrow');
		$.getJSON('/ajax/next_header_photos.php?p='+currentPosition, function(data) { 
			for ( i=0; i < data.bottom.length; i ++ ) {
				$('<img />').appendTo($('#imagetoprow li:first')).attr({ 'src': data.bottom[i].source, 'width' : '150', 'height': '100' });
			}
			for ( i=0; i < data.toprow.length; i ++ ) {
				$('<img />').appendTo($('#imagebottomrow li:first')).attr({ 'src': data.toprow[i].source, 'width' : '150', 'height': '100' });
			}
			$("#hdr-nav-next, #hdr-nav-prev").removeClass('disabled');
		});
	
		$('#imagerows').animate({ left: oldleft+'px'}, 0);
		if(!$('#life-style-header').hasClass('closed')) {
			init_header_click();
		}
	});
}

/**
 *
 * Autocompleet / find as you type
 *
 */
$.fn.autoCompleteSearch = function()
{
	var	jQueryMatchedObj = this,
		offset = $(jQueryMatchedObj).parent().offset();
		current = null;
		
		
	$(jQueryMatchedObj).parent().css({ position: 'relative', zIndex: '900' });
	$(jQueryMatchedObj).css({ position: 'relative', zIndex: '900' });
	$(jQueryMatchedObj).attr("autocomplete", "off");
	$(jQueryMatchedObj).keyup(function(objEvent)
	{
		stopbubble = false;
		switch(objEvent.keyCode) { 
			case 13:	stopbubble = true;
						break;
			case 38:	// up
						if(current != null) {
							current =  $("#autoCompleteFlyOut li[selected='true']");
							previous = current.prev();
							if(previous.length != 0) {
								current.attr("selected","false").removeClass("focus");
								previous.attr("selected","true").addClass("focus");
								$(jQueryMatchedObj).val(previous.text());
							}
						}
						stopbubble = true;
						break;
			case 40:	// down
						if(current == null) {
							current =  $("#autoCompleteFlyOut li:first");
							nextone = current;
							if(nextone.length != 0) {
								current.attr("selected","false").removeClass("focus");
								nextone.attr("selected","true").addClass("focus");
								$(jQueryMatchedObj).val(nextone.text());
							}
						} else {
							current =  $("#autoCompleteFlyOut li[selected='true']");
							nextone = current.next();
							if(nextone.length != 0) {
								current.attr("selected","false").removeClass("focus");
								nextone.attr("selected","true").addClass("focus");
								$(jQueryMatchedObj).val(nextone.text());
							}
						}
						stopbubble = true;
						break;
			default:	q = $(jQueryMatchedObj).val();
						taal = $(jQueryMatchedObj).attr('lang');
						if($('#autoCompleteFlyOut').length == 0 ) {
							$('body').append("<div id='autoCompleteFlyOut'></div>");
						}
						flyOutObj = $('#autoCompleteFlyOut');
						
						$.ajax({
							type: "POST",
							url: "/ajax/se_autocomplete_search.ajax.php",
							data: 'q='+q+'&taal='+taal,
							success: function(html)
							{
								current = null;
								$(flyOutObj).css(
								{
									top:   $(jQueryMatchedObj).offset().top + $(jQueryMatchedObj).outerHeight() +4,
									left: $(jQueryMatchedObj).offset().left,
									width: parseInt($(jQueryMatchedObj).parent().outerWidth()-14)+"px",
									zIndex: '1500'
								})
								.html(html)
								.show();
								$('a', flyOutObj).click(function()
								{
									$(jQueryMatchedObj).val($(this).text());
									return false;
								});
							}
						}); 
		}
		if(stopbubble) { 
			objEvent.stopPropagation();
			objEvent.preventDefault();
		}
	})
	.blur(function()
	{
		setTimeout("$('#autoCompleteFlyOut').hide();", 500);
	});
	
	
};

