jQuery(function() {
  frontpageBGSwitch();
  initCufon();
  initProductPaging();
  initGalleries();
  initNews();
});

function frontpageBGSwitch() {
  var bagroundShift = jQuery("#BagroundShift");
  if (bagroundShift[0]) {
    var settings = {
      shiftStyle: "fade", //fade, fadewhite
      globalShowTime: 4000,
      startDelay: 4000
    };
    bagroundShift.startBannerShift(settings);
  }
}

function initProductPaging() {
  var product = jQuery("#Product");
  if (product[0]) {
    var back = jQuery("#ProductBack"), next = jQuery("#ProductNext"), sub3s = jQuery("#subLevel3 a");
    for (var i = 0; i < sub3s.length; i++) {
      //alert(jQuery(sub3s[i]).hasClass("active"));
      if (jQuery(sub3s[i]).hasClass("active") && sub3s.length > 1) {
        if (i === 0) {
          next.css("display", "block");
        } else if (i === sub3s.length - 1) {
          back.css("display", "block");
        } else {
          next.css("display", "block");
          back.css("display", "block");
        }
        next.click(function() { window.location.href = jQuery(sub3s[i + 1]).attr("href") });
        back.click(function() { window.location.href = jQuery(sub3s[i - 1]).attr("href") });
        break;
      }
    }
  }
}

function initCufon() {
  Cufon.replace('h1, #content h2');
}

function initGalleries() {
  //Rydde op og inds?tte informationer
  var images = jQuery("#CaseImages .paragraph img, #ProductImages .paragraph img");
  if (images[0]) {
    images.each(function() {
      var thisImage = jQuery(this), text = thisImage.next("em").text();
      thisImage.attr("imagehref", thisImage.attr("src").replace(".jpg", "_stor.jpg"))
               .attr("type", "image")
               .attr("description", text)
               .attr("showinfo", (text.length > 0));

    });
    var settings = {
      Corner_TR: '/files/Billeder/Priess2010/gfx/GalleryCorner.gif',
      Corner_BL: '/files/Billeder/Priess2010/gfx/GalleryCorner.gif',
      Corner_BR: '/files/Billeder/Priess2010/gfx/GalleryCorner.gif',
      Corner_TL: '/files/Billeder/Priess2010/gfx/GalleryCorner.gif',
      ArrowPrev: '/files/Billeder/Priess2010/gfx/GalleryPrev.gif',
      ArrowNext: '/files/Billeder/Priess2010/gfx/GalleryNext.gif',
      Close: '/files/Billeder/Priess2010/gfx/btn_close.gif',
      PaddingTop: 14,
      PaddingRight: 14,
      PaddingBottom: 14,
      PaddingLeft: 14,
      Play: null,
      Stop: null,
      showImageList: false,
      imageInformationHeight: 20,
      imageInformationTemplate: '[description]'
    };

    var co3Gallery = $("#CaseImages .paragraph img, #ProductImages .paragraph img").CreateGallery(settings);
  }
}

function initNews() {
  var news = jQuery("#NewsList .news"), imageHits = 0, col2 = jQuery("#col2 .paragraph:first");
  //alert(news.length);
  if (news[0]) {
    for (var i = 0; i < news.length; i++) {
      var thisNews = jQuery(news[i]), image = thisNews.attr("image");
      if (image && image.length > 0) {
        imageHits++;
        //<span class="paragraphimage left"><img alt="" src="/files/billeder/priess2010/stemningsbilleder/arkitektmast.jpg"> </span>
        col2.append('<span class="paragraphimage left"><a href="' + thisNews.attr("link") + '"><img src="' + image + '" title="' + thisNews.attr("imagetext") + '" alt="' + thisNews.attr("imagetext") + '" /></a></span>');
      }
    }
  } else {
    news = jQuery("#NewsListSingle .smallImages span");
    jQuery("#col2 .paragraph:first").append(news);

  }

}
