﻿jQuery(document).ready(function() {
  initScrollBars();
  initCo3Gallery();
  initPrintPageMaster();

  var possDownload = jQuery(".imageList").attr("download");
  if (possDownload == "True") { jQuery(".imageList_download_header").css("display", "block"); } else { jQuery(".imageList_flere_header").css("display", "block"); }
});

window.onload = function() {
  initBanners();
}



function initScrollBars() {
  var settings = {
    width: 185,
    height: 300,
    scrollSpeed: 400,
    hideScrollbars: false,
    upEvents: [{ selector: ".ArrangementBox .uparrow",
      startEvent: "mouseover",
      endEvent: "mouseout",
      hideWhenNotNeeded: false
    },
                { selector: ".ArrangementBox .uparrow",
                  startEvent: "mousedown",
                  endEvent: "mouseup",
                  scrollSpeed: 800,
                  hideWhenNotNeeded: false}],
    downEvents: [{ selector: ".ArrangementBox .downarrow",
      startEvent: "mouseover",
      endEvent: "mouseout",
      hideWhenNotNeeded: false
    },
                { selector: ".ArrangementBox .downarrow",
                  startEvent: "mousedown",
                  endEvent: "mouseup",
                  scrollSpeed: 1000,
                  hideWhenNotNeeded: false}],
    mouseWheelEvent: [{ axis: "y"}]
  };
  var co3Scroll = jQuery(".scrollContent").wrapInScrollbar(settings);
}


//Trim Function
String.prototype.trim = function() { return this.replace(/^\s+|\s+$/, ''); };

//Finds the closest parent that meets the selector requirements
jQuery.fn.findParent = function(selector) {
  var correctParent = this.eq(0).parent();
  while (correctParent.filter(selector).length === 0) {
    correctParent = correctParent.parent();
    if (correctParent.filter("html").length > 0) {
      correctParent = correctParent.slice(0, correctParent.length - 1);
      break;
    }
  }
  return correctParent;
};



function initBanners() {
  var settings = {
    globalShowTime: 5000,
    bannerSelector: ".banner",
    shiftStyle: "fade",
    fadefrontColor: "#ffffff",
    shiftSpeed: 3000,
    resizeSpeed: 500
  }

  jQuery("#topBannerWrap").startBannerShift(settings);

  jQuery(".bannerWrap").each(function() {
    jQuery(this).startBannerShift(settings);
  });
}

function initCo3Gallery() {
  var imageLists = $(".imageList");
  var settings = {
    imageInformationHeight: 40,
    imageInformationTemplate: '[description]'
  }
  jQuery(".imageList img").each(function() {
    var img = jQuery(this);
    var header = img.attr("description").replace(".jpg", "").replace("_", " ").replace("-", " ");
    img.attr("description", header);
  });
  imageLists.each(function() {
    var imageList = jQuery(this);
    if (imageList.attr("download") && imageList.attr("download").toLowerCase() === "true") {
      settings.imageInformationTemplate += '<br /><a href="[downloadHref]">Download billedet</a>';
    }
    jQuery(this).children("img").CreateGallery(settings)
  });
}


function sendToFriend() {
  window.open('/Admin/Public/SendToFriend.aspx?ID=1&amp;M=SendFriend&amp;url=' + location.href, 'sendfriend', 'width=322,height=250')
}

function initPrintPageMaster() {
  jQuery(".printLink").click(function() {
    printPageMaster(jQuery(this).attr("printID"));
  });
}

function printPageMaster(PrintTemplateID) {
  PrintTemplateID = PrintTemplateID ? PrintTemplateID : "2";
  var splitter = location.href.indexOf("?") > -1 ? "&" : "?";
  printWin = window.open(location.href + splitter + 'Printerfriendly=' + PrintTemplateID, 'printit', '');
}
