/*
 * content.js
 * For www.hiteq.org
 * Adapted by Colours. The Internet Company.
 * Last change: 20110722
 *
*/

//simple cookie extra's for developers who didn't know JQuery
function createCookie(name, value, days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
    var expires = "; expires=" + date.toGMTString();
  } else expires = "";
  document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for (var i = 0; i < ca.length; i++) {
    var c = ca[i];
    while (c.charAt(0) == ' ') c = c.substring(1, c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
  }
  return null;
}

function eraseCookie(name) {
  createCookie(name, "", -1);
}

//extending JQuery
(function ($) {
  $.extend({
    equalHeight: function () {
      $('.EqualHeight').children().height('auto');
      $('.EqualHeight').each(function () {
        var maxHeight = 0;
        $(this).children().each(function () {
          if ($(this).height() > maxHeight) {
            maxHeight = $(this).height();
          }
        });
        $(this).children().height(maxHeight);
      });
    },
    inputtextValuesSet: function () {
      $('input[type="text"], textarea').each(function () {
        if ($(this).attr('title') && $.fTrim($(this).val()).length === 0) {
          $(this).val($(this).attr('title'));
        }
        $(this).blur(function () {
          var sValue = $.fTrim(this.value);
          if (sValue.length === 0) {
            $(this).val($(this).attr('title'));
          }
        });
        $(this).focus(function () {
          var sValue = $.fTrim(this.value);
          if (sValue == $(this).attr('title')) {
            this.value = "";
          }
        });
      });
    },
    form_errorFunctionalty: function () {
      if ($('.error').length) {
        $('.error dd').each(function () {
          if ($.fTrim($(this).children().eq($(this).children().length - 1).val()).length === 0) {} else {
            $(this).children().eq($(this).children().length - 1).prev().removeClass('show_error');
          }
          $(this).children().eq($(this).children().length - 1).blur(function () {
            if ($.fTrim(this.value).length === 0) {
              $(this).prev().addClass('show_error');
            }
          });
          $(this).children().eq($(this).children().length - 1).focus(function () {
            //if($.fTrim(this.value).length===0){
            $(this).prev().removeClass('show_error');
            //}
          });
        });
      }
    },
    snippettabsBehavior: function () {
      if ($('.snippettabs').length) {
        $('.snippettabs > li').each(function () {
          $(this).addClass('def_active');
        });
        $('.snippettabs > li > a').click(function () {
          $('.snippettabs > li').each(function () {
            $(this).removeClass('active');
          });
          $(this).parent().toggleClass('active');
          return false;
        });
      }
      if ($('.faqlist').length) {
        $('.faqlist > li').each(function () {
          $(this).addClass('def_active');
        });
        $('.faqlist > li > a').click(function () {
          $(this).parent().toggleClass('active');
          return false;
        });
      }
    },
		/* faqtabsBehavior: function() {
			if($('.faqlist').length){
				$('.faqlist > li').each(function(){
					$(this).addClass('def_active');
				});
				$('.faqlist > li > a').click(function(){
					$(this).parent().addClass('active');
					$(this).parent().siblings().removeClass('active');
					return false;
				});
			}
		},*/
    ieFunctions: function () {
      $(".cols_holder table tbody tr:nth-child(odd)").addClass("odd_tr");
      $(".cols_holder table tbody tr:nth-child(even)").addClass("even_tr");
    },
    fTrim: function (s) {
      return s.replace(/^\s*(.*?)\s*$/, "$1");
    },
    cuSelApply: function () {
      var cuselParams = {
        changedEl: "select",
        visRows: 5,
        scrollArrows: true
      }
      cuSel(cuselParams);
    },
    paginatorActions: function () {
      if ($('.paginator_list').length) {
        $('.paginator_list li:not(.first, .last, .active) a').hover(

        function () {
          $(this).parent().addClass('hover');
        }, function () {
          $(this).parent().removeClass('hover');
        });
      }
    },
    cycleApply: function () {
      if ($('#slide').length) {
        $('#slide').cycle({
          timeout: 6000,
          pauseOnPagerHover: true,
          pager: '.c_controllers',
          pagerEvent: 'click.cycle',
          pagerAnchorBuilder: function (idx, slide) {
            $('.c_controllers').append('<li><a href=""></a></li>')
            return '.c_controllers li:eq(' + idx + ') a';
          },
          fx: 'fade',
          pause: true
        });
      }
    },
    scaleFont: function () {
      $(".font_controllers a.big_link").click(function () {
        $.addActive(this);
        $("body").queue(function (next) {
          eraseCookie("FontSize");
          $("body").removeClass("MediumFont");
          $("body").removeClass("SmallFont");
          next();
        }).queue(function (next) {
          $("body").addClass("LargeFont");
          createCookie("FontSize", "LargeFont", 30);
          next();
        });
        return false;
      });
      $(".font_controllers a.medium_link").click(function () {
        $.addActive(this);
        $("body").queue(function (next) {
          eraseCookie("FontSize");
          $("body").removeClass("LargeFont");
          $("body").removeClass("SmallFont");
          next();
        }).queue(function (next) {
          $("body").addClass("MediumFont");
          createCookie("FontSize", "MediumFont", 30);
          next();
        });
        return false;
      });
      $(".font_controllers a.small_link").click(function () {
        $.addActive(this);
        $("body").queue(function (next) {
          eraseCookie("FontSize");
          $("body").removeClass("LargeFont");
          $("body").removeClass("MediumFont");
          next();
        }).queue(function (next) {
          $("body").addClass("SmallFont");
          createCookie("FontSize", "SmallFont", 30);
          next();
        });
        return false;
      });
    },
    addActive: function (el) {
      $(el).siblings().removeClass('active');
      $(el).addClass('active');
    },
    setUpFont: function () {
      var fs = readCookie("FontSize");
      switch (fs) {
      case "LargeFont":
        $("body").addClass("LargeFont");
        $.addActive('.font_controllers a.big_link');
        break;
      case "SmallFont":
        $.addActive('.font_controllers a.small_link');
        $("body").addClass("SmallFont");
        break;
      default:
        $.addActive('.font_controllers a.medium_link');
        $("body").addClass("MediumFont");
        break;
      }
    },
    init: function () {
      $(window).resize(function () {});
      $(window).scroll(function () {});
      $(window).load(function () {});
      $.inputtextValuesSet();
      $.form_errorFunctionalty();
      $.equalHeight();
      $.snippettabsBehavior();
      //$.faqtabsBehavior();
      $.cuSelApply();
      $.paginatorActions();
      $.cycleApply();
      $.scaleFont();
      $.setUpFont();
      if ($.browser.msie) {
        $.ieFunctions();
      };
    }
  });
})(jQuery);

//start jquery without conflicts
jQuery.noConflict();

//initialize the jquery extensions
jQuery(function ($) {
  $.init();
  /* Open document links in new window */
	$("a[href*=pdf]").attr('target','_blank');
	$("a[href*=docx]").attr('target','_blank');
	$("a[href*=doc]").attr('target','_blank');
	$("a[href*=xls]").attr('target','_blank');
	$("a[href*=xlsx]").attr('target','_blank');
	$("a[href*=ppt]").attr('target','_blank');
	$("a[href*=pptx]").attr('target','_blank');
	$("a[href*=jpg]").attr('target','_blank');
	$("a[href*=png]").attr('target','_blank');
	$("a[href*=gif]").attr('target','_blank');
	$("a[href*=txt]").attr('target','_blank');
});

//this was taken out of the $.extend.init because [lte MSIE8] refused to obey.
var addthis_config = {
	pubid: "xa-4d1a011b517a4731",
	services_compact: 'facebook, twitter, linkedin, email, print, google, stumbleupon, favorites, messenger, myspace, digg',
	ui_click: true,
	ui_language: "nl"
};

