(function ($){
	var hover = {hover: true}, // Cufon config
		photos = $('ul.photos a'), // Photos lightbox
		maps = $('div.maps'), // Google Maps
		windowWidth = (window.innerWidth != null? window.innerWidth: document.documentElement.clientWidth > 0? document.documentElement.clientWidth:document.body.clientWidth)-32, // Slider config
		width = Math.min(($('#footer').width() - 32), 1280), // Slider config
		left = ((width - 1280) / 2), // Slider config
		slider, // Slider
		form = $('#contact-form'); // Contact form

	// CUFON HEADINGS
	Cufon.replace("h1")
				(".home h2")
				("#right-column h2")
				("#center-column h2")
				("#sub-nav a", hover)
				("#navigation a", hover)
				(".tagcloud #center-column a", hover);

	// FOTOS LIGHTBOX
	if (photos.size() > 0){
		photos.fancybox({
			'overlayOpacity'	: 0.5,
			'transitionIn'		: 'none',
			'transitionOut'		: 'none',
			'titlePosition' 	: 'over',
			'titleFormat'       : function(title, currentArray, currentIndex, currentOpts) {
				return '<span id="fancybox-title-over">'+ (currentIndex + 1)+' / '+currentArray.length+'</span>';
			}
		});
	}

	// CONTACT FORM VALIDATION
	// Too simple to use validator plugin
	if(form[0]){
		form.submit(function(ev){
			$('#warning').remove();
			var err = false;
			form.find('#sName, #sEmail, #sMessage, #sAddress, #sNumber, #sPostal, #sCity').each(function(i){
				if($(this).val() == ''){
					err = true;
				}
			});
			if (err){
				$('#contents button').before('<div id="warning" style="margin-bottom: 20px;">Vul aub alle verplichte velden in.</div>');
			}
			return (false === err);
		});
	}

	// SLIDER
	if (headerImgs){
		$('#header')
			.css('background', 'transparent')
			.after(
				'<div id="slider">'+
					headerImgs+
				'</div>');
	}
	slider = $('#slider');
	if (slider.size() > 0){
		var resizeSlider = function(){
			windowWidth = (window.innerWidth != null? window.innerWidth: document.documentElement.clientWidth > 0? document.documentElement.clientWidth:document.body.clientWidth)-32; // Slider config
			width = Math.min(($('#footer').width() - 32), 1280); // Slider config
			left = ((width - 1280) / 2); // Slider config
			if (left < 0){
				width += (left * -2);
			}
			if(windowWidth < 1280){
				left = (1280 - windowWidth) / -2;
			}else if(windowWidth > width){
				left = (windowWidth - width) / 2;
			}else{
				width = windowWidth - 2;
				left = 0;
			}
			slider.width(Math.min(width, windowWidth)).find('li').width(width).css('left', left+'px');
		};
		resizeSlider();
		autoSlide(5000, 0);
		jQuery(window).resize(resizeSlider);
	}

	// GOOGLE MAPS
	if (maps.size() > 0 && GBrowserIsCompatible()){
		var map = new GMap2(maps[0]),
			latlng = new GLatLng(52.376827, 4.599495),
			marker = new GMarker(latlng),
			html = '<strong>Orangerie Elswout</strong><br/>Elswoutslaan 22<br/>2051 AE Overveen<br/>023 524 26 39<br/><a href="http://maps.google.nl/maps?f=q&amp;source=s_q&amp;hl=nl&amp;geocode=&amp;q=orangerie+elswout&amp;sll=52.902276,5.515137&amp;sspn=3.114832,9.876709&amp;ie=UTF8&amp;hq=orangerie+elswout&amp;hnear=&amp;t=h&amp;ll=52.452662,4.65683&amp;spn=0.393347,1.234589&amp;z=10&amp;iwloc=A" target="_blank">Routebeschrijving</a>';
		map.addOverlay(marker);
		map.removeMapType(G_HYBRID_MAP);
		map.addControl(new GMapTypeControl());
		map.addControl(new GSmallMapControl());
		marker.openInfoWindowHtml(html, {maxWidth:'100'});
		GEvent.addListener(marker, 'click', function() {
			marker.openInfoWindowHtml(html, {maxWidth:'100'});
		});
		setTimeout(function(){
			map.setCenter(latlng, 14);
		}, 1000);
	}

	if (document.all){ // IE HOVER FIX
		$('body.home li').mouseover(
			function(ev){$(this).find('a.block-image').addClass('hover');}
		).mouseout(
			function(ev){$(this).find('a.block-image').removeClass('hover');}
		);
	}
}(jQuery));
