if(typeof $.fn.pause == "undefined") {
  $.fn.pause = function(duration, fnc) {
    $(this).animate({ dummy: 1 }, duration, fnc);
    return this;
  };
}
var speed=400;
var opened_class = 'active';
var current_active_menu_class = 'menu-active';
var menu_item_opened_class = 'opened';
var tooltipObj;
$(document).ready(function() {
  if(jQuery.isFunction(jQuery.fn.tooltip)) {
/*    $('.tooltip[title]').each(function () {
      $(this).data('title', $(this).attr('title'));
      $(this).attr('title', '');
    })
*/
    // 2010-05-31 martin@tvarwebu.cz added exception for IE (black border around tooltip)
    p = {
      effect: "fade", // use the built-in fadeIn/fadeOut effect
      fadeOutSpeed: 1000,
      cancelDefault: true,
      position: "center right",
      tip: '#tooltipBox'
    }
    if($.browser.msie) // IE<any> do not like opacitied alpha pngs so we do not like IE :)
      p.effect = 'toggle'; // just show / hide (no effect)
    $(".tooltip").each(function(){
      myP = $.extend({},p, $(this).parseClassToObject('customTooltip'));
      $(this).tooltip(myP);
    });
  }
  //$("#nav ul:has(li.active)").show();
  if(typeof current_page_pid != "undefined")
    menu_set_active($('.menuitem-page-'+current_page_pid));
  $("#nav ul:has(li)").each(menu_hide);
  $("#nav li:has(ul)").mouseout(function () {
    $(this).removeClass(current_active_menu_class)
  });
  $("#nav li:has(ul)").each(function() {
    if(!$(this).hasClass(opened_class))
      my_slide(this);
  });
  $("#nav li:has(ul)").mouseover(function(event){
    $(this).addClass(current_active_menu_class);
    $(this).pause(200, function () {
      if($(this).hasClass(current_active_menu_class)) {
        if (this == event.target || this == event.target.parentNode) {
          var current = this;
          $("#nav li:has(ul)").each(function() {
            parents = $(current).parents('#nav li');
            for(i = 0; i< parents.length; i++) {
              if(parents[i] == this) {
                return ;
              }
            }
            parents = $(this).parents('#nav li');
            for(i = 0; i< parents.length; i++) {
              if(parents[i] == current) {
                return ;
              }
            }
            if (this != current && !$(this).hasClass(opened_class)) my_slide(this);//$(this).children().slideUp(speed);
          });
          $("ul:first", $(this)).slideDown(speed).parent().addClass(menu_item_opened_class);
        }
      }
    });
  });
  $('.open-login').fancybox( {'overlayShow': true, 'hideOnContentClick': false, 'overlayOpacity': 0.8, 'frameWidth': 500, 'frameHeight': 328, 'itemArray': { 'type' : 'ajax' } } );
  $('<a href="#" class="toogle-new-message gray_button_smaller" style="">Show new message form</a>').click( function () {
    $('.new-message-container').slideToggle('slow', function() {

      if(this.style.display == 'none') {
        $('.toogle-new-message').html('Show new message form');
      } else {
        $('.toogle-new-message').html('Hide new message form');
      }
    });
    return false;
  } ).appendTo('.forum_thread_info');//insertBefore('.new-message-container');
  if(typeof forum_show_message != "undefined" && forum_show_message == true)
    $('.toogle-new-message').html('Hide new message form');
    var my_email_address = ''
  $('.email_address_translate').each(function(){
    my_email_address = $(this).html().replace(/ \(at\) /g, '@').replace(/ \(dot\) /g, '.');
    $(this).html('<a href="mailto:'+my_email_address+'">'+my_email_address+'</a>');
  });
  $('.login-container .row-username input').focus();

  var badBrowser = (/MSIE ((5\.5)|6)/.test(navigator.userAgent) && navigator.platform == "Win32");
    if (badBrowser) {
      // get all pngs on page
      $('img[src$=.png]').each(function() {
      if (!this.complete) {
        this.onload = function() { fixPng(this) };
      } else {
        fixPng(this);
      }
    });
  }
});
$.fn.tagName = function() {
    return this.each(function() {
        return this.tagName;
    });
}
 var blank = new Image();
 blank.src = 'site/images/spacer.gif';

 function fixPng(png) {
   // get src
   var src = png.src;
   // set width and height
   if (!png.style.width) { png.style.width = $(png).width(); }
   if (!png.style.height) { png.style.height = $(png).height(); }
   // replace by blank image
   png.onload = function() { };
   png.src = blank.src;
   // set filter (display original image)
   png.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "',sizingMethod='scale')";
 }

function menu_set_active (object) {
  var obj = $(object);
  if(obj.length > 0) {
    if( obj.get(0).tagName != 'UL' || obj.get(0).id != "nav" ) {
      if(obj.get(0).tagName == 'LI')
        obj.addClass('active');
      menu_set_active(obj.parent());
    }
  }
}
function menu_hide () {
  if(!$($(this).parent()).hasClass('active')) {
    $(this).hide();
  }
}
function my_slide(obj) {
  $(obj).removeClass(menu_item_opened_class);
  $(obj).children().each(function() {

    if(this.tagName != "A" && this.tagName != "SPAN") {
      $(this).slideUp(speed);
    }
  })
}
function banners_rotate(array) {
  //return ;
  container = $('<div style="display: none;"><div style="height: 1px"></div></div>').appendTo('#all_docs_outer');
  for(i in array) {
    $('<img src="'+array[i]+'" alt="banner-preload" />').appendTo(container);
  }
  if(array.length > 1)
    $(document).everyTime(4000, function () {
      current_image = $('#banner_image_container').css('background-image').replace('url(', '').replace(')', '');
      is_current = false;
      img = "";
      for( i in array) {
        url_temp = array[i];
        if(is_current == true) {
          img = url_temp;
          break;
        }
        if(current_image.indexOf(url_temp) != -1)
          is_current = true;
      }
      if(is_current == true && typeof img == "undefined" || img.length <= 5) {
        img = array[0];
      }
/*
      do {
        img = array[Math.round(Math.random() * (array.length-1))];
      } while (img == current_image);
*/
      $('#banner_image_container').fadeOut('slow', function() {
        $(this).css('background-image', "url('"+img+"')").fadeIn();
      });
    });
}
function openPrintWindow(url){
  attrib = "width=600,height=780,menubar=yes,personalbar=no,titlebar=no,toolbar=no,location=no,resizable=yes,scrollbars=yes";
  printWnd = window.open(url, 'printWnd', attrib);
  printWnd.focus();
}

/**
  * parseClassToArray(): parses element's class and returns known elements as array
  * @param String prefix: class prefix
  * example:
  *  elements class = 'blah--name--value blah--name2--val'
  *  out = $(element).parseClassToArray('blah')
  *  output: object:{name: 'value', name2: 'val'};
*/
(function(cash) {$.extend($.fn, {
  parseClassToObject: function (prefix) {
    out = {};
    cl = $(this).attr('className');
    if(typeof cl != 'undefined' && cl.length > 0) {
      classes = cl.split(' ');
      reg=/^\-\-(.*)$/;
      for(i=0;i<classes.length;i++) {
        cl = $.trim(classes[i]);
        if(cl.indexOf(prefix) == 0){
          cl = cl.substr(prefix.length);
          if((preg = reg.exec(cl)) && preg[1].length > 0) {
            pos = preg[1].indexOf('--');
            if(pos > 0) {
              out[unescape((''+preg[1]).substr(0, pos))] = unescape((''+preg[1]).substr(pos+2));
            } else {
              out[out.length] = unescape(preg[1]);
            }
          }
        }
      }
    }
    return out;
  }
})}) (jQuery);