// encoding: utf-8
/*@cc_on/*@if(@_jscript_version<5.7)try{document.execCommand('BackgroundImageCache',0,1)}catch(e){}/*@end@*/

// ***** jqreq *****
Req.localPath = Req.localPath || '/skin/basic/'
Req(
  'eutils',
  'fontsizer',
  'autovalidate',
  'labelizor',
  'imgpop',
  'equalizeheights',

  function(){
    var $ = jQuery;

    // labelize search input
    $('#qstr').labelizor();

    if (!window.EPLICA_loggedin)
    {

      // pngfix for IE6
      if ($.browser.msie && $.browser.version < 7) {
        $('img[src$=".png"]').Req('x/ifixpng', function(){
            $(this).ifixpng();
          });
      }

      //zebra tables
      $('tbody tr:nth-child(2n-1)').addClass('odd');
      //$('tbody tr:nth-child(2n)').addClass('even');


      //popup in articles
      var articlelist = $('.articlelist a.img');
      articlelist
          .each(function() {
            var imgsrc = $(this).find('img').attr('src').replace(/\/[^\/]+\/([^\/]+)$/, '/large/$1');
            $(this).attr('href', imgsrc)
          })
          .imgPopper({
              curtainColor : '#ffffff',
              curtainOpacity : '0.75',
              yOffset: 15
            });
  
            
      //popup in articles              
      //var target = $('.twocol div.article div.boxbody, .threecol div.article div.boxbody, .twocol div.articlelist div.boxbody, .threecol div.articlelist div.boxbody'),
      var target = $('div.article div.boxbody, div.articlelist div.boxbody'),
          targetImages = $('> img, p > img, span > img, .item img', target);
      targetImages
          .each(function() {
              var imgSrc = $(this).attr('src'),
                  largeImg = imgSrc.replace(/\/[^\/]+\/([^\/]+)$/, '/large/$1'),
                  imgTitle = $(this).attr('alt');
              $(this)
                  .wrap('<a title="'+ imgTitle +'" href="'+ largeImg +'"></a>');
            });
      targetImages
          .parent()
          .imgPopper({
              curtainColor : '#ffffff',
              curtainOpacity : '0.75',
              yOffset: 15
            });

      // Grabbing alt attrbutes from images and appending them to anchor containing the image.            
      var imgTarget = $('.article').not('.article p.image').find('img');
               
      imgTarget.each(function() {
        var imgAlt = $(this).attr('alt');
        $(this)
           .parents('a')
              .append('<em>' + imgAlt + '</em>')
              .children()
                 .wrapAll('<p class="image"/>');
      });


    } // end loggedin

    //sitemap collapser
    $('.sitemap').Req('treecollapse', function() { 
        $(this).treeCollapse({ 
            branch: 'li:has(ul)', 
            doTogglers: 1, 
            toggler: '> a.expand',
            startOpen: 'ul.level1 > li:has(ul)'
          }); 
        });

    //remove flicker trick
    $('#noflickerCSS').remove();

    // fontsizer
    $('.pagestyle').fontsizer();

    // validate all forms
    $('form').autoValidate();
        
    // equalizeheights
    $('.pgmain .wrap, .pgextra2 .wrap').equalizeHeights();
        
    // Add "send to facebook" link to articles
    $('p.buttons')
        .append(
            $('<a class="btnfacebook" href="#">Senda á Facebook</a>')
                .bind('click', function()  {
              window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(document.location.href)+'&t='+encodeURIComponent($('h1').text()),'sharer','toolbar=0,status=0,width=626,height=436');
                    return false;
              })
          );
                  
    // Add "send to facebook" link to individual blog entries
    $('.blog .item').each(function(){
        var href = $(this).find('h3 a')[0].href;
        if (href) {
          $(this)
            .append(
               $('<a class="btnfacebook" href="#">Senda á Facebook</a>')
                .bind('click', function()  {
              window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(href),'sharer','toolbar=0,status=0,width=626,height=436');
                    return false;
               })
            );
          }
       });
                  
                  
    // Subpage mnav height changes the height of pghead - this is problematic because mnav is absolutely positioned
    var mnavHeight = $('.twocol .mnav').height(),
       pgheadHeight = $('.twocol .pghead .wrap').height();
             if (mnavHeight+10 > pgheadHeight) {  
                $('.twocol .pghead .wrap').height(mnavHeight+23);
             };


  }
);
// **** /jqreq *****

  //init xfbml like button
FB.XFBML.parse(); 
