function el(id)
{
  return document.getElementById(id);
}

function fix_height()
{
  var links_height = el('links').offsetHeight / 2;
  if(el('srodek').offsetHeight < el('menu').offsetHeight - links_height)
  {
    el('links_content').style.display = 'none';
    var button = document.createElement('span');
    el('links').innerHTML += '<span id="links_button" onclick="show_links();">[kliknij aby rozwinąć]</span>';
  }
  
  if(el('srodek').offsetHeight > el('menu').offsetHeight + 290) add_fb_box('menu');
  if(el('srodek').offsetHeight > el('menu').offsetHeight + 290) add_lc_box('menu');
}

function show_post_sidebar()
{
  if(typeof(related_posts_html)!='undefined')
  {
    var related_box = document.createElement('div');
    related_box.setAttribute('class', 'box');
    related_box.innerHTML = '<h2>Podobne wpisy</h2>'+related_posts_html;
    el('menu2').appendChild(related_box);
  }
  
  add_fb_box('menu2');
}

function add_fb_box(container_id)
{
  var like_box = document.createElement('iframe');
  like_box.src = 'http://www.facebook.com/plugins/likebox.php?href=http%3A%2F%2Fwww.facebook.com%2Fpages%2FCichy-Fragles%2F161798967199801%3Fcreated&colorscheme=light&show_faces=true&stream=false&header=false&width=200&height=290';
  like_box.setAttribute('scrolling', 'no');
  like_box.setAttribute('frameborder', '0');
  like_box.setAttribute('allowTransparency', 'false');
  like_box.setAttribute('style', 'clear:both; width:200px; height:290px; overflow:hidden; margin-bottom:10px; border:none; background:#FFF;');
  el(container_id).appendChild(like_box);
}

function add_lc_box(container_id)
{
  //setTimeout(add_lc_box_2, 100);
}

function add_lc_box_2()
{
  if(el('srodek').offsetHeight > el('menu').offsetHeight + el('menu2').offsetHeight + 290)
  {
    el('lc_box').style.display = 'block';
  }
}

function show_links()
{
  el('links_button').style.display = 'none';
  el('links_content').style.display = 'block';
}

function init_page()
{
  search_form.switch_field();
  archive.init();
  fix_height();
  button_loader.init();
}

function highlight_category()
{
  var url = window.location.protocol + '//' + window.location.hostname + window.location.pathname.replace(/(kategoria\/[a-z]+).*$/, '$1/');
  var cats = el('cats').getElementsByTagName('a');
  for(var i=0; i<cats.length; i++)
  {
    if(cats[i].href==url)
    {
      cats[i].style.color = '#FFF';
      break;
    }
  }
}

function highlight_post_cats()
{
  var cats = el('cats').getElementsByTagName('a');
  var post_cats = el('data').getElementsByTagName('a');
  for(var i=0; i<post_cats.length; i++)
  {
    for(var j=0; j<cats.length; j++)
    {
      if(cats[j].href==post_cats[i].href)
      {
        cats[j].style.color = '#FFF';
        break;
      }
    }
  }
}

function fix_trackback()
{
  var tb = el('trackback').getElementsByTagName('li');
  for(var i=0; i<tb.length; i++)
  {
    var a = el(tb[i].id+'_nick').firstChild.nextSibling;
    var p = el(tb[i].id+'_text').firstChild;
    var new_title = document.createElement('h3');
    new_title.innerHTML = '<a href="'+a.href+'">'+p.firstChild.data+'</a>';
    p.removeChild(p.firstChild);
    p.removeChild(p.firstChild);
    p.insertBefore(new_title, p.firstChild);
  }
}

var archive = {
  init : function()
  {
    var temp, year, month, url, number;
    var prev_year = '', first_year = '';
    var content = '<ul>';
    var months = el('archive_content').getElementsByTagName('li');
    for(var i=0; i<months.length; i++)
    {
      temp = months[i].firstChild.innerHTML.split(' ');
      month = temp[0];
      year = parseInt(temp[1]);
      url = months[i].firstChild.href;
      number = months[i].lastChild.innerHTML;
      if(year!=prev_year)
      {
        if(prev_year=='')
        {
          first_year = year;
        }
        else
        {
          content += '</ul></li>';
        }
        content += '<li class="year"><span id="archive_button_'+year+'" onclick="archive.switch_year(\''+year+'\');" style="cursor:pointer;">';
          content += '&nbsp;&#x25B6; ' + year;
        content += '</span>';
        content += '<ul id="archive_'+year+'" style="display:none;">';
        prev_year = year;
      }
      content += '<li><a href="'+url+'">'+month+'</a><span><i>'+number+'</i></span></li>';
    }
    content += '</ul></li></ul>';
    el('archive_content').innerHTML = content;
    archive.switch_year(first_year);
  },
  
  switch_year : function(year)
  {
    if(el('archive_'+year).style.display=='none')
    {
      el('archive_button_'+year).innerHTML = '&#x25BC; '+year;
      el('archive_'+year).style.display = 'block';
    }
    else
    {
      el('archive_button_'+year).innerHTML = '&nbsp;&#x25B6; '+year;
      el('archive_'+year).style.display = 'none';
    }
  }
};

var button_loader = {
  data : {},

  init : function()
  {
    var divs = document.getElementsByTagName('div');
    for(var i=0; i<divs.length; i++)
    {
      if(divs[i].className=='fb_like')
      {
        button_loader.data[divs[i].id] = 0;
      }
    }
    button_loader.check();
  },
  
  check : function()
  {
    var all_loaded = true;
    for(var id in button_loader.data)
    {
      if(button_loader.data[id]==0 && el(id).offsetTop > window.pageYOffset && el(id).offsetTop < window.pageYOffset + window.innerHeight)
      {
        button_loader.load(id);
      }

      if(button_loader.data[id]==0)
      {
        all_loaded = false;
      }
    }
    
    if(!all_loaded)
    {
      setTimeout(button_loader.check, 100);
    }
  },
  
  load : function(id)
  {
    if(button_loader.data[id]==0)
    {
      var url = el(id.replace('fbl', 'link')).href;
      el(id).innerHTML = '<iframe src="http://www.facebook.com/widgets/like.php?href='+url+'" style="width:370px;"></iframe>';
      el(id).style.background = 'transparent';
      button_loader.data[id] = 1;
    }
  }
};

var search_form = {
  default_text : 'Wpisz tutaj tekst do wyszukania...',
  
  switch_field : function()
  {
    if(el('search_field').value=='')
    {
      el('search_field').value = search_form.default_text;
    }
    else if(el('search_field').value==search_form.default_text)
    {
      el('search_field').value = '';
    }
  },

  check : function ()
  {
    if(el('search_field').value=='' || el('search_field').value==search_form.default_text)
    {
      alert('Musisz wpisać tekst do wyszukania!');
      return false;
    }
    return true;
  }
};

