var introMovieLoaded = false;
function introSetMovieLoaded()
{ introMovieLoaded = true; }

var introMovieClick = '';
function introSetMovieClick(ref)
{ introMovieClick = ref; }

function initIntro()
{
    var defBorderTopHeigth = 153;
    var defPhotoHeight = 461;
    var teaserBorderTopHeigth = 16;
    
    var phase = 0;
    var currPageSize = 0;
    var currPhotoHeight = 0;
    var currMenuHeight = 0;
    
    var mbtimer = null;
    
    var effectU = null;
    var effectD = null;
    
    var linkref = '';
    
    var lockResize = false;
    
    $$('html')[0].style.overflowY = 'hidden';
    $('logo').className = 'inv';
    $('logo').style.display = 'none';
    $('menu_service').style.display = 'none';
    $('photobar').style.background = '#000';
    $('pb_bdr_top').style.height = '0';
    $('pb_grt_bottom').style.width = '100%';
    $('pb_grt_bottom').style.left = '0';
    $('pb_grt_bottom').style.marginLeft = '0';
    $('menubar').style.display = 'none';
    $('menubar').style.height = '500px';
    
    var mainm = $('menu_main');
    for(var i=0; i < mainm.getElementsByTagName('li').length; i++)
    {
        var item = mainm.getElementsByTagName('li')[i];
        
        if(item.getElementsByTagName('ul')[0])
        {
            var subitem = item.getElementsByTagName('ul')[0];
            subitem.style.display = 'none';
        }
    }
    
    checkSize();
    
    setHeightToElement('photobar',currPhotoHeight);
    window.onresize = function(){
    if(lockResize == false) fitToScreen(); }
    
    var mloadedint = window.setInterval(function(){
    if(introMovieLoaded == true)
        { startInto(); window.clearInterval(mloadedint); }
    }, 10);
    
    var mclickint = window.setInterval(function(){
    if(introMovieClick.length > 0)
        { phase = 2; menuClick(introMovieClick);
        window.clearInterval(mclickint); }
    }, 10);
    
    
    function startInto()
    {
        if(phase != 0) return;
        phase = 1;
        
        new Effect.Parallel([
            new Effect.Appear('menubar', { sync: true }),
            new Effect.Appear('logo', { sync: true })
        ], { 
          duration: 1.4,
          delay: 0,
          afterFinish: function(){ addMenuIntroFunctions(); }
        });
    }
    
    function showMenuBar()
    {
        if(effectU != null
        || effectD != null)
            return;
        
        lockResize = true;
        checkSize();
                
        effectU = new Effect.Parallel([
            new Effect.Tween('pb_bdr_top', $('pb_bdr_top').offsetHeight, teaserBorderTopHeigth,
            { sync: true }, function(p){ setHeightToElement('pb_bdr_top',p); }),
            new Effect.Tween('photobar', $('photobar').offsetHeight, Math.max(currPhotoHeight-currMenuHeight,defPhotoHeight),
            { sync: true }, function(p){ setHeightToElement('photobar',p); })
        ], { 
          duration: 1.0,
          delay: 0.0,
          afterFinish: function(){ effectU = null; lockResize = false; }
        });
        
        var mainm = $('menu_main');
        for(var i=0; i < mainm.getElementsByTagName('li').length; i++)
        {
            var item = mainm.getElementsByTagName('li')[i];
            
            if(item.getElementsByTagName('ul')[0])
            {
                var subitem = item.getElementsByTagName('ul')[0];
                new Effect.Appear(subitem.id, { duration: 0.5, delay: 0.3 });
            }
        }
    }
    
    function hideMenuBar()
    {
        if(effectD != null)
            return;
        if(effectU != null)
            { effectU.cancel();
            effectU = null; }
        
        lockResize = true;
        checkSize();
        
        effectD = new Effect.Parallel([
            new Effect.Tween('pb_bdr_top', $('pb_bdr_top').offsetHeight, 0, { sync: true }, function(p){ setHeightToElement('pb_bdr_top',p); }),
            new Effect.Tween('photobar', $('photobar').offsetHeight, currPhotoHeight, { sync: true }, function(p){ setHeightToElement('photobar',p); })
        ], { 
          duration: 1.0,
          delay: 0,
          afterFinish: function(){ effectD = null; lockResize = false; }
        });
        
        var mainm = $('menu_main');
        for(var i=0; i < mainm.getElementsByTagName('li').length; i++)
        {
            var item = mainm.getElementsByTagName('li')[i];
            
            if(item.getElementsByTagName('ul')[0])
            {
                var subitem = item.getElementsByTagName('ul')[0];
                new Effect.Fade(subitem.id, { duration: 0.5, delay: 0.3 });
            }
        }
    }
    
    function exitIntro()
    {
        if(phase != 2)
            return;
        
        phase = 3;
        lockResize = true;
        
        checkSize();
        
        new Effect.Fade('logo', { duration: 0.5, afterFinish: function(){ $('logo').className = 'std'; }});
        
        if(effectU != null)
            { effectU.cancel();
            effectU = null; }
        if(effectD != null)
            { effectD.cancel();
            effectD = null; }
        
        new Effect.Parallel([
            new Effect.Tween('pb_bdr_top', $('pb_bdr_top').offsetHeight, defBorderTopHeigth,
            { sync: true }, function(p){ setHeightToElement('pb_bdr_top',p); }),
            new Effect.Tween('photobar', $('photobar').offsetHeight, defPhotoHeight,
            { sync: true }, function(p){ setHeightToElement('photobar',p); })
        ], { 
          duration: 1.3,
          delay: 0.0
        });
        
        new Effect.Parallel([
            new Effect.Appear('logo', { from: 0, to: 1, sync: true }),
            new Effect.Appear('menu_service', { sync: true })
        ], { 
          duration: 0.5,
          delay: 0.9
        });
        
        var mainm = $('menu_main');
        for(var i=0; i < mainm.getElementsByTagName('li').length; i++)
        {
            var item = mainm.getElementsByTagName('li')[i];
            
            if(item.getElementsByTagName('ul')[0])
            {
                var subitem = item.getElementsByTagName('ul')[0];
                new Effect.Fade(subitem.id, { duration: 0.5, delay: 0.5 });
            }
        }
    }
    
    function setHeightToElement(target, height)
    {
        if(!target || height == NaN || height < 0)
            return;
        
        $(target).style.height = parseInt(height,10) + 'px';
    }
    
    function fitToScreen()
    {
        if(phase > 2) return;
        
        checkSize();
        
        setHeightToElement('photobar',currPhotoHeight);  
    }
    
    function checkSize()
    {
        currPageSize = getPageSize(true);
        currPhotoHeight = Math.min(currPageSize.height, Math.max(defPhotoHeight, $('pb_swf_con').offsetWidth * 0.6395));
        currMenuHeight = /*$('menu_main').offsetHeight*/120;
    }
    
    function addMenuIntroFunctions()
    {
        $('menubar').onmouseover = function()
        {
            if(phase != 1 && phase != 2) return;
            phase = 2;
            
            showMenuBar();
            window.clearTimeout(mbtimer);
        }
        
        $('menubar').onmouseout = function()
        {
            if(phase != 2) return;
            
            mbtimer = window.setTimeout(
            function(){ hideMenuBar(); },50);
        }
        
        var mainm = $('menu_main');
        for(var i=0; i < mainm.getElementsByTagName('a').length; i++)
        {
            var item = mainm.getElementsByTagName('a')[i];
            
            if(item.href.length > 0
            && item.href.substr(0,10) != 'javascript')
            {
                item.onclick = function(){ menuClick(this.href); return false; }
            }
        }
    }
    
    function menuClick(ref)
    {
        if(phase != 2) return;
        
        exitIntro();
        linkref = ref;
        
        window.setTimeout(function(){window.location.replace(ref);},1700)
    }
}

if(swfobject.hasFlashPlayerVersion('9.0.0'))
{ initf.push('initIntro'); }
