$(document).ready(function() {
	dropDownMenu();
	autoSlide(5000, 0);
	addSeparators();
	calculator();
});

function dropDownMenu() {

	$("#menu ul a").removeAttr('title');
	$("#menu ul ul").css({display: "none"}); // Fixes opera Bug

	$("#menu ul li").hover(function() {
		$(this).find('ul:first').css({display: "block", opacity: 0}).stop().animate({ opacity: 1 }, 200); //Slides down when hover the UL
		$(this).children('a').addClass("hovered"); //Adds a hovered class, so you can see the menu path you are following
	}
	,function() {
		$(this).find('ul:first').css({display: "none"}); //Slides up on mouseleave
		$(this).children('a').removeClass("hovered"); //removes the hovered class.
	});

}

function nextSlide() {

	$current = $('#slider ul li.current');
	$next = $('#slider ul li.current').next();

	$current_sel = $('#slider_selector ul li.current');
	$next_sel = $('#slider_selector ul li.current').next();


	if($next.children('a').children('img').attr('alt') == undefined) {
		$next = $("#slider ul li:first");
		$next_sel = $("#slider_selector ul li:first");
	} else {
		//Do nothing. The $next element exists
	}

	$next.addClass('next').css({ opacity: 0 }).stop().animate({ opacity: 1 }, 300, function() {

		$next.addClass('current').removeClass('next');
		$current.removeClass('current');

	});

	$current_sel.removeClass('current');
	$next_sel.addClass('current');


}

function autoSlide($time, $stop) {

	$('#slider ul li:first').addClass('current');

	$total_slides = ($('#slider ul li').length);
	$i = 1



	$('#slider_selector').prepend('<ul></ul>');

	$('#slider ul li').each(function() {

		$(this).addClass('slider_'+$i);

		if($i == 1) {
			$('#slider_selector ul').append('<li class="current sel_'+$i+'"></li>');
		} else {
			$('#slider_selector ul').append('<li class="sel_'+$i+'"></li>');
		}

		$i++;

	})

	if(typeof(isClicked) == 'undefined') { var isClicked = 0; }

	$('#slider_selector ul li').click(function() {

		$id = $(this).attr('class').split(' ');

		if($id[0] == 'current' || $id[1] == 'current') {

			//do nothing

		} else {

			$id = $id[0].split('_');
			callSlide($id[1]);

			isClicked = 1;

		}

	});

	if(typeof(isHovered) == 'undefined') { var isHovered = 0; }
	//If no item has been hovered yet, the variable isHovered doesn't exist, so we create it.

	$("#slider ul li").hover(function() { // Whenever an item is hovered
		isHovered = 1; //Setting isHovered 1, we stop the autsliding from going on
	}, function() {
		isHovered = 0;//Setting isHovered 1, we make the autsliding go on
	});

	setInterval(function() {

		if($stop == 1) {

			if(isHovered == 0 && isClicked == 0) { //It only calls the next item IF no item is hovered

				nextSlide();

			}

		} else if(isClicked == 0) {

			nextSlide();

		}



	}, $time);

}

function callSlide($slideID) {

	$current = $('#slider ul li.current');
	$next = $('#slider ul li.slider_'+$slideID);

	$current_sel = $('#slider_selector ul li.current');
	$next_sel = $('#slider_selector ul li.sel_'+$slideID);

	$next.addClass('next').css({ opacity: 0 }).stop().animate({ opacity: 1 }, 300, function() {

		$next.addClass('current').removeClass('next');
		$current.removeClass('current');

	});

	$current_sel.removeClass('current');
	$next_sel.addClass('current');

}

function addSeparators() {

	$('#menu ul:first').children('li').each(function() {

		$(this).after('<li class="border"></li>');

	});

	$('.border:last').remove();

	$('#menu ul li ul').each(function() {

		$(this).children('li:first').children('a').css('border-top', 'none');
		$(this).children('li:last').children('a').css('border-bottom', 'none');

	});

	$('.content_item').each(function() {

		$(this).after('<div class="content_item_separator"></div>');

	});

	$('.content_item_separator:last').remove();

	$('.content_item_full').each(function() {

		$(this).after('<div class="content_item_full_separator"></div>');

	});

	$('.content_item_full_separator:last').remove();

	$('ul.ctabs_content').each(function() {
		$(this).children('li:last').children('a').css({ 'border-bottom': 'none' });
	});

}

function calculator() {

	$("#tiedot").hide();

	$("#laske").click(function(){

		$.Calculation.setDefaults({
		   // a regular expression for detecting European-style formatted numbers
		   reNumbers: /(-|-\$|€)?(\d+(\.\d{3})*(,\d{1,})?|,\d{1,})?/g
		   // define a procedure to convert the string number into an actual usable number
		   , cleanseNumber: function (v){
			  // cleanse the number one more time to remove extra data (like commas and dollar signs)
			  // use this for European numbers: v.replace(/[^0-9,\-]/g, "").replace(/,/g, ".")
			  return v.replace(/[^0-9,\-]/g, "").replace(/,/g, ".");
		   }
		})

		$("[id^=1_o_h_sairaus]").calc(

		// the equation to use for the calculation
		"tyontekijoita * tyopaivia * pituus * 52  * 0.048",
		// define the variables used in the equation, these can be a jQuery object
		{
			tyontekijoita: $("[id=tyontekijoita]"),
			tyopaivia: $("[id=tyopaivia]"),
			pituus: $("[id=tyopaivan_pituus]")
		},
		function (s){
				return s.toFixed(0) + "h"  ;
		}
		);

		$("[id^=1_o_e_sairaus]").calc(
		"sairausaika * tuntipalkka * 1.4",
		{
			sairausaika: $("[id=1_o_h_sairausaika]"),
			tuntipalkka: $("[id=tuntipalkka]")
		},
		function (s){
				return s.toFixed(0).replace(/\./,',') + "€";  ;
		}
		);

		$("[id^=1_o_h_lomaaika]").calc(
		"tyontekijoita * tyopaivia * pituus * 5",
		// define the variables used in the equation, these can be a jQuery object
		{
			tyontekijoita: $("[id=tyontekijoita]"),
			tyopaivia: $("[id=tyopaivia]"),
			pituus: $("[id=tyopaivan_pituus]")
		},
		function (s){
				return s.toFixed(0) + "h"  ;
		}
		);


		$("[id=1_o_h_lomaraha]").calc(
		"lomaaika * 0.5",
		// define the variables used in the equation, these can be a jQuery object
		{
			lomaaika: $("[id=1_o_h_lomaaika]")
		},
		function (s){
				return s.toFixed(0) + "h"  ;
		}
		);

		$("[id^=1_o_h_koulutusaika]").calc(

		// the equation to use for the calculation
		"tyontekijoita * pituus * 5",
		// define the variables used in the equation, these can be a jQuery object
		{
			tyontekijoita: $("[id=tyontekijoita]"),
			pituus: $("[id=tyopaivan_pituus]")
		},
		function (s){
				return s.toFixed(0) + "h"  ;
		}
		);

		$("[id^=1_o_e_koulutusaika]").calc(
		"koulutussaika * tuntipalkka * 1.4",
		{
			koulutussaika: $("[id=1_o_h_koulutusaika]"),
			tuntipalkka: $("[id=tuntipalkka]")
		},
		function (s){
				return s.toFixed(0).replace(/\./,',') + "€";  ;
		}
		);

		$("[id^=1_o_h_iltalisa]").calc(
		"iltavuoroja * 3 * 52",
		{
			iltavuoroja: $("[id=iltavuoroja]")
		},
		function (s){
				return s + "h"  ;
		}
		);

		$("[id=1_o_e_iltalisa]").calc(
		"iltalisa * 4.09",
		{
			iltalisa: $("[id=1_o_h_iltalisa]")
		},
		function (s){
				return s.toFixed(0).replace(/\./,',') + "€";
		}
		);

		$("[id^=1_v_h_iltalisa]").calc(
		"iltavuoroja * 3 * 52",
		{
			iltavuoroja: $("[id=iltavuoroja]")
		},
		function (s){
				return s + "h"  ;
		}
		);

		$("[id=1_v_e_iltalisa]").calc(
		"iltalisa * 4.09 * kerroin",
		{
			iltalisa: $("[id=1_o_h_iltalisa]"),
			kerroin: $("[id=kerroin]")
		},
		function (s){
				return s.toFixed(0).replace(/\./,',') + "€";
		}
		);

		$("[id^=1_o_h_lalisa]").calc(
		"lauantaivuoroja * 5 * 52",
		{
			lauantaivuoroja: $("[id=lauantaivuoroja]")
		},
		function (s){
				return s + "h"  ;
		}
		);

		$("[id=1_o_e_lalisa]").calc(
		"lalisa * 5.34",
		{
			lalisa: $("[id=1_o_h_lalisa]")
		},
		function (s){
				return s.toFixed(0).replace(/\./,',') + "€";
		}
		);

		$("[id^=1_v_h_lalisa]").calc(
		"lauantaivuoroja * 5 * 52",
		{
			lauantaivuoroja: $("[id=lauantaivuoroja]")
		},
		function (s){
				return s + "h"  ;
		}
		);

		$("[id=1_v_e_lalisa]").calc(
		"lalisa * 5.34 * kerroin",
		{
			lalisa: $("[id=1_v_h_lalisa]"),
			kerroin: $("[id=kerroin]")
		},
		function (s){
				return s.toFixed(0).replace(/\./,',') + "€";
		}
		);

		$("[id^=1_o_h_arkipyha]").calc(
		"tyontekijoita * pituus * 7",
		{
			tyontekijoita: $("[id=tyontekijoita]"),
			pituus: $("[id=tyopaivan_pituus]")
		},
		function (s){
				return s + "h"  ;
		}
		);

		$("[id=1_o_e_arkipyha]").calc(
		"tuntipalkka * arkipyha",
		{
			tuntipalkka: $("[id=tuntipalkka]"),
			arkipyha: $("[id=1_o_h_arkipyha]")
		},
		function (s){
				return s.toFixed(0).replace(/\./,',') + "€";
		}
		);

		$("[id^=1_v_h_tehdyt]").calc(
		"tyontekijoita * tyopaivia * pituus * 52",
		{
			tyontekijoita: $("[id=tyontekijoita]"),
			tyopaivia: $("[id=tyopaivia]"),
			pituus: $("[id=tyopaivan_pituus]")
		},
		function (s){
				return s + "h"  ;
		}
		);

		$("[id=1_v_e_tehdyt]").calc(
		"perustunnit * tuntipalkka * kerroin",
		{
			perustunnit: $("[id=1_v_h_tehdyt]"),
			tuntipalkka: $("[id=tuntipalkka]"),
			kerroin: $("[id=kerroin]")
		},
		function (s){
				return s.toFixed(0).replace(/\./,',') + "€";
		}
		);

		$("[id=1_o_h_tehdyt]").calc(
		"tehdyttunnit - arkipyha - sairausaika - lomaaika - koulutusaika",
		{
			tehdyttunnit: $("[id=1_v_h_tehdyt]"),
			arkipyha: $("[id=1_o_h_arkipyha]"),
			sairausaika: $("[id=1_o_h_sairausaika]"),
			lomaaika: $("[id=1_o_h_lomaaika]"),
			koulutusaika: $("[id=1_o_h_koulutusaika]")
		},
		function (s){
				return s + "h"  ;
		}
		);

		$("[id=1_o_e_tehdyt]").calc(
		"perustunnit * tuntipalkka",
		{
			perustunnit: $("[id=1_o_h_tehdyt]"),
			tuntipalkka: $("[id=tuntipalkka]")
		},
		function (s){
				return s.toFixed(0).replace(/\./,',') + "€";
		}
		);

		$("[id^=1_o_e_lomaaika]").calc(
		"0.125 * ((tuntipalkka * (tehdyttunnit + arkipyha + sairausaika + lomaaika + koulutusaika)) + ((4.09 * iltalisa) + (5.34 * lalisa)))",
		{
			tuntipalkka: $("[id=tuntipalkka]"),
			tehdyttunnit: $("[id=1_o_h_tehdyt]"),
			arkipyha: $("[id=1_o_h_arkipyha]"),
			sairausaika: $("[id=1_o_h_sairausaika]"),
			lomaaika: $("[id=1_o_h_lomaaika]"),
			koulutusaika: $("[id=1_o_h_koulutusaika]"),
			iltalisa: $("[id=1_o_h_iltalisa]"),
			lalisa: $("[id=1_o_h_lalisa]")
		},
		function (s){
				return s.toFixed(0).replace(/\./,',') + "€";
		}
		);

		$("[id=1_o_e_lomaraha]").calc(
		"lomaaika * 0.5",
		{
			lomaaika: $("[id=1_o_e_lomaaika]")
		},
		function (s){
				return s.toFixed(0).replace(/\./,',') + "€";
		}
		);

		var sum = $("[id^=1_o_e]").sum();

		$("#yht_o_palkat").text(sum.toFixed(0).replace(/\./,',') + "€");

		var sum = $("[id^=1_v_e]").sum();

		$("#yht_v_palkat").text(sum.toFixed(0).replace(/\./,',') + "€");

		$("[id=perh_o_palkat]").calc(
		"palkatyhteensa / tunnit",
		{
			palkatyhteensa: $("[id=yht_o_palkat]"),
			tunnit: $("[id=1_v_h_tehdyt]")
		},
		function (s){
				return s.toFixed(2).replace(/\./,',') + "€/h";
		}
		);

		$("[id=perh_v_palkat]").calc(
		"palkatyhteensa / tunnit",
		{
			palkatyhteensa: $("[id=yht_v_palkat]"),
			tunnit: $("[id=1_v_h_tehdyt]")
		},
		function (s){
				return s.toFixed(2).replace(/\./,',') + "€/h";
		}
		);

		$("[id=2_o_v_svm]").calc(
		"palkat * 0.0213",
		{
			palkat: $("[id=yht_o_palkat]")
		},
		function (s){
				return s.toFixed(0).replace(/\./,',') + "€";
		}
		);

		$("[id=2_o_v_tvm]").calc(
		"palkat * 0.0080",
		{
			palkat: $("[id=yht_o_palkat]")
		},
		function (s){
				return s.toFixed(0).replace(/\./,',') + "€";
		}
		);

		$("[id=2_o_v_tyel]").calc(
		"palkat * 0.1815",
		{
			palkat: $("[id=yht_o_palkat]")
		},
		function (s){
				return s.toFixed(0).replace(/\./,',') + "€";
		}
		);

		$("[id=2_o_v_rhvm]").calc(
		"palkat * 0.0007",
		{
			palkat: $("[id=yht_o_palkat]")
		},
		function (s){
				return s.toFixed(0).replace(/\./,',') + "€";
		}
		);

		$("[id=2_o_v_ttvm]").calc(
		"palkat * 0.01",
		{
			palkat: $("[id=yht_o_palkat]")
		},
		function (s){
				return s.toFixed(0).replace(/\./,',') + "€";
		}
		);

		var sum = $("[id^=2_o_v]").sum();

		$("#yht_o_vakuutukset").text(sum.toFixed(0).replace(/\./,',') + "€");

		$("[id^=3_o_h]").calc(
		"tyontekijoita * 1",
		{
			tyontekijoita: $("[id=tyontekijoita]")
		},
		function (s){
				return s ;
		}
		);

		$("[id=3_o_e_normtvs]").calc(
		"tyontekijoita * 100",
		{
			tyontekijoita: $("[id=tyontekijoita]")
		},
		function (s){
				return s.toFixed(0).replace(/\./,',') + "€";
		}
		);

		$("[id=3_o_e_saitvs]").calc(
		"tyontekijoita * 100",
		{
			tyontekijoita: $("[id=tyontekijoita]")
		},
		function (s){
				return s.toFixed(0).replace(/\./,',') + "€";
		}
		);

		$("[id=3_o_e_tthuolto]").calc(
		"tyontekijoita * 250",
		{
			tyontekijoita: $("[id=tyontekijoita]")
		},
		function (s){
				return s.toFixed(0).replace(/\./,',') + "€";
		}
		);

		$("[id=3_o_e_tilipussi]").calc(
		"tyontekijoita * 120",
		{
			tyontekijoita: $("[id=tyontekijoita]")
		},
		function (s){
				return s.toFixed(0).replace(/\./,',') + "€";
		}
		);

		var sum = $("[id^=3_o_e]").sum();

		$("#yht_o_sivukulut").text(sum.toFixed(0).replace(/\./,',') + "€");

		var sum = $("[id^=yht_o]").sum();

		$("#yhteensa_o_kulut").text(sum.toFixed(0).replace(/\./,',') + "€");

		var sum = $("[id^=yht_v]").sum();

		$("#yhteensa_v_kulut").text(sum.toFixed(0).replace(/\./,',') + "€");

		$("[id=perh_o_kulut]").calc(
		"kulutyhteensa / tunnit",
		{
			kulutyhteensa: $("[id=yhteensa_o_kulut]"),
			tunnit: $("[id=1_v_h_tehdyt]")
		},
		function (s){
				return s.toFixed(2).replace(/\./,',') + "€/h";
		}
		);

		$("[id=perh_v_kulut]").calc(
		"kulutyhteensa / tunnit",
		{
			kulutyhteensa: $("[id=yhteensa_v_kulut]"),
			tunnit: $("[id=1_v_h_tehdyt]")
		},
		function (s){
				return s.toFixed(2).replace(/\./,',') + "€/h";
		}
		);

		$("#tiedot").show();

	});

	$(".tip").tooltip({
	delay: 0,
	showURL: false,
	showBody: " - ",
	fade: 250
	});

}


