// 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',
  'labelizor',
  'x/ifixpng',

  function(){
    var $ = jQuery;
    
    var mnav = $('.mnav ul');
    mnav.append('<li class="whitespace">&nbsp;</li>')

      // pngfix for IE6
      if ($.browser.msie && $.browser.version < 8) {
        $('.whitespace', mnav).css ({ 'width' : 901 - mnav.width()  })
      }
    
    // labelize search input
    $('#qstr').labelizor();

    if (!window.EPLICA_loggedin)
    {

      // pngfix for IE6
      if ($.browser.msie && $.browser.version < 7) {
        $('img[src$=".png"]').ifixpng();
      }
      
      $('#samtalmap').run(function() {
          var isOpen = false;
          $('#kannavilja, #samtalpop').bind('click', function() {
              if (!isOpen) {
                $('#samtalimg').css({ opacity: .5 });
                $('#samtalpop').show();
                isOpen = true;
              } else {
                $('#samtalimg').css({ opacity: 1 });
                $('#samtalpop').hide();
                isOpen = false;
              }
            })
        });


      $('.pgmain .article img.right').wrap('<div class="right" />');


      //content collapsing
      $('.article.collapsed').find('h2').each(function(){
          var head = $(this);
          head.addClass('collapse-active collapse-closed').nextUntil('h2, hr, p.buttons').wrapAll('<div class="collapseouter" style="display:none" />').wrapAll('<div class="collapsebox" />');
          head.wrapInner('<a href="#" />').find('a').prepend('<span class="icn" />').bind('click', function (e) {
              head
                  .toggleClass('collapse-closed')
                  .next().toggle();
              return false;
            });
        });

    }


    $('.sjalfsprof').each(function(i) {
      var _this = $(this),
          button = $('.fi_btn input', _this),
          pointContainer = $('<div class="points" />');
      _this.prepend('<a class="focustarget" href="#">.</a>');
      button
        .bind('click', function() {
            var points = 0;
            $('.fi_rdo', _this)
                .each(function() {
                    if ($(this).find('input:checked').val() == 1) {
                      $(this).addClass('true').removeClass('false');
                      points++;
                    } else {
                      $(this).addClass('false').removeClass('true');
                    }
                  });

            pointContainer.html('<p>Þú hefur fengið <b>'+ points +'</b> stig af <strong>'+ $('.fi_rdo', _this).length +'</strong> mögulegum <br /><br /> <a id="reset'+ i +'" href="#">Taka prófið upp á nýtt</a></p> ').insertAfter( button.parent() );
            $('a#reset'+i).bind('click', function() {
                $('.fi_rdo', _this)
                    .removeClass('true')
                    .removeClass('false')
                    .find('input')
                        .attr('checked', '');
                pointContainer.remove();
                _this.reset();
                $('> .focustarget', _this).focus();
                return false;
              })
              .focus();

            return false;
          });
      });
      
      $('.afengisprof').each(function () {
        
        //global numkey list
            numKeys = [
                          8, // BACKSPACE
                          9, // TAB
                         37, // LEFT
                         39, // RIGHT
                         46, // DEL
                         48, // 0
                         49, // 1
                         50, // 2
                         51, // 3
                         52, // 4
                         53, // 5
                         54, // 6
                         55, // 7
                         56, // 8
                         57, // 9
                         96, // 0 (numpad)
                         97, // 1 (numpad)
                         98, // 2 (numpad)
                         99, // 3 (numpad)
                        100, // 4 (numpad)
                        101, // 5 (numpad)
                        102, // 6 (numpad)
                        103, // 7 (numpad)
                        104, // 8 (numpad)
                        105, // 9 (numpad)
                        188, // ,
                        110, // ,
                        190  // .
                      ];

            $('.gcalc input:text')
              .bind('keydown', function (e) {
                  if ( $.inArray( e.keyCode, numKeys ) > -1 ) {}
                  else {
                    e.preventDefault();
                  }
                }).bind('change', function (e) {
                    $(this).val($(this).val().replace(',','.'));
                  });

          //Calculates grams of acohol in ml where the precentage is given.
          $('#ml-magn, #alc').bind('change', function (e) {
              var gcmagn = $('#ml-magn').val(),
                  gcalc = $('#alc').val(),
                  gcgrams = "";

              if ( gcmagn.length && gcalc.length )
              {
                gcgrams = (gcmagn * gcalc * 0.79) / 100;
                $('#grams').val(gcgrams);
              }
            });

            var step1vals = $('#step1 .fi_txt input'),
                step2vals = $('#step2 .fi_txt'),
                total,
                val,
                gender;
            
          $('.btn-skoda').bind('click', function (e) {
              step1vals.each(function (i) {
                  val = step1vals.eq(i).val();
                  step2vals.eq(i).find('td:first-child input').val(val).trigger('change');
                });
            });

          //Forward - backward buttons
          var step = 0,
              steps = $('.step').hide(),
              isMale = $('.fi_rdo input:checked').attr('id') == 'male';
              genderIs = $('.fi_rdo input:checked').attr('id');
            steps.eq(0).show();
          $('.btn-afram').bind('click', function (e) {
              step++;
              steps.hide().filter('.step'+step).show();
              isMale ? steps.filter('.step-female').hide() : steps.filter('.step-male').hide();           
            });
          $('.btn-back').bind('click', function (e) {
              step--;
              steps.hide().filter('.step'+step).show();
              isMale ? steps.filter('.step-female').hide() : steps.filter('.step-male').hide();
            });

          step2vals.find('td:first-child input').each(function (i) {
              $(this).bind('change', function (e) {
                var calculated = step2vals.eq(i).find('input.calculated');
                  val = parseInt($(this).val(), 10) || 0;
                  calculated.val( parseFloat(calculated.attr('data-calc'), 10) * val ).trigger('change');
                });
            });

          step2vals.find('input.calculated').bind('change', function (e) {
              total = 0;
              step2vals.find('input.calculated').each(function () {
                  total += parseFloat($(this).val(), 10);
                });
              $('#calc-foot').val(total);

                //Check gender and set green/yellow/red values accordingly.
                gender = parseInt($('.fi_rdo input:checked').val());
                if (gender == 1)
                {
                  var green = 37,
                      yellow = 38,
                      red = 63;
                } else
                {
                  var green = 19,
                      yellow = 20,
                      red = 41;
                }
                if (total >= yellow && total < red)
                {
                  $('.step3')
                      .addClass('yellow')
                          .removeClass('red green')
                              .find('.area')
                                  .text('gula');

                } else if (total > red)
                {
                  $('.step3')
                      .addClass('red')
                          .removeClass('yellow green')
                              .find('.area')
                                  .text('rauða');
                } else
                {
                  $('.step3')
                      .addClass('green')
                          .removeClass('red yellow')
                              .find('.area')
                                  .text('græna');
                }

            });
          
        });


    $('#noflickerCSS').remove();
    $('.pagestyle').fontsizer();
    $('form').autoValidate();

  }
);
// **** /jqreq *****

