(function($) {
   $.fn.equalizeCols = function(children){
      var child = Array(0);
      if (children) child = children.split(",");
      var maxH = 0;
      this.each( function(i) { if (this.offsetHeight>maxH) maxH = this.offsetHeight;   })
      .css("height", "auto")
      .each( function(i) { var gap = maxH-this.offsetHeight;
         if (gap > 0) {
            t = document.createElement('div');
            /*$(t).attr("class","fill").css("height",gap+"px");*/

            if (child.length > i) {
               $(this).find(child[i]).children(':last-child').after(t);
            } else {
               $(this).children(':last-child').after(t);
            }
         }
      });
   }
})(jQuery);


/* Global Javascript */
$(document).ready(function(){ 
   $('#region').change(function(event){
      window.open($(this).val());
   });
   $('#region-ogp').change(function(event){
      this.form.submit();
   });
   $('#student_programs').change(function(event){
      //window.location = $(this).val();
      window.open($(this).val());
   });

   $(".section-about li.join-our-team").remove();

   /* ----------------------- initialize buttons */
   $('div.tier4 div.title').prepend('<div class="teaser collapse">Expand for full article and pictures</div>');

   $('div.tier4 div.title:first').addClass('first-title');
   $('div.tier4 div.title:not(:first)').addClass('next-title');
   $('div.tier4 div.content:first').addClass('first-content');
   $('div.tier4 div.content:not(:first)').addClass('next-content');

   /* ----------------------- wrap all elements inside of content with divs */
   $('div.tier4 div.content').children().wrap('<div></div>');

   $('div.tier4 div.first-content').children('div:not(:first-child)').hide();
   $('div.tier4 div.first-content img:not(:first)').hide();

   $("div.tier4 div.first-title").click(function(){
      $(this).children('div.teaser').each(function(){
         var t = $(this).parent().attr("class");
         if (t == "title first-title expand") {
            $(this).empty().append('Expand for full article and pictures').removeClass('expand').addClass('collapse');
         } else {
            $(this).empty().append('Hide this article').removeClass('collapse').addClass('expand'); // append minus sign
         }
      });
      
      $(this).next('div.content').children('div:not(:first)').slideToggle('fast');
      $(this).next('div.content').find('img:not(:first)').slideToggle('fast');
      $(this).toggleClass("expand");
   });

   /* ------------------------ hide all images and elements wrapped in divs after 1st post */
   $('div.tier4 div.next-content').children('div:not(:first-child)').hide();
   $('div.tier4 div.next-content img').hide();
   $("div.tier4 div.next-title").click(function(){
      $(this).children('div.teaser').each(function(){
         var t = $(this).parent().attr("class");
         if (t == "title next-title expand") {
            $(this).empty().append('Expand for full article and pictures').removeClass('expand').addClass('collapse');
         } else {
            $(this).empty().append('Hide this article').removeClass('collapse').addClass('expand'); // append minus sign
         }
      });

      $(this).next('div.content').children('div:not(:first)').slideToggle('fast');
      $(this).next('div.content').find('img').slideToggle('fast');
      $(this).toggleClass("expand");
   });


   $('div#nav li').removeClass('expanded').removeClass('collapsed');
   $("div#nav ul:first-child").children().attr("id", function (arr) { return "navid" + arr;        });

   $("div#nav ul").superfish({ pathClass:  'active-trail'  });

   $("div#nav a:contains('Corporate Video Library')").parent().remove();

   //$("legend").replaceWith("<h4><b>" + $("legend").text() + "</b></h4>");
});

$().ajaxComplete(function() {
   $('div.tier4 div.teaser').each(function(){
      removeClass('collapse');
      removeClass('expand');
   });

   /* ----------------------- initialize buttons */
   $('div.tier4 div.title').prepend('<div class="teaser collapse">Expand for full article and pictures</div>');

   $('div.tier4 div.title:first').addClass('first-title');
   $('div.tier4 div.title:not(:first)').addClass('next-title');
   $('div.tier4 div.content:first').addClass('first-content');
   $('div.tier4 div.content:not(:first)').addClass('next-content');

   /* ----------------------- wrap all elements inside of content with divs */
   $('div.tier4 div.content').children().wrap('<div></div>');

   $('div.tier4 div.first-content').children('div:not(:first-child)').hide();
   $('div.tier4 div.first-content img:not(:first)').hide();

   $("div.tier4 div.first-title").click(function(){
      $(this).children('div.teaser').each(function(){
         var t = $(this).parent().attr("class");
         if (t == "title first-title expand") {
            $(this).empty().append('Expand for full article and pictures').removeClass('expand').addClass('collapse');
         } else {
            $(this).empty().append('Hide this article').removeClass('collapse').addClass('expand'); // append minus sign
         }
      });

      $(this).next('div.content').children('div:not(:first)').slideToggle('fast');
      $(this).next('div.content').find('img:not(:first)').slideToggle('fast');
      $(this).toggleClass("expand");
   });

   /* ------------------------ hide all images and elements wrapped in divs after 1st post */
   $('div.tier4 div.next-content').children('div:not(:first-child)').hide();
   $('div.tier4 div.next-content img').hide();
   $("div.tier4 div.next-title").click(function(){
      $(this).children('div.teaser').each(function(){
         var t = $(this).parent().attr("class");
         if (t == "title next-title expand") {
            $(this).empty().append('Expand for full article and pictures').removeClass('expand').addClass('collapse');
         } else {
            $(this).empty().append('Hide this article').removeClass('collapse').addClass('expand'); // append minus sign
         }
      });

      $(this).next('div.content').children('div:not(:first)').slideToggle('fast');
      $(this).next('div.content').find('img').slideToggle('fast');
      $(this).toggleClass("expand");
   });
});

