$(document).ready(function(){
	
	
	
	$("#frontpage .scrolling-text").textScroll();
	$("#frontpage .slideshow").slideshow(); 
	$("#history .slideshow").historySlide();
	
	$("#contactForm form").simpleValidation();
	
	//bind dropdown
	$('#search .sites select').change(function(){
		window.location.href = $(this).val();
	});
	
	//bind all slideshows
	$('.slideshow-holder').slideshowFrontpage();
	
	$('#contactMap .contact').mouseenter(function(){
		$(this).css('background','url(../images/contactmap/contact-bg-active.jpg) no-repeat');
		$(this).find('.vcard').fadeIn('slow');
	});
	
	$('#contactMap .contact').mouseleave(function(){
		$(this).css('background','url(../images/contactmap/contact-bg.jpg) no-repeat');
		$(this).find('.vcard').fadeOut('slow');
	});
	
});


/*
 * Simple form of validation for formfields
 */

jQuery.fn.simpleValidation = function(){
	
	if($(this).length != 1){
		return false;
	}

	function isEmail(input){
		var mailRegExp = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z])?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]|[0-9]{1,2})\.)(25[0-5]|2[0-4][0-9]|1[0-9]|[0-9]{1,2})\]?$)/i);
		return mailRegExp.test(input);
	}

	function isFilled(input){
		if(input.val().length > 0)
			return true;
		else
			return false;
	}
	
	function isName(input){
		if(input.val().length > 1 && input.val().length < 255)
			return true;
		else
			return false;
	}
	
	$(this).submit(function(){
		var errors = 0;
		
		//clear errors
		$(this).find('.isError').each(function(){
			$(this).removeClass('isError');
		});
		
		$(this).find('.validate').each(function(){
			
			if($(this).hasClass('email'))
				if(!isEmail($(this).val())){
					++errors;
					$(this).addClass('isError');
				}
				
			if($(this).hasClass('name'))
				if(!isName($(this))){
					++errors;
					$(this).addClass('isError');
				}
			
			if($(this).hasClass('filled'))
				if(!isFilled($(this))){
					++errors;
					$(this).addClass('isError');
				}
				
		});
		
		if(errors > 0)
			return false;
		else
			return true;
		
	});

};




/*
 * =============
 * historySlide 
 * =============
 */


jQuery.fn.historySlide = function(){
	if($(this).length != 1){
		return;
	}
	
	var next = $(this).find('.text-and-buttons .next');
	var prev = $(this).find('.text-and-buttons .prev');
	
	
	
	var slides = $(this).find('.picture-layer ul');
	var interval;
	var locked = false;
	var lastId = 0;	
	
	function init(){
			slides.find('li').each(function(i){
				$(this).metadata().id = i;
				lastId = i;
			});
			next.click(function(){
				next_slide(); 
				return false;
			});
			prev.click(function(){
				prev_slide(); 
				return false;
			});
			showInfo();
	}
	
	
	function next_slide(){
		if(locked)
			return;
		
		if(slides.find('li:first').metadata().id == lastId)
			return;
		
		hideInfo();	
		slides.find('li:first').animate({marginLeft:'-'+slides.find('li:first').width() +'px'},1500,"swing", function(){
			slides.append(slides.find('li:first'));	
			slides.find('li:last').css('margin-left','0px');
			showInfo();
		});			
	}
	
	function prev_slide(){
		if(locked)
			return;
		
		if(slides.find('li:first').metadata().id == 0)
			return;
		
		hideInfo();	
		slides.find('li:last').css('margin-left','-'+slides.find('li:last').width() +'px');
		slides.prepend(slides.find('li:last'));
		
		slides.find('li:first').animate({marginLeft:'0px'},1500,"swing", function(){
			showInfo();		
		});
		
	}
	
	function hideInfo(){
	
		locked = true;	
	}
	
	function showInfo(){
		if(slides.find('li:first').metadata().id == 0)
			prev.fadeOut('fast');
		else	
			prev.fadeIn('fast');
		
		
		if(slides.find('li:first').metadata().id == lastId)
			next.fadeOut('fast');
		else	
			next.fadeIn('fast');	
			
		locked = false;
	}
	
	init();
	

};


/*
 * =============
 * TextScroll
 * =============
 */
jQuery.fn.textScroll = function(){
	if ($(this).length != 1) {
		return;
	}
	
	var move_speed = 25;
	var slides = $(this).find('ul');
	function init(){
		
		slides.find('li').each(function(){
			$(this).mouseenter(paus);
			$(this).mouseleave(resume);
		});
		
		var w = slides.parent().width();
		slides.find('li:first').css('margin-left',w + 'px');
		slides.find('li:first').animate({marginLeft:'-'+slides.find('li:first').width() +'px'},slides.find('li:first').width()+w*move_speed,"linear", function(){
			finnish_move();
		});
	}
	
	function move(){
		slides.find('li:first').animate({marginLeft:'-'+slides.find('li:first').width() +'px'},slides.find('li:first').width()*move_speed,"linear", function(){
			finnish_move();
		});
	}
	
	function finnish_move(){
		slides.append(slides.find('li:first'));
		slides.find('li:last').css('margin-left','0px');
		move();		
	}
	
	function paus(){
		slides.find('li:first').stop();
	}
	
	function resume(){
		var w = slides.find('li:first').width();
		var ml = parseFloat(slides.find('li:first').css('margin-left'));
		
		var distance = w + ml;
		if(distance < 0)
			distance = -distance;  
		
		slides.find('li:first').animate({marginLeft:'-'+slides.find('li:first').width() +'px'},distance*move_speed,"linear", function(){
			finnish_move();
		});
	}
	
	init();
	
}


/*
 * =============
 *  Slideshow
 * =============
 */

jQuery.fn.slideshow = function(){
	if($(this).length != 1){
		return;
	}
	var button_layer = $(this).find('.text-and-buttons');
	var next = $(this).find('.text-and-buttons .next');
	var prev = $(this).find('.text-and-buttons .prev');
	
	var text = $(this).find('.text-and-buttons .text');
	var link = $(this).find('.text-and-buttons .read-more');
	
	var slides = $(this).find('.picture-layer ul');
	var interval;
	var locked = false;
	
	function init(){
			next.click(function(){
				//resetInterval();
				next_slide(); 
				return false;
			});
			prev.click(function(){
				//resetInterval();
				prev_slide(); 
				return false;
			});
			showInfo();
			
			//resetInterval();
	}
	
	function resetInterval(){
		if(interval)
			clearInterval(interval);
		
		interval = setInterval(next_slide,8000);
	}
	
	
	function next_slide(){
		if(locked)
			return;
		
		hideInfo();	
		slides.find('li:first').animate({marginLeft:'-'+slides.find('li:first').width() +'px'},1500,"swing", function(){
			slides.append(slides.find('li:first'));	
			slides.find('li:last').css('margin-left','0px');
			showInfo();
		});			
	}
	
	function prev_slide(){
		if(locked)
			return;
		
		hideInfo();	
		slides.find('li:last').css('margin-left','-'+slides.find('li:last').width() +'px');
		slides.prepend(slides.find('li:last'));
		
		slides.find('li:first').animate({marginLeft:'0px'},1500,"swing", function(){
			showInfo();		
		});
		
	}
	
	function hideInfo(){
		text.fadeOut('slow');
		link.fadeOut('slow');
		locked = true;	
	}
	
	function showInfo(){
		text.html(slides.find('li:first').metadata().text);
		link.attr('href',slides.find('li:first').metadata().url);
		text.fadeIn('slow');
		link.fadeIn('slow');
		locked = false;
	}
	
	init();
	

};





jQuery.fn.slideshowFrontpage = function(){
	
	$(this).each(function(i, item){
		
		var slides = [];
		var num_slides = 0;
		var current_slide = 1;
		var view;
		
		function init(obj){
			view = $(item).find('.view');
			$(item).find('.list li').each(function(i,slide){
				slides.push($(slide).find('a'));
				++num_slides;
			});	
			
			
			setInterval(nextSlide,8000);
		}
		
		function nextSlide(){
			if(current_slide == num_slides){
				view.append(slides[0]);
				current_slide = 1;
			}else{
				view.append(slides[current_slide]);
				++current_slide;
			}
			view.find('a:last').hide();
			view.find('a:last').fadeIn('slow',function(){
				view.find('a:first').remove();	
			});
		}
		
		
		
		init();
	});

};

