G_opacityDuration = 800;
G_pageType  = 'intro';

G_scrollTime  = 1500;
G_scrollArray = new Array();
G_scrollIndex = 0;
G_scrollElements = 13;

G_menuCoverTime = 5000;
G_menuCoverDuration = 300;

G_loopingMusicPlay = true;
G_tracks = new Array();
G_tracks[1] = "media/music/track1.mp3";
G_tracks[2] = "media/music/track2.mp3";
G_tracks[3] = "media/music/track3.mp3";
G_tracks[4] = "media/music/track4.mp3";
G_currentTrack = 1;
G_musicPath = G_tracks[ G_currentTrack ];

G_thumbScrollerInit = false;

G_currentPic = 0;
G_nextPic   = 0;
G_PicInterval = null;
G_loadingPic = false;

G_SlideshowInterval = null;
G_SlideshowDuration = 6000;

G_loadingGallery = false;
G_galleryStack = new Array();

G_urlStack   = null;
G_urlLoading = false;

G_contentFirstFx = false;
G_musicFirstStarted = false;
G_slideShowInited = false;


G_menuImages = new Array();
G_menuImagesId = new Array();
G_menuCursor = 0;
G_firstIntro = true;

PreloaderFX = Fx.Base.extend(
{
  initialize: function (onlyLogo,options) 
  {

    $('progressBox').setStyle('opacity', 0);
  /*
    $('preloaderLogo').setStyle('opacity', 0);
  */  
    this.onlyLogo = onlyLogo;
    this.setOptions(options);
  },

  setNow: function() 
  {
    this.now = this.compute(this.from, this.to);
  },
  compute: function(from, to) 
  {
    return (to - from) * this.delta + from;
  },

  increase: function() 
  {
    $('preloaderLogo').setStyle('opacity', this.now);
    if ( this.onlyLogo )
    {
      if ( this.from == 1 && $('progressBox').getStyle('opacity') != 0) 
      {
        $('progressBox').setStyle('opacity', this.now);
      }
    }
  }
});

PageFx = Fx.Base.extend(
{
  initialize: function (obj,background,s,options) 
  {
    $('pageTitle').setStyle('opacity', 0);
    $('pageTitle').setText( ( obj && obj.title.length > 0 ) ? obj.title : '' );
    
    $('pageTitleBg').setStyle('opacity', 0);    
    $('contentBg'  ).setStyle('opacity', 0);
    
    //var bg =  ( obj && obj.id != 0 && obj.background ) ? 'url(' + background.src + ')' : 'none';
    //$('contentBg').setStyle('background-image',bg);
    
    $('contentBox').setStyle('opacity', 0);
    if ( obj && obj.body.length > 0 )
    {
      $('contentBox').setHTML( obj.body );
      this.scroll = adjustScroll();
    }
    else
    {
      $('contentBox').setHTML( '' );
      this.scroll = false;
    }
    
    $('scrollPane').setStyle('opacity',0);
    $('knob').setStyle('opacity', 0);
    $('progressBox').setStyle('opacity', 0);
    this.setOptions(options);
  },

  setNow: function() 
  {
    this.now = this.compute(this.from, this.to);
  },
  compute: function(from, to) 
  {
    return (to - from) * this.delta + from;
  },

  increase: function() 
  {
    $('contentBox').setStyle('opacity', this.now);
    $('contentBg').setStyle('opacity', this.now);
    $('pageTitle').setStyle( 'opacity', this.now);
    /*
    if ( this.now < 0.2 )
    {
      $('pageTitleBg').setStyle('opacity', this.now);
    }
    */
    $('pageTitleBg').setStyle('opacity', this.now / 5);
    
    if ( this.scroll || ( this.from == 1 && $('scrollPane').getStyle('opacity') != 0)) // 
    {
        $('scrollPane').setStyle('opacity', this.now);
        /*
        if ( this.now < 0.5 )
        {
            $('knob').setStyle('opacity', this.now);
        }
        */
        $('knob').setStyle('opacity', this.now / 2);
    }
    if ( $('preloaderLogo').getStyle('opacity') != 0 )
    {
      $('preloaderLogo').setStyle('opacity', 1 - this.now);
    }
  }
});


var mouseX = 0;
var mouseY = 0;

function getMouseXY(e) 
{
    mouseX = document.all ? (event.clientX + document.body.scrollLeft) : e.pageX;
    mouseY = document.all ? (event.clientY + document.body.scrollTop ) : e.pageY;

    mouseX = (mouseX < 0) ? 0 : mouseX;
    mouseY = (mouseY < 0) ? 0 : mouseY;    
    return true;
}


function disableAllSoundButtons()
{
    $('soundControl').getChildren().each( function(elem) 
    {
        var s = $(elem).getProperty('src');
        if ( $(elem).getProperty('id') != 'sound' && s.indexOf('_dis') == -1)
        {
            $(elem).setProperty('src', 'img/' + s.substring(s.indexOf('/'), s.indexOf('.')) + '_dis.gif');
        } 
    })
}

function playSound( trackNo, elem)
{    
    G_loopingMusicPlay = true;
    disableAllSoundButtons()
    $(elem).setProperty('src', 'img/' + trackNo +'.gif');    
    if ( $('sound').getProperty('src').indexOf('dis') != '-1' )
    {
      $('sound').setProperty('src', "img/eql.gif");
    }
    G_currentTrack = trackNo;
    
    G_musicPath = G_tracks [ trackNo ];
    soundManager.stopAll();
    lounchMusic();
}

function toggleSound()
{
    if ( $('sound').getProperty('src').indexOf('dis') != '-1' )
    {
        G_loopingMusicPlay = true;
        $('sound').setProperty('src', "img/eql.gif"); 
        $('sound' + G_currentTrack).setProperty('src', 'img/' + G_currentTrack + '.gif');
        lounchMusic();
    }
    else
    {
      stopSound();
    }
}

function stopSound() {
  G_loopingMusicPlay = false;
  if ( !G_contentFirstFx && !G_slideShowInited) 
  {
    G_musicFirstStarted = true;
  }        
  soundManager.stopAll();
  disableAllSoundButtons();
  $('sound').setProperty('src', "img/eql_dis.gif");
}
  

function lounchMusic()
{
    if( G_loopingMusicPlay )
    {
        try 
        {
            if ( !soundManager.getSoundById( G_musicPath ) )
            {
                soundManager.createSound({
                  id: G_musicPath,
                  url: G_musicPath,
                  onfinish: lounchMusic
                });
            }
            soundManager.play(G_musicPath);   
            soundManager.setVolume(G_musicPath, G_volume);
            if ( !G_contentFirstFx )
            {
                G_musicFirstStarted = true;
            }         
        }
        catch ( e )
        {
            setTimeout('lounchMusic()',500);
        }
    }
    
    if ( !G_contentFirstFx && G_musicFirstStarted && !G_slideShowInited) 
    {
      G_slideShowInited = true;
      slideshowInit();
    }
}



G_volumeFadeInterval = null;
G_volumeFadeValue = 100;
G_volume = 100;

function volumeFade(fadeout)
{
  G_volumeFadeValue = fadeout ? 100 : 0;
  G_volumeFadeInterval = setInterval('volumeChange(' + fadeout +')',100);
}

function volumeChange(down)
{
  G_volumeFadeValue = down ?  (G_volumeFadeValue - 10) : (G_volumeFadeValue + 10);
  
  if ( down )
  {
    if ( G_volumeFadeValue < 0 )
    {
      clearInterval ( G_volumeFadeInterval )
      return true;
    }
  }
  else
  {
    if ( G_volumeFadeValue > 100 )
    { 
      clearInterval ( G_volumeFadeInterval )
      return true;
    } 
  }

  G_volume = G_volumeFadeValue;
  soundManager.setVolume(G_musicPath, G_volume);
}

function sRight()
{
    if ( G_scrollArray[ G_scrollIndex + 1 ] )
    {
        $('thumbsSrcoller').scroll.toElement( $( G_scrollArray[ ++G_scrollIndex ] ) )    
    }
}

function sLeft()
{
    if ( G_scrollArray[ G_scrollIndex - 1 ] )
    {
        $('thumbsSrcoller').scroll.toElement( $( G_scrollArray[ --G_scrollIndex ] ) )    
    }
}

function showIntro(el)
{
    if ( $('imageControl').getStyle('opacity') != 0 )
    {
      $('imageControl').fx.start(0);
    }
    if ( $('slideshowControl').getStyle('opacity') != 0 )
    {
      $('slideshowControl').fx.start(0);
    }
    if ( $('zoomControl').getStyle('opacity') != 0 )
    {
      $('zoomControl').fx.start(0);
    }
    
    showMenuCover();
    if ( G_pageType == 'info' )
    {
      P2Pfade = new  PageFx(null,null,true,{wait: false, duration: 1000});
      P2Pfade.start(1,0);
    }
    else
    {
      if( $('photoBox').getFirst() != null  )
      {    
        var fx = $('photoBox').effect('opacity', {wait: false, duration: G_opacityDuration }).start(1,0);
        fx.addEvent('onComplete', function(event) { $('photoBox').empty() });
      } 
      if ( $('preloaderLogo').getStyle('opacity') != 0 )
      {
        new PreloaderFX(false, {duration:500}).start(1,0);
      }
      hideThumbs();
      setTimeout('$("preloadCircle").fx.start(1);',500);
    } 
    
    menuHit(el);
    G_slideshowStop = false;
    G_urlLoading = false;
    G_pageType = 'intro';
    slideshowInit();
}
    
G_nextIdToLoad = 0;
G_nextImageToLoad = null;

function loadPic( id, interval)
{    
    if ( G_loadingPic )
    {
        if ( id != G_nextPic )
        {
            clearInterval( G_PicInterval );
            G_PicInterval = setInterval ( 'loadPic(' + id + ',true)',500);
        }        
        return false;
    }
    
    if ( interval )
    {
        clearInterval( G_PicInterval );
    } 
    if ( id == G_currentPic )
    {
        return false;
    }
    G_loadingPic = true;
    $('photoBox').fx.start(0);
    //(function(){
      G_nextIdToLoad = id;
      new Asset.image('cmd.php?cmd=img&class=pageimage&id=' + id + '&field=image&rand=' + (Math.random()*99999999),{onload: picOnLoad});

//      G_nextImageToLoad = image;
//      picOnLoad(id);
    //}).delay(G_opacityDuration);
}

function fitPic( ret )
{
    var newHeight = parseInt($$('.photo').getStyle('height'));
    var newWidth  = parseInt($$('.photo').getStyle('width' ));

    maxHeight = 427;
    maxWidth  = 642;
    if ( newHeight > maxHeight ) {
      newWidth = (newWidth / newHeight) * maxHeight;
      newHeight = maxHeight;
    }
    if (newWidth > maxWidth ) {
      newHeight = (newHeight / newWidth) * maxWidth;
      newWidth  = maxWidth;
    }

    $$('.photo').setStyle('width'  , newWidth   + 'px');
    $$('.photo').setStyle('height' , newHeight  + 'px');

    if ( newWidth < maxWidth ) {
     $$('.photo').setStyle('margin-left',((maxWidth - newWidth) / 2) + 'px');
    }       
    if ( newHeight < 427 ) {
     $$('.photo').setStyle('margin-top',((newHeight - newHeight) / 2) + 'px');
    }   

    $$('.photo')[0].removeEvent('click');
    $$('.photo')[0].addEvent('click', nextImage);
}
function picOnLoad()
{
    G_nextImageToLoad = this;
    picOnLoad2();
}
function picOnLoad2()
{
    if ( $('photoBox').getStyle('opacity') > 0 )
    {        
      setTimeout('picOnLoad2()',200);
      return;
    }
    
    $('photoBox').empty();
    $(G_nextImageToLoad).addClass('photo').injectInside( $('photoBox') );
    fitPic();
    $('photoBox').fx.start(1);
    setTimeout('applyPic('+G_nextIdToLoad+')',G_opacityDuration);
    G_nextImageToLoad = null;
}

function applyPic(id)
{
    G_loadingPic = false;
    G_currentPic = id;
}
function closeVideo()
{
    if ( G_loopingMusicPlay )
    {
      volumeFade(false);
    }
    $('windowLayer').effect('opacity',{duration:700}).start(0.7,0);
    $('videoPlayer').setHTML( '' ).setOpacity(0);
}

function showVideo( u )
{
    if ( G_loopingMusicPlay )
    {
      volumeFade(true);
      //setTimeout( stopSound, 1500);
    }
    var url = 'flvplay.swf?file='+ u +'&autostart=true';
    var flv = '<object style="background-color:black" width="401" height="335"><param name="movie" value="'+ url +'"></param><param name="wmode" value="transparent"></param><embed src="'+ url +'" type="application/x-shockwave-flash" wmode="opaque" width="401" height="335"  style="background-color:black" ></embed></object>'
    var close = '<a id="closeButton" href="#" onclick="closeVideo()">X</a><br />';
    
    var fx = $('windowLayer').effect('opacity',{duration:700}).start(0, 0.7);
    fx.addEvent('onComplete', function(event) 
    {
        $('videoPlayer').setHTML( close + flv ).setOpacity(1);
        var windowSize = window.getSize().size;
        var videoSize  = $('videoPlayer').getSize().size;
        var videoPosX = (windowSize.x - videoSize.x)/2;
        var videoPosY = (windowSize.y - videoSize.y)/2
        $('videoPlayer').setStyles({'left': videoPosX, 'top': videoPosY});
        $('videoPlayer').makeDraggable();
    }); 
}

function adjustScroll()
{
    windowHeight = parseInt($('contentBox').getStyle('height'))  + 30;
    scrollHeight = parseInt($('scroll').getStyle('height'));
    contentHeight = $('contentBox').getSize().scrollSize.y;

    if ( (contentHeight - 10) > windowHeight )
    {
        maxScrollOffset = contentHeight - windowHeight;
        knobHeight = windowHeight - maxScrollOffset;
        knobHeight = ( knobHeight < 13 ) ? 13 : (( knobHeight > scrollHeight ) ? scrollHeight : knobHeight) 
        
        $('knob').setStyle('height', knobHeight + 'px');
        mySlide = new Slider($('scroll'), $('knob'), 
        { 
          steps: maxScrollOffset, 
          mode: 'vertical', 
          onChange: function(step){ $('contentBox').scrollTo(0, step) }
        }).set(0);
        
        $('scrollUp'  ).addEvents({'mouseup':stopScrolling, 'mouseout':stopScrolling, 'mousedown':scrollStart});
        $('scrollDown').addEvents({'mouseup':stopScrolling, 'mouseout':stopScrolling, 'mousedown':scrollStart});
        $('contentBox').addEvent('mousewheel', function(event)
        {
          var event = new Event(event);
          if (event.wheel > 0)
            mySlide.set(mySlide.step-18);
          else
            mySlide.set(mySlide.step+18);
          event.stop();
        });
        return true;
    }
    else
    {
      return false;
    }
}

function insertContent(request)
{    
    /*
          if ( $('preloaderLogo').getStyle('opacity') != 0 )
          {
            new PreloaderFX(false, {duration:500}).start(1,0);
          }
    */
    result = eval( "(" + request + ")");
    if ( result.id != 0 && result.background)
    {
      var x = new Asset.image('cmd.php?cmd=img&class=page&id=' + result.id + '&field=background',{alt:'', 
      onload:function(image)
      {
        $('contentBg').setStyle('opacity',0);
        $('contentBg').setStyle('background-image','url(' + this.src + ')');
        
        $('preloadCircle').fx.start(0);
        new PreloaderFX(true, {duration:500}).start(1,0);
        
        (function(){
          P2P = new  PageFx(result, this,null,{wait: false, duration: 1000});
          P2P.start(0,1);
        }).delay(500);
      }})
    }
    else
    {
      $('preloadCircle').fx.start(0);
      new PreloaderFX(true, {duration:500}).start(1,0);
      
      (function(){
        $('contentBg').setStyle('background-image','none');
        P2P = new  PageFx(result,null, {wait: false, duration: 1000});
        P2P.start(0,1);
      }).delay(500);
    }
    setTimeout('checkWaitingUrl()',3000);
}

G_loadedImagesId = new Array();
G_currentImageId = 0;

function buildGalleryNav(request)
{
    if ( G_pageType != 'gallery' ) 
    {
        return false;
    }
    if ( G_loadingGallery )
    {
        G_galleryStack.push ( request )
        return false;
    }
    
    G_loadingGallery = true;
    var imgs         = new Array();
    var loadedImages = {};
    var result       = eval( "(" + request + ")");    
    G_scrollArray    = [];
    G_scrollIndex    = 0;
    if ( !result )
    {
      if ( $('preloaderLogo').getStyle('opacity') == 0 || $('preloaderLogo').getStyle('visibility') == 'hidden')
      {
        $('preloaderLogo').effect('opacity').start(0,1);
      }
      else
      {
        $('preloaderLogo').effect('opacity',{onComplete:function(){$('preloaderLogo').effect('opacity').start(0,1)}}).start(1,0);
      }
      G_loadingGallery = false;
      $('thumbsList').empty();
      hideThumbs();
      hidePreloader();
      checkWaitingUrl();
      return false;
    }
    else
    {
      showThumbs();
    }
    
    $('photoBox').empty();
    result.each( function(elem, index) {
      imgs.push('cmd.php?cmd=img&class=pageimage&id=' + elem.id + '&field=image' + (imgs.length != (result.length - 1) ? '&type=t' : '') + '&rand=' + (Math.random()*99999999)); // + '&rand=' + (Math.random()*99999999)
    });
    var imageSet = new Asset.images(imgs, {
        onComplete: function()
        {
            if ( G_galleryStack.length > 0 )
            {
                request = G_galleryStack.getLast();                
                G_galleryStack = new Array();
                G_loadingGallery = false;
                buildGalleryNav( request )
                return false;
            }
            
            $("thumbsList").setOpacity(0);
            $("thumbsList").empty();
            G_loadedImagesId = new Array();
            imgs.each ( function(xxx, index)
            {
                if ( index != imgs.length - 1)
                {
                    var id = xxx.match(/id=(\d+)/)[1];
                    var elem = loadedImages[ id ];
                    var fx = new Fx.Styles(elem, {wait:false, duration:1000});
                    fx.set({'border-top-color' :'#000000','border-bottom-color' :'#000000','border-left-color' :'#000000','border-right-color' :'#000000'});
                    elem.addEvent('click', function(event)
                    {
                      loadPic( $(this).getProperty('id').substring( $(this).getProperty('id').indexOf('_') + 1 ).toInt() );
                    });
                    elem.addEvent('mouseover',function(event){fx.start({'border-top-color' :'#ffffff','border-bottom-color' :'#ffffff','border-left-color' :'#ffffff','border-right-color' :'#ffffff'})});
                    elem.addEvent('mouseout' ,function(event){fx.start({'border-top-color' :'#000000','border-bottom-color' :'#000000','border-left-color' :'#000000','border-right-color' :'#000000'})});
                                        
                    src = elem.getProperty('src')
                    /*
                    id  = src.substring( src.indexOf('&id=') + 4, src.indexOf('&field=') );
                    */

                    G_loadedImagesId.push( id );
                    elem.addClass('tn').setProperties({'id' : 'thumb_' + id, 'alt':''});

                    elem.injectInside( $("thumbsList") );

                    if ( index % G_scrollElements == 0 ) {
                      G_scrollArray.push('thumb_' + id); 
                    } 
                }
            });
            $("thumbsList").effect('opacity',{duration:1000,wait:false}).start(0,1);
            $('photoBox').empty();
            
            if ( G_pageType == 'gallery' )
            {
                new Asset.image( imgs.getLast() + '&rand=' + (Math.random()*99999999) ,{alt:'', onload: function(image)
                {
                    $( this ).addClass('photo').injectInside( $('photoBox') )
                    fitPic(); 
                    G_currentPic = imgs.getLast().substring( src.indexOf('&id=') + 4, src.indexOf('&field=') )
                    G_currentPic = G_currentPic.replace(/&/, "");
                    
                    $('progressBox').effect('opacity',{onComplete:function()
                    {
                      if ( G_loadedImagesId.length > 1 )
                      {
                        $('imageControl').fx.start(1);
                        $('slideshowControl').fx.start(1);
                        $('zoomControl').fx.start(1);
                      }
                      $('photoBox').fx.start(1);
                    }}).start(1,0);
                    setTimeout('G_loadingGallery = false',1200);
                    setTimeout('checkWaitingUrl()',1200);
                }});
            }
            else
            {
                G_loadingGallery = false;
                checkWaitingUrl();
            }
        },
        onProgress: function(i)
        {
            var progress =  Math.floor( ((i+1) / imgs.length)*100 );
            if ( progress <= 100 )
            {
              $('progress').setStyle('width', progress);
              $('progressPerc').setText( progress.toString() + ' %');
            }            
            var id = this.src.match(/id=(\d+)/)[1];
            loadedImages[ id] = this;
        }                           
    });    
}

function nextImage()
{
  if ( G_loadedImagesId.length == 0 )
  {
    return false;
  }
  for ( var i = 0; i < G_loadedImagesId.length; i++ )
  {
    if ( G_loadedImagesId[ i ] == G_currentPic && i != (G_loadedImagesId.length - 1 ))
    {
      loadPic ( G_loadedImagesId[ i + 1 ] );
      return true;
    }
    else if (G_loadedImagesId[ i ] == G_currentPic)
    {
      loadPic ( G_loadedImagesId[ 0 ] );
      return true;
    }
  }
  return false;
}

function prevImage()
{
  if ( G_loadedImagesId.length == 0 )
  {
    return false;
  }
  
  for ( var i = 0; i < G_loadedImagesId.length; i++ )
  {
    if ( G_loadedImagesId[ i ] == G_currentPic && i > 0)
    {
      loadPic ( G_loadedImagesId[ i - 1 ] );
      return true;
    }
    else if ( G_loadedImagesId[ i ] == G_currentPic )
    {
      loadPic ( G_loadedImagesId[ G_loadedImagesId.length - 1 ] );
      return true;
    }
  }
  return false;
}
function menuHit( el )
{
    $$('.menuImg').each( function(element)
    {
        src = $(element).getProperty('src');
        if ( src.indexOf('_hit') != '-1' )
        {
            var newSrc = src.substring(0, src.indexOf('_hit')) + src.substring(src.indexOf('_hit') + 4); 
            $(element).setProperty('src',newSrc);
        }
    });
    
    var src = $(el).getProperty('src');
    var newSrc = src.substring(0, src.indexOf('.png')) + '_hit' + src.substring(src.indexOf('.png'));    
    $(el).setProperty('src',newSrc); 
}

function hideBackground()
{
    if ( $('contentBg').getStyle('background-image') != 'none' )
    {
        fx = $('contentBg').effect('opacity').start(1,0);        
        fx.addEvent('onComplete', function()
        {        
            $('contentBg').setStyle('background-image','');
            $('contentBg').setOpacity(1);
        });       
    }
}

function showBackground( background )
{
    if ( $('contentBg').getStyle('background-image') != 'none' )
    {
        var fx = $('contentBg').effect('opacity').start(1,0);        
        fx.addEvent('onComplete', function()
        {
            $('contentBg').setStyle('background-image','url(cmd.php?cmd=img&class=page&id=' + result.id + '&field=background)');;
            $('contentBg').effect('opacity').start(0,1);
         });
    }
    else
    {
        $('contentBg').setOpacity(0);
        $('contentBg').setStyle('background-image','url(cmd.php?cmd=img&class=page&id=' + result.id + '&field=background)');
        $("contentBg").effect("opacity").start(0,1);
    }
}



function showMenuCover( withTimeout )
{
    if ( G_pageType == 'info' )
    {
      return false;
    }
    
    $("menuCover").setStyle('width' , $('menu').getSize().size.x);
    $("menuCover").setStyle('height', ($('menu').getSize().size.y != 0) ? $('menu').getSize().size.y : (($('menu').getChildren().length / 2) * 20));
    xMin = $("menuCover").getLeft();
    xMax = xMin + $("menuCover").getSize().size.x;
    yMin = $("menuCover").getTop();
    yMax = yMin + $("menuCover").getSize().size.y;
         
    if ( ( mouseX > xMin && mouseX < xMax ) && (mouseY > yMin && mouseY < yMax) )
    {
        setTimeout('showMenuCover(true)', 1000);
        return true;
    }
    
    if ( $("menuCover").getProperty("opacity") == 0 || $("menuCover").getStyle('visibility') == 'hidden')
    {
        if ( withTimeout )
        {
          setTimeout('showMenuCover()', G_menuCoverTime - 1000);
        }
        else
        {
          showMenuCoverFx();
        }
    }
}

function showMenuCoverFx()
{
  $("menuCover").fx.options.duration = 3 * G_menuCoverDuration;
  $("menuCover").fx.start(0.8);
}
function hideMenuCover()
{
    if ( $("menuCover").getProperty("opacity") != 0 )
    {
      $("menuCover").fx.options.duration = G_menuCoverDuration;
      $("menuCover").fx.start(0);
    }
}

function hidePageTitle()
{
  if ( $("pageTitle").getStyle('visibility') != 'hidden' && $("pageTitle").getProperty('opacity') != 0)
  {
    $('pageTitle').effect('opacity', { onComplete: function()
    {
        $('pageTitleBg').setStyle('visibility','hidden')
    }}).start(1,0);
  }
}

function showPreloader()
{
  $('progressPerc').setText('1 %');
  $('progress'    ).setStyle('width', 1);
  $('progressBox' ).effect('opacity',{duration:500,wait:false}).start(0,1);  
  $('preloader'   ).effect('opacity',{duration:500,wait:false}).start(0,1);
}
function hidePreloader()
{
  
  $('progressBox' ).effect('opacity',{duration:500,wait:false}).start(1,0);
  $('preloader'  ).setStyle('visibility','hidden');
  $('preloaderLogo').setStyle('visibility','hidden');
}

function showThumbs()
{
  if ( !G_thumbScrollerInit )
  {
    $('thumbsSrcoller').scroll = new Fx.Scroll('thumbsSrcoller', {wait:false, duration: G_scrollTime, transition: Fx.Transitions.Expo.easeOut});
    G_thumbScrollerInit = true;
  }
  
  new Fx.Scroll('thumbsSrcoller', {wait:false, duration: 50}).toLeft();
  $('thumbsList').empty();
  if( $('thumbs').getStyle('visibility') == 'hidden' || $('thumbs').getProperty('opacity') == 0 )
  {
    $('thumbs').effect('opacity', {duration: 500}).start(0,1);        
  }
  $('authors').addClass('withThumbs');
}
function hideThumbs()
{
  if( $('thumbs').getStyle('visibility') == 'visible' || $('thumbs').getProperty('opacity') == 1 )
  {
    $('thumbs').effect('opacity', {duration: 500}).start(1,0);
    setTimeout('$("thumbsList").setOpacity(0);',500);        
  }
  else
  {
    $('thumbsList').setOpacity(0);
  }
  $('authors').removeClass('withThumbs');
}

menuShow = null;
/**
 *  Function called on DOM ready
 */
function onLoad()
{
    if (!document.all) 
    {
        document.captureEvents(Event.MOUSEMOVE)
    }
    document.onmousemove = getMouseXY;
    
    $('preloadCircle').setStyles(
    {
      'left': (646 - 35)/2,
      'top' : (431 - 35)/2 
    });
    $('preloadCircle').fx = $('preloadCircle').effect('opacity',{wait:false,duration:200});
    $('preloadCircle').fx.set(0);
    
    $('imageControl').fx  = $('imageControl').effect('opacity',{wait:false,duration:500});
    $('imageControl').fx.set(0);
    $('slideshowControl').fx  = $('slideshowControl').effect('opacity',{wait:false,duration:500});
    $('slideshowControl').fx.set(0);
    $('zoomControl').fx  = $('zoomControl').effect('opacity',{wait:false,duration:500});
    $('zoomControl').fx.set(0);
    
    var windowSize = window.getSize().size;
    if ( windowSize.x < 600 ) windowSize.x = 600;
    if ( windowSize.y < 300 ) windowSize.y = 300;
    
    //var top = Math.round((windowSize.y - ($('content').getCoordinates().top + $('content').getCoordinates().height)) / 2) - 58;
    var top = Math.round((windowSize.y - ($('content').getCoordinates().top + 431)) / 2) - 58;
    
    if ( top > 0 )
    {
        $('mainFrame').setStyle('top', top);
    }
    
    
    var cursor = 0;
    menuimgs = new Array();
    unusedImg = true;
    maxUnusedImgLoops = 10;
    
    $('preloadCircle').fx.start(1);
    var menuImages = new Asset.images(G_menuImages,
    {
        onProgress : function() 
        { 
          menuimgs.push (this)
        },
        onComplete: function()
        {
          setTimeout('populateMenu();',200);         
          menuShow = $('menu').effect('opacity',{duration:1000}).set(0);
          var accordion = new Fx.Accordion('img.toggler', 'div.stretch', 
          {
              start: 'all-closed',
              opacity: false,
              alwaysHide: true
          });
        }
    }); 

    $('photoBox' ).fx = $('photoBox' ).effect('opacity', {wait: false, duration: G_opacityDuration }).set(0);
    $('menuCover').fx = $('menuCover').effect('opacity', {wait: false, duration: G_menuCoverDuration }).set(0)
    $('menuCover').addEvent('mouseenter',hideMenuCover);
    $('menuCover').addEvent('mouseleave',function(){setTimeout('showMenuCover()',G_menuCoverTime)} );
    
    //new Asset.javascript("js/soundmanager/script/soundmanager2.js");
    //setTimeout('lounchMusic()',1500);
    lounchMusic()
    $('slideshowStart').addEvent('click', slideshowStartCallback);
    $('slideshowStop' ).addEvent('click', slideshowStopCallback);
    $('zoomControl').addEvent('click', zoomPhoto);
}

function slideshowStartCallback()
{
  nextImage();
  G_SlideshowInterval = setInterval(nextImage, G_SlideshowDuration);
  $('slideshowStart').setStyle('visibility', 'hidden');
  $('slideshowStop').setStyle('visibility', 'visible');
}

function slideshowStopCallback()
{
  clearInterval(G_SlideshowInterval);
  $('slideshowStart').setStyle('visibility', 'visible');
  $('slideshowStop').setStyle('visibility', 'hidden');
}

function zoomPhoto()
{
  if ($$('.photo').length > 0) {
    Slimbox.open($$('.photo')[0].src + '&type=p');
  }
}


function populateMenu()
{
  var unusedImg = true;
  for ( var i = 0; i < G_menuImagesId.length; i++)
  {
    elem  = G_menuImagesId[ i ];
    for ( var j = 0; j < menuimgs.length; j++)
    {
      str = menuimgs[j].src;
      tmp = str.substr( str.indexOf('-') + 1 );
      id = tmp.substr( tmp.indexOf('-') + 1) + tmp.substr( 0, tmp.indexOf('-'))

      if ( id == elem )
      {
        $(elem).setProperty('src', menuimgs[j].src);
        unusedImg = false;
        break;
      }
      else
      {
        unusedImg = true;
      }
    }
  }
  if ( unusedImg )
  {
    setTimeout('populateMenu()',200);
    return;
  }
  
  return unusedImg;
}

function checkWaitingUrl()
{
    G_urlLoading = false;
    if ( G_urlStack && G_urlStack.length > 0 )
    {
        contentUrl(G_urlStack[0], G_urlStack[1], G_urlStack[2])
        G_urlStack = null;
    }
}

function contentUrl(url, el, gallery)
{
    slideshowStopCallback();
    if ( G_firstIntro )
    {
        return false;
    }
    if ( G_urlLoading )
    {
        G_urlStack = new Array(url,el,gallery);
        return false;
    }
    
    G_urlLoading = true;
    if  ( url == 'intro' )
    {
      showIntro(el);
      return false;    
    }
    var id      = url.substring(url.indexOf(':') + 1);
    var classId = url.substring(0, url.indexOf(':') );
    
    if ( !G_slideshowStop )
    {
        G_slideshowStop = true;
        if ( !(slideshowIteration%2) )
        {
            $("Fg").setStyle('visibility','hidden');
            $("Im2").setStyle('visibility','hidden');
            $('Im1').effect('opacity',{wait:false,duration:1000}).start(1,0);
        }
        else
        {
            $('Im1').setStyle('visibility','hidden');
            FxF.start(1,0);
            FxF.removeEvent('onComplete').addEvent('onComplete',function() 
            {        
                $("Fg").setStyle('visibility','hidden');
                $("Im2").setStyle('visibility','hidden');
            });
        }        
    }
    
    
    menuHit(el) 
    if( $('photoBox').getFirst() != null  )
    {
        if ( !gallery && G_pageType != 'info')
        {
          $('preloaderLogo').setOpacity(0);
        }
        $('photoBox').fx.start(0);
        setTimeout('$("photoBox").empty()',G_opacityDuration);
    }
    
    if ( $('imageControl').getStyle('opacity') != 0 )
    {
      $('imageControl').fx.start(0);
    }

    if ( $('slideshowControl').getStyle('opacity') != 0 )
    {
      $('slideshowControl').fx.start(0);
    }
    if ( $('zoomControl').getStyle('opacity') != 0 )
    {
      $('zoomControl').fx.start(0);
    }
    
    if ( gallery )
    {
        if ( G_pageType == 'info' )
        {
          P2Pfade = new  PageFx(null,null,true,{wait: false, duration: 1000});
          P2Pfade.start(1,0);
          
          (function(){
            $("contentBg").setStyle("background-image","none");
            $("contentBg").effect("opacity").start(0,1);
            if ( $('preloaderLogo').getStyle('opacity') != 1 )
            {
              var Preloader = new PreloaderFX(false, {duration:500});
              Preloader.start(0,1);
            }
            showPreloader();
          }).delay(800);
        }
        else
        {
            showPreloader();
        }
        
        if ( $('thumbsList').getStyle('opacity') != 0 )
        {
            $('thumbsList').effect('opacity',{duration:500,wait:false}).start(1,0);
        }
        
        G_pageType = 'gallery';
        (function(){
            new Ajax('ajax.php?function=getGalleryNav', {method: 'post', postBody: Object.toQueryString({id: id}),onSuccess: buildGalleryNav}).request();
            slideshowStartCallback();
            setTimeout('showMenuCover()',G_menuCoverTime);
        }).delay(1000);        
    }
    else
    {
        if ( G_pageType == 'info' )
        {
          P2Pfade = new PageFx(null,null,true, {wait: false, duration: 1000});
          P2Pfade.start(1,0);
          (function(){
            new PreloaderFX(false, {duration:1000}).start(0,1);
            $("preloadCircle").fx.start(1)
          }).delay(1500);
          
        }
        else
        {
          //new PreloaderFX(false, {duration:500}).start(1,0);
          $("preloaderLogo").setStyle("opacity",1);
          setTimeout('$("preloadCircle").fx.start(1);',500);
          hideThumbs();
        }
        
        G_pageType = 'info';
        (function(){
            new Ajax("ajax.php?function=getContent", 
            {
              method: "post", 
              postBody: Object.toQueryString( {classId: classId, id: id} ),
              onSuccess: insertContent
            }).request();
        }).delay(1500 + 1000);        
    }
}
function disapearMessageOK()
{
    $('messageOkDiv').effect('opacity',{duration:800}).start(1,0);
    (function(){
      $('messageOk').setStyle('display','none');
    }).delay(900);    
}

function handleSendContactForm( request )
{
  res = eval( "(" + request + ")");
  
  var fields = new Array();
  fields.push('recipient');
  fields.push('input_name');
  fields.push('input_phone');
  fields.push('input_email');
  fields.push('input_code');
  fields.push('input_body');
  
  fields.each( function(elem)
  {
    $(elem).setStyle('background-color','');
  });
  
  if ( res.result == 'OK')
  {
    $('messageError').setStyle('display','none');
    $('messageOk').setStyle('display','block');
    $('messageOkDiv').effect('opacity',{duration:800}).start(0,1);
    $('objectForm').reset();
    setTimeout('disapearMessageOK()',5000);
  }
  else
  {
    $('messageError').setStyles('display:block');
    $('messageErrorDiv').setText(res.errorDesc);
    $('messageErrorDiv').effect('opacity',{duration:800}).start(0,1);
    $('input_' + res.errorField).setStyle('background-color','red');
  }
  
  $('sendContactFormButton').setProperty('disabled','');
}
function sendContactForm()
{
  $('sendContactFormButton').setProperty('disabled','true');
  var params = Object.toQueryString( getFormAsArray('objectForm') );
  new Ajax('ajax.php?function=sendContactForm', {method: 'post', postBody: params,onSuccess: handleSendContactForm}).request();
  result = false;
}

