var quotes={
	quotesSelector:'#quotes .quote',
	holdDuration:5000 ,
  	fadeDuration:1000,
	maxLeft:'200',
	maxTop:'100',
	animIn:'',
	animOut:'',
	objArray:'',
	divIn:'',
	divOut:'',

	fadeInOut: function(){
		if( quotes.divIn == quotes.divOut ) {quotes.getNext(); return;}
		
		/*if( window.ie6 ){
			quotes.divOut.setStyle( 'visibility', 'hidden' );
			quotes.divIn.setStyle( 'visibility', 'visible' );
			quotes.getNext.delay( quotes.holdDuration );
		}else{*/
			quotes.animIn = new Fx.Style( quotes.divIn, 'opacity', { duration: quotes.fadeDuration, transition:Fx.Transitions.linear})
			quotes.animOut = new Fx.Style( quotes.divOut, 'opacity', { duration: quotes.fadeDuration, transition:Fx.Transitions.linear, onComplete:quotes.animFin});
			quotes.animIn.start(0,1);	
			quotes.animOut.start(1,0);
		//}
	},
	animFin: function(){
		quotes.getNext.delay( quotes.holdDuration );
		quotes.animIn = '';	
		quotes.animOut = '';

	},
	setPosition: function(){
		var l = $random(100, quotes.maxLeft).toString() + 'px';
		var t = $random(10, quotes.maxTop).toString() + 'px';
		quotes.divIn.setStyles( { 'left':l, 'top':t } );
		quotes.fadeInOut();
	},
	getNext: function(){
		var r = $random(0, quotes.objArray.length - 1);
		quotes.divOut = quotes.divIn;
		quotes.divIn = quotes.objArray[r];
                if( quotes.divIn == quotes.divOut ){ quotes.getNext(); return; }
		quotes.setPosition();
	},
	init: function(){
		quotes.objArray = $$( quotes.quotesSelector );
		quotes.divIn = quotes.objArray[0];
		quotes.objArray.setStyle('visibility', 'hidden');
		quotes.divIn.setStyle('visibility', 'visible');
		quotes.getNext();
	}
}

var nav={
	ulId: 'navlist',
	activeDiv: '',
	upComingDiv:'',
	links: '',
	roll: '',
      errCatch:0,
      inProgress:false,
	animIn:'',
	animOut:'',
	
	switchDivs: function(divIn, divOut){
		if( divIn == divOut){ return; }
            nav.upComingDiv = divIn;
		//divIn.setStyles({ 'visibility' : 'hidden', 'display' : 'block' });
            nav.animOut = new Fx.Style(divOut, 'opacity', {transition:Fx.Transitions.linear } )
            nav.animIn = new Fx.Style(divIn, 'opacity', {transition:Fx.Transitions.linear, onComplete:nav.animFin});
		//if( window.ie ){
			nav.animIn.set(1);
			nav.animOut.set(0);
			nav.activeDiv = nav.upComingDiv;
			nav.inProgress = false;
			nav.animIn = '';
		   	nav.animOut = '';	

		/*}else{
			nav.animIn.start(0, 1);
			nav.animOut.start(1, 0);
		}*/
	},
	animFin: function(){
             //  nav.activeDiv.setStyle('display', 'none');
               nav.activeDiv = nav.upComingDiv;
		   nav.inProgress = false;
		   nav.animIn = '';
		   nav.animOut = '';	
		
 	},
        swapLinks: function(nLink){
               for( p in nav.roll ){
               	if( nav.roll[p].active == false ){
                  	nav.roll[p].src = nav.roll[p].offsrc
                        nav.roll[p].active = true;
                  }
                }
		var newLink = nLink.getElementsByTagName('img')[0].id 
		nav.roll[newLink].src = nav.roll[newLink].onsrc;
                nav.roll[newLink].active = false;
        },
	handleClick: function(ev){
            var e = new Event(ev);
		e.preventDefault();
		this.blur();
            if( nav.inProgress == true ) { return; }
            nav.inProgress = true;
		
		nav.swapLinks(this);
		nav.switchDivs($(this.title), nav.activeDiv);
	},
	init: function(){
		$('body').setOpacity(0.95);

            nav.roll = $class('rollover');
            nav.roll.rollover('_o');
		nav.roll['imghome'].active = false;
		nav.roll['imghome'].src = nav.roll['imghome'].onsrc;
		nav.links = $$('#' + nav.ulId + ' li a');
            nav.activeDiv = $('home');    
		for(var i=0; i<nav.links.length; i++){
			nav.links[i].addEvent('click', nav.handleClick);
		}
		
               /* this bit relies on a .htaccess file and won't work on windows servers
		var fullPath = location.pathname.replace('.html', '');
                var path = fullPath.replace('/', '');
           
                if( path.length > 0){
                       nav.activeDiv = $(path);
                    try{
                         var teste = nav.activeDiv.id;
                          
                   }catch(e){
                        path = 'home';
                        nav.activeDiv = $(path);
                   }
                }else {
                       path = 'home';
                        nav.activeDiv = $(path);
                }
		
                $('home').setStyle('display', 'none');
               nav.activeDiv.setStyle('display', 'block');
                 
                nav.swapLinks( $(path + 'Link'));
		*/
	}
}



window.addEvent('domready', nav.init);
window.addEvent('domready', quotes.init);	
window.addEvent('unload', function(){ nav = ''; quotes=''; } );	