$(document).ready(function() {
    paperFoldMouseOver();
    initAccordion();
    initTeaser();
    setTimeout(initNavDrop,2000);
    t = setTimeout(flowTeaser,8000);
    watchTeaser();
});

var teaserInt = 1;
var lastTeaser = 1;
var t;

function initTeaser() {
  $(".teaser_box").hide();
  $("#teaser1").fadeIn("slow");
}

function paperFoldMouseOver() {
  $('a.navLink2').hover(function() {
    $('#navLevel2').css({"z-index": "-100"});
    $('#content_area').css({"z-index": ""});
    $('#navLevel2Filters').css({"z-index": "-100"});
  }, function () {
    $('#navLevel2').css({"z-index": ""});
    $('#navLevel2Filters').css({"z-index": ""});
    initNavDrop();
  });
  $('a.navLink3').hover(function() {
    $('#navLevel3').css({"z-index": "-100"});
    $('#content_area').css({"z-index": ""});
  }, function () {
    $('#navLevel3').css({"z-index": ""});
    initNavDrop();
  });
  $('a.navLink4').hover(function() {
    $('#navLevel4').css({"z-index": "-100"});
    $('#content_area').css({"z-index": ""});
  }, function () {
    $('#navLevel4').css({"z-index": ""});
    initNavDrop();
  });
  $('a.navLink5').hover(function() {
    $('#navLevel5').css({"z-index": "-100"});
    $('#content_area').css({"z-index": ""});
  }, function () {
    $('#navLevel5').css({"z-index": ""});
    initNavDrop();
  });
}

function initAccordion() {
  $("#accordion").accordion({ header: "h2" , collapsible: true ,  autoHeight: false });
}

function initNavDrop() {
  $('#content_area').css({"z-index": "500"});
}

function flowTeaser() {
  moveNextTeaser();
  t = setTimeout(flowTeaser,8000);
}

function watchTeaser() {
  setLastTeaser();
  $(".next_btn a").click(function () {
    clearTimeout(t);
    moveNextTeaser()
  });
  $(".prev_btn a").click(function () {
    clearTimeout(t);
    movePrevTeaser();
  });
}

function moveNextTeaser() {
    nextTeaser = teaserInt + 1;
    if ($('#teaser'+nextTeaser).length > 0) {
      transTeaser(teaserInt,nextTeaser);
      teaserInt = teaserInt + 1;
    } else {
      transTeaser(teaserInt,1);
      teaserInt = 1;
    }
}

function movePrevTeaser() {
  prevTeaser = teaserInt - 1;
  if (prevTeaser == 0) {
    prevTeaser = lastTeaser;
    transTeaser(teaserInt,prevTeaser);
    teaserInt = prevTeaser;
  } else {
    transTeaser(teaserInt,prevTeaser);
    teaserInt = prevTeaser;
  }
}

function transTeaser(oldTeaser,newTeaser) {
  $("#teaser"+oldTeaser).fadeOut("slow",function() {
    $("#teaser"+newTeaser).fadeIn("slow");
  });
}

function setLastTeaser() {
  i = 0;
  $(".teaser_box").each( function() {
    i = i + 1;
  });
  lastTeaser = i;
}


