function ieWarning() {
  if(jQuery.browser.msie && 
      (jQuery.browser.version=='5.0' ||
       jQuery.browser.version=='5.5' ||
       jQuery.browser.version=='6.0')){
    $("body").append('<p id="attention_ie6"><span>Uwaga! Używana wersja przeglądarki jest nieaktualna. Zaktualizuj by poprawnie wyświetlać tę stronę.</span></p>')
             .css('top', '25px');
    $("#attention_ie6").css('display','none');
    $("#attention_ie6").slideDown();
    $("#attention_ie6").click(function(){
      $("body").css('top', '0');
      $(this).hide();
    });
  }
}

function gallery() {
  $("div.publish div.thumbframe").each(function(){
    $(this).find('a:first').attr('href',$(this).find('h4 a').attr('href'));
  });

  $("div.project div.thumbframe, div.projectlist div.thumbframe, div.index div.thumbframe").each(function(){
    $(this).click(function(){
      var id=$(this).attr('id');
      var prev;

      id=id.substr(6,id.length);

      prev=$.briks.gallerycurrent;
      $.briks.gallerycurrent=id;

      idold="div#image-"+prev+" img";
      id="div#image-"+id+" img";

      if (prev!=0) {
        $(idold).fadeOut(500,function(){
          $(id).fadeIn(500);
        });
      } else {
        $(id).fadeIn(500);
      }
      
      $(this).find("div.posttext").each(function(i,o){
        $("div.postplace").fadeOut(500,function(){
          $(this).empty().append($(o).html()).fadeIn(500);
        });
      });
    });
  });

// use or not?
  $('div.images div.thumbframe:last').css('paddingBottom','0px');
  $('div.entry p:last, p:last, div.entry li:last').each(function() {
    if (!$(this).next()[0] && $(this).parent().parent().hasClass('scrollable')) {
      $(this).css('marginBottom','0px');
      $(this).css('paddingBottom','0px');
    }
  });
}

function fixIE() {
  if (jQuery.browser.msie && jQuery.browser.version=='6.0') {
    $("ul#mainmenu li ul").each(function(){
      $(this).css({top:20});
    });
    
    jQuery.ifixpng('/images/clear.gif');
    $('div.statement').ifixpng();
  }
}

function heightSetup() {
  var h=$(window).height()-124;

  $('div.maincontainer').height(h);
  $('div.bar').each(function(){
    $(this).height(h);
  });

  $('div.scrollable').each(function(){
    if ($(this).height()>(h-60)) {
      $(this).height(h-60);
    }
  });

  $('div.imageframe').each(function(){
    var w=$(this).parent().width();
    $(this).width(w);
    $(this).height($(this).find('img').height()+20);
  });
}

function scrollSetup() {
  $('div.scrollable').each(function(i,sc) {
    if ($(this).hasClass('images')) {

      $.briks.cimage=0;
      $.briks.nrimages=0;
      $(this).find('div.thumbframe').each(function(i,o){
        $.briks.imagepos[i]={};
        $.briks.nrimages=$.briks.nrimages+1;
        if (i>0 && $(o).next()[0]) {
          $.briks.imagepos[i].t=$(o).position().top;
          $.briks.imagepos[i-1].b=$(o).position().top;
        } else if (i==0) {
          $.briks.imagepos[i].t=0;
        } else if (!($(o).next()[0])) {
          $.briks.imagepos[i-1].b=$(o).position().top;
          $.briks.imagepos[i].t=$(o).position().top;
          $.briks.imagepos[i].b=$(o).position().top+$(o).height();
        }
      });

      $(this).jScrollPane({
                   scrollBoxes: true, 
                   wheelSpeed: 1,
                   animateStep: 10, 
                   showArrows: true, 
                   animateTo: true, 
                   scrollbarWidth: 0, 
                   scrollbarMargin: 0,
                   reinitialiseOnImageLoad: true});
    } else {
      if ($(this).attr('scrollHeight')>$(this).parent().height()) {
        $(this).jScrollPane({animateStep: 10, 
                     scrollBoxes: false,
                     showArrows: false, 
                     animateTo: true, 
                     scrollbarWidth: 2, 
                     scrollbarMargin: 10,
                     dragMinHeight: 20,
                     dragMaxHeight: 20,
                     reinitialiseOnImageLoad: true});
      }
    }

  var currentproject=-1;
  $(this).children('div.thumbframe').each(function(j,thumb) {
    if ($(thumb).attr('id')==$(sc).attr('title')) {
      currentproject=j;
    }
  });

  if (currentproject!=-1) {
    $('div.scrollable').animate({'top': '-'+parseInt($.briks.imagepos[currentproject].t)+'px'},300);
    $.briks.cimage=currentproject;
  }

});
}

function menuSetup() {
  $('ul#mainmenu>li').each(function(i,val) {
    $(val).hover(function(){
      if (!$(val).hasClass('current_page_item') && !$(val).hasClass('current_page_ancestor')) {
        $(val).find('ul').show();
      }
    },
    function(){
      if (!$(val).hasClass('current_page_item') && !$(val).hasClass('current_page_ancestor')) {
        $(val).find('ul').hide();
      }
    });

    var item=$(this).find(':first');
    cl=$.briks.lang+'_menu_0_'+i;

    $(item).hover(function(){
//      $(this).addClass('menu-on');
    },
    function(){
//      $(this).removeClass('menu-on');
    });
  });
}

function setupProgress() {
  $('div#progressbar').show();

  $('div.imageframes img').load(function() {
    $('div#progressbar').width($('div#progressbar').width()+($(window).width()/$.briks.imgs));
  });
}

$(window).load(function() {
  $('div#progressbar').width($(window).width());
  $('div#progressbar').fadeOut(500);

  // show first images and first post
  $('div.posttext:first').each(function(i,o){
    $("div.postplace").hide().append($(o).html()).fadeIn(500);
  });

  $('div.project div.imageframe:first img').add('div.index div.imageframe:first img').each(function(){
    $(this).fadeIn(500);
    var id=($(this).parent().attr('id'));
    id=id.substr(6,id.length);
    $.briks.gallerycurrent=id;
  });
});

$(document).ready(function() {
  $.briks={};
  $.briks.imgs=$('div.imageframes img').length;

  setupProgress();

  $.briks.gallerycurrent=0;
  $.briks.imagepos={};
  $.briks.menus={};
  $.briks.menus[0]=88;
  $.briks.menus[1]=64;
  $.briks.menus[2]=48;
  $.briks.menus[3]=56;
  $.briks.menus[4]=56;
  $.briks.lang=$('body').attr('lang');
  $.briks.pf=2;
  $.briks.wheelskip=6;
  $.briks.wheelcount=0;

  ieWarning();

  var fStyle=new FLIRStyle({mode:'wrap', realFontHeight: true, resizeBox: true, useBackgroundMethod: true});
  //var fStyle=new FLIRStyle({mode:'fancyfonts', ff_Wrap: true, ff_BlurEdges: true, realFontHeight: true, resizeBox: true, useBackgroundMethod: true});

  $.preloadCssImages();
  FLIR.init();
  $("h2, h3, h4, div.postmetadata").each(function() {
//    FLIR.replace(this,fStyle);
  });

  menuSetup();  
  fixIE();
  heightSetup();
  gallery();

  $('div.statement').show();
  $("div.cycle").cycle({before: function(){
    if ($.briks.pf==0) {
      $('div.statement').remove();
    } else {
      $.briks.pf=$.briks.pf-1;
    }
  }});

  $('div.entry p+ul').prev().css('marginBottom', '5px');

  scrollSetup();

  $(window).resize(function(){
    heightSetup();
    scrollSetup();    
  });
});
