$(document).ready(function(){

  if (/MSIE 6/i.test(navigator.userAgent) || /MSIE 7/i.test(navigator.userAgent)) {
    $("hr").remove();
  }
  if (/MSIE 6/i.test(navigator.userAgent) == false) {
    Cufon.replace("#menu a", {hover: true});
    Cufon.replace("h2");
    Cufon.replace("h3");
    Cufon.replace(".title span");
    Cufon.replace("#slogan");
  }
  
  $(".text, textarea").inputDefault();
  
  $("ul").each(function(){
    $(this).find("li").last().addClass("last");
    $(this).find("li").first().addClass("first");
  });
  
  $(".tab").each(function(){
    $(this).find(".row").last().addClass("last");
    $(this).find(".row").first().addClass("first");
  });
  
  $("tr").each(function(){
    if ( $(this).find("td").size() > 1 ) {
      $(this).find("td").last().addClass("last");
    }
    $(this).find("td").first().addClass("first");
  });
  
  $("table").each(function(){
    $(this).find("tr:even").addClass("even");
  });
    
  $(".fancybox").fancybox({
    titleShow: false
  });
  
  $("form .double").first().addClass("first");
  
  for (i=0;i<$("#banner li").size();i++) {
    $("#banner-buttons").append("<div class='banner-button' />");
  }
  
  $(".banner-button").first().addClass("act");
  $("#banner li").hide().first().show();

  var bannerPos = 0;
  var maxPos = $("#banner li").size() -1;

  function autoFade() {
    bannerPos++;
    $(".banner-button.act").removeClass("act").next().addClass("act");
    if (bannerPos > maxPos) {
      bannerPos = 0;
      $("#banner li").not(":first").fadeOut(1000);
      $(".banner-button.act").removeClass("act");
      $(".banner-button").first().addClass("act");
    } else {
      $("#banner li").eq(bannerPos).fadeIn(1000);
    }
  }

  setInterval(autoFade, 5000);

  //sidebar
  
  $(".sidebar-menu").each(function(){
    $(this).find(".item").first().addClass("first");
    $(this).find(".item").last().addClass("last");
  });
  
  $("#sidebar .title").mouseenter(function(){
    $(this).find("img").stop(true,true).animate({
	  right: 15
	},100);
	$(this).mouseleave(function(){
	  $(this).find("img").animate({
	    right: 10
	  },100);
	});
  });
  
  $("#sidebar .item img").css({opacity:0});
  
  $("#sidebar .item").mouseenter(function(){
    $(this).find("img").stop(true,true).animate({
	  left: 20,
	  opacity: 1
	},100);
    $(this).stop(true,true).animate({
	  paddingLeft: 30
	},100);
	$(this).mouseleave(function(){
	  $(this).find("img").animate({
	    left: 10,
		opacity: 0
	  },100);
	  $(this).animate({
		paddingLeft: 20
      },100);
	});
  });
  
  //etusivu
  
  $("#etusivu-links .quad").last().addClass("last");
  
  
  //btn
  
  $(".btn.arrowed img").css({opacity:0});
  $(".btn.arrowed").mouseenter(function(){
    $(this).find("img").stop(true,true).animate({
	  opacity: 1
	},100);
    $(this).stop(true,true).animate({
	  paddingRight: 35
	},100);
	$(this).mouseleave(function(){
	  $(this).find("img").animate({
		opacity: 0
	  },100);
	  $(this).animate({
		paddingRight: 15
      },100);
	});
  });
  
  //käyttäjätuki
  
  $(".tab .content").not(".nohide").hide();
  if (window.location.hash) {
    var openTab = window.location.hash.substr(1,window.location.hash.length);
    $("#"+openTab+"").show().prev().find(".trigger").text("Sulje välilehti");
  }
  
  $(".tab .trigger").click(function(){
    if ($(this).text() == "Sulje välilehti") {
      $(this).parent().next().slideUp(200);
      $(this).text("Avaa välilehti");
    } else {
      $(".tab .trigger").text("Avaa välilehti");
      $(this).text("Sulje välilehti");
      $(".tab .content:visible").slideUp(200);
      $(this).parent().next().slideDown(200);
    }
  });

  //forms
  
  $(".checkbox.mandatory").removeClass("mandatory").closest(".input").addClass("mandatory checkbox");
  $(".radio.mandatory").removeClass("mandatory").closest(".input").addClass("mandatory radio");
  $("select.mandatory").removeClass("mandatory").closest(".input").addClass("mandatory select");
  $("textarea").addClass("text");
  
  var formOk = false;
  var checkNum = 0;
  var checked = 0;
  
  $("#content form").submit(function(){
    checkNum = 0;
    checked = 0;
    formOk = true;
    $(".mandatory").each(function(){
      if ($(this).hasClass("text")) {
        if ($(this).attr("value") == "") {
          formOk = false;
          $(this).addClass("error");
          $(this).closest(".field_wrapper").find(".input_title").addClass("error");
        } else {
          $(this).removeClass("error");
          $(this).closest(".field_wrapper").find(".input_title").removeClass("error");
        }
      }
      if ($(this).hasClass("checkbox")) {
      checkNum++;
        if ($(this).find("input.checkbox:checked").length > 0) {
          checked++;
        }
      }
      if ($(this).hasClass("radio")) {
      checkNum++;
        if ($(this).find("input.radio:checked").length > 0) {
          checked++;
          $(this).closest(".field_wrapper").find(".input_title").removeClass("error");
        } else {
          $(this).closest(".field_wrapper").find(".input_title").addClass("error");
        }
      }
      if ($(this).hasClass("select")) {
      checkNum++;
      //console.log($(this).find("option:first"), $(this).find("option:selected"), "asd");
        if ($(this).find("select").val() != "") {
          checked++;
          $(this).closest(".field_wrapper").find(".input_title").removeClass("error");
        } else {
          $(this).closest(".field_wrapper").find(".input_title").addClass("error");
        }
      }
    });
    //console.log(checkNum, checked);
    if (checked != checkNum) formOk = false;
    if (formOk == true) {
      //console.log("ok");
      $.ajax({
        type: "POST",
        url: $("#content form").attr("action"),
        data: $("#content form").serialize(),
        success: function(msg){
          $("#content .form_container").slideUp(500, function(){
            $("#content .form_container").html("<div class='col single'><h4>Kiitos</h4><p>Lomake vastaanotettiin onnistuneesti. Olemme sinuun yhteydessä asian tiimoilta mahdollisimman pian.</p></div><div class='clearer'></div>");
            $("#content .form_container").slideDown(500);
          });
        }
      });

    }  
    return false;
  });
  
});
function feedbackCheck(value) {
	if(value == 'Muu') {
		$('#field_623_wrapper').show();
	}
	else {
		$('#field_623_wrapper').hide();
	}
}
// 
