﻿jQuery(function () {
  initAddThis();
  //initValidations();
  initForhandlerDropDown();
  initKontaktFormManip();
  boxStat();
});

window.onload = function () {
  orderCols();
};

var gridHeight = 170, gridMargin = 12;
function orderCols() {
  //Fixer højderne på alle undersiders kolonneindhold
  jQuery("#ContentArea:not(.frontpage #ContentArea,) .col").each(function (i) {
    var col = jQuery(this), boxes = col.find(".box .boxInside");
    
    boxes.each(function (y) {
      var thisBox = jQuery(this),
          addMargin = thisBox.parent().children(".boxTop").css("display") === "none" && thisBox.parent().children(".boxBottom").css("display") === "none",
          boxOuterHeight = thisBox.outerHeight(),
          padding = parseInt(thisBox.css("padding-top")) + parseInt(thisBox.css("padding-bottom")),
          boxHeight = thisBox.height(),
          numberOfRows = Math.ceil(boxOuterHeight / (gridHeight + gridMargin)),
          calcHeight = numberOfRows * gridHeight + (gridMargin * (numberOfRows - 1)) - padding,
          isNotLast = thisBox.parent().next().is(".box");
      if (addMargin /*&& isNotLast*/) {
        thisBox.parent().css("margin-bottom", gridMargin);
      }
      thisBox.height(calcHeight);
    });
  });

  var frontpageBoxes = jQuery(".frontpage #ContentArea .col .box");
  if (frontpageBoxes[0]) {
    frontpageBoxes.each(function() {
      var thisBox = jQuery(this),
      boxOuterHeight = parseInt(thisBox.attr("backgroundImageHeight")),
      padding = parseInt(thisBox.css("padding-top")) + parseInt(thisBox.css("padding-bottom"));

      var addMargin = thisBox.parent().children(".boxTop").css("display") === "none" && thisBox.parent().children(".boxBottom").css("display") === "none",
          boxOuterHeight = thisBox.outerHeight(),
          padding = parseInt(thisBox.children(".boxBg").css("padding-top")) + parseInt(thisBox.children(".boxBg").css("padding-bottom")),
          boxHeight = thisBox.height(),
          numberOfRows = Math.ceil(boxOuterHeight / (gridHeight + gridMargin)),
          calcHeight = numberOfRows * gridHeight + (gridMargin * (numberOfRows - 1)) - padding;
      thisBox.children(".boxBg").height(calcHeight);
    });
  }
}

function initForhandlerDropDown() {
  jQuery("#ForhandlerSelect").bind("change", function () {
    var chosenoption = this.options[this.selectedIndex];
    if (chosenoption.value != "nothing") {
      window.location.href = chosenoption.value;
    }

  });
}

function initAddThis() {
  var newsList = jQuery("#ContentArea .box4 .newsList, #ContentArea .box4 .newsItem");
  if(newsList[0]){
    newsList.closest(".boxInside").append('<a class="addthis_button" href="http://www.addthis.com/bookmark.php?v=250&amp;username=xa-4bf4f58e6de0955d"><img src="http://s7.addthis.com/static/btn/sm-share-en.gif" width="83" height="16" alt="Bookmark and Share" style="border:0"/></a><script type="text/javascript" src="http://s7.addthis.com/js/250/addthis_widget.js#username=xa-4bf4f58e6de0955d"></script>');
  }
}

  function initKontaktFormManip() {
    jQuery("select[name='fmanpkonaf']").parent().parent().css("display","none");
    jQuery("input[name='kontaktes_af']").parent().parent().css("display","none");
    jQuery("input[name='kontaktes_af']").parent().parent().prev().css("display","none");
    jQuery(".KontaktManip").append("<br />"+ jQuery("select[name='fmanpkonaf']").parent().html());
    jQuery("select[name='fmanpkonaf']").addClass("selectFormManipOrg");
    jQuery(".KontaktManip select").removeClass("selectFormManipOrg");
    jQuery(".KontaktManip select").addClass("selectFormManipNy");

    jQuery(".selectFormManipNy").change(function() {
      jQuery("input[name='kontaktes_af']").val(jQuery(".selectFormManipNy").val());
    });
  }

function boxStat() {
  jQuery(".boxStat").each(function(){
    _gaq.push(['_trackEvent', 'Hot Spots', jQuery(this).attr("name"), 'Visninger']);
    jQuery(this).bind("click", function(){
      _gaq.push(['_trackEvent', 'Hot Spots', jQuery(this).attr("name"), 'Klik']);
    });
  });
}
