$(document).ready(function() {
  // STH: 2009-05-25: Fix for fejl hvor 1. afsnit blev vist 2 gange p? siden
  //$('div.imgTxtOnDark_greyborderBottom:first').slideDown(1600);

  addHover();
  if ($('#mycarousel').length > 0) {
    initCarousel();
  }
  if ($('a.lightbox').length > 0) {
    $('a.lightbox').lightbox();
  }
  openInNewWindow(['pdf']);
  if ($.browser.msie) {
    $('#container').css('height', $('#container').height() + 'px');
    $('.IEFix').css({ visibility: 'hidden', display: 'block' })
  }
  // Midlertidig hack da DW ikke retunerer venstremenu og breadcrumb p? paragraphsetup
  try {
    var $currentLeftMenu = $('.venstremenu:first')
    if ($currentLeftMenu.html().indexOf('@venstremenu') > -1) {
      $currentLeftMenu.html($('#venstremenucontainer').html());
    }

    var $breadcrumb = $('#breadcrumb:first')
    if ($breadcrumb.html().indexOf('@DwLegend') > -1) {
      $breadcrumb.html($('#breadcrumbcontainer').html());
    }

  } catch (e) { }
});

function openInNewWindow(aExtentions) {
  for (var i = 0; i < aExtentions.length; i++) {
    var sSelector = 'a[href*="' + aExtentions[i] + '"]';
    $(sSelector).each(function() {
      $(this).attr('target', '_blank');
    });
  }
}

function addHover() {
  addImageHover();

  var aHoverEffect = [{ selector: '.hoverType'}];
  $(aHoverEffect).each(function() {
    var oElement = $(this.selector);
    oElement.bind('focus', function() {
      $(this).addClass('focus').addClass('active');
    });
    oElement.bind('blur', function() {
      $(this).removeClass('focus').removeClass('active');
    });
    oElement.bind('mouseover', function() {
      $(this).addClass('focus');
    });
    oElement.bind('mouseout', function() {
      if (!$(this).hasClass('active')) {
        $(this).removeClass('focus').removeClass('active');
      }
    });
  });

}


function addImageHover() {
  var aThemes = ['Blue'];

  $(aThemes).each(function() {
    $('.but' + this).each(function() {
      var oElement = $(this);
      var oAnchor = $('a', oElement);
      oElement.bind('mouseover', function() {
        $(this).addClass('hover');
      });
      oElement.bind('mouseout', function() {
        $(this).removeClass('hover');
      });
      if (typeof oAnchor != 'undefined') {
        $('div', oElement).bind('click', function() {
          oAnchor[0].click();
        });
      }
    });
  })
}


/* carousel */




function mycarousel_itemVisibleInCallback(carousel, item, i, state, evt) {
  // The index() method calculates the index from a
  // given index who is out of the actual item range.
  var idx = carousel.index(i, mycarousel_itemList.length);

  var oElement = carousel.add(i, mycarousel_getItemHTML(mycarousel_itemList[idx - 1]));
  oElement.hover(function() {
    $(this).addClass('hover');
  }, function() {
    $(this).removeClass('hover');
  })

  if (typeof mycarousel_itemList[idx - 1].onerror != 'undefined') {
  }
};

function mycarousel_itemVisibleOutCallback(carousel, item, i, state, evt) {
  carousel.remove(i);
};

/**
* Item html creation helper.
*/
function mycarousel_getItemHTML(item) {
  return $(item).html();
};

var mycarousel_itemList = [];


function initCarousel() {
  mycarousel_itemList = $('.CarouselItemContainer_TopBanner ul li');
  var easing = ((typeof window.easing != 'undefined') ? window.easing : 'easeInBack');
  $('#easing option').each(function() {
    $(this).attr('selected', ($(this).attr('text') == easing) ? true : false)
  });
  window.carousel = jQuery('#mycarousel').jcarousel({
    auto: 3,
    wrap: 'circular',
    animation: 2000,
    easing: easing,
    scroll: 1,
    itemVisibleInCallback: { onBeforeAnimation: mycarousel_itemVisibleInCallback },
    itemVisibleOutCallback: { onAfterAnimation: mycarousel_itemVisibleOutCallback },
    initCallback: mycarousel_initCallback
  });
}

function mycarousel_initCallback(carousel) {
  // Pause autoscrolling if the user moves with the cursor over the clip.
  carousel.clip.hover(function() {
    carousel.stopAuto();
  }, function() {
    carousel.startAuto();
  });

};


// Inds?t medarbejder info
/*  Medarbejderer hentes ud via templates og gemmes i et "madarbejder objekt":
aMedarbejder[prodId] = {navn, type, initialer....{andre egenskaber}}
*/

/*
var Template_MedarbejderInfoShort = '<a href="default.aspx?id=52&amp;ProductID=%prodId%"><div style="float: left; margin-right: 12px; background: url(/admin/public/getImage.aspx?Image=/Files/Billeder/Ecom/medarbejdere/%initialer%_thumb.jpg&Format=jpg&Width=100) no-repeat 0px 0px"  class="medarbejderThumb">&#160;</div>';
Template_MedarbejderInfoShort+='%navn%</a>';
var Template_RelateretLoesningInfoShort = '<div class="relateretLoesninger"><div style="float: left; margin-right: 12px; background: url(%thumb%) no-repeat 0px 0px"  class="relateretLoesningThumb">&#160;<img src="%thumb%" onerror="$(\'.relateretLoesningThumb:first\', $(this).parent().parent()).css(\'background-image\', \'url(/admin/public/getImage.aspx?Image=/files/system/grafik/thumbMangler.jpg&Format=jpg&Width=100)\')" style="display: none;" /></div>';
Template_RelateretLoesningInfoShort+='<br clear="both" /><a href="default.aspx?id=52&amp;ProductID=%prodId%">%navn%</a></div>';
*/

var Template_MedarbejderInfoShort = '<a href="default.aspx?id=52&amp;ProductID=%prodId%"><img style="float: left; margin-right: 12px;"  class="medarbejderThumb" src="/admin/public/getImage.aspx?Image=/Files/Billeder/Ecom/medarbejdere/%initialer%_thumb.jpg&Format=jpg&Width=100" /></div>';
Template_MedarbejderInfoShort += '%navn%</a>';


var Template_RelateretLoesningInfoShort = '<div class="relateretLoesninger"><div><img style="float: left; margin-right: 12px;"  class="relateretLoesningThumb" src="%thumb%" onerror="src=\'/admin/public/getImage.aspx?Image=/files/system/grafik/thumbMangler.jpg&Format=jpg&Width=100\'" /></div>';
Template_RelateretLoesningInfoShort += '<br clear="left" /><a href="default.aspx?id=52&amp;ProductID=%prodId%">%navn%</a></div></div>';

function indsaetMedarbejderInfoUdfraType(aMedarbejdere, sType, sTargetSelector, sTemplate, removeSelector_IfNoneFound) {
  var sHTML = '';
  var sTemplate = (typeof sTemplate != 'undefined' && sTemplate != '') ? sTemplate : Template_MedarbejderInfoShort;
  for (prodID in aMedarbejdere) {
    var aMedarbejder = aMedarbejdere[prodID];
    if (aMedarbejder.type.indexOf(sType) == 0) {
      var sTemp = sTemplate;
      sTemp = sTemp.replace(/%initialer%/g, aMedarbejder.initialer);
      sTemp = sTemp.replace(/%prodId%/g, prodID);
      sTemp = sTemp.replace(/%navn%/g, aMedarbejder.navn);
      sTemp = sTemp.replace(/%thumb%/g, aMedarbejder.thumb);
      sHTML += sTemp;
    }
  }
  if (sHTML != '') {

    $(sTargetSelector).each(function() {
      $(this).html(sHTML);
      if ($.trim($(this).html()) == '') {
        try {
          document.getElementById(sTargetSelector.substr(1, sTargetSelector.length - 1)).innerHTML = sHTML;
        } catch (e) {
        }
      }

    });
  } else {
    if (typeof removeSelector_IfNoneFound != 'undefined') {
      try {
        $(removeSelector_IfNoneFound).hide();
      } catch (e) {
      }
    }
  }


}

function genererThumbnailBillederMedLightboxTilReferenceSide() {
  var sHTML = '';
  var aDivs = $('#details div')
  var iNoOfImages = aDivs.length / 2;
  var iThumbWidth = 100;
  var iLargeWidth = 222;
  for (var i = 0; i < iNoOfImages; i++) {
    var sImagePath = $.trim($(aDivs[i]).html());
    var iWidth = (i > 0) ? iThumbWidth : iLargeWidth;
    var sClass = (i > 0) ? 'small' : 'large';
    sHTML += '<a class="lightbox" rel="referenceBilleder" href="' + sImagePath + '"';
    sHTML += ' title="' + $(aDivs[i + iNoOfImages]).html() + '">';
    sHTML += '<img class="' + sClass + '" src="/admin/public/getImage.aspx?Image=' + sImagePath + '&amp;Format=jpg&amp;Width=' + iWidth + '"/></a>';
    if (i == 0) {
      sHTML += '<br />'
    }
  }
  $('#refImages').html(sHTML + '<div class="clear"></div>');
  $('a.lightbox').lightbox();
}

(function($) {
  $.fn.fixOverflow = function() {
    if ($.browser.msie) {
      return this.each(function() {
        if (this.scrollWidth > this.offsetWidth) {
          $(this).css({ 'padding-bottom': '20px', 'overflow-y': 'hidden' });
        }
      });
    } else {
      return this;
    }
  };
})(jQuery);


function gotoURLUsingPIDFromMainParagraph(sURL) {
  var sPID = '';
  if (typeof sMainParagraphPID != 'undefined') {
    sPID = 'PID=' + sMainParagraphPID;
    if (sURL.indexOf('?') == -1) {
      sPID = '?' + sPID;
    } else {
      sPID = '&' + sPID;
    }
  }
  var l = document.location;
  var s = l.protocol + '//' + l.hostname + '/' + sURL + sPID;
  document.location = s;
}