if(!jsFrontend) { var jsFrontend = new Object(); }

jsFrontend = {
	// datamembers
	debug: false,
	
	// init, something like a constructor
	init: function() {
		// fix the a/img problem
		$('.content a > img').parent().addClass('linkedImage');
		
		// slideshow
		if($('#slideshow').length > 0) jsFrontend.slideshow.init();
		
		// modal
		jsFrontend.modal.init();

		// Some fixes for team page
		$('#teamMembers p:first-child, #teamMembers tr td:first-child').addClass('firstChild');
		$('#teamMembers tr td:last-child').addClass('lastChild');

		// Run once onload for IE7
		slideshowPosition();

		// Make sure slideshow stays in place on window resize
		function slideshowPosition() {
			var windowWidth = $(window).width();
			var adjustPosition = parseInt(-(1650 - windowWidth)/2);
			$('#slideshow img').css('left', adjustPosition + "px");
		}
		$(window).resize(function() {
			slideshowPosition();
		});
		
		// Add lightbox to gallery
		$('#gallery a').lightBox({fixedNavigation:true});	
	},

	// end
	eof: true
}


jsFrontend.slideshow = {
	items: null,
	current: null,
	timer: null,

	// init, something like a constructor
	init: function() {
		jsFrontend.slideshow.items = slideshow;

		// preload images
		var preloadedImage = new Image();
		for(var i in jsFrontend.slideshow.items) { preloadedImage.src = jsFrontend.slideshow.items[i].image; }

		// fix layout stuff
		$('.slideshowNav li:first').addClass('firstChild');
		$('.slideshowNav li:last').addClass('lastChild');

		// bind links
		$('.slideshowNav a').bind('click', function(evt) {
			// prevent
			evt.preventDefault();

			// get id
			var id = parseInt($(this).attr('href').replace('#slide-', '')) - 1;

			// reset id
			if($(this).parent('li').hasClass('previousSlide')) 
			{
				// set
				id = jsFrontend.slideshow.current - 1;
				
				// validate
				if(id < 0) id = jsFrontend.slideshow.items.length - 1;
			}

			// reset id
			if($(this).parent('li').hasClass('nextSlide')) 
			{
				// set
				id = jsFrontend.slideshow.current + 1;
				
				// validate
				if(id > jsFrontend.slideshow.items.length - 1) id = 0;
			}

			// stop all animations
			$('#slideshow img').stop(true, true);

			// clear the interval
			clearInterval(jsFrontend.slideshow.timer);

			// slide
			jsFrontend.slideshow.slide(id);
		});

		// slide to first
		jsFrontend.slideshow.slide(0);
		// set the timer
		jsFrontend.slideshow.timer = setInterval(function() {
			// init var
			var to = jsFrontend.slideshow.current + 1;
			
			// reset if needed
			if(to > (jsFrontend.slideshow.items.length - 1)) to = 0;
			
			// slide
			jsFrontend.slideshow.slide(to);
		}, 5000);
	},

	slide: function(to) {
		// if to == current don't do anything
		if(jsFrontend.slideshow.current == to) return;

		// set new current
		jsFrontend.slideshow.current = to;

		// add class
		$('.slideshowNav li').removeClass('selected');
		$('.slideshowNav a[href="#slide-'+ (to + 1) +'"]').parent().addClass('selected');

		// set new link
		$('.slideshowLink').attr('href', jsFrontend.slideshow.items[to].link);

		// set new image as bottom image
		$('#slideshowImageBottom').hide()
								  .attr('src', jsFrontend.slideshow.items[to].image)
								  .fadeIn(1500);
		
		// fade out top
		$('#slideshowImageTop').fadeOut(1000, function() {
			// set new image as top image
			$('#slideshowImageTop').attr('src', jsFrontend.slideshow.items[to].image).fadeTo('fast', 1);
			
			// hide bottom
			$('#slideshowImageBottom').hide(0);
		});
	},
	
	// end
	eof: true
}

jsFrontend.modal = {

		
	// init, something like a constructor
	init: function() {

		// fetch country from cookie
		country = '';
		if(document.cookie.length && document.cookie.indexOf('country=') > -1)
		{
			country = document.cookie.substr(document.cookie.indexOf('country=') + 8);
			if(country.indexOf(';') > -1) country = country.substr(country.indexOf(';'));
		}
			
		// country not set = show dialog
		if(!country)
		{
			$('#country-modal').dialog({
				width: 750,
				dialogClass: 'countryModal',
				resizable: false,
				closeOnEscape: false,
				draggable: false,
				modal: true
			});
			
			$('#country-modal a').click(function(e)
			{
				e.preventDefault();

				// determine clicked country
				country = $(this).attr('href').replace('#', '');
				
				// set cookie (valid for 365 days)
				var date = new Date();
				date.setTime(date.getTime() + (365 * 24 * 60 * 60 * 1000));
				document.cookie = 'country=' + escape('i:'+ country +';') +'; expires='+ date.toGMTString() +'; path=/';

				// close modal
				$('#country-modal').dialog('close');
				
				// refresh
				window.location.reload(true);
			});
		}
	},
	
	// end
	eof: true
}

$(document).ready(function() { jsFrontend.init(); });
