window.addEvent('domready', function()
{
   //Empty Link Guard
   var baseURL = $(document.head).getElement('base').href;
   $each($$('a'), function(el) { if( el.href == baseURL + '#' ) { el.addEvent( 'click', function( fn ) { return false; } ); } });

   //Search Form & Input
   if( $('searchKeyword') && $('searchForm') )
   {
     var searchKeywordDefault = $('searchKeyword').value;
     $('searchKeyword').addEvent( 'focus', function( fn ) { if( this.value == searchKeywordDefault ) this.value = ''; });
     $('searchKeyword').addEvent( 'blur', function( fn ) { if( this.value == '' ) this.value = searchKeywordDefault; });
     $('searchForm').addEvent( 'submit', function( fn )
     {
        if( $('searchKeyword').value == searchKeywordDefault || $('searchKeyword').value == '' )
        {
          $('searchKeyword').style.backgroundColor = '#e8cfcc';
          $('searchKeyword').morph({backgroundColor: '#9f3a2b'});
          return false;
        }
      });
   }
   
   if( $('madContent') && ( $('madContent').innerHTML == '' || $('madContent').innerHTML == ' ' ) )
   {
      $('madTitle').style.display = 'none';
   }
   
   //Advanced Search Form & Input
   if( $('extSearchKeyword') && $('extSearchForm') )
   {


     var searchExtKeywordDefault = $('extSearchKeyword').value;
     $('extSearchKeyword').addEvent( 'focus', function( fn ) { if( this.value == searchExtKeywordDefault ) this.value = ''; });
     $('extSearchKeyword').addEvent( 'blur', function( fn ) { if( this.value == '' ) this.value = searchExtKeywordDefault; });
     $('extSearchForm').addEvent( 'submit', function( fn )
     {
        if( ( $('extSearchKeyword').get('rel') != 'noDefault' && $('extSearchKeyword').value == searchExtKeywordDefault ) || $('extSearchKeyword').value == '' )
        {
          $('extSearchKeyword').style.backgroundColor = '#e8cfcc';
          $('extSearchKeyword').morph({backgroundColor: '#9f3a2b'});
          return false;
        }
      });


   }

   $$('.cbox_mask').set('styles', {
       'opacity': '0.8'
   });

   $$('.cbox').addEvent( 'mouseover', function( fn )
   {
      $( 'plogo' ).set('styles', { 'display': 'none' });

      $( this.id + '_cbox' ).set('styles', {
       'display': 'block',
       'left': (this.getPosition($('main')).x - 7) + 'px'
     });
   });
   $$('.cbox').addEvent( 'mouseout', function( fn )
   {
      $( this.id + '_cbox' ).style.display = 'none';
      
      $( 'plogo' ).set('styles', { 'display': 'block' });
   });
   
   //MainPage:
   if( $('bTab_1') || $('bTab_2') || $('bTab_3') || $('bTab_4') || $('bTab_5') )
   {
      $('bTab_1').addEvent( 'click', function( fn ) { toggleMainTab(1); } );
      $('bTab_2').addEvent( 'click', function( fn ) { toggleMainTab(2); } );
      $('bTab_3').addEvent( 'click', function( fn ) { toggleMainTab(3); } );
      $('bTab_4').addEvent( 'click', function( fn ) { toggleMainTab(4); } );
		$('bTab_5').addEvent( 'click', function( fn ) { toggleMainTab(5); } );
   }
   
   //Class StepByStep
   var StepByStep = new Class({
        Implements: Chain,
        initialize: function(){
            this.chain.apply(this, arguments);
        }
   });

   var myTodoList = new StepByStep(
        function(){ alert('get groceries'); },
        function(){ alert('go workout'); },
        function(){ alert('code mootools documentation until eyes close involuntarily'); },
        function(){ alert('sleep'); }
    );
    
    if( $('konkurs_cnt') && $('konkurs_cnt').innerHTML == '' )
    {
       $('konkurs').style.display = 'none';
    }
    
    if( $('konkurs_wyniki_cnt') && $('konkurs_wyniki_cnt').innerHTML == '' )
    {
       $('konkurs_wyniki').style.display = 'none';
    }



});

function toggleMainTab( id )
{
   setTabBtn( id );
   setTabView( id );
}

function setTabBtn( id )
{
   switch( id )
   {
      case 1:
           $('bTab_1').setProperty('class', 'fl mr b_active');
           $('bTab_2').setProperty('class', 'fl mr b_unactive');
           $('bTab_3').setProperty('class', 'fl mr b_unactive');
           $('bTab_4').setProperty('class', 'fl mr b_unactive');
			  $('bTab_5').setProperty('class', 'fl mr b_unactive');	
           break;
      case 2:
           $('bTab_2').setProperty('class', 'fl mr b_active');
           $('bTab_1').setProperty('class', 'fl mr b_unactive');
           $('bTab_3').setProperty('class', 'fl mr b_unactive');
           $('bTab_4').setProperty('class', 'fl mr b_unactive');
			  $('bTab_5').setProperty('class', 'fl mr b_unactive');		
           break;
      case 3:
           $('bTab_3').setProperty('class', 'fl mr b_active');
           $('bTab_1').setProperty('class', 'fl mr b_unactive');
           $('bTab_2').setProperty('class', 'fl mr b_unactive');
           $('bTab_4').setProperty('class', 'fl mr b_unactive');
			  $('bTab_5').setProperty('class', 'fl mr b_unactive');		
           break;
      case 4:
           $('bTab_3').setProperty('class', 'fl mr b_unactive');
           $('bTab_1').setProperty('class', 'fl mr b_unactive');
           $('bTab_2').setProperty('class', 'fl mr b_unactive');
           $('bTab_4').setProperty('class', 'fl mr b_active');
			  $('bTab_5').setProperty('class', 'fl mr b_unactive');		
           break;  
		case 5:
           $('bTab_3').setProperty('class', 'fl mr b_unactive');
           $('bTab_1').setProperty('class', 'fl mr b_unactive');
           $('bTab_2').setProperty('class', 'fl mr b_unactive');
           $('bTab_4').setProperty('class', 'fl mr b_unactive');
			  $('bTab_5').setProperty('class', 'fl mr b_active');		
           break;
				         
   }
}

function setTabView( id )
{
   switch( id )
   {
      case 1:
           $('mTab_1').style.display = 'block';
           $('mTab_2').style.display = 'none';
           $('mTab_3').style.display = 'none';
           $('mTab_4').style.display = 'none';
			  $('mTab_5').style.display = 'none';	
           break;
      case 2:
           $('mTab_2').style.display = 'block';
           $('mTab_1').style.display = 'none';
           $('mTab_3').style.display = 'none';
           $('mTab_4').style.display = 'none';
			  $('mTab_5').style.display = 'none';		
           break;
      case 3:
           $('mTab_3').style.display = 'block';
           $('mTab_1').style.display = 'none';
           $('mTab_2').style.display = 'none';
           $('mTab_4').style.display = 'none';
			  $('mTab_5').style.display = 'none';		
           break;
      case 4:
           $('mTab_3').style.display = 'none';
           $('mTab_1').style.display = 'none';
           $('mTab_2').style.display = 'none';
           $('mTab_4').style.display = 'block';
			  $('mTab_5').style.display = 'none';		
           break;  
		case 5:
           $('mTab_3').style.display = 'none';
           $('mTab_1').style.display = 'none';
           $('mTab_2').style.display = 'none';
           $('mTab_4').style.display = 'none';
			  $('mTab_5').style.display = 'block';		
           break;    
   }
}

function selectAll( objClass )
{
   $$(objClass).each(function(el) { el.checked = true; });
}

function unselectAll( objClass )
{
   $$(objClass).each(function(el) { el.checked = false; });
}

function showAnnouncements( year, month )
{
   if( !year )
      year = $('ann_year').value;
   if( !month )
      month = $('ann_month').value;   
      
   document.location.href = '/zapowiedzi/' + year + '/' + month + '/';
}
