﻿jQuery(function () {
  initAddThis();
  //initValidations();
  initForhandlerDropDown();
  initKontaktFormManip();
  initdivisionsTable();
  initdivisionsTable_2();
  initdivisionsTable_3();
  initdivisionsTable_4();
});

window.onload = function () {
  orderCols();
  boxStat();
};

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 initValidations() {
//  
//  /*
//  settings skal sendes med i ReadyValidate metoden, når man gør een 
//  eller flere formularer klar til validering
//  */
//  var settings = {
//   /*
//    onFieldValidateFalse og onFieldValidateTrue
//    Sender man en string med i disse to vil de
//    blive sat som class på det enkelte felt,
//    hvis det validerer eller fejler.
//    Alternativt kan man sende en funktion med.
//    funktionerne bliver kaldt efter valideringsprocessen,
//    med følgende parametre:
//    onFieldValidateFalse(ele, failStr);
//    onFieldValidateTrue(ele);
//    */
//    onFieldValidateFalse: "fail",
//    onFieldValidateTrue: "valid",
//    /*
//    valArr er en samling af valideringer der skal køres på en eller flere formularer.
//    Det er ikke nødvendigvis alle valideringer der skal køres på samme formular
//    Pluginet vil teste om formularen indeholder nogle af felterne, men først on submit.
//    Rækkefølgen for er: valideringstype, css select der skal ramme feltet, fejlbesked, 
//    et antal ekstra parametre (bestemmes af valideringstypen)
//    */
//    valArr: [["notempty", "[name=AccessUserEmail]", "Email skal udfyldes"],
//              ["isMail", "[name=AccessUserEmail]", "Email skal være en email (logisk)"],
//              ["notempty", "[name=AccessUserName]", "Navn skal udfyldes"]]
//  };

//  jQuery("form.newsletterSubscribe, form.newsletterUnSubscribe").ReadyValidate(settings);

//}

  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());
    });
  }



/* Divsions START */
String.prototype.trim = function () { return this.replace(/^\s+|\s+$/, ''); };

var divisionsTable, dataListCols, dataListCol1;
function initdivisionsTable() {
  divisionsTable = jQuery("#DivisionsTable");

  if (divisionsTable[0]) {
    dataListCols = divisionsTable.find(".dataListCol");
    dataListCol1 = dataListCols.filter("#DataListCol1");
    addGroupHeaders();
    splitDataList();
    finalizeDataList();
    divisionsLinks();
    initFirstDivisionItem();
  }
}

function addGroupHeaders() {
  var lastGroupHeader = "";
  dataListCol1.children().each(function (i) {
    var thisDD = jQuery(this);
    if (lastGroupHeader !== thisDD.attr("productgroups")) {
      lastGroupHeader = thisDD.attr("productgroups");
      thisDD.before("<dt>" + lastGroupHeader + "</dt>");
    }
  });
}

function finalizeDataList() {
  var lastEles = [];
  dataListCols.each(function (i) {
    var thisCol = jQuery(this);

    if(thisCol.children(":first")[0]) {
      if (thisCol.children(":first")[0].tagName.toLowerCase() === "dd") {
        var lastDT = null;
        for (var y = i - 1; y >= 0; y--) {
          lastDT = jQuery(dataListCols[y]).children("dt:last");
          if (lastDT[0]) {
            break;
          }
        }
        thisCol.prepend(lastDT.clone());
      }
    }

  });
}

function splitDataList() {
  var totalHeight = dataListCol1.height(), columnHeight = totalHeight / 3 +35;
  for (var i = 0; i < dataListCols.length - 1; i++) {
    var currentChildren = jQuery("#" + dataListCols[i].id).children(), heightCounter = 0;
    for (var y = 0; y < currentChildren.length; y++) {
      var thisChild = jQuery(currentChildren[y]);
      heightCounter += thisChild.outerHeight();
      if (columnHeight < heightCounter) {
        var grabFrom = y - 1;
        if (thisChild[0].tagName === "DD" && currentChildren[y - 1].tagName === "DT") {
          grabFrom = y - 2;
        } else if (thisChild[0].tagName === "DD") {
          thisChild.addClass("topItem");
        }
        jQuery(dataListCols[i + 1]).append(currentChildren.filter(":gt(" + grabFrom + ")"));
        break;
      }
    }
  }
}


function divisionsLinks() {
  jQuery(".medarb_link").each(function () {

    jQuery(this).click(function() {
      jQuery(".medarb_link").each(function () { jQuery(this).removeClass("medarb_valgt"); });
      jQuery(this).addClass("medarb_valgt");
      jQuery(".dataListColInfoArea").html(jQuery(this).parent().find("span").html());
    });

  });
}

function initFirstDivisionItem() {
  jQuery(".dataListColInfoArea").html(jQuery(".medarb_link:first").parent().find("span").html());
  jQuery(".medarb_link:first").addClass("medarb_valgt");
}

/* Divsions END */



/* Divsions 2 START */
var divisionsTable_2, dataListCols_2, dataListCol1_2;
function initdivisionsTable_2() {
  divisionsTable_2 = jQuery("#DivisionsTable_2");

  if (divisionsTable_2[0]) {
    dataListCols_2 = divisionsTable_2.find(".dataListCol_2");
    dataListCol1_2 = dataListCols_2.filter("#DataListCol1_2");
    addGroupHeaders_2();
    splitDataList_2();
    finalizeDataList_2();
    divisionsLinks_2();
    initFirstDivisionItem_2();
  }
}

function addGroupHeaders_2() {
  var lastGroupHeader_2 = "";
  dataListCol1_2.children().each(function (i) {
    var thisDD_2 = jQuery(this);
    if (lastGroupHeader_2 !== thisDD_2.attr("productgroups")) {
      lastGroupHeader_2 = thisDD_2.attr("productgroups");
      thisDD_2.before("<dt>" + lastGroupHeader_2 + "</dt>");
    }
  });
}

function finalizeDataList_2() {
  var lastEles_2 = [];
  dataListCols_2.each(function (i) {
    var thisCol_2 = jQuery(this);

    if(thisCol_2.children(":first")[0]) {
      if (thisCol_2.children(":first")[0].tagName.toLowerCase() === "dd") {
        var lastDT_2 = null;
        for (var y = i - 1; y >= 0; y--) {
          lastDT_2 = jQuery(dataListCols_2[y]).children("dt:last");
          if (lastDT_2[0]) {
            break;
          }
        }
        thisCol_2.prepend(lastDT_2.clone());
      }
    }

  });
}

function splitDataList_2() {
  var totalHeight_2 = dataListCol1_2.height(), columnHeight_2 = totalHeight_2 / 3 +35;
  for (var i = 0; i < dataListCols_2.length - 1; i++) {
    var currentChildren_2 = jQuery("#" + dataListCols_2[i].id).children(), heightCounter_2 = 0;
    for (var y = 0; y < currentChildren_2.length; y++) {
      var thisChild_2 = jQuery(currentChildren_2[y]);
      heightCounter_2 += thisChild_2.outerHeight();
      if (columnHeight_2 < heightCounter_2) {
        var grabFrom_2 = y - 1;
        if (thisChild_2[0].tagName === "DD" && currentChildren_2[y - 1].tagName === "DT") {
          grabFrom_2 = y - 2;
        } else if (thisChild_2[0].tagName === "DD") {
          thisChild_2.addClass("topItem");
        }
        jQuery(dataListCols_2[i + 1]).append(currentChildren_2.filter(":gt(" + grabFrom_2 + ")"));
        break;
      }
    }
  }
}


function divisionsLinks_2() {
  jQuery(".medarb_link_2").each(function () {

    jQuery(this).click(function() {
      jQuery(".medarb_link_2").each(function () { jQuery(this).removeClass("medarb_valgt_2"); });
      jQuery(this).addClass("medarb_valgt_2");
      jQuery(".dataListColInfoArea_2").html(jQuery(this).parent().find("span").html());
    });

  });
}

function initFirstDivisionItem_2() {
  jQuery(".dataListColInfoArea_2").html(jQuery(".medarb_link_2:first").parent().find("span").html());
  jQuery(".medarb_link_2:first").addClass("medarb_valgt_2");
}


/* Divsions END */


/* Divsions 3 START */
var divisionsTable_3, dataListCols_3, dataListCol1_3;
function initdivisionsTable_3() {
  divisionsTable_3 = jQuery("#DivisionsTable_3");

  if (divisionsTable_3[0]) {
    dataListCols_3 = divisionsTable_3.find(".dataListCol_3");
    dataListCol1_3 = dataListCols_3.filter("#DataListCol1_3");
    addGroupHeaders_3();
    splitDataList_3();
    finalizeDataList_3();
    divisionsLinks_3();
    initFirstDivisionItem_3();
  }
}

function addGroupHeaders_3() {
  var lastGroupHeader_3 = "";
  dataListCol1_3.children().each(function (i) {
    var thisDD_3 = jQuery(this);
    if (lastGroupHeader_3 !== thisDD_3.attr("productgroups")) {
      lastGroupHeader_3 = thisDD_3.attr("productgroups");
      thisDD_3.before("<dt>" + lastGroupHeader_3 + "</dt>");
    }
  });
}

function finalizeDataList_3() {
  var lastEles_3 = [];
  dataListCols_3.each(function (i) {
    var thisCol_3 = jQuery(this);

    if(thisCol_3.children(":first")[0]) {
      if (thisCol_3.children(":first")[0].tagName.toLowerCase() === "dd") {
        var lastDT_3 = null;
        for (var y = i - 1; y >= 0; y--) {
          lastDT_3 = jQuery(dataListCols_3[y]).children("dt:last");
          if (lastDT_3[0]) {
            break;
          }
        }
        thisCol_3.prepend(lastDT_3.clone());
      }
    }

  });
}

function splitDataList_3() {
  var totalHeight_3 = dataListCol1_3.height(), columnHeight_3 = totalHeight_3 / 3 +35;
  for (var i = 0; i < dataListCols_3.length - 1; i++) {
    var currentChildren_3 = jQuery("#" + dataListCols_3[i].id).children(), heightCounter_3 = 0;
    for (var y = 0; y < currentChildren_3.length; y++) {
      var thisChild_3 = jQuery(currentChildren_3[y]);
      heightCounter_3 += thisChild_3.outerHeight();
      if (columnHeight_3 < heightCounter_3) {
        var grabFrom_3 = y - 1;
        if (thisChild_3[0].tagName === "DD" && currentChildren_3[y - 1].tagName === "DT") {
          grabFrom_3 = y - 2;
        } else if (thisChild_3[0].tagName === "DD") {
          thisChild_3.addClass("topItem");
        }
        jQuery(dataListCols_3[i + 1]).append(currentChildren_3.filter(":gt(" + grabFrom_3 + ")"));
        break;
      }
    }
  }
}


function divisionsLinks_3() {
  jQuery(".medarb_link_3").each(function () {

    jQuery(this).click(function() {
      jQuery(".medarb_link_3").each(function () { jQuery(this).removeClass("medarb_valgt_3"); });
      jQuery(this).addClass("medarb_valgt_3");
      jQuery(".dataListColInfoArea_3").html(jQuery(this).parent().find("span").html());
    });

  });
}

function initFirstDivisionItem_3() {
  jQuery(".dataListColInfoArea_3").html(jQuery(".medarb_link_3:first").parent().find("span").html());
  jQuery(".medarb_link_3:first").addClass("medarb_valgt_3");
}


/* Divsions END */




/* Divsions 4 START */
var divisionsTable_4, dataListCols_4, dataListCol1_4;
function initdivisionsTable_4() {
  divisionsTable_4 = jQuery("#DivisionsTable_4");

  if (divisionsTable_4[0]) {
    dataListCols_4 = divisionsTable_4.find(".dataListCol_4");
    dataListCol1_4 = dataListCols_4.filter("#DataListCol1_4");
    addGroupHeaders_4();
    splitDataList_4();
    finalizeDataList_4();
    divisionsLinks_4();
    initFirstDivisionItem_4();
  }
}

function addGroupHeaders_4() {
  var lastGroupHeader_4 = "";
  dataListCol1_4.children().each(function (i) {
    var thisDD_4 = jQuery(this);
    if (lastGroupHeader_4 !== thisDD_4.attr("productgroups")) {
      lastGroupHeader_4 = thisDD_4.attr("productgroups");
      thisDD_4.before("<dt>" + lastGroupHeader_4 + "</dt>");
    }
  });
}

function finalizeDataList_4() {
  var lastEles_4 = [];
  dataListCols_4.each(function (i) {
    var thisCol_4 = jQuery(this);

    if(thisCol_4.children(":first")[0]) {
      if (thisCol_4.children(":first")[0].tagName.toLowerCase() === "dd") {
        var lastDT_4 = null;
        for (var y = i - 1; y >= 0; y--) {
          lastDT_4 = jQuery(dataListCols_4[y]).children("dt:last");
          if (lastDT_4[0]) {
            break;
          }
        }
        thisCol_4.prepend(lastDT_4.clone());
      }
    }

  });
}

function splitDataList_4() {
  var totalHeight_4 = dataListCol1_4.height(), columnHeight_4 = totalHeight_4 / 3 +35;
  for (var i = 0; i < dataListCols_4.length - 1; i++) {
    var currentChildren_4 = jQuery("#" + dataListCols_4[i].id).children(), heightCounter_4 = 0;
    for (var y = 0; y < currentChildren_4.length; y++) {
      var thisChild_4 = jQuery(currentChildren_4[y]);
      heightCounter_4 += thisChild_4.outerHeight();
      if (columnHeight_4 < heightCounter_4) {
        var grabFrom_4 = y - 1;
        if (thisChild_4[0].tagName === "DD" && currentChildren_4[y - 1].tagName === "DT") {
          grabFrom_4 = y - 2;
        } else if (thisChild_4[0].tagName === "DD") {
          thisChild_4.addClass("topItem");
        }
        jQuery(dataListCols_4[i + 1]).append(currentChildren_4.filter(":gt(" + grabFrom_4 + ")"));
        break;
      }
    }
  }
}


function divisionsLinks_4() {
  jQuery(".medarb_link_4").each(function () {

    jQuery(this).click(function() {
      jQuery(".medarb_link_4").each(function () { jQuery(this).removeClass("medarb_valgt_4"); });
      jQuery(this).addClass("medarb_valgt_4");
      jQuery(".dataListColInfoArea_4").html(jQuery(this).parent().find("span").html());
    });

  });
}

function initFirstDivisionItem_4() {
  jQuery(".dataListColInfoArea_4").html(jQuery(".medarb_link_4:first").parent().find("span").html());
  jQuery(".medarb_link_4:first").addClass("medarb_valgt_4");
}


/* Divsions END */

function boxStat() {
  jQuery(".boxStat").each(function(){
    if(jQuery(this).find(".boxStatCI").attr("name")) { 
      _gaq.push(['_trackEvent', 'Hot Spots', jQuery(this).find(".boxStatCI").attr("name"), 'Visninger']);
    } else {
      _gaq.push(['_trackEvent', 'Hot Spots', jQuery(this).attr("name"), 'Visninger']);
    }
  });
}
