(function(){

  var whiterel;

  $(window).resize(function(){
    var columnW = 252;
    if($(window).width()>503){
      var primaryW = Math.floor( $(window).width() / columnW ) * columnW;
      $('.mosaic-list').css({width: primaryW});
      $('.list-head').css({width: primaryW});
    }
    else {
      $('.mosaic-list').css({width: 'auto'});
      $('.list-head').css({width: primaryW});
    }
    whiterel.remove();
    if($(window).width()<1260){
      $('.rel-2').eq(0).before(whiterel);
    } else {
      $('.rel-1').eq(1).before(whiterel);
    }
    $('.size-l img').origsize2height();
    $('.rel-1').css("height","auto").equalHeights();
    if ($('.mosaic-list').data("masonry")) {
      $('.mosaic-list').masonry("reload");
    }
  });

  $(window).bind('orientationchange',function(e){
    $(window).resize();
  });

  $(function(){

    whiterel = $('.rel-1').eq(1);

    $(".article").fitVids();

    // Carousel:
    $('#rbmacarousel').carousel({
      slider: '.header-carousel',
      slide: '.slide',
      addPagination: true,
      addNav: true,
      speed: 300
    });

    // Countdown
    var date = new Date($("#countdown-time").text());
    var now = new Date();
    var diffDays = (date.getTime() - now.getTime()) / 86400000;
    $("#countdown-time").countdown({
      "htmlTemplate" : diffDays >= 1 ? "%{d}d %{h}h %{m}m" : "%{h}h %{m}m %{s}s",
      "date"         : $("#countdown-time").text()
    });

    // Other stuff
    $(window).resize();

    // determine current height for images
    $('.mosaic-item img').origsize2height();

    // masonry
    if($(window).width()>504){
      $('.teaser-countdown, .teaser-twitter, .teaser-facebook').css("display", "block").equalHeights();
      $('.mosaic-list').masonry({
        itemSelector : '.mosaic-item',
        columnWidth  : 252
      });

      // Equal Heights
      $('.rel-1').equalHeights();
    }

    // Here begineth the section that deals with the notice on the page header:
    // is the notice currently on the page?
    //var has_nc = false;
    // has the notice been removed before?
    //var remove_nc = new RegExp('(?:^|; )remove_nc=([^;]*)').exec(document.cookie);
    //remove_nc = (remove_nc !== null && remove_nc[1] == "true");
    // show the notice if it's not already there and if it has not been removed previously
    //function show_nc()  {
    //  if (!remove_nc && !has_nc) {
    //    $('<div id="notice_container"><div class="notice_container_text"><a href="/blog.html">Echa un vistazo a nuestro blog en castellano con los últimos contenidos de Madrid 2011!</a></div><a id="notice_container_close"><div style="width: 16px; height: 16px"></div></a></div>').prependTo('#page').hide().delay(500).slideDown().animate({opacity: 0.3}, 200 ).animate({opacity: 1}, 500 );
    //    has_nc = true;
    //  }
    //}
    //show_nc();
    // close the notice and never show it again:
    //$('#notice_container_close').live('click',function(){
    //  $('#notice_container').slideUp();
    //  document.cookie = "remove_nc=true";
    // remove_nc = true;
    //});


    // Only for demo purposes! To be removed in final implementation!
    var uri_arr = window.location.search.split('?');
    var live = (uri_arr[1] === 'live') ? true : false;
    if (live == true) {
      $('#rbmacarousel').hide();
      $('.video-panel').show();
    }

  });

})();

