﻿$(document).ready(function() {
  initMenu();
  setFixedMenu();
  setHeaderTimer(8000);
  setVisionaryArchive();
  setPDF();

  //set events handlers to search button and textfield
  $("#btnSearch").click(function() {
    search($("#txtSearch").val());
  });
  $("#txtSearch").keyup(function(event) {
    if (event.keyCode == '13') {
      search($("#txtSearch").val());
    }
  });
  //event handler for contact box
  $("ul.list li h3").click(function() {
    $("#contactbox_container").css("height", "auto");
    $("ul.list li div").slideUp();
    if ($(this).next().css("display") == "none") {
      $(this).next().slideDown();
    } else {
      $(this).next().slideUp();
    }
  });
  setContactFormDefault();
  setVendorHover();
  $('#usp marquee').marquee();
});

function setVendorHover() {
  $("#vendors img").each(function() {
    $(this).mouseover(function(event) {
      $(this).attr("src", $(this).attr("src").replace(".jpg", "") + "_hover.jpg");
    });
    $(this).mouseout(function(event) {
      $(this).attr("src", $(this).attr("src").replace("_hover.jpg", ".jpg"));
    });
  });
}


function setHeaderTimer(time) {
  setInterval("changeHeader()", time);
}

function setContactFormDefault() {
  var url = window.location.toString();
  if (url.indexOf("/products/") > -1 || url.indexOf("/produkte/") > -1 || url.indexOf("/produits/") > -1 || url.indexOf("/produkten/") > -1) {
    $("#contactbox_container").css("height", "auto");
    $(".list li").eq(0).find("div").show();
  } else if (url.indexOf("visionary") > -1) {
    $("#contactbox_container").css("height", "auto");
    $(".list li").eq(2).find("div").show();
  } else if (url.indexOf("/home.html") == -1) {
    $("#contactbox_container").css("height", "auto");
    $(".list li").eq(1).find("div").show();
  }
}

function setFixedMenu() {
  var url = window.location.pathname;
  var urlSplit = url.split("/");
  url = url.split("/")[1];
  if (urlSplit.length > 2 && window.location.pathname.indexOf("/weblog") == -1) {  // we hebben een vast submenu!
    var submenu = $(".submenu a[href*='" + url + "']").parents(".submenu");  //reference to submenu
    var menu = $(submenu).html();
    if (menu != null) {
      $("<div id='menu' class='fixed'>" + menu + "</div>").insertAfter("#menu");
      $(submenu).remove();
    }
  }
}

function setPDF(objects) {
  var force = true;
  if (!objects) { objects = $("a[href*='.pdf']"); force = false; }

  $(objects).each(function() {
    if ($(this).is(":visible") || (force)) {
      var wrapper = $(this).wrap('<div class="pdfcontainer" />');
      $("<img src='/images/pdf_icon2.gif' alt='PDF' title='PDF' />").prependTo($(wrapper).parent());
      $(this).addClass("pdflink");
    }
  });
}

function initMenu() {
  //if ie6....
  var browser = parseInt(jQuery.browser.version);
  if (browser == 6) { //only when ie6
    $("#menu ul div").css("display", "inline").css("padding", "0px");
    $("#menu ul div.submenu div.clear").css("display", "none");
    $("#menu ul div.submenu").css("margin-top", "15px").css("display", "none");
    $("#wrapper").width($("#center").width());
    $("#footer").hide(); //ugly! but for now
    $("#content").css("padding-bottom", "0px"); //ugly! but for now
  }
  //Add last class
  $('#menu ul').each(function() {
    $('li:last', this).addClass('last');
  });

  var configin = {
    sensitivity: 1, // number = sensitivity threshold (must be 1 or higher)    
    interval: 0, // number = milliseconds for onMouseOver polling interval
    over: function() {
      if ($('div', $(this).parents('li')).length > 1) {
        $('div:eq(0)', $(this).parents('li')).addClass('active');
        $('div:eq(1)', $(this).parents('li')).css('display', 'block');
      }
    }, // function = onMouseOver callback (REQUIRED)    
    timeout: 500, // number = milliseconds delay before onMouseOut    
    out: function() {
      //Nothing...
    } // function = onMouseOut callback (REQUIRED)
  };


  var configout = {
    sensitivity: 1, // number = sensitivity threshold (must be 1 or higher)    
    interval: 0, // number = milliseconds for onMouseOver polling interval
    over: function() {
      //Nothing...
    }, // function = onMouseOver callback (REQUIRED)    
    timeout: 500, // number = milliseconds delay before onMouseOut    
    out: function() {
      if ($('div', this).length > 1) {
        $('div:eq(0)', this).removeClass('active');
        $('div:eq(1)', this).css('display', 'none');
      }
    } // function = onMouseOut callback (REQUIRED)
  };

  $("#menu > ul > li > div > a").hoverIntent(configin)
  $("#menu > ul > li").hoverIntent(configout)
}

function search(keyword) {
  window.location = "/search.asp?keyword=" + keyword;
}

function changeHeader() {
  var list = ['header_index.gif', 'header_index2.jpg', 'header_index3.jpg', 'header_index4.jpg'];
  headerIndex = Math.floor(Math.random() * list.length);
  if ($("#header").css("background-image").indexOf(list[headerIndex]) == -1) {
    $("#header").fadeOut("slow", function() {
      $("#header").css("background-image", "url(/images/" + list[headerIndex] + ")").fadeIn("slow");
    });
  } else {
    changeHeader();
  }
}
function setVisionaryArchive() {
  var html = "";
  $('.visionary_archive a').each(function() {
    if ($(this).html().indexOf('2010') > -1 || $(this).html().indexOf('2009') > -1) {
      $(this).css({ 'display': 'inline' });
    } else {
      $(this).css({ 'display': 'none' });
    }
  });
}

function viewOlder(obj) {
  setPDF($('.visionary_archive a:hidden'));
  $('.visionary_archive a').css({ display: 'inline' });
  //$(obj).hide();
}
