﻿$(function() {
 
  $('div#content a.blank').click(function() {
  window.open(this.href);
  return false;
 });
 $('div#content div.redirBox a.sub').click(function() {
  $(this).parents('div.redirBox').find('input.submit').click();
  return false;
 });
 $('input#phrase').each(function() {
  $(this).addClass('virgin').focus(function() {
   if($(this).hasClass('virgin')) {
    $(this).attr('rel',$(this).val()).val('').removeClass('virgin');
  }
  }).blur(function() {
   if($(this).val().length < 1) {
     $(this).val($(this).attr('rel')).attr('rel','').addClass('virgin');
  }
  });
 });
 $('div#navigation li').not($('div#navigation div.source li')).hover(function() {
  if(ie6()) {
    //$('div#right select,#gfilters select').hide();
 }
  if($('div#navigation li.last').length < 1) {
    $('div#navigation li.selected').addClass('last');
  }
  $('div#navigation li.selected').removeClass('selected').next().removeClass('none');
  $(this).addClass('selected').next().addClass('none');
   $('div#navigation div.further ul').html($(this).find('div.source ul').eq(0).html()).parents('div.further').show();
 },function() {});
 $('div#navigation').hover(function() {},function(e) {
  if(ie6()) {
    //$('div#right select,#gfilters select').show();
 }
   $('div#navigation div.further ul').empty().parents('div.further').hide();
   $('div#navigation li.selected').removeClass('selected').next().removeClass('none')
   $('div#navigation li.last').removeClass('last').addClass('selected').next().addClass('none');
 });
 $('ul#contextList').each(function() {
  if($(this).children().length > 0) {
    $('div#context ul').html($(this).html());
    $('div#context ul a.blank').click(function() {
      window.open(this.href);
      return false;
  });
 } else {
  $('div#context').remove();
 }
 $(this).remove();
 });
 
 var dateMonths = [
  'stycznia',
  'lutego',
  'marca',
  'kwietnia',
  'maja',
  'czerwca',
  'lipca',
  'sierpnia',
  'września',
  'października',
  'listopada',
  'grudnia'
 ];
 $('div#news ul.news li h5 span.date').each(function() {
  var txt = $(this).text();
  var year =  txt.substring(0,4);
  var month = txt.substring(5,7);
  if(month.indexOf('0') == 0) {
   month = month.substring(1);
 }
 month = parseInt(month)-1;
  var day = txt.substring(8,10);
  if(day.indexOf('0') == 0) {
   day = day.substring(1);
 }
  var ds = day+" "+dateMonths[month]+" "+year;
  $(this).text(ds);
 });
 $('#navigation ul').eq(0).each(function() {
  $(this).find('li:first').addClass('none');
  $(this).find('li.selected').next('li').addClass('none');
 });
 $('#tabs ul').eq(0).each(function() {
  $(this).find('li:last').addClass('none');
  $(this).find('li.selected').prev('li').addClass('none');
 });
 $('div#main table.prices').each(function() {
  $(this).addClass('prices');
 var i = 0;
  $(this).find('tr').each(function() {
    i++;
    $(this).addClass(i%2 == 0 ? 'even' : 'odd');
 });
 });
 $('div#main table.big').each(function() {
  $(this).wrap('<div class="tbWrap"><div class="tbIn"></div></div>');
 });
 $('div#left ul.style,div#left ol.style').each(function() {
  $(this).wrap('<div class="ulWrap"></div>');
 });
 $('div#left ol.style').each(function() {
  var i = 0;
  $(this).find('li').each(function() {
    i++;
    $(this).prepend('<span class="no">'+i+'</span>');
  });
 });
 if($.browser.msie) {
  $('hr').before('<div class="hr bodyBg"></div>').remove();
 }
 $.fn.directSwap = function(needle) {
  return $(this).each(function() {
    var html = $(this).html();
    var re = new RegExp(needle,'gi')
    html = html.replace(re,'<cite>direct</cite>');
    var re = new RegExp('\<cite\>\<cite\>'+needle+'\<\/cite\>\<\/cite\>')
    html = html.replace(re,'<cite>direct</cite>');
    $(this).html(html);
 });
 };
 $('h2,h3,p.bigLink a,div.grayBox a.btn dfn,div.button a.btn dfn').directSwap('direct');
 $('div#news h4,div#news h5,div#promo h4,div#docs h4,div#docs li a span').directSwap('direct');
 $('div#hp div.leads h5 a').directSwap('direct');
 $('div#right div#context li a,div#right div#menu li a').directSwap('direct');
 $('div#content ol.slide div.head p,div#content ol.slide div.text a.btn dfn').directSwap('direct');
 $('div#docs li,div#docsEmbed li').each(function() {
  var file = $(this).find('a').attr('href');
  file = file.substring(file.lastIndexOf('.')+1);
  $(this).addClass('file_'+file);
 });
 $('div#content div.slideFiles div.file').each(function() {
  var file = $(this).find('a').attr('href');
  file = file.substring(file.lastIndexOf('.')+1);
  $(this).addClass('file_'+file);
  var txt = $(this).find('a').text();
  $(this).find('a').html(txt+'<br />w formacie '+file.toUpperCase());
 });
 $.fn.pager = function(opts) {
  return this.each(function() {
    opts = opts || {};
    opts.atPage = opts.atPage || 5;
    opts.offset = opts.offset || 3;
    var that = this;
    var page = 0;
    var ul = null;
    if(!opts.noHash) {
     var pageHash = getHash('page');
     if(pageHash != null) {
       page = parseInt(pageHash)-1;
    }
  }
  var li = $(this).find('li');
  var pages = Math.ceil(li.length/opts.atPage);
  if((page >= pages) || (page < 0)) {
  page = 0;
  if(!opts.noHash) {
  setHash('page',1);
 }
 }
   if(pages > 1) {
   var ul = null;
    if(opts.onTop) {
      $(that).parent().before('<ul class="pager"></ul>');
      ul = $(that).parent().prev('ul.pager');
  } else {
     $(that).after('<ul class="pager"></ul>');
     ul = $(that).next('ul.pager');
  }
   for(var i=0;i<pages;i++) {
    $(ul).append('<li><a href="#">'+(i+1)+'</a></li>');
   }
   $(ul).prepend('<li class="prev"><a href="#">wstecz</a></li>');
   $(ul).append('<li class="next"><a href="#">dalej</a></li>');
   $(ul).find('li a').click(function() {
    var par = $(this).parent();
    if($(par).hasClass('prev')) {
     $(ul).find('li.active').prev().find('a').click();
    } else if($(par).hasClass('next')) {
     $(ul).find('li.active').next().find('a').click();
    } else {
     $(ul).find('li.active').removeClass('active');
     $(par).addClass('active');
     page = $(par).prevAll('li').length-1;
     $(li).hide();
     for(var i=0;i<opts.atPage;i++) {
      $(li).eq(page*opts.atPage+i).show();
     }
     if(page == 0) {
      $(ul).find('li.prev').hide();
     } else {
      $(ul).find('li.prev').show();
     }
     if(page == pages-1) {
      $(ul).find('li.next').hide();
     } else {
      $(ul).find('li.next').show();
     }
     if(!opts.noHash) {
    setHash('page',page+1);
   }
     if(opts.afterChange != null) {
      opts.afterChange();
   }
   $(ul).find('li').addClass('hidden');
   $(ul).find('li.prev,li.next').removeClass('hidden');
   for(var i=0-opts.offset;i<=opts.offset;i++) {
     var eq = page+i;
     if(eq<0) continue;
     if(eq>=pages) continue;
    $(ul).find('li').eq(eq+1).removeClass('hidden');
   }
    }
    return false;
   });
    $(ul).find('li').eq(page+1).find('a').click();
  }
  });
 };
 $('div#news ul.news').pager();
 $('div#promo ul.promo').pager();
 $('div#left div.articles div.list ul').pager({ atPage: 4 });
 $('form.redirect').ajaxForm({ beforeSubmit: function() { return false; } });
 $('form.redirect input.submit').click(function() {
  var sel = $(this).parents('form').find('select').eq(0);
  if($(this).parents('form').hasClass('self')) {
      window.location.href = $(sel).val();
   } else {
     var opt = $(sel).find('option').eq($(sel).get(0).selectedIndex);
     if($(opt).attr('class') == 'domain') {
      $(opt).click();
     } else {
      window.open($(sel).val());
     }
  }
  return false;
 });
 $('form.redirect select').change(function() {
  if($(this).parents('form').hasClass('self')) {
      window.location.href = $(this).val();
  } else if($(this).find('option').eq($(this).get(0).selectedIndex).attr('class') != 'domain') {
     window.open($(this).val());
  }
 });
 $('div#right div.brand:first').each(function() {
  $('div#right div#offers').remove();
 });
 
 $('div#content ol.slide').each(function() {
  var i = 0;
  $(this).find('li.item').each(function() {
   i++;
   var head = $(this).find('div.head:first');
   var span = $(head).find('span').text();
   $(head).find('span').remove();
   var txt = $(head).find('p').text();
   $(head).find('p').html(i+'. '+txt+'<span>'+span+'</span>').directSwap('direct');
   
   $(head).find('p').click(function() {
    var li = $(this).parents('li');
    if($(li).hasClass('working')) return false;
    $(li).addClass('working');
    if($(li).hasClass('active')) {
     $(li).find('div.text').slideUp(300,function() {
     $(li).removeClass('working');
     $(li).removeClass('active');
    });
   } else {
    $(li).find('div.text').slideDown(300,function() {
     $(li).removeClass('working');
     $(li).addClass('active');
    });
   }
   });
   
  });
 });
 
 $('#auction').each(function() {
  $(this).find('div.items ul li a').each(function() {
    var href = $(this).attr('href');
    var img = $(this).find('img');
    if(href.length > 0) {
      $(this).parent().find('sub').click(function() {
        window.location.href = $(this).parent().find('a').eq(0).attr('href');
      }).css('cursor','pointer');
    } else {
      $(this).before(img);
      $(this).remove();
    }
  });
 });
 
 $('#guide').each(function() {
  var ol = $(this).find('ol').eq(0);
  var sh = $(this).find('ul.shorts').eq(0);
  if($(ol).find('li').length < 1) return false;
  $(ol).find('li').find('div.head').click(function() {
   if($(ol).attr('class').indexOf('working') >= 0) return false;
   var li = $(this).parents('li').eq(0);
   $(ol).find('li.selected').find('div.text').slideUp(300,function() {
    $(ol).find('li.selected').removeClass('selected');
   });
   if($(li).attr('class').indexOf('selected') < 0) {
    $(ol).addClass('working');
    $(li).addClass('working').find('div.text').slideDown(300,function() {
     $(li).addClass('selected');
     $(li).removeClass('working');
     $(ol).removeClass('working');
    });
   }
   return false;
  });
  $(sh).find('a').click(function() {
   var href = $(this).attr('href');
   if(href.length > 1) {
    var hd = $(ol).find(href).find('div.head').eq(0);
    if(hd == null) return false;
    if($(href).attr('class').indexOf('selected') < 0) $(hd).click();
    return true;
   } else return false;
  });
 });
 $('div#hp div.hpBoxes').each(function() {
  var hp = $(this).find('div.hpItem');
  if(hp.length > 1) {
    var eq = Math.floor(Math.random()*hp.length);
    $(hp).eq(eq).show().siblings('div.hpItem').remove();
  }
  $(this).find('div.bigBox').each(function() {
    var el = $(this).find('div.img,div.swf');
    var eq = Math.floor(Math.random()*el.length);
    $(el).eq(eq).show().siblings('div.img,div.swf').remove();
  });
  $(this).find('div.smallBoxes li').each(function() {
    var el = $(this).find('div.img,div.swf');
    var eq = Math.floor(Math.random()*el.length);
    $(el).eq(eq).show().siblings('div.img,div.swf').remove();
  });
 });
 $('#dwz').each(function() {
  $('#dwzFlash').each(function() {
    var df = $(this).find('div.dwzFlash');
    var eq = Math.floor(Math.random()*df.length);
    $(df).eq(eq).show().siblings('div.dwzFlash').remove();
 });
 });
 $('form#search').each(function() {
 $(this).attr('rel',$(this).find('input.text').val());
  $(this).ajaxForm({
   beforeSubmit: function(d,f,o) {
   var val = $(f).find('input.text').val();
   if((val.length > 0) && val != $(f).attr('rel')) {
      if(window.location.href.indexOf('szukaj') >= 0) {
        window.location = $(f).attr('action')+'#search='+val;
        window.location.reload();
   } else {
     window.location = $(f).attr('action')+'#search='+val;
   }
   }
     return false;
  }
 });
 });
 $('form#bigSearch').each(function() {
  $(this).attr('rel',$(this).find('input.text').val());
  $(this).ajaxForm({
   beforeSubmit: function(d,f,o) {
   var val = $(f).find('input.text').val();
   if((val.length > 0) && val != $(f).attr('rel')) {
      if(window.location.href.indexOf('szukaj') >= 0) {
        window.location = $(f).attr('action')+'#search='+val;
        window.location.reload();
   } else {
     window.location = $(f).attr('action')+'#search='+val;
   }
   }
     return false;
  }
 });
 });
 $('input#bigPhrase').focus(function() {
  $(this).val('');
 });
 $('select').selectbox();
 $('#exchange').ajaxSubmit({
  type: 'post',
  url: '/exchange.txt',
  success: function(re,st) {
  $('#exchange span.euroBuy').text(re.substring(0,6));
  $('#exchange span.euroSell').text(re.substring(7,13));
    $('#exchange span.euroDate').text(re.substring(14,24));
  $('#exchange').show();
 }, error: function () {}
 });
 var cnt = 0;
 $('div#hp div.leadsPromo div.list li').each(function() {
  cnt++;
  if($(this).find('a.right').length > 0) {
    $(this).addClass('wide');
    cnt = 0;
 }
 if(cnt == 2) {
   $(this).after('<li class="clear"></li>');
   cnt = 0;
 }
 });
});
function setHash(key,value) {
  var hash = window.location.hash;
  if(hash.length > 0) {
    var pos = hash.indexOf(key+'=');
    if(pos > 0) {
   var hash1 = hash.substring(0,pos);
   var hash2 = hash.substring(pos);
   pos = hash2.indexOf(',');
   if(pos > 0) {
     hash2 = hash2.substring(pos);
   } else {
     hash2 = '';
   }
   window.location.hash = hash1+key+'='+value+hash2;
  } else {
    window.location.hash = hash+','+key+'='+value;
  }
 } else {
   window.location.hash = '#'+key+'='+value;
 }
 }
 function getHash(key) {
  var hash = window.location.hash;
  var pos = hash.indexOf(key+'=');
  if(pos > 0) {
    hash = hash.substring(pos+key.length+1);
    pos = hash.indexOf(',');
    if(pos > 0) {
      hash = hash.substring(0,pos);
  }
  return hash;
 } else {
   return null;
 }
 }
 
var ie6 = function() {
  return $.browser.msie && ($.browser.version == "6.0");
};