$(window).load( function() {
  $('a').click( linkClick );
});

function makeActive( obj, activate ) {
  if( obj.is('.alwaysactive') || obj.is('.alwaysinactive') )
    return;
    
  if( activate ) {
    obj.addClass( 'active' );
    
    /* IE6 */
    if( isOldIE() ) {
      obj.find('img.hover').show();
      obj.find('img.normal').hide();
    }
            
    if( obj.is('.button') )
      buttonActivated( obj );
  } else {
    obj.removeClass( 'active' );
    
    /* IE6 */
    if( isOldIE() ) {
      obj.find('img.hover').hide();
      obj.find('img.normal').show();
    }
  }
}

function isActive( obj ) {
  if( obj.is('.alwaysinactive') )
    return false;
    
  if( obj.is('.alwaysactive') || obj.is('.active') )
    return true;
  
  return false;
}

function menuActivated( menu ) {
  switch( menu.attr('id') ) {
    case 'Menu10':
      $('#MenuContent').css({ 'bottom': '12px' });
      break;
      
    case 'Menu11':
    case 'Menu12':
      $('#MenuContent').css({ 'bottom': '42px' });
      break;
  }
}

function buttonActivated( btn ) {
/*  switch( btn.attr('id') ) {

  }*/
}

function visible( obj ) {
  return (obj.css('display') != 'none');
}

function menuDeactivateButtons( menuId, ommit ) {
  $('a.'+menuId).each( function() {
    if( $(this).attr('id') != ommit.attr('id') )
      makeActive( $(this), false );
  });
}

function linkClick( event ) {
  var obj = $(this);

  // If this button belongs to menu, make all buttons inside this menu inactive.
  var classes = obj.attr('class');
  classes = classes.split(' ');
  for( i_class in classes ) {
    if( classes[i_class].substr(0, 5) == 'menu-' )
      menuDeactivateButtons( classes[i_class], obj );
  }


  if( obj.is('.button') )
    makeActive( obj, !isActive(obj) );
  
  document.getElementById('unfocus').focus();
  return !obj.is('.button');
}

function hide( id ) {
  document.getElementById(id).style.display = 'none';
}

function show( id, visible ) {
  if( visible==null || visible )
    $('#'+id).show();
  else
    hide( id );
    //$('#'+id).show();
  //document.getElementById(id).style.display = 'display';
}


function beautyShow( obj, fn ) {
  switch( textShowMethod ) {
    case 'none':
      obj.show();
      if( fn != null )
        fn(); 
      break;
    
    case 'slide':
      obj.slideDown( textShowSpeed, fn );
      break;
      
    case 'fade':
      obj.fadeIn( textShowSpeed, function(){
        obj.css({ 'opacity' : '' });
    
        if( fn != null )
          fn(); 
      });
      break;
  }
}

function beautyHide( obj, fn ) {
  switch( textHideMethod ) {
    case 'none':
      obj.hide();
      if( fn != null )
        fn(); 
      break;
    
    case 'slide':
      obj.slideUp( textHideSpeed, fn );
      break;
      
    case 'fade':
      obj.fadeOut( textHideSpeed, function(){
        //obj.css({ 'opacity' : '' });
    
        if( fn != null )
          fn(); 
      });
      break;
  }
}

function loadContent( id, sourceUrl, unrollObj, fn, hideContent ) {
  if( hideContent!=null && hideContent==true ) {
    beautyHide( $('#'+id) );
    return;
  }
  
  
  var obj = $("#"+id);
  var url = base_path()+getLang()+"/get_content?el="+sourceUrl;
  
  if( unrollObj && !obj.is(":hidden") ) {
    beautyHide( obj, function(){
      loadContent( id, sourceUrl, false );   //WARN: Moze wystapic rekurencja.
    });
    return;
  }
    
  obj.load( url, function() {
    beautyShow( obj );
    if( fn != null )
      fn();
  });
}


function showLayer( layerId ) {
  var obj = jqueryGetElements( 'div', 'layer' );  //document.getElementsByTagName('div');
  for( i in obj ) {
    if( obj[i].attr('id') == layerId )
      obj[i].css({ 'visibility':'visible' });
    else
      obj[i].css({ 'visibility':'hidden' });
  }
}


function printObj( obj ) {
  var s = '';
  for( var prop in obj ) {
    s += prop + ' value :'+ obj[prop]+'\n';
  }
  alert( s );
}
