var fxDuration=0.5;
var key = null;



function loadPage(url) {

  new Effect.Pulsate($('mirrorball'), {duration:0.5, from:0.7,  pulses:1, queue: 'front'});
  new Effect.Fade($('main_frame_wrapper'), {duration:fxDuration, from:1.0, to:0.1, queue: 'end'});
  new Effect.Appear($('preloader'), {duration:fxDuration, from:0.0, to:1.0, queue: 'end',
    beforeStart: function () {
      $('preloader_inner').setStyle({display: 'block', opacity: '1.0'});
      $('preloader').setStyle({display: 'block', opacity: '0.0'});
      initPreloader();
    },
    afterFinish: function () {
       new Ajax.Request(url,
       {
           method:'get',
           onSuccess: function (transport, json) {
            arr = url.split("/");
            key = "main_frame";
            if (arr[1]!="") {
              key += "_"+arr[1];
            }
            $('main_frame_inner_wrapper').innerHTML = 
                        '<div id="'+key+'" class="main_frame"><div id="mirrorball" onclick="loadPage(\'/bio\')"></div>'+
                        '<div id="logo"></div>'+
                        '<div id="live"></div>'+
                        '<div id="main_content">'+
                        transport.responseText+
                        '</div></div>';
                        
             
            
             

             fxAfter();
             /*console.log(arr[1]);
             divs = document.getElementsByTagName('div');
             for (i=0;i<divs.length;i++) {
               myDiv = divs[i];
               if (myDiv.className=="main_frame") {
                 myDiv.id="main_frame_"+arr[1];
               }
              ;
             }*/
             
          }
      });
      
    //  document.location.href=url;
    }
  });
}

function fxAfter() {
  initPreloader();
  new Effect.Fade($('preloader'), {duration:fxDuration, from:1.0, to:0.0, queue: 'end'});
  new Effect.Appear($('main_frame_wrapper'), {duration:fxDuration, from:0.1, to:1.0, queue: 'end'});
}

function initPreloader() {
 
  var dim =getWindowSize();

  $('preloader').setStyle( {
    width: dim[0]+'px',
    height: dim[1]+'px'
  });
  
  initBio();
    
  initScroller();
  if (replaceSIFR) {
      replaceSIFR();
  }
}

function initBio() {
    var mapFrom=0.5;

     $$('.bio_menu').each(function(node){ node.setStyle({opacity: mapFrom}); });

     $$('.bio_menu').each(function(node){ 
       Event.observe(node, 'mouseover', function(event) {
           Effect.Fade(node, {duration: 0.5, from:mapFrom, to:0.1, queue: { position: 'end', scope: node.id,
           limit: 2 }});
           
           
       });
   		 new Tooltip(node, {mouseFollow: false});
       Event.observe(node, 'mouseout', function(event) {
          Effect.Fade(node, {duration: 0.5, from:0.1, to:mapFrom, queue: { position: 'end', scope: node.id,
            limit: 2 }});
        });
     });
     
     $$('.event').each(function(node){ 
   		 new Tooltip(node, {mouseFollow: false});
     })
     
     $$('.photo').each(function(node){ 
   		 new Tooltip(node, {mouseFollow: false});
     })
}

function initScroller() {
    if ($('content_wrapper')) {
	    $('scrollbar').setStyle({
	      display: 'block'
	    });
	    makeScrollable({
        scrollSpeed: 30,
        scrollStep: 6,
        scrollableId: 'content_wrapper',
        scrollerId: 'scroller',
        scrollbarId: 'scroll_line',
        wrapperId: 'scrollbar'
      });

	  }
	  
	  if ($('events')) {
	    $('scrollbar').setStyle({
	      display: 'block'
	    });
	    makeScrollable({
        scrollSpeed: 30,
        scrollStep: 6,
        scrollableId: 'events',
        scrollerId: 'scroller',
        scrollbarId: 'scroll_line',
        wrapperId: 'scrollbar'
      });

	  }
	  
	  if ($('bio_content')) {
	    $('scrollbar').setStyle({
	      display: 'block'
	    });
	    makeScrollable({
        scrollSpeed: 30,
        scrollStep: 6,
        scrollableId: 'bio_content',
        scrollerId: 'scroller',
        scrollbarId: 'scroll_line',
        wrapperId: 'scrollbar'
      });

	  }
  
}

document.observe("dom:loaded", function() {
  new Effect.Fade($('preloader_inner'), {duration:fxDuration, from:1.0, to:0.0, queue: 'front',
    beforeStart: function () {    
      initPreloader();
      $('preloader').setStyle({display: 'block', opacity: '1.0'});
    
    }
    
  });

  new Effect.Fade($('preloader'), {duration:fxDuration*2, from:1.0, to:0.0, queue: 'end',
    afterFinish: function () {
      if (startMyLightbox) {
        startMyLightbox();
      }
     
    
  }});
 
});
