  
var menuOpen = false;

var myScrollBar = false;
document.addEvent('domready' ,function(){


    if(Browser.Engine.trident4){
        setIE6Height();
    }



//    replaceHeadline();
//    replaceSubHeadline();
    replaceLatestWorks();
    
    


    
    $('first_level_navigation').getElements('a').setProperty('title','');        
    
    
    activateScroller()
    
    
    
    if(!Browser.Engine.trident4){
        if(! /iPhone/.test(navigator.userAgent)){    
        
        
            $(document.body).addClass('js_enabled');
        

            var subMenus =  $('first_level_navigation').getElements('.level_1 .level_2');
            subMenus.set( 'tween', {duration: 0}); 
            subMenus.fade('out');
    
    
            
            var leftColumn = $('left').getElement('.inside');
            
            var activeMenu =  $('first_level_navigation').getElement('.level_1 li.trail .level_2');
            if(! activeMenu) activeMenu =  $('first_level_navigation').getElement('.level_1 li.active .level_2');        
                
            if(activeMenu){
                    leftColumn.set( 'tween', {duration: 0}); 
                    leftColumn.tween("width", 511);
            
        
                    activeMenu.set( 'tween', {duration: 0}); 
                    activeMenu.fade('in');
            
        
                    
            }else{
                    leftColumn.set( 'tween', {duration: 0}); 
                    leftColumn.tween("width", 425);
            }
            
            $('first_level_navigation').getElements('.level_1 li').addEvent('mouseenter',showSubMenu);
            $('first_level_navigation').getElements('.level_1 li').addEvent('mouseleave',hideSubMenu);
            
            
            
            if($(document.body).hasClass('new_style')){
                createScrollBarNewStyle();
                $(window).addEvent('resize',function(event){
                   createScrollBarNewStyle();
                });
            
            
            }else{
                createScrollBar();            
                $(window).addEvent('resize',function(event){
                   createScrollBar();
                });
            
                
            
            }
            

            
            
        
        }
    }else{
    
    
    
    
    
            if($(document.body).hasClass('new_style')){
            
                myMain = $('main');
                footerHeight = 110;
                topMargin = parseInt(myMain.getStyle('top'),10);
                topMargin = (topMargin>0)?topMargin:0 ;         
                myFooter = $('home-footer');
                if(myFooter) footerHeight = 160;
                remainingHeight = $(window).getSize().y - footerHeight - 10 - topMargin;
                remainingHeight = (remainingHeight>0)?remainingHeight:0 ;    
        
        
                $(myMain).setStyle('height', remainingHeight); 
            
            
                createScrollBarNewStyle();
            
            
            }else{
                createScrollBar();            
                $(window).addEvent('resize',function(event){
                   createScrollBar();
                });
            
                
            
            }
            

    }

    
    
        
    var oi= 0;



});



function createScrollBar(){

    if($('scrollbar1')){$('scrollbar1').destroy();}
            
    myScrollbar  = new Element('div', {id: 'scrollbar1', 'class':'scrollbar-vert'});
    var myScrollbarHandle  = new Element('div', {id: 'handle1', 'class':'handle-vert'});
    var myScrollbarScale  = new Element('div', {id: 'scale1', 'class':'scale-vert'});    
    

    myScrollbarScale.inject(myScrollbar);    
    myScrollbarHandle.inject(myScrollbar);    
    myScrollbar.inject($('main'),'after');
    
    makeScrollbar($('main'),$('scrollbar1'), $('handle1'));

   $('scrollbar1').set( 'tween', {duration: 0}); 
    
    if($('main').getSize().y < $('main').getScrollSize().y ){
        $('scrollbar1').fade('in')
    }else{
        $('scrollbar1').fade('out');

    }


}

function createScrollBarNewStyle(){

    if($('scrollbar1')){$('scrollbar1').destroy();}
    
    myX = 50;
    if($(document.body).hasClass('home'))    myX = 20;
    
    myScrollingArticle = $('scrolling');
    firstArticleHeight = $('main').getElement('.mod_article').getSize().y;    
    remainingHeight = $('main').getSize().y -firstArticleHeight - myX;
    remainingHeight = (remainingHeight>0)?remainingHeight:0 ;    


    $(myScrollingArticle).setStyle('height', remainingHeight); 

    
            
    myScrollbar  = new Element('div', {id: 'scrollbar1', 'class':'scrollbar-vert'});
    var myScrollbarHandle  = new Element('div', {id: 'handle1', 'class':'handle-vert'});
    var myScrollbarScale  = new Element('div', {id: 'scale1', 'class':'scale-vert'});    
    $(myScrollbar).setStyle('top', firstArticleHeight + 50); 
    
    
    
/*    
    alert(myScrollingArticle.getSize().y);
    alert( myScrollingArticle.getScrollSize().y);
    alert(remainingHeight);
*/

    myScrollbarScale.inject(myScrollbar);    
    myScrollbarHandle.inject(myScrollbar);    
    myScrollbar.inject(myScrollingArticle,'after');
    
    makeScrollbar(myScrollingArticle,$('scrollbar1'), $('handle1'));

   $('scrollbar1').set( 'tween', {duration: 0}); 
    
    if((myScrollingArticle.getSize().y + 7) < myScrollingArticle.getScrollSize().y ){
        $('scrollbar1').fade('in')
    }else{
        $('scrollbar1').fade('out');

    }


}





function showSubMenu(e){
        var leftColumn = $('left').getElement('.inside');

        var subMenuToShow = this.getElement('.level_2');


        var activeMenu =  $('first_level_navigation').getElement('.level_1 li.trail .level_2');
        if(! activeMenu) activeMenu =  $('first_level_navigation').getElement('.level_1 li.active .level_2');                


        if(activeMenu && this.parentNode.className == "level_1"){
            activeMenu.fade("out");
        }

        
        
        if(subMenuToShow){


            leftColumn.set( 'tween', {duration: 'short'}); 
            leftColumn.tween("width", 511);
            subMenuToShow.set( 'tween', {duration: 'long'}); 
            subMenuToShow.fade('in');		    
        }
        
        
        
        
}

function hideSubMenu(e){
        var leftColumn = $('left').getElement('.inside');

        var subMenuToShow = this.getElement('.level_2');        
        
        var activeMenu =  $('first_level_navigation').getElement('.level_1 li.trail .level_2');        
        if(! activeMenu) activeMenu =  $('first_level_navigation').getElement('.level_1 li.active .level_2');        
        
        if(subMenuToShow ){        

            subMenuToShow.set( 'tween', {duration: 'short'}); 
            subMenuToShow.fade('out');		    
            
            
            

            if(activeMenu){
                leftColumn.set( 'tween', {duration: 0}); 
                leftColumn.tween("width", 511);
                activeMenu.fade("in");
                
            }else{            
                leftColumn.set( 'tween', {duration: 'long'});
                leftColumn.tween("width", 425);
            }
            
            

        }
        
        
       if(!subMenuToShow && this.parentNode.className == "level_1"){
            if(activeMenu){
                leftColumn.set( 'tween', {duration: 0}); 
                leftColumn.tween("width", 511);
                activeMenu.fade("in");
                
            }else{            
                leftColumn.set( 'tween', {duration: 'long'});
                leftColumn.tween("width", 425);
            }

       
       
       }
        
        
            
        
        
        
}




function replaceHeadline(){

    var sectionName = $('top').get('class').replace(/ js_enabled/, "");
    sectionName = sectionName.replace(/ scrolling-body/, "");
     
    var validSectionNames = ['news','home', 'technology','services', 'expertise','aboutus','whylocteam'];

    var headline = $('headline').getElement('h1');    
    headline.set('class', 'font_replaced');
    if(validSectionNames.contains(sectionName)){        
    
        headline.set('html', '<img src="tl_files/tiles/heading_'+sectionName+'.png"  alt="LocTeam" style=""  width="219" height="44" />');
        
    }else{
        headline.set('html', '<img src="tl_files/tiles/heading_home.png"  alt="LocTeam" width="219" height="44"  />');
    }
}

function replaceSubHeadline(){
    var subheadline = $('headline').getElement('h2');
    subheadline.set('html', '<img src="tl_files/tiles/heading_mac_localization.png" alt="LocTeam" id="sub_headline" width="226" height="12"  />');    
}


function replaceLatestWorks(){
    var sectionName = $('top').get('class').replace(/ js_enabled/, "");
    sectionName = sectionName.replace(/ scrolling-body/, "");
    sectionName = sectionName.replace(/ new_style/, "");    
    sectionName = sectionName.replace(/ home_new_style/, "");         
    var validSectionNames = ['news','home', 'technology','services', 'expertise','aboutus','whylocteam'];

    var subheadline = $('Bottom_News_Scroller').getElement('h1');
    subheadline.addClass('image_replacement');
    
    
    if(validSectionNames.contains(sectionName)){        
    
        
            subheadline.set('html', '<img src="tl_files/tiles/latest-works_'+sectionName+'.png" alt="Latest Works by LocTeam" id="latest-works-image"/>');            

    }else{
    
            subheadline.set('html', '<img src="tl_files/tiles/latest-works_other.png" alt="Latest Works by LocTeam" id="latest-works-image"/>');            
    }
} 



function makeScrollbar(content,scrollbar,handle,horizontal,ignoreMouse){
	var steps = (horizontal?(content.getScrollSize().x - content.getSize().x):(content.getScrollSize().y - content.getSize().y))
	var slider = new Slider(scrollbar, handle, {	
		steps: steps,
		mode: (horizontal?'horizontal':'vertical'),
		onChange: function(step){
			// Scrolls the content element in x or y direction.
			var x = (horizontal?step:0);
			var y = (horizontal?0:step);
			content.scrollTo(x,y);
		}
	}).set(0);
	if( !(ignoreMouse) ){
		// Scroll the content element when the mousewheel is used within the 
		// content or the scrollbar element.
		$$(content, scrollbar).addEvent('mousewheel', function(e){	
			e = new Event(e).stop();
			var step = slider.step - e.wheel * 30;	
			slider.set(step);					
		});
	}
	// Stops the handle dragging process when the mouse leaves the document body.
	$(document.body).addEvent('mouseleave',function(){slider.drag.stop()});
}


function setIE6Height(){
    var headerEndsAt = $('header').getPosition().y + $('header').getSize().y ;
    var mainEndsAt = $('main').getPosition().y + $('main').getSize().y ;
    var optimalMainHeight =  mainEndsAt - headerEndsAt - 5;
    var optimalScrollBarHeight = optimalMainHeight ;

    $('main').setStyle('height', ''+optimalMainHeight+'px');


}


function activateScroller(){
    var duration = 200000
    var scroller = new Fx.Scroll('Bottom_News_Scroller_Container', {
    	link: 'chain' ,
        duration: duration,
        transition: Fx.Transitions.linear
    })
    
    scroller.toRight();
    
   window.setTimeout("resetScroller()",duration);
   window.setTimeout("activateScroller()",duration + 30);   

}

function resetScroller(){
    var scroller = new Fx.Scroll('Bottom_News_Scroller_Container', {

        duration: 0,
        transition: Fx.Transitions.linear
    })
    scroller.toLeft();
}



