// 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',
  'mailtoenabler',
  'tabswitcher',
  
    Req.localPath+'js/cufon-yui.js',
    Req.localPath+'js/Eurostile_LT_Condensed_400.font.js',  
  function(){

    var $ = jQuery,
        old_msie = $.browser.msie && parseInt('0'+$.browser.version, 10)<8;

    // labelizor
    $('#qstr').labelizor();


    $('ul.tabs').tabSwitcher();

    $('div.flytileidir').each(function() {
        var block = $('.boxbody', this);
        block.hide();
        $('.boxhead', this)
            .wrapInner('<a href="#"></a>')
            .find('a')
                .bind('click', function() {
                      block.slideToggle(200);
                      return false;
                  })
      });


    if (!window.EPLICA_loggedin)
    {

      //popup in articles
      var imglist = $('div.imagelist div.boxbody');
      
      $('a:has(img)', imglist)
          .each(function() {
            var imgsrc = $(this).find('img').attr('src').replace(/\/[^\/]+\/([^\/]+)$/, '/large/$1');
            $(this).attr('href', imgsrc);
          })
          .imgPopper({
              curtainColor : '#cccccc',
              curtainOpacity : '0.75'
          });
          
      $('div.pgr, div.pgs', imglist).clone().prependTo(imglist);
          
      $('img').each(function(){
          $(this).attr('title', $(this).attr('alt'));
        });
      

      $('p.buttons')
          .prepend(
              $('<a class="btnfacebook">Senda á Facebook</a>')
                  .attr(
                      'href',
                      'http://www.facebook.com/sharer.php?u='+encodeURIComponent(document.location.href)+'&t='+encodeURIComponent($('h1').text()+' | '+$('img.logo:first').attr('alt') )
                    )
                  .bind('click', function() {
                      window.open($(this).attr('href'),'sharer','toolbar=0,status=0,width=626,height=436');
                      return false;
                    })
            );

      //employee list ajax
      $('div.people tbody tr:nth-child(2n-1)').addClass('even');
      $('div.people').Req('/bitar/common/personas/loadEmployee.js', function(){ this.eplicaEmployeeLoader(); }); 
          
      $('.netfang').mailtoEnabler();
      
      //Cufon-ization of header elements
      Cufon.replace('.twocol .pgmain .boxhead, .threecol .pgmain .boxhead');
      
      
    }

    $('#noContentFlashHack').remove();
    old_msie && (document.body.className += ""); // kick the rendering engine in MSIE 6&7 to trigger reflow.


    $('div.pagestyle').fontsizer();
    $('form').autoValidate();
  }
);
// **** /jqreq *****


// portus javascript gallery
(function() {

  var imageIndex = null,
      currentImage = null,
      viewPort,
      captionElm,
      viewImage,
      titleElm,
      popupTemplate = '' +
        '<div class="curtain"></div>' +
        '<div class="popup">' + 
        '<div class="popupimgwrap">' +
        '<img src="%{img}" alt="" />' +
        '<div class="act"><a class="close" href="#"><strong>Loka mynd</strong></a></div>' + // close button
        '</div></div>';
  
  var imgPopBody = document.createElement('div');
  document.body.appendChild(imgPopBody);
  
  var makeView = function ( src, img )
  {
    viewPort   = DOM.makeElement('<div class="imgbox"><div class="boxbody"></div></div>');
    bigImgHref = document.createElement('a');
    viewImage  = document.createElement("img");
    imgContent = DOM.makeElement('<div class="imgboxbody"></div>');
    titleElm   = DOM.makeElement('<div class="imgname"></div>');
    captionElm = DOM.makeElement('<div class="imgtext"></div>');
    
    EEvent.add(bigImgHref, "click", function(e, imgPopBody, popupTemplate){
      var values = { img : this.href };
      
      imgPopBody.innerHTML = popupTemplate.injectVars(values);
      
      var closeButton = DOM.get('a.close', imgPopBody)[0];
      if(closeButton)
      {
        EEvent.add(closeButton, 'click', function(e, imgPopBody) {
          imgPopBody.innerHTML = "";
          return false;
        }, null, imgPopBody);
      }
      
      var imgCurtain = DOM.get('div.popup', imgPopBody)[0];
      
      DOM.setPos(imgCurtain, [null, DOM.scrollPos()[1]] );
      
      if(imgCurtain)
      {
        EEvent.add(imgCurtain, 'click', function(e, imgPopBody) {
          imgPopBody.innerHTML = "";
          return false;
        }, null, imgPopBody);
      }
      
      return false;
    }, null, imgPopBody, popupTemplate);
    
    
    DOM.appendChild(viewImage, bigImgHref);
    
    DOM.appendChild(titleElm,   imgContent);
    DOM.appendChild(captionElm, imgContent);
    
    DOM.appendChild(bigImgHref,  DOM.firstElmOf(viewPort));
    DOM.appendChild(imgContent, DOM.firstElmOf(viewPort));
    
    updateView( null, src, img, 0);

    return viewPort;
  };
  
  
  var updateView = function ( e, src, img, newIndex )
  {
    for(var i = 0; i < aElms.length; i++)
    {
      DOM.removeClass(aElms[i].parentNode, 'current');
    }
    
    DOM.addClass(aElms[newIndex].parentNode, 'current');
  
    bigImgHref.href = src.replace('mlarge', 'elarge');
    viewImage.src = src;
    
    var caption = img.alt   || '';
    var title   = img.title || '';

    captionElm.innerHTML = caption;
    captionElm.style.display = (caption) ? '' : 'none';

    titleElm.innerHTML = title;
    titleElm.style.display = (title) ? '' : 'none';
    
    currentImage = newIndex;
    
    DOM.removeNode(bigImgHref);
    DOM.prependChild(bigImgHref, DOM.firstElmOf(viewPort));

    return false;
  };


  var imageIndex = DOM.get('div.gallery ul.thumbs');
  
  for(var i = 0; i < imageIndex.length; i++)
  {
    aElms = DOM.get('li a', imageIndex[i]);
    
    if(aElms.length && DOM.firstElmOf(aElms[0]))
    {
      var imgBox = makeView( aElms[0].href, DOM.firstElmOf(aElms[0]) );
      DOM.insertBefore(imgBox, imageIndex[i]);
    
      for(var j = 0; j < aElms.length; j++)
      {
        EEvent.add( aElms[j], 'click', updateView, null, aElms[j].href, DOM.firstElmOf(aElms[j]), j );
      }
    }
  }

})();