$(document).ready(function() {
  // pokaz tylko 1 oferte (losowo)
  var offers = $('.offers .content li');
  offers.hide().eq(Math.floor(offers.size()*Math.random())).show();
  $('.offers').removeClass('hidden');
  
  // pokaz tylko 1 ogłoszenie (losowo)
  var announcements = $('.announcements .content li');
  announcements.hide().eq(Math.floor(announcements.size()*Math.random())).show();
  $('.announcements').removeClass('hidden');  


  // taby z top3 niusami
  $('#featured #container').tabs({ fx: { opacity: 'toggle' } }).tabs('rotate', 9000);
  
  
  $('#featured p.next a').click(function() {
    var tabs_size = $('.ui-tabs-nav li').size();
    var tabs = $('#featured #container').tabs();
    var selected_tab = tabs.data('selected.tabs');
    var select_next = selected_tab+1;
    
    if(select_next >= tabs_size) {
      select_next = 0;
    }
    
    $('#featured #container').tabs('select', select_next);
    return false;
  });
  
  //$('#featured > .next_nav > ul').tabs('select', select_next);
  //  return false;
  //});
  //body .postbitlegacy .postrow
  ImageResize.parse('body .postbitlegacy .postrow');

  // drop shadow
  /*
  Shadow.drop('.threadlisthead a', '#efefef');
  Shadow.drop('.forumhead .forumtitle a', '#efefef');
  Shadow.drop('.forumhead .forumtitle', '#efefef');
  
  Shadow.drop('.forumhead .forumlastpost', '#efefef');
  Shadow.drop('.forum_info .forumoptiontitle', '#efefef');
  Shadow.drop('.options_block_container h4', '#efefef');
  Shadow.drop('#wgo h2', '#efefef');
  Shadow.drop('.blocktitle', '#efefef');
  //Shadow.drop('.blockhead', '#efefef')
  */




});


var Shadow = {
  
  drop: function(selector, shadowColor) {
    $(selector).each(function() {
      
      if($(this).find('.shadowjs').length == 0) { 
        var shadow = $(this).clone().addClass('shadowjs').css({
          top: (this.offsetTop - 1), left: (this.offsetLeft - 1)
        });
      
        $(this).css({color: shadowColor}).before(shadow);
      }
      
    })
    
    
  }
  
}


var ImageResize = {
  margin: 8,
  
  parse: function(selector, margin) {
    if(margin) { this.margin = margin; }
    var max_width = $(selector).width() - this.margin;
    
    $(selector + ' img').each(function(i, img) { 
      ImageResize.resize(img, max_width); 
    });    
  },
  
  resize: function(img, max_width) {
    // if image is bigger that max_width we add them width = max_width and don't check image again
    if (img.width > max_width) {
      img.style.width = max_width + 'px';
      return true;
    }
    
    // if image isn't complete loaded just bind to check size again after 5 ms
    if (!img.complete) {
      setTimeout(function() { 
        ImageResize.resize(img, max_width); 
      }, 5);
    }
    return false; 
  }
}



