var gazeta_pl = {};

var g_insp = {
  init: function() {
    var _ = this;
    $('#insp_box .c0').prepend('<div id="insp_selector"></div>');
    var o_sel = $('#insp_selector');

    for (var section in jsonTagi) {
      var first = true;
      for (var elem in jsonTagi[section]) {
        var el = jsonTagi[section][elem];
        if (first) {
          var htm = '<strong>'+el+'</strong><ul><li>'+(section == 'wnetrze' ? 'Dowolne' : 'Dowolny')+'</li>';
          first = false;
        } else
          htm += '<li>'+el+'</li>';
      }
      htm += '</ul>';
      o_sel.append(htm);
    }

    $('#insp_selector li').click(function() {
      if ($(this).hasClass('sel')) return;
      $(this).parent().find('.sel').removeClass('sel');
      $(this).removeClass('hover');
      $(this).addClass('sel');

      var res = [];
      $('#insp_selector .sel').each(function() {
        res.push(_.fix_value($(this).html()));
      });
      document.location.href = document.location.href.replace(/(\/[0-9]+,[0-9]+).*$/, '$1') + ',,' + res.join('_') + '.html';
    });

    $('#insp_selector li').hover(
      function() { 
        if ($(this).hasClass('sel')) return;
        $(this).addClass('hover');
      },
      function() { 
        if ($(this).hasClass('sel')) return;
        $(this).removeClass('hover');
      }
    );

    var args = document.location.href.split(',').pop().replace(/\.html.*$/, '');

    if (args.match(/_/)) {
      args = args.split(/_/);
      while (args.length < 3) args.push('1');
    } else {
      args = ['1', '1', '1'];
    }

    for (var i = 0; i < args.length; i++) {
      $('#insp_selector ul').eq(i).find('li').each(function() {
        if (_.fix_value($(this).html()) == args[i]) {
          $(this).addClass('sel');
        }
      });
    }
  },

  fix_value: function(str) {
    str = str.toLowerCase();
    str = str.replace(/ /, '');
    str = str.replace(/dowoln[ey]/, '1');
    str = str.replace(/[±¡]/, 'a');
    str = str.replace(/[æÆ]/, 'c');
    str = str.replace(/[êÊ]/, 'e');
    str = str.replace(/[³£]/, 'l');
    str = str.replace(/[ñÑ]/, 'n');
    str = str.replace(/[óÓ]/, 'o');
    str = str.replace(/[¶¦]/, 's');
    str = str.replace(/[¼¬]/, 'z');
    str = str.replace(/[¿¯]/, 'z');
    return str;
  }
};

var g_switchbox = {
  init: function() {
    $('.switchBox').each(function() {
      $(this).find('.tabs li:first').addClass('active');
      $(this).find('.tabs a').click(function() {
        var subtarget = $(this).parent().parent().attr('id').split(/_/).shift();
        var a_tab = $(this).parent().parent().find('.active').eq(0);
        var a_target = '#' + subtarget + '_' + a_tab.find('a').eq(0).attr('href').replace(/#/, '');
        var c_target = '#' + subtarget + '_' + $(this).attr('href').replace(/#/, '');

        $(a_target).hide();
        $(a_tab).removeClass('active');

        $(c_target).show();
        $(this).parent().addClass('active');
      });
    });
  }
};

$(document).ready(function() {
  
  $('#k12 .index .head,.ZJwrp1 h1,.galeriesBox h4,.MZBChead,.ZBCspecH h3,.zajSzukaj h1,.wideoIndex h2').each(function() {
    if ($(this).find('a').length == 0) {
      $(this).wrapInner('<span>');     
    } else {
      $(this).find('a').wrapInner('<span>');
    }
  });

  $('.MZBCwrp_5 .MZBClead a').hover(
    function() { $(this).parent().parent().addClass('hover'); },
    function() { $(this).parent().parent().removeClass('hover'); }
  );

  $('.MZBCwrp_4 .MZBimgC').each(function() {
    $(this).find('img').eq(0).show();
  });

  $('.MZBCwrp_4 .MZBimgC').hover(
    function() { $(this).find('img').eq(0).hide(); $(this).find('img').eq(1).show(); },
    function() { $(this).find('img').eq(0).show(); $(this).find('img').eq(1).hide(); }
  );

  // embed flash
  (function() {
    if (gazeta_pl.flash_holders) {
      var l = gazeta_pl.flash_holders.length;
      if (l > 0) {
        var params = { wmode: 'opaque', allowScriptAccess: 'always' };
        for (var i = 0; i < l; i++) {
          var fl = gazeta_pl.flash_holders[i];
          params.flashvars = fl.vars;
          swfobject.embedSWF(fl.url, fl.id, fl.width, fl.height, '9.0.0', '/info/expressInstall.swf', {}, params);
        }
      }
    }
  }());

  $('.SONwrp').prepend('<div class="SONtitle"><span>Sonda¿</span></div>');
  $('.SONwrp .sondaBut').attr('src', '/i/obrazki/czterykaty/vote-btn-green.gif');
  /*
  $('.SONwrp .sondaBut').hover(
    function() { $(this).attr('src', '/i/obrazki/czterykaty/vote-btn-active.png'); },
    function() { $(this).attr('src', '/i/obrazki/czterykaty/vote-btn.png'); }
  ); */
  $('.SONwrp form').css({ display: 'block' });
  $('.SONwrp .sondaBut').show();

  var wi = $('.wideoIndex .index_str');
  if (wi.length > 0) {
    if (wi.find('.prev a').length == 0) wi.find('.prev').append('<span>&laquo; poprzednia</span>');
    if (wi.find('.next a').length == 0) wi.find('.next').append('<span>nastêpna &raquo;</span>');
  }

  if ($('#insp_box').length > 0) g_insp.init();
  if ($('.switchBox').length > 0) g_switchbox.init();

  $('.MZBCwrp_4').each(function() {
    var t1 = $(this).find('.MZBCw');
    t1.insertAfter($(this).parent());
    t1.addClass('MZBCw_outer');
  });

  fixIEpng();

  $('#navH li').hover(
    function() { $(this).addClass('hover'); },
    function() { $(this).removeClass('hover'); }
  );
});


