function runOnLoad(f) {
    if (runOnLoad.loaded) f();    // If already loaded, just invoke f() now.
    else runOnLoad.funcs.push(f); // Otherwise, store it for later
}

runOnLoad.funcs = []; // The array of functions to call when the document loads
runOnLoad.loaded = false; // The functions have not been run yet.

// Run all registered functions in the order in which they were registered.
// It is safe to call runOnLoad.run() more than once: invocations after the
// first do nothing. It is safe for an initialization function to call
// runOnLoad() to register another function.
runOnLoad.run = function() {
    if (runOnLoad.loaded) return;  // If we've already run, do nothing

    for(var i = 0; i < runOnLoad.funcs.length; i++) {
        try { runOnLoad.funcs[i](); }
        catch(e) { /* An exception in one function shouldn't stop the rest */ }
    }

    runOnLoad.loaded = true; // Remember that we've already run once.
    delete runOnLoad.funcs;  // But don't remember the functions themselves.
    delete runOnLoad.run;    // And forget about this function too!
};

// Register runOnLoad.run() as the onload event handler for the window
if (window.addEventListener)
    window.addEventListener("load", runOnLoad.run, false);
else if (window.attachEvent) window.attachEvent("onload", runOnLoad.run);
else window.onload = runOnLoad.run;

$(document).ready( function() {


  /* header menu*/
  // Google search Stylish select
//  $("#cse-search-box select").sSelect();

  $('#google_phrase').css('background', 'url(/hg/images/header2011/google_phrase_bg.png) no-repeat 5px 50%');
  $('#google_phrase').blur(function() {
    if ($(this).val() == '')
      $(this).css('background', 'url(/hg/images/header2011/google_phrase_bg.png) no-repeat 5px 50%');
  });

  // INIT TOPMENU
  $('#nav li.active > a, #nav li.active > ul').addClass('active');
  $('#nav li.active').removeClass('active');

  $('#nav > ul > .parent_node > a').click(function() {
    $('#nav > ul > .parent_node > ul').hide();
    $('#nav a.active').removeClass('active');
    $(this).parent().children('ul > li > a').addClass('active');
    $(this).parent().children('ul').show();
    return false;
  });

  $('#nav .submenu .parent_node:has(.groups)').hover(function() {
    $(this).parent().children('ul.active').hide();
    $(this).children('ul').show();
  }, function() {
    $(this).children('ul').hide();
  });

  $('#nav > ul > li').mouseleave(function() {
//    $('ul.active').show().siblings('a').addClass('active');
  });

  /* end header menu*/

  /* footer network */
  $('.network').mouseover(function() {
    $('#footer_popup').fadeIn('fast');
  });

  $('#footer_popup').mouseleave(function() {
    $('#footer_popup').fadeOut('fast');
  });
  /* end footer network */



/*
runOnLoad(function(){
  if( $.jGrowl ){
    $.jGrowl("Ismerős?! <br/><a href='/oldal/2-udvozoljuk-az-uj-hg-n'>Új funkciók és olvasóbarátabb tördelés <br/>a megújult hg-n.</a>", { sticky: true });
  }
*/
  /*var captionWidth = $('#image-big img').css("width");
  $('#image-big #caption').css("width", captionWidth);

  var captionBigWidth = $('#theimage img').css("width");
  $('#image-big span').css("width", captionBigWidth);
  */
  /*var $columns = $(".column, #sidemenu, #articles, #banners-250").equalizeCols();
*/
  $('a.popup').click( function() {
    window.open($(this).attr('href'),'null','mywindow","location=1,status=1,scrollbars=1,width=625,height=633');
    return false;
  });

  $('a.popup-big').click( function() {
    window.open($(this).attr('href'),'big','mywindow","location=1,status=1,scrollbars=1,width=1000,height=810');
    return false;
   });

  $('a.popup-wide').click( function() {
    window.open($(this).attr('href'),'wide','mywindow","location=1,status=1,scrollbars=1,width=1000,height=810');
    return false;
   });


  if($('#container.wide #theimage')){

  }

/*
  if($("body.greybody").length > 0 ) {
    var x = $('#theimage img').width();
    var y = $('#theimage img').height();
    if(x > 940) {
      window.resizeTo(x + 200, y + 300);
    }
  }
  */
  $('.styleswitch').click(function()
        {
            switchStylestyle(this.getAttribute("rel"));
            if ( $(".column.blog.full") ){
                $(".column.blog.full").css('height', 'auto');
            }
            if ( $("#articles") ){
                $("#articles").css('height', 'auto');
            }
            this.scrollTo(0,0);
            return false;
        });

  if ( $(".increaseFont") ){
    $(".increaseFont").click(function(){
        var currentFontSize = $('.article').css('font-size');
        var currentFontSizeNum = parseFloat(currentFontSize, 10);
        var newFontSize = currentFontSizeNum*1.2;
        $('.article').css('font-size', newFontSize);
        return false;
      });
  }

  if ( $(".decreaseFont") ){
    $(".decreaseFont").click(function(){
      var currentFontSize = $('.article').css('font-size');
      var currentFontSizeNum = parseFloat(currentFontSize, 10);
      var newFontSize = currentFontSizeNum*0.8;
      $('.article').css('font-size', newFontSize);
      return false;
    });
  }

  if(window.interstitialTimeout){
    show_interstitial();
    setTimeout(function() { hide_interstitial(); }, interstitialTimeout);
  }

  //swfobject.embedSWF("/upload/hg_holcim250x250.swf", "hg_holcim250x250", "250", "250", "9.0.0", {}, {}, {wmode:'transparent', salign:'tl'});
  //swfobject.embedSWF("/upload/hg_holcim920x110.swf", "hg_holcim920x110", "920", "110", "9.0.0", {}, {}, {wmode:'transparent', salign:'tl'});


//  $('.subscribe a.newsletter').click(function() {
//    if (parseInt($('#newsletter_tab').css('left')) == 0) {
//      $('#newsletter_tab').stop(true, true).animate({left: -1*(parseInt($('#newsletter_tab').css('width')))});
//    } else {
//      hide_subscribe_tab()
//      $('#newsletter_tab').stop(true, true).animate({left: '0px'});
//    }
//    return false;
//  });

  $('.subscribe a.contact').click(function() {
    if (parseInt($('#contact_tab').css('left')) == 0) {
      $('#contact_tab').stop(true, true).animate({left: -1*(parseInt($('#contact_tab').css('width')))});
    } else {
      hide_subscribe_tab()
      $('#contact_tab').stop(true, true).animate({left: '0px'});
    }
    return false;
  });
/*
  $('.stickyfloat').css('height', parseInt($(document).height()-$('#footer2011').height()-100) + 'px');
  $('#subscribe_tab, .subscribe_tab_content').stickyfloat({
    offsetY: 250
  });
*/
  // Hide subscribe tab if the page narrower then the wrapper
  /*$(window).resize(function(){
    if ($(window).width() < 1020) {
      $('#subscribe_tab').stop().animate({left: -35}, 100);
    } else {
      $('#subscribe_tab').stop().animate({left: 0}, 100);
    }
  }).resize();
*/
});


function hide_subscribe_tab() {
  $('.subscribe_tab_content').each(function() {
    $(this).stop(true, true).animate({left: -1*(parseInt($(this).css('width')))});
  });
}

function setWidth(id,value){
  $(id).width = value;
}

function chooseExpertise(){
  key = $('#expertise_select').val();
  document.location.href='/szakterulet/' + key;
}

function print_mail_to_link(lhs, rhs, classname, name) {
  if(classname == "") {
    document.write("<a href=\"mailto");
  } else {
    document.write("<a class=\"" + classname + "\" href=\"mailto");
  }
  document.write(":" + lhs + "@");
  if(name == "") {
    document.write(rhs + "\">" + lhs + "@" + rhs + "<\/a>");
  } else {
    document.write(rhs + "\">" + name + "<\/a>");
  }
}

function switchStylestyle(styleName)
{
        $('link[@rel*=style][@title]').each(function(i)
        {
                this.disabled = true;
                if (this.getAttribute('title') == styleName) this.disabled = false;
        });
}

function setSponsor(id){
  $('#sponsor_header .tab_sp').each(function() {
    $(this).hide();
  });
  $('#tab_menu_sp_1').removeClass("active");
  $('#tab_menu_sp_2').removeClass("active");
  $('#tab_menu_sp_3').removeClass("active");
  $('#tab_menu_sp_4').removeClass("active");
  $('#tab_sp_' + id).show();
  $('#tab_menu_sp_' + id).addClass("active");
}

function fb_close(){
  if ( window.opener ){
    window.opener.location.href = '/';
    window.opener.window.location.href = '/';
    window.close();
  }
  else{
    document.location.href = '/';
  }
}

function show_interstitial() {
  //Get the screen height and width
  var maskHeight = $(document).height();
  var maskWidth = $(window).width();

  //Set height and width to mask to fill up the whole screen
  $('#interstitial-mask').css({'width':maskWidth,'height':maskHeight});

  //transition effect
  $('#interstitial-mask').fadeIn(1000);
  $('#interstitial-mask').fadeTo("slow",0.7);

  center_interstitial();

  //transition effect
  $('#interstitial-box').fadeIn(1000);

}
function hide_interstitial() {
//  $('#interstitial-box').fadeOut(500);
//  $('#interstitial-mask').fadeOut(500);
  $('#interstitial-box').hide();
  $('#interstitial-mask').hide();
}
function center_interstitial() {
  //Get the window height and width
  var winH = $(window).height();
  var winW = $(window).width();

  var Btop = winH/2-$('#interstitial-box').height()/2;
  var Bright = winW/2-$('#interstitial-box').width()/2;

  if ( Btop < 0 ) { Btop = 0 }
  if ( Bright < 15 ) { Bright = 15 }

  //Set the popup window to center
  $('#interstitial-box').css('top', Btop );
  $('#interstitial-box').css('right', Bright);
}
