/*******************************
 * Sifr
 *******************************/

if ($.browser.msie && typeof sIFR == "object")
{
  sIFR.bHideBrowserText = true;

  var arial =
  {
    src: 'images/fonts/arial.swf',
    ratios: [6, 1.41, 9, 1.35, 15, 1.29, 21, 1.25, 22, 1.22, 27, 1.24, 29, 1.21, 34, 1.22, 41, 1.21, 45, 1.2, 46, 1.21, 59, 1.2, 68, 1.19, 69, 1.2, 96, 1.19, 97, 1.18, 102, 1.19, 103, 1.18, 107, 1.19, 108, 1.18, 112, 1.19, 114, 1.18, 116, 1.19, 120, 1.18, 121, 1.19, 1.18]
  };


  var arialnarrow =
  {
    src: 'images/fonts/arialnarrow.swf',
    ratios: [6, 1.41, 9, 1.35, 15, 1.29, 21, 1.25, 22, 1.22, 27, 1.24, 29, 1.21, 34, 1.22, 41, 1.21, 45, 1.2, 46, 1.21, 59, 1.2, 68, 1.19, 69, 1.2, 96, 1.19, 97, 1.18, 102, 1.19, 103, 1.18, 107, 1.19, 108, 1.18, 112, 1.19, 114, 1.18, 116, 1.19, 120, 1.18, 121, 1.19, 1.18]
  };

  sIFR.activate(arial, arialnarrow);

  sIFR.replace(arialnarrow,
  {
    selector: '#topMenu li',
    css: [
        '.sIFR-root { color: #f0c827; font-size: 14px; text-align: center; }',
        'a { color: #f0c827; font-size: 14px; text-decoration: none; font-weight: bold;}',
        'a:hover { color: #f0c827; }'
    ],
    filters: {
      DropShadow: {
        distance: 2,
        color: '#000000',
        strength: 2,
        alpha: 0.75
      }
    },
    wmode: 'transparent',
    selectable: true
  });
};




$(function()
{
  if ($.browser.msie && $.browser.version == 6)
  {
    fixAllPNG();
    fixbgpng.fnLoadPngs();
  }
  
  /**
   * Date picker
   */
  $('.date_input').datepicker(
  {
    dateFormat: 'yy.mm.dd.',
    showOtherMonths: true,
    selectOtherMonths: true,
    changeYear: true,
    yearRange : '1910:2000'
  });
  
  /**
   * Submenü
   */
  var topmenuTimer = null;
  var openTopmenu = null;
  $('.topMenu > ul > li').hover(function()
  {
    if (topmenuTimer)
    {
      clearTimeout(topmenuTimer);
      topmenuTimer = null;
    }
    
    if (openTopmenu)
    {
      $('ul', openTopmenu).hide();
      $('.bgDiv', openTopmenu).hide();
      openTopmenu = null;
    }
    
    var width = 0;
    var $ul = $('ul', this);
    if ($ul.width() < 150) $ul.css('width', '150px');
    $ul.show();
    openTopmenu = this;
    
    if ($.browser.msie && $.browser.version < 7)
    {
      $ul.css('width', '120px');
      $('li', $ul).css('width', '120px');
    }

    var ulMargin = parseInt($ul.css('margin-left'));
    if ($.browser.msie && $.browser.version < 7)
    {
      ulMargin += parseInt($('a:eq(0)', this).css('padding-left'));
      ulMargin += parseInt($('a:eq(0)', this).css('padding-right'));
    }

    width = $ul.width();
    width += ulMargin;
    

    var margin = $('a', $ul).outerWidth() - $('a', $ul).width()
    $('a', $ul).css('width', $ul.width() - margin);

    if ($.browser.msie && $.browser.version < 7)
      width -= margin;
      
    $('.bgDiv', this)
      .css({'width': (width+5) + 'px', 'height': $ul.height() + 'px'})
      .show();
      
  }, function()
  {
    var li = this;
    topmenuTimer = setTimeout(function()
    {
      $('ul', li).hide();
      $('.bgDiv', li).hide();
      if (topmenuTimer)
        topmenuTimer = null;
      openTopmenu = null;
    }, 1000);
  });


  /**
   * Kék <label> elemek
   */
  $('.designCheckbox').click(function()
  {
    var meta = $(this).metadata();
    var chk = $(meta.elem, this);
    var checked = chk.hasClass(meta['class'][1]);

    if (meta.type == 'checkbox')
    {
      chk
        .removeClass(checked ? meta['class'][1] : meta['class'][0])
        .addClass(checked ? meta['class'][0] : meta['class'][1]);

      if (meta.name)
        $('input[name=' + meta.name + ']').val(checked ? '' : meta.value);
    }
    
    else if (meta.type == 'radio')
    {
      $('.designCheckbox').each(function()
      {
        var meta2 = $(this).metadata();

        if (meta2.name != meta.name) return;
        
        var chk2 = $(meta2.elem, this);
        if (meta2.value == meta.value)
        {
          chk2
            .removeClass(meta['class'][0])
            .addClass(meta['class'][1]);
            
          if (meta.name)
            $('input[name=' + meta.name + ']').val(meta.value);
        }
        else
        {
          chk2
            .removeClass(meta['class'][1])
            .addClass(meta['class'][0]);
        }
      });
    }
  });
  
  /**
   * Kezdőoldali flash video
   */
  if ($(".advertFlash #video").length > 0)
  {
    var so = new SWFObject("swf/player.swf?imagePath=images/flash.jpg&videoPath=ormester.flv&autoStart=false&autoHide=false&autoHideTime=5&hideLogo=true&volAudio=60&newWidth=280&newHeight=194&disableMiddleButton=false&playSounds=false&soundBarColor=0x0066FF&barColor=0x0066FF&barShadowColor=0x91BBFB&subbarColor=0xffffff", "video", "280", "194", "8", "#ffffff");
    so.addParam("allowFullScreen", "true");
    so.write("video");
  }
  
  /**
   * Nyelv választó flash
   */
  if ($("#map").length > 0)
  {
    var so = new SWFObject("swf/map.swf", "video", "208", "206", "8", "#ffffff");
    so.addParam("allowFullScreen", "true");
    so.write("map");
  }
  
  if ($("#topBanner").length > 0)
  {
    var so = new SWFObject("swf/46860_ormester_tavf_banner.swf", "topBanner", "468", "60", "8", "#ffffff");
    so.write("topBanner");
  }  
  
  defaultInputTexts =
  [
    {
      'input'    : $("#searchForm input[name=search]"),
      'errorMsg' : $("#emptyErrorText").html(),
      'button'   : $("#searchBtn")
    },
    {
      'input'    : $('#newsLetterSubscribe input[name=name]')
      //,'button'   : $('#newsLetterSubscribe .okButton')
    },
    {
      'input'    : $('#newsLetterSubscribe input[name=email]')
      //,'button'   : $('#newsLetterSubscribe .okButton')
    },
    {
      'input'    : $('#newsLetterSubscribe input[name=address]')
      //,'button'   : $('#newsLetterSubscribe .okButton')
    }
  ];
  
  for (var i=0; i<defaultInputTexts.length; i++)
  {
    var o = defaultInputTexts[i];
    
    if (!o.input || !o.input.length) continue;
    if (o.input.hasClass('noFocus')) continue;
    o.text = o.input.val();
    o.input[0].defIdx = i;

    o.input.focus(function()
    {
      if ($(this).val() == defaultInputTexts[this.defIdx].text)
        $(this).val('');
    });

    o.input.blur(function()
    {
      if ($(this).val() == '')
        $(this).val(defaultInputTexts[this.defIdx].text);
    });
  
    if (o.button)
    {
      o.button.click(function()
      {
        for (var i=0; i<defaultInputTexts.length; i++)
        {
          var o = defaultInputTexts[i];
          if (o.button[0] != this) continue;
          
          var inp = o.input;
          if (inp.val() == defaultInputTexts[i].text)
          {
            if (typeof defaultInputTexts[i].errorMsg == 'undefined')
              $(inp).val('');
            else
            {
              alert(defaultInputTexts[i].errorMsg);
              return false;
            }
          }
        }
      });
    }
  }
  
  
  /**
   * Nyelv választás
   */
  /*
  var obj = $('.selectedLang')[0];
  $('.selectedLang, .langSelect').hover(function()
  {
    $('.langSelect').show();
    obj.open = true;
    if (obj.timer)
    {
      clearInterval(obj.timer);
      delete(obj.timer);
    }
  }, function()
  {
    if (!obj.open) return;
    obj.timer = setTimeout(function()
    {
      if (!obj.open) return;
      $('.langSelect').hide();
      obj.open = false;
    }, 1000);
  });
  $('body').click(function()
  {
    if ($('.selectedLang')[0].open)
    {
      $('.langSelect').hide();
      $('.selectedLang')[0].open = false;
    }
  });
  */
});

function errorDialog(errors, title, translate)
{
  if (!$('#errorDialog').length)
    $('<div id="errorDialog" />').appendTo('body');

  if (translate != null)
  {
    for (var i in errors)
      errors[i] = translate[errors[i]];
  }
  $('#errorDialog *').remove();
  var ul = $('<ul>');
  for (var i in errors)
    $('<li>').html(errors[i]).appendTo(ul);

  $('#errorDialog').append(ul);
  ul = null;

  $('#errorDialog').dialog({'title': title, 'modal': true});
};

function msgDialog(msg, title, translate)
{
  if (!$('#msgDialog').length)
    $('<div id="msgDialog" />').appendTo('body');

  if (typeof msg == 'string') msg = [msg];

  if (translate != null)
  {
    for (var i in msg)
      msg[i] = translate[msg[i]];
  }
  $('#msgDialog *').remove();
  if (msg.length > 1)
  {
    var ul = $('<ul>');
    for (var i in errors)
      $('<li>').html(errors[i]).appendTo(ul);

    $('#msgDialog').append(ul);
    ul = null;
  }
  else
  {
    $('<p>').html(msg[0]).appendTo('#msgDialog');
  }

  $('#msgDialog').dialog({'title': title, 'modal': true});
};

function openPopup(anchor, w, h)
{
  if (!anchor.href) return;
  window.open(anchor.href, "","toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,top=-1,left=-1,width=" + (w?w:640) + ",height=" + (h?h:480));
  return false;
};

function submitForm(id)
{
  var o = document.getElementById(id) || eval('document.' + id);
  o.submit();

  return false;
};

function printSite()
{
  window.open("nyomtatas.php?pg=" + document.URL.replace("http://" + document.domain + "/", "").replace(/\/$/, ""), "print", "status=0,toolbar=0,width=687,height=480,scrollbars=yes,resize=no");
};

String.prototype.trim = function()
{
  return this.replace(/^\s+|\s+$/g, '');
};

function currencyWrap(text)
{
  text += '';

  if (!/^\d+([\.,]\d+)?$/.test(text)) return text;
  if (text.match(/^(\d+)([\.,])(\d+)$/))
  {
    var r = [RegExp.$1, RegExp.$2, RegExp.$3];
    return currencyWrap(r[0]) + r[1] + currencyWrap(r[2]);
  }

  var ret = '';
  var l = -1;
  for (var i=text.length-1; i>=0; i--)
  {
    l++;
    if (i > 0 && l%3 == 2)
      ret = ' ' + text.charAt(i) + ret;
    else
      ret = text.charAt(i) + ret;
  }

  return ret;
}

