/* Author: Ilkka Hartikainen

*/

$(document).ready(function(){

// show the infobox content on frontpage

$(".johtoryhma td").hover(function () {

	$(this).children(".infobox").slideToggle("fast");

}, function () {
	$(this).children(".infobox").slideToggle("fast");
});


//  Resize the backround image
  updateBackground = function() {
    
    
    
    if ($(window).width() >= 920) {
      //headerbg resize
      $("#headerbg").width("100%");
      // resize background container
      //$("#background-container").width("100%");
      $("#background img").width($(window).width());
      /*$("#background-shade").css({
        marginLeft: $(window).width() / 2 - 528
      });*/
      
    /*if ($(window).height() >= $(window).width()) {
      $("#background img").height($(window).height());
      $("#background img").width($(window).height());
    }
    else {
      $("#background img").width($(window).width());
      $("#background img").height($(window).width());
    }*/
    }
    else {
      //headerbg resize
      $("#headerbg").width(920);
    // resize background container
      //$("#background-container").width(960);
      $("#background img").width(920);
      /*$("#background-shade").css({
         marginLeft: "-62px"
      });*/
      /*if ($(window).height() >= $(window).width() && $(window).height() >= 920) {
        $("#background img").height($(window).height());
        $("#background img").width($(window).height());
      }
      else {
        $("#background img").width(920);
        $("#background img").height(920);
      }*/
      
    }
      
      // resize header
      if ($(window).width() <= 1056 && $(window).width() > 920) {
        $("#headerbg img").css("marginLeft", - Math.round((1056 - $(window).width()) / 2) + "px");
      }
      else if ($(window).width() > 1056)
        $("#headerbg img").css("marginLeft", "0");
      else {
        $("#headerbg img").css("marginLeft", "-62px");
      }
    
    
    
  }
  
  
  
  $(window).resize(function() {
      updateBackground();
  });
  
// initiate background on page load
  updateBackground();
  // display the positioned and scaled background entity for the first time
  //$("#headerbg, #background, #background-container").show();
  $("body").show();
// End main function

// update navigation indicator locations
	if ($('#navigation .selected').length)
		$("#navigation").css("backgroundPosition", ($('#navigation .selected').offset().left - $('#navigation').offset().left - 325 + ($('#navigation .selected').width() / 2) + 156) + "px bottom");
// set the current navigation location as a variable for later centering
	var originalMenuBGPosition = $("#navigation").css("backgroundPosition");
// menu animations
$("#navigation li").hover(function () {
	if (!$(this).children("a").hasClass("selected")) {
		$("#navigation").stop().animate({
			backgroundPosition: ($(this).offset().left - $('#navigation').offset().left - 325 + ($(this).width() / 2) + 166) + "px bottom"}, 100);
		}
	}, function () {
	$("#navigation").stop().animate({
		backgroundPosition: originalMenuBGPosition
	}, 500);
});

// synchronize intro and news box height
var newsBoxHeight = $("#news").height();
var introBoxHeight = $("#introduction").height();
$("#news, #introduction").height(Math.max(newsBoxHeight, introBoxHeight));


// Cases

// show and hide controls and texts
// $("#case-controls").fadeTo("0.5");

$("#cases").hover(
	function() {
		$("#case-previous, #case-next").show();
		$("#case-previous, #case-next").stop(true, true).animate({
			opacity: 0.5
		});
		$("#case-descriptions, #case-titles").stop(true, true).animate({
			opacity: 1
		});
	},
	function() {
		$("#case-previous, #case-next").stop(true, true).animate({
			opacity: 0.4
		});
		$("#case-descriptions, #case-titles").stop(true, true).animate({
			opacity: 0.75
		});
	}
);

$("#case-previous").hover(
	function() {
		$(this).stop(true, true).animate({
			opacity: 1
		}, 'fast');
	},
	function() {
		$(this).stop(true, true).animate({
			opacity: "0.5"
		});
	}
);

$("#case-next").hover(
	function() {
		$(this).stop(true, true).animate({
			opacity: 1
		}, 'fast');
	},
	function() {
		$(this).stop(true, true).animate({
			opacity: "0.5"
		});
	}
);

// add case numbering
$("#case-titles>div").each(function (index) {
	$(this).text("Case " + (index + 1) + "/" + $("#case-titles>div").length + ": " + $(this).text());
});

// show random case
var rndRoof = $("#case-titles>div").length;
rndRoof--;
var rndN = Math.round(Math.random() * rndRoof);
//alert(rndN);
$("#case-titles>div:eq(" + rndN + "),#case-descriptions>div:eq(" + rndN + ")").fadeIn();
//$("#case-titles>div:eq(1),#case-descriptions>div:eq(1)").fadeIn();
$("#case-backgrounds").css("marginLeft", -960 * rndN + "px");

$("#case-next").click(function () {
	if ($("#cases").find(":animated").length)
		return false;
	var currentTitle = $("#case-titles").children(":visible");
	var currentDescription = $("#case-descriptions").children(":visible");
	var bgPosition = parseInt($("#case-backgrounds").css("margin-left"));
	currentTitle.hide();
	currentDescription.hide();
	if (currentTitle.next().length) {
		currentTitle.next().fadeIn('fast', 'easeInExpo');
		currentDescription.next().fadeIn('fast', 'easeInExpo');
		$("#case-backgrounds").animate({
			marginLeft: bgPosition - 960 + "px"
		}, 250, 'easeInExpo');
	}
	else {
		currentTitle.parent().children(":first").fadeIn('fast', 'easeInExpo');
		currentDescription.parent().children(":first").fadeIn('fast', 'easeInExpo');
		$("#case-backgrounds").animate({
			marginLeft: "0px"
		}, 500, 'easeInOutExpo');
	}
});
$("#case-previous").click(function () {
	if ($("#cases").find(":animated").length > 0)
		return false;
	var currentTitle = $("#case-titles").children(":visible");
	var currentDescription = $("#case-descriptions").children(":visible");
	var bgPosition = parseInt($("#case-backgrounds").css("margin-left"));
	currentTitle.hide();
	currentDescription.hide();
	if (currentTitle.prev().length) {
		currentTitle.prev().fadeIn('fast', 'easeInExpo');
		currentDescription.prev().fadeIn('fast', 'easeInExpo');
		$("#case-backgrounds").animate({
			marginLeft: bgPosition + 960 + "px"
		}, 250, 'easeInExpo');
	}
	else {
		currentTitle.parent().children(":last").fadeIn('fast', 'easeInExpo');
		currentDescription.parent().children(":last").fadeIn('fast', 'easeInExpo');
		$("#case-backgrounds").animate({
			marginLeft: - ($("#case-backgrounds").children().length - 1) * 960 + "px"
		}, 500, 'easeInOutExpo');
	}
});  
      
});




















