$(function () {
  var loopTimer = {};
  jQuery.changeFocus = function(to) {
    //CLEARS EXCISTING TIMEOUTS 
    $.clearTimer(loopTimer);

    //SELECTS THE CURRENT IMAGE (FADE IN) AND FADES OTHERS OUT (TIME IN MILISECONDS)
    var billede = $("#animation-image" + to + "");
    billede.stop().fadeTo(400,1);
    var andrebilleder = $("#image-holder img");
    andrebilleder.not(billede).stop().fadeTo(1000,0);

    //SELECTS THE CURRENT DOT AND CHANGES ITS SRC, CHANGES ALL OTHERS BACK 
    var dot = $("#animation-selector #knap" + to + "");
    var alldots = $("#animation-selector img");
    valgt="fileadmin/templates/aarhusvalgmenighed_dk/images/animation-dot-valgt.png";
    ikkevalgt="fileadmin/templates/aarhusvalgmenighed_dk/images/animation-dot.png";
    alldots.not(dot).attr("src",ikkevalgt);
    dot.attr("src",valgt);

    //ANIMATES TEXT TO TOP:0 LEFT:0. OTHERS OUT...
    var textbox = $("#animation-tekst-box" + to + "");
    var alltextboxes = $(".animation-tekst-box");
    textbox.stop().animate({ 
    top: "0px",
    opacity: 1
    }, 800 );
    alltextboxes.not(textbox).stop().animate({ 
    top: "300px",
    opacity: 0
    }, 800 );
    
    //COUNTS THE DOTS AND DETERMINES NEXT AUTO-SHIFT AND TIMEOUT (5000ms)
    var antal = alldots.length;
    var nyFocus = parseInt(to)+1;
    if(nyFocus > antal) { var nyFocus = 1; }
    loopTimer = $.timer(5000,"$.changeFocus(" + nyFocus + ")");  
  }  
      
  //MAKES THE DOTS CLICKABLE 
  $("#animation-selector img").click(function() {
    var ny = this.id.replace(/knap/, "");
    $.changeFocus(ny);
  });
  $(".animation-tekst-box-inner p").click(function() {
    var link = $(this).find("a:first").attr("href");
    parent.location=link;
  });
    
  //RUNS THE changeFocus FUNCTION AND SETS TIMEOUT TO FIRST SHIFT
  $("#animation-tekst-box1").css({ "top": "0px", "opacity": "1" });
  $.changeFocus(1);
  loopTimer = $.timer(7000,"$.changeFocus(2)");


//FORSIDE
  //KALENDER
  $("#kalender ul li").mouseover(function() {
    $(this).addClass("ro");
  }).mouseout(function() {
    $(this).removeClass("ro");
  }).click(function() {
    var link = $(this).find("a").attr("href");
    parent.location=link;
  });
  
  //SEARCH
  $("#search input").focus(function() {
    var defvalue=this.defaultValue;
    if(this.value==defvalue) {
      this.value="";
    }
    $(this).css({"color":"#000000"});
  }).blur(function() {
    if(this.value=="") {
      this.value = this.defaultValue;
      $(this).css({"color":"#7c7151"});
    }
    if(this.value==" ") {
      this.value = this.defaultValue;
      $(this).css({"color":"#7c7151"});
    }
  });
  
  //ROLLOVER SOCIAL-BOX
  $("#social img").mouseover(function() {
    var sti = $(this).attr("src");
    var nysti = sti.replace(".", "-ro.");
    $(this).attr("src",nysti);
  }).mouseout(function() {
    var sti = $(this).attr("src");
    var glsti = sti.replace("-ro.", ".");
    $(this).attr("src",glsti);  
  });
  $("#kalender span.sehele").mouseover(function() {
    var sti = $(this).find("img").attr("src");
    nysti = sti.replace(".", "-ro.");
    $(this).find("img").attr("src",nysti);
    $(this).addClass("ro");
  }).mouseout(function() {
    var sti = $(this).find("img").attr("src");
    nysti = sti.replace("-ro.", ".");
    $(this).find("img").attr("src",nysti);
    $(this).removeClass("ro");
  }).click(function() {
    var link = $(this).find("a:first").attr("href");
    parent.location=link;
  });
  
  
  //ROLLOVER BOTTOM
  $("#footerWrap a img").mouseover(function() {
    var sti = $(this).attr("src");
    var nysti = sti.replace(".", "-ro.");
    $(this).attr("src",nysti);
  }).mouseout(function() {
    var sti = $(this).attr("src");
    var glsti = sti.replace("-ro.", ".");
    $(this).attr("src",glsti);  
  });

  //LIGHTBOX MAP

  $("#bottomWrap span.sehele").mouseover(function() {
    var sti = $(this).find("img").attr("src");
    nysti = sti.replace(".", "-ro.");
    $(this).find("img").attr("src",nysti);
    $(this).addClass("ro");
  }).mouseout(function() {
    var sti = $(this).find("img").attr("src");
    nysti = sti.replace("-ro.", ".");
    $(this).find("img").attr("src",nysti);
    $(this).removeClass("ro");
  })
  
  $("#adresse span.sehele").click(function() {
    $("#fadeall").css("display","block");
    $("#lightbox").css("display","block");
  });
  $("#closelightbox").click(function() {
    $("#fadeall").css("display","none");
    $("#lightbox").css("display","none");
  });
  
  $("#om span.sehele, .subpageboxes .box, .kalenderboxes .kalenderbox, .tx-tfcal-pi2 .kalenderbacklink, .tx-mp3list-pi1 .sermondownloaditem").mouseover(function() {
    $(this).addClass("ro");
  }).mouseout(function() {
    $(this).removeClass("ro");
  }).click(function() {
    parent.location = $(this).find("a").attr("href");
  });

  
  
});
//FONT REPLACEMENT
Cufon.replace('div #animation-tekst div p, #content h1, #contentWrap #topBillede #tekstfelt h1 a, #indholdWrap #undersidekarusel #undersidekaruseltekst span', { fontFamily: 'Apex New Book' });
Cufon.replace('#bottomWrap h2, #kalender h2, #indholdWrap #rightColumn h1, #kalender ul li div.date, #contentWrap #indholdWrap #subMenu h1, #content .subpageboxes .box h1, #content .kalenderboxes h2, #content .button a', { fontFamily: 'Apex New Medium', hover:true });
