﻿Array.prototype.foreach = function( callback ) {
  for( var k=0; k<this .length; k++ ) {
    callback( k, this[ k ] );
  }
}

var iInterval = 3000;
var iPause = 4000;
var iFadeDuration = 1.0;
var oTimer = 0;
var sLastSavedTopMenuTitle = '';
var lock=0;
var lockMarket=0;

//test
/*
var iInterval = 1000;
var iPause = 5000;
*/
//_test

Event.observe(window, 'load', function() {
        startTimer();
      });

function startTimer(bLongInterval) {
  if(bLongInterval == undefined) iInt = iPause; else iInt = iInterval;
  clearInterval(oTimer);
  oTimer = setInterval("showTopMenuTitle(sTopMenuTitle,1)", iInt);
}

function stopTimer() {
  clearInterval(oTimer);
}

function changeTopMenuTitle() {
  switch(sTopMenuTitle) {
  case 'dynamisch':
    sTopMenuTitle = 'klassisch';
    break;
  case 'klassisch':
    sTopMenuTitle = 'optimiert';
    break;
  case 'optimiert':
    sTopMenuTitle = 'dynamisch';
    break;
  }
}

function saveTopMenuTitle(sClassName) {
  if(sLastSavedTopMenuTitle != sClassName) {
    sLastSavedTopMenuTitle = sClassName;
    new Ajax.Request(
        'http://www.online-forum.net/?page=cms&saveTopMenuTitle='+sClassName,
        {
          method: 'get',
          onFailure: function() {
            //alert('ERROR');
            $(sIndicator).hide();
          }
        }
      );
  }
}

function saveNewsPosition() {
  new Ajax.Request(
      'http://www.online-forum.net/?page=cms&saveNewsPosition='+iNewsPosition,
      {
        method: 'get',
        onFailure: function() {
          //alert('ERROR');
          $(sIndicator).hide();
        }
      }
    );
}

function showContentTopMenuTitle(sClassName) {

  $$('.dynamicContent').foreach( function( k, v ) {
    $$('.dynamicContent')[k].removeClassName('active');
  });

  $$('.dynamicContent.'+sClassName).foreach( function( k, v ) {
    $$('.dynamicContent.'+sClassName)[k].addClassName('active');
  });

  saveTopMenuTitle(sClassName);
}

function fadeOutTopMenu(sClassName) {
  Effect.Queues.get('fadeInTopMenu').invoke('cancel');
  Effect.Queues.get('fadeInBeams').invoke('cancel');
  Effect.Queues.get('fadeOutTopMenu1').invoke('cancel');
  Effect.Queues.get('fadeOutTopMenu2').invoke('cancel');
  Effect.Queues.get('fadeOutBeams1').invoke('cancel');
  Effect.Queues.get('fadeOutBeams2').invoke('cancel');
  switch(sClassName) {
  case 'dynamisch':
    $$('.topMenuContainer.klassisch')[0].fade({duration: iFadeDuration, queue: {position: 'end', scope: 'fadeOutTopMenu1'}});
    $$('.topMenuContainer.optimiert')[0].fade({duration: iFadeDuration, queue: {position: 'end', scope: 'fadeOutTopMenu2'}});
    $$('.smallBeam.klassisch')[0].fade({duration: iFadeDuration, queue: {position: 'end', scope: 'fadeOutBeams1'}});
    $$('.smallBeam.optimiert')[0].fade({duration: iFadeDuration, queue: {position: 'end', scope: 'fadeOutBeams2'}});
    break;
  case 'klassisch':
    $$('.topMenuContainer.dynamisch')[0].fade({duration: iFadeDuration, queue: {position: 'end', scope: 'fadeOutTopMenu1'}});
    $$('.topMenuContainer.optimiert')[0].fade({duration: iFadeDuration, queue: {position: 'end', scope: 'fadeOutTopMenu2'}});
    $$('.smallBeam.dynamisch')[0].fade({duration: iFadeDuration, queue: {position: 'end', scope: 'fadeOutBeams1'}});
    $$('.smallBeam.optimiert')[0].fade({duration: iFadeDuration, queue: {position: 'end', scope: 'fadeOutBeams2'}});
    break;
  case 'optimiert':
    $$('.topMenuContainer.klassisch')[0].fade({duration: iFadeDuration, queue: {position: 'end', scope: 'fadeOutTopMenu1'}});
    $$('.topMenuContainer.dynamisch')[0].fade({duration: iFadeDuration, queue: {position: 'end', scope: 'fadeOutTopMenu2'}});
    $$('.smallBeam.klassisch')[0].fade({duration: iFadeDuration, queue: {position: 'end', scope: 'fadeOutBeams1'}});
    $$('.smallBeam.dynamisch')[0].fade({duration: iFadeDuration, queue: {position: 'end', scope: 'fadeOutBeams2'}});
    break;
  }
  return false;
}

function fadeInTopMenu(sClassName) {
  $$('.topMenuContainer.'+sClassName)[0].appear({duration: iFadeDuration, queue: {position: 'end', scope: 'fadeInTopMenu'}});
  $$('.smallBeam.'+sClassName)[0].appear({duration: iFadeDuration, queue: {position: 'end', scope: 'fadeInBeams'}});
  return false;
}

function showTopMenuTitle(sClassName,bLongInterval) {
  changeTopMenuTitle();

  fadeOutTopMenu(sClassName);
  fadeInTopMenu(sClassName);


  /*$$('.bigIconWithBeam').foreach( function( k, v ) {
    $$('.bigIconWithBeam')[k].removeClassName('active');
  });*/
  /*$$('.smallBeam').foreach( function( k, v ) {
    $$('.smallBeam')[k].removeClassName('active');
  });*/
  $$('.smallIcon').foreach( function( k, v ) {
    $$('.smallIcon')[k].removeClassName('activeSmall');
  });
  /*$$('.topMenuContainer').foreach( function( k, v ) {
    $$('.topMenuContainer')[k].removeClassName('active');
  });*/

  $$('.bigIconWithBeam.'+sClassName).foreach( function( k, v ) {
    $$('.bigIconWithBeam.'+sClassName)[k].addClassName('active');
  });
  $$('.smallBeam.'+sClassName).foreach( function( k, v ) {
    $$('.smallBeam.'+sClassName)[k].addClassName('active');
  });
  $$('.smallIcon.'+sClassName).foreach( function( k, v ) {
    $$('.smallIcon.'+sClassName)[k].addClassName('activeSmall');
  });
  $$('.topMenuContainer.'+sClassName).foreach( function( k, v ) {
    $$('.topMenuContainer.'+sClassName)[k].addClassName('active');
  });

  saveTopMenuTitle(sClassName);
  startTimer(bLongInterval);
}

function marketScroll(iDirection) {
  if(!lockMarket) {
    new Ajax.Request(
      'http://www.online-forum.net/', {
        method: 'post',
        onFailure: function() {
          alert('FATAL ERROR!');
        },
        onSuccess: function(transport) {
          var mReturn = transport.responseText;
          if(mReturn) {
            lockMarket = 1;
            $('marktItemClone').update(transport.responseText);
            if(iDirection < 0) {
              iMove = -120;
            } else {
              iMove = 120;
            }
            $('marktItemClone').style.left = iMove+'px';
            new Effect.Move('marktItemClone', { x:iMove*-1, y:0, mode: 'relative', afterFinish:function(){
              lockMarket=0;
              $('marktItem').innerHTML = $('marktItemClone').innerHTML;
              $('marktItemClone').style.left = '120px';
            }, queue: {position: 'end', scope: 'market'}});
          } else {
            marketScroll(iDirection);
          }
        },
        parameters: {
          'page'       : 'resources',
          'sAction'    : 'getResource',
          'iDirection' : iDirection
        }
      }
    );
  }
}

//bRight == 1 -> scroll right, else left
function newsScroll(bRight) {
  if(lock==0) {
    lock=1;
    Effect.Queues.get('news').invoke('cancel');
    if(bRight) {
      if(iNewsPosition < iNewsRows) {
        $('newsNews').fade({ duration: 0.5, from: 1, to: 0.2, queue: {position: 'end', scope: 'blind'}});
        new Effect.Move('newsBoxShuttle', { x:-400, y:0, mode: 'relative', afterFinish:function(){lock=0;}, queue: {position: 'end', scope: 'news'}});
        $('newsNews').appear({ duration: 0.5, from: 0.2, to: 1, queue: {position: 'end', scope: 'blind'}});
        iNewsPosition++;
        saveNewsPosition();
      }
    } else {
      if(iNewsPosition > 1) {
        $('newsNews').fade({ duration: 0.5, from: 1, to: 0.2, queue: {position: 'end', scope: 'blind'}});
        new Effect.Move('newsBoxShuttle', { x:400, y:0, mode: 'relative', afterFinish:function(){lock=0;}});
        $('newsNews').appear({ duration: 0.5, from: 0.2, to: 1, queue: {position: 'end', scope: 'blind'}});
        iNewsPosition--;
        saveNewsPosition();
      }
    }
    if(iNewsPosition == iNewsRows) {
      $('newsBoxArrowRight').fade();
    } else {
      $('newsBoxArrowRight').appear();
    }
    if(iNewsPosition == 1) {
      $('newsBoxArrowLeft').fade();
    } else {
      $('newsBoxArrowLeft').appear();
    }
  }
}

function openResCategory(iCatId) {
  var ProductsBlock = $('catContainer_'+iCatId);
  var currentStatus = ProductsBlock.getStyle('display');
  var ProdContainer = $$('div.productContainer');
  var iProdCount    = ProdContainer.length;
  var i             = 0;
  for (i;i<iProdCount;i++) {
    ProdContainer[i].hide();
  }
  if (currentStatus == 'none') {
    ProductsBlock.setStyle({display: "block"});
  } else {
    ProductsBlock.setStyle({display: "none"});
  }
}

function openNewsBlock(iContentId, sOpener, sClose) {
  var newsBlock = $('hContent_'+iContentId);
  var descBlock = $('sDescription_'+iContentId);
  var currentStatus = newsBlock.getStyle('display');

  if (currentStatus == 'none') {
    $('openerBtn_'+iContentId).innerHTML = '&raquo;'+sClose;
    newsBlock.setStyle({display: "block"});
    descBlock.setStyle({display: "none"});
  } else {
    $('openerBtn_'+iContentId).innerHTML = '&raquo;'+sOpener;
    newsBlock.setStyle({display: "none"});
    descBlock.setStyle({display: "block"});
  }
}

function hideProdContainer() {
  $('resourceDetailFrame').hide();
}

function movingFooter() {
    var contentHeight   = $('contentFrame').getHeight();
    var leftHeight      = $('leftFrame').getHeight();
    var topHeight       = $('topFrame').getHeight();
    var totalHeight     = contentHeight+topHeight;
    var totalLeftHeight = leftHeight+topHeight;
    var footerTop       = $('footerRow').getStyle('top');

    var windowSize      = document.viewport.getHeight();
    var frameHeight     = $('frameContainer').getHeight();

    if(totalHeight > windowSize) {
      $('footerRow').setStyle({ position: 'static'}).addClassName('clearingItem');
    } else {
      var content = windowSize - topHeight - 40;
      $('contentFrame').setStyle({height: content+"px"});
    }

    //alert(totalHeight+" "+frameHeight+" "+contentHeight);
    /*if (footerTop != null) {
      var footerTopValue = footerTop.split('px');
    }
    else {
      var footerTopValue = 0;
    }

    if ((totalHeight > footerTopValue[0]) || (totalLeftHeight > footerTopValue[0])) {
       //var marginBottom = totalHeight-footerTopValue[0];

       $('footerRow').setStyle({ position: 'static'}).addClassName('clearingItem');
    }*/
    //Event.observe(window, 'resize', movingFooter);
}
function init() {
var aMenus     = $$('#leftFrame > ul ul');
var iMenuCount = aMenus.length;
var oUpperLi   = null;

//  alert(iMenuCount);

  for (i = 0; i < iMenuCount; i++) {
  var oActiveMenu = aMenus[i];
  if (((oUpperLi = oActiveMenu.up('li')).hasClassName('active')) || (oActiveMenu.down('.active'))) {
    oActiveMenu.show();

    if (oUpperLi) {
      oUpperLi.addClassName('active');
    }
  } else {
    oActiveMenu.hide();
  }

  oUpperLi = null;
  }
}
function invisibleMenu () {
 var menuField = $('')
}

