/* Copyright (c) 2006 Brandon Aaron (http://brandonaaron.net)
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) 
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 *
 * $LastChangedDate: 2007-07-21 18:44:59 -0500 (Sat, 21 Jul 2007) $
 * $Rev: 2446 $
 *
 * Version 2.1.1
 */
eval(function(p,a,c,k,e,r){e=function(c){return(c<a?'':e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--)r[e(c)]=k[c]||e(c);k=[function(e){return r[e]}];e=function(){return'\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p}('(b($){$.m.E=$.m.g=b(s){h($.x.10&&/6.0/.I(D.B)){s=$.w({c:\'3\',5:\'3\',8:\'3\',d:\'3\',k:M,e:\'F:i;\'},s||{});C a=b(n){f n&&n.t==r?n+\'4\':n},p=\'<o Y="g"W="0"R="-1"e="\'+s.e+\'"\'+\'Q="P:O;N:L;z-H:-1;\'+(s.k!==i?\'G:J(K=\\\'0\\\');\':\'\')+\'c:\'+(s.c==\'3\'?\'7(((l(2.9.j.A)||0)*-1)+\\\'4\\\')\':a(s.c))+\';\'+\'5:\'+(s.5==\'3\'?\'7(((l(2.9.j.y)||0)*-1)+\\\'4\\\')\':a(s.5))+\';\'+\'8:\'+(s.8==\'3\'?\'7(2.9.S+\\\'4\\\')\':a(s.8))+\';\'+\'d:\'+(s.d==\'3\'?\'7(2.9.v+\\\'4\\\')\':a(s.d))+\';\'+\'"/>\';f 2.T(b(){h($(\'> o.g\',2).U==0)2.V(q.X(p),2.u)})}f 2}})(Z);',62,63,'||this|auto|px|left||expression|width|parentNode||function|top|height|src|return|bgiframe|if|false|currentStyle|opacity|parseInt|fn||iframe|html|document|Number||constructor|firstChild|offsetHeight|extend|browser|borderLeftWidth||borderTopWidth|userAgent|var|navigator|bgIframe|javascript|filter|index|test|Alpha|Opacity|absolute|true|position|block|display|style|tabindex|offsetWidth|each|length|insertBefore|frameborder|createElement|class|jQuery|msie'.split('|'),0,{}))

$(document).ready(function() {
	$("#tabmenu > ul > li").each(function(){
        var r = $(this);
        r.hover(function(){
          $(this).children("ul").removeClass('closed').bgiframe();
        },function(){
          $(this).children("ul").addClass('closed');
        });
    });	

  var templateUrl = function(filename) {
    //De url van de static content server returnen
    var url = $("head > link[href*=identity]").attr("href");
    if (url.substr(0, 7) == "http://") { url = url.substr(7); }
    if (url.indexOf("/") >= 0) { url = url.substr(0, url.indexOf("/")); }
    return "http://" + url + "/5/js/helpdesk/" + filename;
  };
  
  //Paragrafen fixen
  $("div.faqs p:empty").remove();
  $("div.faqs p:last-child").css("margin-bottom", 0);
  $("div.faqs p:first-child").css("margin-top", 0);
  $("h2, h3").next("p").css("margin-top", 0);
  
  //FAQ-entries
  $("div.faqs").revealer().find("dt:first-child").addClass("first");
  if (document.location.hash.length > 1) {
    var faqid = document.location.hash.replace(/\#openfaq(\d+)/i, "$1");
    $("dt#faq" + faqid + ":first").click().each(function() {
      $.getScript(templateUrl("scrollto.js"), function() {
        $.scrollTo("#faq" + faqid);
      });
    });
  }
  $("div.faqs").next("div.listblock.manuals").css("margin-top", "0");
  
  //Fix voor Safari
  $("#mailform textarea").each(function() { $(this).css("max-width", $(this).width() + "px"); });
  
  //Handleiding printen
  $("div.printmanual:last").css("margin-bottom", "3em");
  $("div.printmanual").show().find("a").click(function() {
    window.print();
    return false;
  });
  
  //Gewone tables
  $("tr:nth-child(even)").addClass("oddrow");
  $("tr td:last-child, tr th:last-child").addClass("last");
  $("table").prev("h2, h3").addClass("tablehead");
  
  //Troubleshooter moven
  var troubleshooterPlaceHolder = $("#__troubleshooter_placeholder"), troubleshooter = $("div.troubleshooter");
  if (troubleshooter.length > 0) {
    if (troubleshooterPlaceHolder.length > 0) {
      troubleshooterPlaceHolder.replaceWith(troubleshooter);
    }
    else {
      $("#breadcrumbs + div > div:not(.troubleshooter)").children(":header:first-child").after(troubleshooter);
    }
  }
  
  //Related items moven
  $("div.listblock.helpfooter").before($("div.listblock.related"));
  
  //Blokken hiden in overzichtspagina's
  if ($("#contentWide2 div.teaser").length > 0) {
    $("div.listblock.helpfooter, div.listblock.related, #mijnTelfort").hide();
  }
  
  //Troubleshooter conclusie meepassen naar contactformulier
  $("div.troubleshooter").each(function() {
    var conclusion = $(this).children("div.conclusion");
    if (conclusion.length > 0) {
      var contactLink = $(this).find("div.footer > div.listblock.contact a[href*=Contact.aspx]");
      var form = contactLink.makePostForms();
      form.append($("<input type=\"hidden\" name=\"foundsolution\"/>").attr("value", conclusion.find("h3.solution").next("div").text()));
      $("#mainContainer").append(form);
      contactLink.click(function() {
        form.submit();
        return false;
      });
    }
  });
  
  //Troubleshoot-oplossing printen
  $("div.troubleshooter div.conclusion.content").prepend($("<a class=\"print\" href=\"#\">Oplossing printen</a>").click(function() {
    $("body").addClass("printing-ts");
    window.print();
    window.setTimeout(function() {
      $("body").removeClass("printing-ts");
    }, 15000);
    return false;
  }));
  
  //Zoeken in "Antwoord niet gevonden"
  $("div.listblock.helpfooter", $("body.withsearch")).append(
    $("<div/>").append(
      "<label for=\"txtFooterSearch\">Zoeken binnen Klantenservice internet</label>"
    ).append(
      $("<input type=\"text\" id=\"txtFooterSearch\"/>").keydown(function(e) {
        if (e.keyCode == 13 || e.which == 13) {
          $(this).next("button").click();
          return false;
        }
      })
    ).append(
      $("<button type=\"button\">Zoek</button>").click(function() {
        var val = $.trim($(this).prev("input").val());
        if (val.length > 0) {
          //Prevent the ASP.NET form from submitting
          $(this).parents("form").bind("submit", function() { return false; });
          location.href = $(this).parents("div.listblock.helpfooter")
            .find("a[href*=contact],a[href*=Contact]")
            .attr("href").replace(
              /contact(-telefoon)?\.aspx.*/i, "search.aspx?q=" + escape(val)
            );
        }
      })
    )
  );
  
  //Zoeken op de zoekpagina
  $("#searchcontainer input[type=text]", $("body.withsearch")).keydown(function(e) {
    if (e.keyCode == 13) {
      location.href = "search.aspx?q=" + escape($(this).val());
      return false;
    }
  });
  
  //Als we op een van beide contactformulieren zitten...
  if ($("#mailform,.contactphone,div.callcost").length > 0 && $("#mailform.overstappen").length == 0) {
    
    $("fieldset.prodtype + fieldset.product, fieldset.description, fieldset.personalinfo, input[type=submit]", $("#mailform")).hide();
    $("fieldset.prodtype, fieldset.product").addClass("alone");
    
    $.getScript(templateUrl("contact.js"), function() {
      
      //Gesprekskosten-tabel
      $("div.callcost").callCost();
      
      $("div.revealers").revealer({ element: "h2", container: "div.container" });
  
      if ($("ul.categories").length > 0) {
        var product = $("fieldset.product");
        
        if ($("fieldset.prodtype").length == 1) {
          $("#rdbInternet").bind("change keyup click", function() {
            if ($(this).is(":checked")) {
              product.show();
            }
          }).change();
        }
        else {
          product.show();
        }
        
        //De categorieën-selector
        var fieldsets = $("fieldset.description,fieldset.personalinfo");
        $("fieldset.suggested").show();
        $("#mailform input[type=submit]").addClass("showform").val("Mijn vraag per e-mail stellen").click(function(e) {
          if ($(this).is(".showform")) {
            $(this).removeClass("showform").val("Verzenden");
            fieldsets.show();
            $("#mailform .error").hide();
            return false;
          }
          return true;
        });
        $(document).bind("categories.fullselected", function(e, data) {
          //Antwoord-suggestie "tip" fixen
          $("fieldset.suggested li:first-child:not(:has(a))").addClass("paragraph");
          
          if (data.answers == 0 || (data && data.initial)) {
            $("#mailform input[type=submit]").show().click();
          }
          if (data.answers > 0) {
            $("#mailform input[type=submit]").show();
          }
          $("fieldset.product").removeClass("alone");
          $("#mailform .error").hide();
        });
        $(document).bind("categories.selected", function(e, data) {
          $("fieldset.suggested li > a[href*=Mededelingen]").addClass("announcement");
          var action = $("ul.categories #" + data.name).hasClass("invoicefields") ? "show" : "hide";
          $("#mailform fieldset > div.invoice")[action]();
          $("fieldset.suggested div.suggested").each(function() { $(this)[$.trim($(this).text()).length == 0 ? "hide" : "show"](); });
        });
        $("#mailform").categoriesSelector({
          categories: "ul.categories",
          insertpoint: "div.categories",
          suggested: "fieldset.suggested",
          emptyitem: "- Maak een keuze -",
          labels: [ "Waarmee kunnen we u helpen?", "Waar gaat uw verzoek over?", "Welk product betreft het?" ],
          hiddeninput: $("fieldset.product > input[type=hidden]")
        });
      }
      else {
        //Oude situatie - kan weg als de release van 2009-01-14 gedeployed is.
        $("#mailform > div").append("<div class=\"clear\"></div>");
        $("#mailform fieldset.product div div").css({ padding: "0", border: "0" });
        $("#mailform fieldset.product").show();
      }
      
      //Textareas fixen
      $("#mailform textarea").each(function() { this.spellcheck = false; }).one("focus", function() {
        $(this).setCaretPosition(0);
      });
      
      //Calldrukte-grafiek
      /*
	  var day = ["zondag", "maandag", "dinsdag", "woensdag", "donderdag", "vrijdag", "zaterdag"][new Date().getDay()];
      $(".contactphone h1").after(
        $("<div class=\"calloccupation\"/>").addClass(day).append(
          $("<div><p>Gemiddelde drukte telefonische klantenservice op <strong>" + day + "</strong>:</p><div id=\"occupationgraph\"></div></div>")
        )
      );
	  */
    });
  }
  
  //Statistieken
  $.installAnalytics("UA-6213875-1");
});

//Extension funcie voor in- en uitklappen van FAQs
$.fn.revealer = function(options) {
  options = $.extend({
    element: "dt",
    container: "dd"
  }, options);
  
  var animSettings = { 
    marginTop: "5px",
    marginBottom: "8px",
    height: "show"
  };
  var resetSettings = {
    marginTop: 0,
    marginBottom: 0
  };
  
  var resetHeight = function(initial) {
    this.each(function() {
      if (initial) {
        this.originalHeight = this.offsetHeight + "px";
      }
      $(this).height(this.originalHeight);
    });
    return this;
  };
  
  $(options.element, this).filter(function() {
    //Alleen elementen die gevolgd worden door een container.
    var next = $($(this).next().get(0));
    if (next.is(options.container)) {
      resetHeight.call(next, true).hide();
      return true;
    }
    return false;
  }).click(function() {
    if ($(this).hasClass("open")) {
      //Class "open" uit het element en de container verwijderen, eventuele lopende animatie stoppen, en verbergen.
      resetHeight.call($(this).removeClass("open").next().stop().hide().removeClass("open")).css(resetSettings);
    }
    else {
      //Class "open" aan het element en de container toevoegen en weergeven.
      resetHeight.call($(this).parent().find(options.container).stop().hide().removeClass("open")).css(resetSettings).prev().removeClass("open");
      $(this).addClass("open").next().addClass("open").each(function() {
        //$(this).slideDown("slow");
        $(this).animate(animSettings, "slow", "swing");
      });
    }
    if (this.id.length > 0) {
      window.location.href = "#open" + this.id;
    }
    return false;
  });
  return this;
};

$.fn.makePostForms = function() {
  var forms = [];
  this.each(function() {
    var form = $("<form method=\"post\"></form>");
    form.attr("action", this.href);
    forms.push(form.get(0));
  });
  return $(forms);
};

$.installAnalytics = function(code) {
  if (document.location.protocol == "file:" || document.location.protocol == "about:") {
    return;
  }
  $.getScript((document.location.protocol == "https:" ? "https://ssl." : "http://www.") + "google-analytics.com/ga.js", function() {
    if (typeof window._gat == "object" && typeof window._gat._getTracker == "function") {
      window.pageTracker = window._gat._getTracker(code);
      $.getScript((document.location.protocol == "https:" ? "https://" : "http://") + "www.telfort.nl/javascript/ga/custom_se-ga.js", function() {
        if (typeof window.pageTracker == "object") {
          if (typeof window.pageTracker._setDomainName == "function") {
            window.pageTracker._setDomainName("telfort.nl");
          }
          if (typeof window.pageTracker._trackPageview == "function") {
            window.pageTracker._trackPageview();
          }
        }
      });
    }
  });
};