//SLIDER
var borderSlider = 10;
var borderColor = "#FFF";
var slidingSpeed = 200;
var draggingSpeed = 1000;
var clickEnabled = true;
var posBefore, moveT, liikuttuYht = 0;
var isDragging = false;
var kuljettuMatka, aloitaLiike, lopetaLiike, liikkeenKesto;
//
$(document).ready(function(){
	$(".photo, .video").each(function() {
		var img = new Image();
		img.src = $(this).children("img").attr("src");
		//var thisItemWidth = img.width;
		var currentWidth = $(this).parent(".slider_photos").css("width");
		$(this).parent(".slider_photos").width(currentWidth);
	});

	var sliderWidth = $("#container").width();

	$("#slider1").css({"overflow": "hidden", "background": borderColor, "border": 0+"px solid "+borderColor, "width": sliderWidth-2*borderSlider});
	$("#slider2").css({"overflow": "hidden", "background": borderColor, "border": 0+"px solid "+borderColor, "width": 525-2*borderSlider});

	$(".button_right").click(function() {
		$(this).siblings(".button_left").stop().fadeTo("fast", 0.5);
		var widthNow = $(this).siblings(".slider_photos").position().left;
		var stepWidth = new Array();
		var steps = 0;
		var liiku = 0;
		$(this).siblings(".slider_photos").children(".photo, .video").each(function(i) {
			steps += $(this).width()+borderSlider;
			stepWidth[i] = -steps;
		});
		var k = 0;
		while(widthNow <= stepWidth[k]){
			liiku = k+1;
			k++;
		}
		if(-stepWidth[liiku]<=(($(this).siblings(".slider_photos").width()-$(this).siblings(".slider_photos").children("div:last").width()-($(this).parent(".slider").width()-$(this).siblings(".slider_photos").children("div:last").width())))){
			$(this).siblings(".slider_photos").animate({left: stepWidth[liiku] }, slidingSpeed, "swing");
		} else {
			$(this).siblings(".slider_photos").animate({left: -(($(this).siblings(".slider_photos").width()-$(this).siblings(".slider_photos").children("div:last").width()-($(this).parent(".slider").width()-$(this).siblings(".slider_photos").children("div:last").width())))+borderSlider }, slidingSpeed, "swing", function(){ $(this).siblings(".button_right").stop().fadeTo("fast", 0.0); });
			$(this).stop().fadeTo("fast", 0.0);
		}
	});
	$(".button_left").click(function() {
		$(this).siblings(".button_right").stop().fadeTo("fast", 0.5);
		var widthNow = $(this).siblings(".slider_photos").position().left;
		var stepWidth = new Array();
		var steps = 0;
		var liiku = 0;
		$(this).siblings(".slider_photos").children(".photo, .video").each(function(i) {
			steps += $(this).width()+borderSlider;
			stepWidth[i] = -steps;
		});
		var k = stepWidth.length-1;
		while(widthNow >= stepWidth[k]){
			liiku = k-1;
			k--;
		}
		if(liiku < 0){
			$(this).siblings(".slider_photos").animate({ left: 0 }, slidingSpeed, "swing", function(){ $(this).siblings(".button_left").stop().fadeTo("fast", 0.0); });
			$(this).stop().fadeTo("fast", 0.0);
		}
		else {
			$(this).siblings(".slider_photos").animate({ left: stepWidth[liiku] }, slidingSpeed, "swing");
		}
	});

	$(".button_left, .button_right").stop().fadeTo("fast", 0.0);
	$(".slider").hover(function(){
		if($(this).children(".slider_photos").position().left>=0){
			$(this).children(".button_right").stop().fadeTo("fast", 0.5);
		}
		else if((-$(this).children(".slider_photos").position().left+borderSlider)==(($(this).children(".slider_photos").width()-$(this).children(".slider_photos").children("div:last").width()-($(this).width()-$(this).children(".slider_photos").children("div:last").width())))){
			$(this).children(".button_right").stop().fadeTo("fast", 0.0);
			$(this).children(".button_left").stop().fadeTo("fast", 0.5);
		} else{
			$(this).children(".button_left, .button_right").stop().fadeTo("fast", 0.5);
		}
	}, function() {
		$(this).children(".button_left, .button_right").stop().fadeTo("fast", 0.0);
	});
	$(".button_left, .button_right").css("opacity", 0.5).hover(function() {
		if($(this).siblings(".slider_photos").position().left>=0&&$(this).attr("class")=="button_left"){
			$(this).stop().fadeTo("fast", 0.0);
		} else if((-$(this).parent(".slider").children(".slider_photos").position().left+borderSlider)==(($(this).siblings(".slider_photos").width()-$(this).siblings(".slider_photos").children("div:last").width()-($(this).parent(".slider").width()-$(this).siblings(".slider_photos").children("div:last").width()))) && $(this).attr("class")=="button_right"){
			$(this).stop().fadeTo("fast", 0.0);
		} else {
			$(this).stop().fadeTo("fast", 1.0);
		}
	}, function() {
		if($(this).siblings(".slider_photos").position().left>=0&&$(this).attr("class")=="button_left"){
			$(this).stop().fadeTo("fast", 0.0);
		} else if((-$(this).parent(".slider").children(".slider_photos").position().left+borderSlider)==(($(this).siblings(".slider_photos").width()-$(this).siblings(".slider_photos").children("div:last").width()-($(this).parent(".slider").width()-$(this).siblings(".slider_photos").children("div:last").width()))) && $(this).attr("class")=="button_right"){
			$(this).stop().fadeTo("fast", 0.0);
		} else {
			$(this).stop().fadeTo("fast", 0.5);
		}
	});

	$(window).mousemove(function(e){
		kuljettuMatka = e.pageX - posBefore;
		posBefore = e.pageX;
		if(isDragging==true){
			clearTimeout(liikkeenKesto);
			if(kuljettuMatka != 0){
				liikkeenKesto = setTimeout(function(){ aloitaLiike = new Date().getTime(); liikuttuYht = 0; }, 50);
				liikuttuYht += kuljettuMatka;
				return false;
			}		
		}
	});
	$("#slider1").children(".slider_photos").draggable({axis: 'x',
		start: function(event, ui) {
			$(this).css("cursor","move");
			isDragging = true;
			$(this).stop();
			aloitaLiike = new Date().getTime();
			liikuttuYht = 0;
			clickEnabled = false;
		},
		stop: function(event, ui) {
			isDragging = false;
			lopetaLiike = new Date().getTime();
			moveT = 400*(liikuttuYht/(lopetaLiike-aloitaLiike));
			clearTimeout(liikkeenKesto);
			$(this).css("cursor","pointer");

			if($(this).position().left > 0){
				$(this).animate({left: "0px"}, slidingSpeed, "swing");
				$(this).siblings(".button_left").stop().fadeTo("fast", 0.0);
				$(this).siblings(".button_right").stop().fadeTo("fast", 0.5);
			}
			else if($(this).position().left+moveT > 0){
				$(this).animate({left: "100px" }, slidingSpeed, "swing", function(){ $(this).animate({left: "0px" }, 50, "swing"); } );
				$(this).siblings(".button_left").stop().fadeTo("fast", 0.0);
				$(this).siblings(".button_right").stop().fadeTo("fast", 0.5);
			}
			else if(-($(this).position().left)>(($(this).width()-$(this).children("div:last").width()-($(".slider").width()-$(this).children("div:last").width())))){
				$(this).siblings(".button_left").stop().fadeTo("fast", 0.5);
				$(this).siblings(".button_right").stop().fadeTo("fast", 0.0);
				var moveLastPos = -($(this).width()-($(this).children("div:last").width()+borderSlider)-($(".slider").width()-($(this).children("div:last").width())));
				$(this).animate({left: moveLastPos }, slidingSpeed, "swing");
			}
			else if(-($(this).position().left+moveT) > (($(this).width()-$(this).children("div:last").width()-($(".slider").width()-$(this).children("div:last").width())))){
				$(this).siblings(".button_left").stop().fadeTo("fast", 0.5);
				$(this).siblings(".button_right").stop().fadeTo("fast", 0.0);
				$(this).animate({left: -($(this).width()-($(this).children("div:last").width()+borderSlider)-($(".slider").width()-($(this).children("div:last").width())))-100 }, slidingSpeed, "swing", function(){ $(this).animate({left: -($(this).width()-($(this).children("div:last").width()+borderSlider)-($(this).parents(".slider").width()-($(this).children("div:last").width()))) }, 100, "swing"); } );				
			}
			else {
				$(this).siblings(".button_left, .button_right").stop().fadeTo("fast", 0.5);
				$(this).animate({"left": "+="+moveT+"px" }, 1100, "swing");
			}
			return false;
		}
	});

	////

	var currentUrl = window.location.href.split("#")[1];
	if(currentUrl){
		var moveToSection = $("#content #content_sections #section_"+currentUrl).position().left;
		if(moveToSection>=0){
			$("#content_sections").animate({left:(-moveToSection)}, 400, "swing");
		}
		else {
			$("#content_sections").animate({left:0}, 400, "swing");
		}
	}

	$("#logo_blog img, #languages a").hover(function(){
		$(this).stop().fadeTo("fast",0.6);
	}, function(){
		$(this).stop().fadeTo("fast",1.0);
	});
	$("#menu_holder a img").hover(function(){
		if(!$(this).parents("h3").attr("class")){
			$(this).parents("h3").stop().fadeTo("fast",0.6);
		}
	}, function(){
		if(!$(this).parents("h3").attr("class")){
			$(this).parents("h3").stop().fadeTo("fast",1.0);
		}
	});
	$(".outlink").click(function(){
	    window.open(this.href);
	    return false;
	});

    $("#menu #menu_holder h3, #logo_img, #logo_text").click(function(){
		if($(this).attr("id")!="logo_img" && $(this).attr("id")!="logo_text"){
			var moveToSection = $("#content #content_sections #section_"+$(this).children("a").attr("href").split("#")[1]).position().left;
		}
		if(moveToSection>=0){
			$("#content_sections").animate({left:(-moveToSection)}, 400, "swing");
			$("#menu #menu_holder h3").removeClass("menu_active");
			$(this).addClass("menu_active");
			$("#menu #menu_holder h3").not(".menu_active").fadeTo("fast",1.0);
		}
		else{
			$("#content_sections").animate({left:0}, 400, "swing");
		}
	});
});

