$(document).ready(function() {

  /* hide search */

  $(".search input[type=text]").focus(function(){ this.value = ""; });

  /* carousel */

  $("#carousel .next_button").click(function(){
  
    $(this).hide();
    $("#carousel .carousel_text").animate({width: "0px"}, 200, carouselNext);
    return false;
    
  });

  $("#carousel .prev_button").click(function(){
  
    $(this).hide();
    $("#carousel .carousel_text").animate({width: "0px"}, 200, carouselPrev);
    return false;
    
  });

  function carouselNext(){
  
    $("#carousel img.current")
      .css("zIndex", 3)
      .animate({
        left: "0px",
        top: "134px",
        width: "160px",
        height: "86px"
      }, 500, "easeOutBack", function(){ $(this).removeClass("current").addClass("previous"); });
      
    $("#carousel img.previous")
      .css("zIndex", 1)
      .animate({
        left: "400px",
        top: "154px",
        width: "100px",
        height: "54px"
      }, 500, "easeOutBack", function(){ $(this).removeClass("previous").addClass("behind"); });
    
    $("#carousel img.behind")
      .css("zIndex", 1)
      .animate({
        left: "800px",
        top: "134px",
        width: "160px",
        height: "86px"
      }, 500, "easeOutBack", function(){ $(this).removeClass("behind").addClass("next"); });

    $("#carousel img.next")
      .css("zIndex", 4)
      .animate({
        left: "166px",
        top: "4px",
        width: "628px",
        height: "339px"
      }, 500, "easeOutBack", function(){ $(this).removeClass("next").addClass("current");

        $("#carousel .carousel_text").animate({width: "305px"}, 300);
        carouselText = "#" + $(this).attr("id") + "_text";
        $("#carousel .carousel_text div").hide();
        var teksti = $(carouselText).html();
        $("#carousel .carousel_text").append("<div>"+teksti+"</div>");
        $("#carousel .next_button").show();


//        $("#carousel .carousel_text").animate({width: "305px"}, 300);
//        carouselText = "#" + $(this).attr("id") + "_text";
//        $("#carousel .carousel_text div").hide();
//        $(carouselText).show();
//        $("#carousel .next_button").show();
      });
  }
    

  function carouselPrev(){
  
    $("#carousel img.current")
      .css("zIndex", 3)
      .animate({
        left: "800px",
        top: "134px",
        width: "160px",
        height: "86px"
      }, 500, "easeOutBack", function(){ $(this).removeClass("current").addClass("next"); });
      
    $("#carousel img.previous")
      .css("zIndex", 4)
      .animate({
        left: "166px",
        top: "4px",
        width: "628px",
        height: "339px"
      }, 500, "easeOutBack", function(){ $(this).removeClass("previous").addClass("current");
        $("#carousel .carousel_text").animate({width: "305px"}, 300);
        carouselText = "#" + $(this).attr("id") + "_text";
        $("#carousel .carousel_text div").hide();
        var teksti = $(carouselText).html();
        $("#carousel .carousel_text").append("<div>"+teksti+"</div>");
        $("#carousel .prev_button").show();
      });
    
    $("#carousel img.behind")
      .css("zIndex", 2)
      .animate({
        left: "0px",
        top: "134px",
        width: "160px",
        height: "86px"
      }, 500, "easeOutBack", function(){ $(this).removeClass("behind").addClass("previous"); });

    $("#carousel img.next")
      .css("zIndex", 1)
      .animate({
        left: "400px",
        top: "154px",
        width: "100px",
        height: "54px"
      }, 500, "easeOutBack", function(){ $(this).removeClass("next").addClass("behind"); });
        
  }
  
  
  function triggerCarousel(){
  
    $("#carousel .next_button").triggerHandler("click");
  
  }
  
  $("#carousel").hover(function(){

    window.clearInterval(play);

  }, function(){
  
    play=window.setInterval(triggerCarousel, 4000);
  
  });

  play=window.setInterval(triggerCarousel, 4000);
  
  



  /* references */
  var height = 0;
    $(".references_page").each(function() {
    height = ($(this).height() > height) ? $(this).height() : height;
  });
  
  $(".references_page, .references_spacer, .references_spacer_yrityksille").css('height',height+'px');
  $(".references_main, .references").css('height',(height+20)+'px');

  $(".scrollable_main").scrollable({
    circular: "true",
    items: ".references_page",
    next: ".references_next",
    prev: ".references_prev"
  }).navigator({
    navi: ".references_navi"
  });

  $(".scrollable").scrollable({
    circular: "true",
    items: ".references_page",
    next: ".references_next",
    prev: ".references_prev"
  }).navigator({
    navi: ".references_navi"
  });


  /* stretch blog */
  
  leftColumn = $(".column_left_main").height();
  rightColumn = $(".column_right_main").height();
  blogHeight = $(".blogi").height();
  rekryHeight = $(".rekry").height();

  if (leftColumn < rightColumn){
  
    stretch = blogHeight + (rightColumn - leftColumn) +10;
    $(".blogi").height(stretch);

  }
  
  if (leftColumn > rightColumn){
  
    stretch = rekryHeight + (leftColumn - rightColumn) -10;
    $(".rekry").height(stretch);
  
  }
  
  
  /* write comment */
  
  $(".writecomment").click(function(){
    $("form.comment textarea").focus();  
  });
  
  
  /* share */
  
  $(".share .mail").click(function(){
    $("#share").show();
    return false;
  });

  $("#share .close").click(function(){
    $("#share").hide();
    return false;
  });


  /* lightbox */

  $(".sidegallery a.thumb, .gallery_wrapper a").lightBox({
    overlayBgColor: "#e0e0e0",
    overlayOpacity: 0.8,
    containerResizeSpeed: 100,
    imageBtnClose: '/templates/saranen/images/lightbox_close.png',
    imageBtnPrev: '/templates/saranen/images/lightbox_prev.png',
    imageBtnNext: '/templates/saranen/images/lightbox_next.png',
    txtImage: 'Kuva',
    txtOf: '/'
  });
  
  $('.gray480_top.programBox').click(function() {
    $(this).next().find('a.toggleArrow').click();
    return false;
  });
  $('.gray480_bottom.programBox').click(function() {
    $(this).prev().find('a.toggleArrow').click();
    return false;
  });
  $('.gray480.programBox .title').click(function() {
    $(this).find('a').click();
    return false;
  });
  $('.gray480.programBox a.toggleArrow').click(function() {
    $(this).parent().next().slideToggle(200).parent().toggleClass('open');
    return false;
  });
  
  $('table.list.sort').tablesorter();

  $('#pagerNeeded').pager('ul');

  $('a.external').click(function() { 
    window.open($(this).attr('href'));
    return false; 
  });
  
});

function fbs_click() {
  u=location.href;
  t=document.title;
  window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');
  return false;
}
function linkedIn_click() {
  u=location.href;
  t=document.title.substr(0,document.title.length-24)
  s=encodeURIComponent('Saranen Consulting Oy');
  window.open('http://www.linkedin.com/shareArticle?mini=true&url='+encodeURIComponent(u)+'&title='+encodeURIComponent(t)+'&source='+s,'linkedin','toolbar=0,status=0,width=520,height=570');
  
  return false;
}
function twitter_click() {
  var u = location.href;
    var t = document.title.substr(0,document.title.length-24)

  var str = t+' - '+u; 
  str = str.replace(/ /g,'+');
  window.open('http://twitter.com/home/?status='+str);
  
  return false;
}
function cv_popup(opening_id) {
  if($('#cvPopup:visible').length) {
    $('#cvPopup').fadeOut(200);
  }
  else {
    $('#cvPopup').fadeIn(200);
    $('#cvPopup input.opening_id').val(opening_id);
  }
}
