var references = {

  init: function() {
  
    this.referencehash = "referenssi-"+window.location.hash.substring(1, window.location.hash.length);
    
    if (window.location.hash.length > 1) {
      $("#"+this.referencehash).css("display","block");
    } 
    
    else {
      $(".referenssi-content:first").css("display","block");
      window.location.hash = $(".referenssi-content:first").attr("id").substring(11,$(".referenssi-content:first").attr("id").length);
      this.getReferenceHash();
    }
    
    $(".referenssi").qcordion({
      useSlide: "yes", 
      slideContainer: ".qslider-container"
    });
    $(".referenssikuva").qnote();
    
  },
  
  getReferenceHash: function() {//haetaan uusi hash
  
    this.referencehash = "referenssi-"+window.location.hash.substring(1, window.location.hash.length);
    return this.referencehash;
    
  }/*,
  
 
  referenceSlide: function() {//referenssien slaidaaminen 
  
    $(".referenssi-otsikko").click(function(e){

      //kytketään vanha bind pois
      //$(".ref-navi").unbind();
      $(this).next().find(".referenssikuva-container").easySlider();
      
      //asetetaan oikeat kuvaurlit
      //references.setRefUrls(this);
      
      if ($(this).next().is(":visible") == false) {
        $(".referenssi-content:visible").not(this).slideToggle("slow");
        $(this).next().slideToggle("slow");
      }
      
    });
    
  },
  
  setRefUrls: function(element) {//tehdään taulukko urleista
     
    this.refUrls = [];
    //console.log(element);
    
    //laitetaan ensimmäiseksi oletuskuvan url
    this.refUrls.push($(element).next().find(".oletuskuva").attr("src"));
    
    //sitten loput
    $(element).next().find(".ref-urls").each(function(){
      references.refUrls.push($(this).html());
    });
    
    //console.log(this.refUrls);
    
  },
  
  toggleNavi: function(element) {//näytetään/piilotetaan kuvanavigointi
    console.log("togglenavi", element);
    $(element).find(".ref-navi").stop(true,true).fadeIn("fast");
    $(element).mouseleave(function(){
      $(this).find(".ref-navi").fadeOut("fast");
    });
  }*/

}
function addMap(){
  $("#kartta").remove();
  $("<iframe id='kartta' width='425' height='350' frameborder='0' scrolling='no' marginheight='0' marginwidth='0' src='http://maps.google.fi/maps?f=q&amp;source=s_q&amp;hl=fi&amp;geocode=&amp;q=vaajakoskentie+56+c+15,+40400+jyv%C3%A4skyl%C3%A4&amp;sll=62.243244,25.796432&amp;sspn=0.000638,0.00284&amp;ie=UTF8&amp;hq=&amp;hnear=Vaajakoskentie+56,+40400+Jyv%C3%A4skyl%C3%A4&amp;ll=62.248905,25.800362&amp;spn=0.013988,0.036478&amp;z=14&amp;iwloc=A&amp;output=embed'></iframe>").appendTo($("#kartta-holder"));
}

function palvelutInit() {
  var www = ["0px", "500px", "#e0027f", "0px"];
  var printti = ["-640px", "650px", "#b3cc07", "97px"];
  var ilme = ["-1280px", "500px", "#e10c0c", "194px"];
  var muut = ["-1920px", "400px", "#10a1d7", "291px"];
  
  var palveluhash = window.location.hash.substring(1, window.location.hash.length);
  if (window.location.hash.length > 1) {
    switch(palveluhash) {
      case "palvelu-www":
          $("#palvelut-entrycontainer").css("marginTop",www[0]);        
          $("#palvelut-content-back").css("backgroundColor",www[2]);        
          $("#palvelut-buttons-block").css({
            backgroundColor: www[2],
            top: www[3]
          });
        break;
      case "palvelu-printtisuunnittelu":
          $("#palvelut-entrycontainer").css("marginTop",printti[0]);        
          $("#palvelut-content-back").css("backgroundColor",printti[2]);        
          $("#palvelut-buttons-block").css({
            backgroundColor: printti[2],
            top: printti[3]
          });
        break;
      case "palvelu-ilme":
          $("#palvelut-entrycontainer").css("marginTop",ilme[0]);        
          $("#palvelut-content-back").css("backgroundColor",ilme[2]);        
          $("#palvelut-buttons-block").css({
            backgroundColor: ilme[2],
            top: ilme[3]
          });
        break;
      case "palvelu-muut-palvelut":
          $("#palvelut-entrycontainer").css("marginTop",muut[0]);        
          $("#palvelut-content-back").css("backgroundColor",muut[2]);        
          $("#palvelut-buttons-block").css({
            backgroundColor: muut[2],
            top: muut[3]
          });
        break;
    }
  } else {
    $(".palvelut-entrycontainer").css("marginTop",www[0]);
  }
  $(".palvelu-button").click(function(e){
    switch($(this).attr("id")) {
      case "palvelut-buttons-www":
        $("#palvelut-buttons-block").animate({
          top: www[3],
          backgroundColor: www[2]
        });
        $("#palvelut-content-back").animate({
          backgroundColor: www[2]
        });
        $("#palvelut-entrycontainer").animate({
          marginTop: www[0]
        });
        break;
      case "palvelut-buttons-print":
        $("#palvelut-buttons-block").animate({
          top: printti[3],
          backgroundColor: printti[2]
        });
        $("#palvelut-content-back").animate({
          backgroundColor: printti[2]
        });
        $("#palvelut-entrycontainer").animate({
          marginTop: printti[0]
        });
        break;
      case "palvelut-buttons-ilme":
        $("#palvelut-buttons-block").animate({
          top: ilme[3],
          backgroundColor: ilme[2]
        });
        $("#palvelut-content-back").animate({
          backgroundColor: ilme[2]
        });
        $("#palvelut-entrycontainer").animate({
          marginTop: ilme[0]
        });
        break;
      case "palvelut-buttons-muut":
        $("#palvelut-buttons-block").animate({
          top: muut[3],
          backgroundColor: muut[2]
        });
        $("#palvelut-content-back").animate({
          backgroundColor: muut[2]
        });
        $("#palvelut-entrycontainer").animate({
          marginTop: muut[0]
        });
        break;
    }
  });  
}

function yhteysInit() {
  var henkilosto = ["0px"];
  var paamaja = ["71px"];
  
  var yhteyshash = window.location.hash.substring(1, window.location.hash.length);
  if (window.location.hash.length > 1) {
    switch(yhteyshash) {
      case "henkilosto":
          $("#yhteystiedot-buttons-block").css("top",henkilosto[0]);
          $("#yhteystiedot-paamaja").css("display","none");          
        break;
      case "paamaja":
          $("#yhteystiedot-buttons-block").css("top",paamaja[0]);     
          $("#yhteystiedot-henkilosto").css("display","none");          
        break;
    }
  } else {
    $("#yhteystiedot-buttons-block").css("top",henkilosto[0]);
    $("#yhteystiedot-paamaja").css("display","none");
  }
  $("#yhteystiedot-buttons a").click(function(e){
    switch($(this).attr("id")) {
      case "yhteystiedot-button-henkilosto":
        $("#kartta").remove();
        $("#yhteystiedot-buttons-block").animate({
          top: henkilosto[0],
        });
        $("#yhteystiedot-content").fadeOut("fast", function(){
          $("#yhteystiedot-henkilosto").css("display","block");
          $("#yhteystiedot-paamaja").css("display","none");
          $(this).fadeIn("fast");
        });
        break;
      case "yhteystiedot-button-paamaja":
        $("#yhteystiedot-buttons-block").animate({
          top: paamaja[0],
        });
        $("#yhteystiedot-content").fadeOut("fast", function(){
          $("#yhteystiedot-paamaja").css("display","block");
          $("#yhteystiedot-henkilosto").css("display","none");
          $(this).fadeIn("fast", addMap());
        });
        break;
    }
  });  
}



$(document).ready(function(){

  //cufon
  Cufon.replace('h2');
  Cufon.replace('h3');
  Cufon.replace('.referenssi-link');
  Cufon.replace('#palvelut-buttons a');
  Cufon.replace('#yhteystiedot-buttons a');
  Cufon.replace(".premantle");
  Cufon.replace("#menu a");
  Cufon.replace("#etusivu-uusin-referenssi span");

  if (window.location.pathname.indexOf("referenssit") > -1) {
  
    references.init();    
    $(".referenssi-content").mouseenter(function(e) {
      //references.toggleNavi(this);
    });
    
  }
  
  if (window.location.pathname.indexOf("palvelut") > -1) {
    palvelutInit();
  }
  
  if (window.location.pathname.indexOf("yhteystiedot") > -1) {
    yhteysInit();
    if(window.location.hash.substring(1,window.location.hash.length) == 'paamaja'){
      addMap();
    }    
    
  }
  
  $(".text-input").inputDefault();
  
  $("h1 img").css("display","none");
  
  //logo hover
  $("h1").mouseenter(function(){
    $(this).find("img").fadeIn(1000);
    /*$(this).mouseleave(function(){
      $(this).find("img").fadeIn("slow");
    });*/
  });
});