$(document).ready(function(){

  if (/MSIE 6/i.test(navigator.userAgent) || /MSIE 7/i.test(navigator.userAgent)) {
    $("hr").remove();
  }
  if (/MSIE/i.test(navigator.userAgent) == false) {
    Cufon.replace("#menu a", {hover: true});
  }
  if (/MSIE 6/i.test(navigator.userAgent) == false) {
    Cufon.replace("h2");
    Cufon.replace("#pagetitle h2", {textShadow: "#016ca2 0px -1px 0px"});
    Cufon.replace("h3");
    Cufon.replace("h4", {hover:true});
    Cufon.replace("h5");
    Cufon.replace("#banner-yritys span", {textShadow: "#006bcb 0px -1px 0px"});
    Cufon.replace("#banner-sidemenu li a", {hover:true, textShadow: "#006bcb 0px -1px 0px"});
    Cufon.replace("#sidebar-menu li a", {hover:true, textShadow: "#006bcb 0px -1px 0px"});
  }
  
  $("ul").each(function(){
    $(this).find("li").last().addClass("last");
    $(this).find("li").first().addClass("first");
  });
  
  $("tr").each(function(){
    if ( $(this).find("td").size() > 1 ) {
      $(this).find("td").last().addClass("last");
    }
    $(this).find("td").first().addClass("first");
  });
  
  $("#palvelut-banner").qslider({
    transition: "fade"
  });
  
  $(".text, textarea").inputDefault();
  
  //banner
  
  $("#banner-slider li").mouseenter(function(){
    $(this).find(".banner-luelisaa").stop(true,true).fadeIn(200);
    $(this).mouseleave(function(){
      $(this).find(".banner-luelisaa").fadeOut(200);
    });
  });
  
  $("#banner-slider").qslider({
    autoDelay: 6000
  });
  
  //$("#banner-sidemenu li").first().addClass("act");
  
  $("#banner-sidemenu a").click(function(){
    if ($(this).parent().hasClass("act")) return false;
    $("#banner-sidemenu li.act").removeClass("act");
    $(this).parent().addClass("act");
  });
  
  $(".banner-luelisaa").each(function(){
    $(this).css({
        bottom: $(this).prev().height()
    }); 
  });
  
  //tabbed
  
  $("#tabbed-header a").first().addClass("act");
  
  $("#tabbed-header a").each(function(e){
    $(this).attr("rel",e);
  });
  $(".tabbed-content").each(function(e){
    $(this).attr("rel",e);
  });
  $("#tabbed-content").css({
    height: $(this).find(".tabbed-content:first").outerHeight()
  });
  
  function swapTab(rel) {
    var index = rel;
    $("#tabbed-content").animate({
      height: $(".tabbed-content").eq(rel).height()
    });
    if (index == 0) {
      $("#tabbed-content-wrap").animate({
        marginTop: 0
      }, 500, "easeInOutQuad");    
    } else {
      var newHeight = 0;
      for (i=0;i<index;i++) {
        newHeight += $(".tabbed-content").eq(i).outerHeight()
      }
      $("#tabbed-content-wrap").animate({
        marginTop: -newHeight
      }, 500, "easeInOutQuad");
    }
  }
  
  $("#tabbed-header a").click(function(){
    if ($(this).hasClass("act")) return false;
    $("#tabbed-header a.act").removeClass("act");
    $(this).addClass("act");
    Cufon.refresh();
    swapTab($(this).attr("rel"));
  });
  
  //forms
  
  $(".checkbox.mandatory").removeClass("mandatory").closest(".input").addClass("mandatory checkbox");
  $("#palaute").hide();
  $("#tarjous").hide();
  $("#vikailmoitus").hide();
  $("#ajankohtaiset").hide();
  
  $("#field_476_wrapper input").live('change', function(){
    if ($(this).attr("id") == "field_476_cb1") {
      if ($(this).is(":checked")) {
        $("#placer").after($("#tarjous"));
        $("#tarjous").slideDown(function(){
           if (/MSIE 6/i.test(navigator.userAgent) || /MSIE 7/i.test(navigator.userAgent)) {
            $("html:not(:animated),body:not(:animated)").animate({ scrollTop: $("#tarjous").offset().top -40}, 500 );
          }       
        });
      } else {
        $("#tarjous").slideUp();
      }
    }
    if ($(this).attr("id") == "field_476_cb2") {
      if ($(this).is(":checked")) {
        $("#placer").after($("#palaute"));
        $("#palaute").slideDown(function(){
          if (/MSIE 6/i.test(navigator.userAgent) || /MSIE 7/i.test(navigator.userAgent)) {
            $("html:not(:animated),body:not(:animated)").animate({ scrollTop: $("#palaute").offset().top -40}, 500 );
          }        
        });
      } else {
        $("#palaute").slideUp();
      }
    }
    if ($(this).attr("id") == "field_476_cb3") {
      if ($(this).is(":checked")) {
        $("#placer").after($("#vikailmoitus"));
        $("#vikailmoitus").slideDown(function(){
          if (/MSIE 6/i.test(navigator.userAgent) || /MSIE 7/i.test(navigator.userAgent)) {
            $("html:not(:animated),body:not(:animated)").animate({ scrollTop: $("#vikailmoitus").offset().top -40}, 500 );
          }        
        });
      } else {
        $("#vikailmoitus").slideUp();
      }
    }
    if ($(this).attr("id") == "field_476_cb4") {
      if ($(this).is(":checked")) {
        $("#placer").after($("#ajankohtaiset"));
        $("#ajankohtaiset").slideDown(function(){
          if (/MSIE 6/i.test(navigator.userAgent) || /MSIE 7/i.test(navigator.userAgent)) {
            $("html:not(:animated),body:not(:animated)").animate({ scrollTop: $("#ajankohtaiset").offset().top -40}, 500 );
          }        
        });
      } else {
        $("#ajankohtaiset").slideUp();
      }
    }
  });
  
  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").indexOf("*") > 0) {
          formOk = false;
          $(this).addClass("error");
        } else {
          $(this).removeClass("error");
        }
      }
      if ($(this).hasClass("checkbox")) {
      checkNum++;
        if ($(this).find("input.checkbox:checked").length > 0) {
          checked++;
          $(this).closest(".field_wrapper").prev().removeClass("error");
          Cufon.refresh();
        } else {
          $(this).closest(".field_wrapper").prev().addClass("error");
          Cufon.refresh();
        }
      }
    });
    //console.log(checkNum, checked);
    if (checked != checkNum) formOk = false;
    if (formOk == true) {
      $(".section:hidden").remove(); 
      //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("<h4>Kiitos</h4><p>Lomake vastaanotettiin onnistuneesti. Olemme sinuun yhteydessä asian tiimoilta mahdollisimman pian.</p>");
            $("#content .form_container").slideDown(500);
          });
        }
      });

    } else {
      $("html:not(:animated),body:not(:animated)").animate({ scrollTop: $(".error:first").offset().top -40}, 500 );  
    }
    return false;
  });

});
