// 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(
  'fontsizer',
  'autovalidate',
  'equalizeheights',
  'anchortags',
  'labelizor',
  'eutils',

  function(){
    var $ = jQuery,
    isIS = $('html').attr('lang') == 'is';

    // labelize search input
    $('#qstr').labelizor();

    if (!window.EPLICA_loggedin)
    {

      $('div.pgmain, div.pgextra2').equalizeHeights();

      // 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');



      // When browse.all insert filter into bnav
      $('.browse.all').each(function() {
          var filterer  = $('.bnav .boxbody'),
              filterbox = $('<div class="filterfiles">' +
                              '<div class="textfilter"><label for="textfilter">Filter projects</label> <input type="text" id="textfilter" value="" /></div>' +
                            '</div>'),
              filterable = $('li', this);

          //input filter
          $('#textfilter', filterbox)
              .bind('focus', function() { this.select(); })
              .bind('change keyup', function (e) {
                  var val = $(this).val().toLowerCase();
                  filterable.each(function(i){
                      var text = $(this).text().toLowerCase() || '',
                          pos;
                      if ( text.indexOf(val) >= 0 )
                      {
                        $(this).show();
                      }
                      else
                      {
                        $(this).hide();
                      }
                    });
                });

          //inject filter box
          filterer.append(filterbox);
        });



      $('div.project').each(function(){
          var project = $(this);

          //wrap content into boxes
          $('h2.detailhead', project)
              .each(function() {
                  $(this)
                      .nextUntil('h2.detailhead, .btnrow').andSelf()
                          .wrapAll('<div class="project-box" />')
                          .not('h2.detailhead')
                              .wrapAll('<div class="project-details" />');

                  //add classnames to containers
                  var className = this.className.split(/\s+/g)[1];
                  $(this).parent().addClass(className);
                });



          var projectBox = $('.project-box', project);

          // Toggle project long description 
          $('div.long-desc', projectBox).hide();
          $('span.long-desc', projectBox)
              .wrapInner('<a href="#" />').find('a')
                  .bind('click', function (e) {
                      $(this)
                          .parent()
                              .toggleClass('active')
                              .next('.long-desc')
                                  .toggle();
                      return false;
                    });

          // Toggle project specs 
          projectBox
              .eq(0).addClass('first').end()
              .not(':first')
                  .find('.project-details').hide().end()
                  .find('h2.detailhead')
                      .wrapInner('<a href="#" />').find('a')
                          .bind('click', function (e) {
                              $(this)
                                  .parent()
                                      .toggleClass('active')
                                      .next('.project-details')
                                          .toggle();
                              return false;
                            });


          //tag external urls
          $('a', projectBox)          
              .anchorTags({
                  usePatterns:  ['doc', 'pdf', 'xls']
                })
              .filter('a.external, a.file_pdf, a.file_doc, a.file_xls')
                  .each(function() {
                      var match = $(this)[0].className.match(/(^| )file_([a-z]+)( |$)/);
                      if ( match ) 
                      {
                        var text = match[2];
                        $(this).attr('title', text+' skjal').append(' <span class="icon">('+ text +')</span>');
                      }
                      else if ( $(this).is('.external') )
                      {
                        var externalText = isIS ? 'Opnast í nýjum vafraglugga' : 'Open in new window';
                        $(this).attr('title', externalText).append(' <span class="icon">('+ externalText +')</span>');
                      }
                    });
                    
                    

        });
        
        
        //tag external urls
        var reports = $('.reports');
        $('a', reports)          
            .anchorTags({
                usePatterns:  ['doc', 'pdf', 'xls']
              })
            .filter('a.external, a.file_pdf, a.file_doc, a.file_xls')
                .each(function() {
                    var match = $(this)[0].className.match(/(^| )file_([a-z]+)( |$)/);
                    if ( match ) 
                    {
                      var text = match[2];
                      $(this).attr('title', text+' skjal').append(' <span class="icon">('+ text +')</span>');
                    }
                    else if ( $(this).is('.external') )
                    {
                      var externalText = isIS ? 'Opnast í nýjum vafraglugga' : 'Open in new window';
                      $(this).attr('title', externalText).append(' <span class="icon">('+ externalText +')</span>');
                    }
                  });
                    
                    
        // Add "send to facebook" link to articles                    
        $('p.buttons')
            .append(
                $('<a class="btnfacebook" href="#">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;
                      })
              );
                      
                      
        //init videolist script
        $('.videolist').Req(
            'http://codecentre.eplica.is/jquery/quickies/quicky-videoplayer-min.js',
            function() {
              $(this).videoList({ 
                  vidWidth: 550,
                  vidHeight: 335,
                  autostart: 'all',
                  defaultImg: ''
                });
            }
          );
        

    } // end not loggedin

    $('.pagestyle').fontsizer();
    $('form').autoValidate();
    
    
  }
);
// **** /jqreq *****

