$(document).ready(function() {

    var eventFilter = new EventFilters();
  eventFilter.init();
  
  renderTabbedBlocks();
  
try{

  $('a.btnNewsletterSettings').parent().find(".newsletterSettings").hide();

  $('a.btnNewsletterSettings').live("click", function(){
    $(this).blur();
    var panel = $(this).parent().find(".newsletterSettings");
    
    if(panel.is(".rollin"))
    {  }
    else
    {
      panel.addClass("rollin");
      panel.stop().slideToggle(250, function(){ 
        panel.removeClass("rollin");
      });
    }
    return false;
  });

}catch(ex){}

try{
  $(".content").each(function(){
    $(this).find("a").each(function(){
      $(this).css("color", $(this).parents(".content").css("color"));
    });
  });
}catch(ex){}

try{
  $('div.block_img').each(function(){
    
    if($(this).find('a:first').length > 0)
    {
      $(this).css('cursor','pointer');
      $(this).bind('click', function() {
        var link = $(this).find('a:first').attr('href');
        if (link.indexOf(location.hostname) == -1)
          window.open (link,"mywindow"); 
        else
          location.href = link;
      });
    }
  });
}catch(ex){}

try{
  // doc2form styling
  $('.Doc2Form .umbEditorTextField,#contour input.text').each(function(){
    $(this).before($('<div class="newsletter_front png_bg">&nbsp;</div>'));
    $(this).after($('<div class="newsletter_back png_bg">&nbsp;</div>'));
  });
  $('.Doc2Form input[type="checkbox"]').each(function(){
    
    //var txt = $(this).prev().text();
    //$(this).prev().html('&nbsp;');
    $(this).addClass('checkbox');
    // $(this).next().text(txt).addClass('checkboxLabel');
    $(this).next().addClass('checkboxLabel');
    //$(this).after($('<label for="' + $(this).attr('id') + '">&nbsp;</label>').text(txt));
    //alert($(this).attr('class'));
  });
}catch(ex){}

try{
  /* link icons */
  var parentSelector = "#blocks ";
  // Add pdf icons to pdf links
  $(parentSelector + "a[href$='.pdf']").addClass("pdf").addClass("png_bg").attr("target", "_blank");
 
  // Add txt icons to document links (doc, rtf, txt)
  $(parentSelector + "a[href$='.doc'], a[href$='.txt'], a[href$='.rtf']").addClass("txt").addClass("png_bg").attr("target", "_blank");
 
  // Add zip icons to Zip file links (zip, rar)
  $(parentSelector + "a[href$='.zip'], a[href$='.rar']").addClass("zip").addClass("png_bg").attr("target", "_blank"); 
 
  // Add email icons to email links
  // not used Gino was not in favor of icons in front of email links
  // $(parentSelector + "a[href^='mailto:']").addClass("email").addClass("png_bg");
 
  // add pngfix to buttons in rte environement
  $(parentSelector + ".richtextButtonBig").addClass("png_bg");
  $(parentSelector + ".richtextButtonSmall").addClass("png_bg");

  //Add external link icon to external links - 
  $(parentSelector + 'a').filter(function() {
    //Compare the anchor tag's host name with location's host name
      return this.hostname && this.hostname !== location.hostname;
    }).attr("target", "_blank"); // pssible to add  .addClass("external").addClass("png_bg");

  /* end link icons */

}catch(ex){}

try{

    // THIS IS NEW CODE FOR THE AUTOMATIC INFINITE CAROUSEL

  $('.carrouselcontent').each(function(i, item){
    var currentCarrousel = $(this);
        var autoscrolling = true;
    var duration=5000;

        currentCarrousel.infiniteCarousel().mouseover(function () { autoscrolling = false; }).mouseout(function () { autoscrolling = true; });

    if (currentCarrousel.attr('rel')) { duration=currentCarrousel.attr('rel'); }

        setInterval(function () {
             if (autoscrolling) { currentCarrousel.trigger('next'); }
        }, duration);
  });


}catch(ex){}

try{
  if($('#breadcrumb').length > 0 )
  {
    $('.sidebar .shareblock').css('marginTop','-30px');
  }else{
    $('.sidebar .shareblock').css('marginTop','2px');
  }
}catch(ex){}


});


// added by sander since timetables
function renderTabbedBlocks(){
  var blocks = $('.tabbedBlock');
  for(var i = 0; i < blocks.length; i++)
  {
    var blk = blocks[i];
    var block = $(blk);

    block.find('div.tabContent').hide();
    block.find('div.tabContent:first').show();
    block.find('ul.tabs li:first').addClass('active');

    block.find('ul.tabs li a').bind("click", function(){
      $(this).blur();
      var targetDiv = $(this).attr('rel');
      block.find('ul.tabs li.active').removeClass('active');
      $(this).parents('li').addClass('active');
      block.find('div.tabContent').hide();
      block.find('div.' + targetDiv).show();
      return false;
    });
    
  }
}



// added by sander event filters
function EventFilters() {
  var options = {
    filters: $('#eventFilters'),
    list: $('#list')
  };
  this.init = function(){
    if(options.filters.length > 0)
    {
      var themeFilter = $('.themeFilter select', options.filters);
      this.sortThemes(themeFilter);
      var dateFilter = $('.dateFilter select', options.filters);
      var items = $('.list_item', options.list).show();
      
      // fill date filter select
      for(var i = 0; i <= items.length -1; i++)
      {
        var item = items[i];
        var s = $(item).attr('rel');
        $(item).removeAttr('rel');
        var sp = s.split('#');
                      
        var l = $(dateFilter).find('option').filter(function(){
          return $(this).val() == 'filter-' + sp[0];
        }).length;

        if(l == 0)
        {
          var opt = $('<option value="filter-' + sp[0] + '">' + sp[1] + '</option>');
          dateFilter.append(opt); 
        }
      }
      
      
      // theme filter
      themeFilter.change(function(){
        dateFilter.val('all');

        items.show();
        var selected = $(this).val();
        //console.log(selected);
        if(selected != 'all')
        {
           items.not('.' + selected).hide();
          $('#no-items').toggle(items.filter('.' + selected).length == 0);
        }
      });
      
      // date filter
      dateFilter.change(function(){
        themeFilter.val('all');
        
        items.show();
        var selected = $(this).val();
        //console.log(selected);
        if(selected != 'all')
        {
           items.not('.' + selected).hide();
          $('#no-items').toggle(items.filter('.' + selected).length == 0);
        }
      });
    }
  };
  this.sortThemes = function(mylist){
    var listitems = mylist.children('option').get();
    listitems.sort(function(a, b) {
      var compA = $(a).text().toUpperCase();
      var compB = $(b).text().toUpperCase();
      return (compA < compB) ? -1 : (compA > compB) ? 1 : 0;
    })
    $.each(listitems, function(idx, itm) { mylist.append(itm); });
  };
};

