///////////////////////////////////////////////////
/////////////foxallassociates.com/////////////////
/////////////////////////////////////////////////






//From flash

// Handle all the FSCommand messages in a Flash movie.
function dragos_towers_v2_DoFSCommand(command, args) {
	$('intro').fade({afterFinish:function(){$('intro').remove()}});
	document.location = 'en.htm'
}
// Hook for Internet Explorer.
if (navigator.appName && navigator.appName.indexOf("Microsoft") != -1 && navigator.userAgent.indexOf("Windows") != -1 && navigator.userAgent.indexOf("Windows 3.1") == -1) {
	document.write('<script language=\"VBScript\"\>\n');
	document.write('On Error Resume Next\n');
	document.write('Sub dragos_towers_v2_FSCommand(ByVal command, ByVal args)\n');
	document.write('	Call dragos_towers_v2_DoFSCommand(command, args)\n');
	document.write('End Sub\n');
	document.write('</script\>\n');
}
/// --- end from flash


	var Content = Class.create({
		initialize:function(buttons,content,hideAfterDelay,closeOnSecondClick){
			this.closeOnSecondClick = closeOnSecondClick;
			this.hideAfterDelay = hideAfterDelay;
			this.buttons = buttons;
			this.buttons.each(function(b,i){
				$(b).observe('mouseover',this.hover.bindAsEventListener(this,i+1));
				$(b).observe('mouseout' ,  this.out.bindAsEventListener(this,i+1));
				$(b).observe('click'    ,this.click.bindAsEventListener(this,i+1));
			}.bind(this))
			this.content_el = $(content);
		},
		click:function(e,i){
			
			this.s = (!this.closeOnSecondClick)?i:(this.s == i)?0:i;
			this.render();
			this.renderContent();
		},
		hover:function(e,i){
			this.h = i;
			this.render();
			this.cancelClose();
		},
		out:function(e,i){
			this.h = 0;
			this.render();
			this.startClose();
		},
		startClose:function(){
			if(this.hideAfterDelay){
				this.cancelClose();
				this.closeAfter = new PeriodicalExecuter(function(pe) {
				  pe.stop();
					this.s = 0;
					this.h = 0;
					this.render();
					this.renderContent();
				}.bind(this), 2);
			}
		},
		cancelClose:function(){
			if(this.hideAfterDelay){
				if (this.closeAfter){	
					this.closeAfter.stop();
				}
			}
		},
		render:function(){
			this.buttons.each(function(b,i){
				if(i == (this.s-1)){
						$(b).addClassName('selected')
				} else {
						$(b).removeClassName('selected')
				}
			}.bind(this))
		},
		renderContent:function(){
			//this should be implemented
			c = ''
			if(this.s > 0 && this.buttons[this.s-1] && $(this.buttons[this.s-1]).down('.content')){
				c = $(this.buttons[this.s-1]).down('.content').innerHTML
			}
			this.content_el.update(c)
			this.content_el.select('.lb').each(function(el){el.remove()})
			
			//sort out the scrolling
			if($(this.content_el).select('.subcontent').length > 0){
				subcontentscroller.setScrollArea($(this.content_el).select('.subcontent')[0])
			} else {
					subcontentscroller.setScrollArea('empty_scroll')
			}
		}
	})
	

	
	/*
	
		Quick links
	
		todo :
			- make this use a common class for displaying/hiding content (override with the display)
			- abstract to have the menu items in a list 
	*/
	var QL = Class.create({
		initialize:function(){
			this.s = 0;
			this.h = 0;
			
			this.render();
			
			$('ql_tel').observe('mouseover',this.hover.bindAsEventListener(this,1));
			$('ql_email').observe('mouseover',this.hover.bindAsEventListener(this,2));
			$('ql_mail').observe('mouseover',this.hover.bindAsEventListener(this,3));
			
			
			$('ql_tel').observe('mouseout', this.out.bindAsEventListener(this,1));
			$('ql_email').observe('mouseout',this.out.bindAsEventListener(this,2));
			$('ql_mail').observe( 'mouseout',this.out.bindAsEventListener(this,3));
			
			$('ql_content').observe( 'mouseout',this.out.bindAsEventListener(this,0));
			
			$('ql_tel').observe(  'click',this.click.bindAsEventListener(this,1));
			$('ql_email').observe('click',this.click.bindAsEventListener(this,2));
			$('ql_mail').observe( 'click',this.click.bindAsEventListener(this,3));
			
			
			$('ql_content').observe('mouseover',this.cancelClose.bind(this));
			$('ql_content').observe('mouseout' ,this.startClose.bind(this));
			
			
			
		},
		click:function(e,i){
			this.s = this.s == i?0:i;
			this.render();
			this.renderContent();
		},
		hover:function(e,i){
			this.h = i;
			this.render();
			this.cancelClose();
		},
		out:function(e,i){
			this.h = 0;
			this.render();
			this.startClose();
		},
		startClose:function(){
			this.cancelClose();
			this.closeAfter = new PeriodicalExecuter(function(pe) {
			    pe.stop();
				this.s = 0;
				this.h = 0;
				this.render();
				this.renderContent();
			}.bind(this), 2);
		},
		cancelClose:function(){
			if (this.closeAfter){	
				this.closeAfter.stop();
			}
		},
		
		
		render:function(){
			$('ql_tel').style.backgroundPosition   =  '-0px ' + (this.s == 1 ? '-80px': this.h == 1 ? '-40px' : '0px');
			$('ql_email').style.backgroundPosition = '-52px ' + (this.s == 2 ? '-80px': this.h == 2 ? '-40px' : '0px');
			$('ql_mail').style.backgroundPosition  = '-83px ' + (this.s == 3 ? '-80px': this.h == 3 ? '-40px' : '0px');
		},
		renderContent:function(){
			c = ''
			if(this.s == 1){
				c = $('ql_tel').down('.content').innerHTML;
				$('ql_content').morph('width:300px',{duration:0.5});
			} else if(this.s == 2){
				c = $('ql_email').down('.content').innerHTML;
				$('ql_content').morph('width:250px',{duration:0.5});
			} else if(this.s == 3){
				c = $('ql_mail').down('.content').innerHTML;
				$('ql_content').morph('width:600px',{duration:0.5});
			}
			// console.log(c);
			if(c != ''){
			} else {
				$('ql_content').morph('width:0px',{duration:0.5});
			}
			$('ql_content').update(c)
		}
	});
	
	
	
	
	/*
		Background 
			todo 
				- support cycling through images (instead of using setTimeouts)
	*/
	
	
	var BG = Class.create({
		initialize:function(w,h){
			//Note,  this requires background css
			this.w = w;
			this.h = h;
			$(document.body).insert({bottom:new Element('div',{id:'bg'})})
			Event.observe(window, 'resize',this.resizeBG);
		},
		setImage:function(url){
			if(!$('bgimage')){
				im = new Element('img',{src:url,id:'bgimage','style':'display:none;'});
				im.observe('load',function(e){
					Event.element(e).appear();
				})
				$('bg').update(im);
			} else {
				im = new Element('img',{src:url,id:'bgimage2',style:'display:none'});
				im.observe('load',function(e){
					Event.element(e).appear({duration:2,
						afterFinish:function(){
							//when it has faded in - replace the original with it.
							$('bgimage').remove();
							$('bgimage2').writeAttribute('id','bgimage');
						}});
				})
				$('bg').insert({top:im});
			}
			this.resizeBG();
		},
		resizeBG:function(){
			vw = (document.viewport.getWidth() + 100);
			vh = (document.viewport.getHeight() + 100);
				s = Math.max(
					vw / this.w,
					vh / this.h
				);
				w = parseInt(s * this.w);
				h = parseInt(s * this.h);
				ox = parseInt((vw - w)/2);
				oy = parseInt((vh - h)/2);
				
			if($('bgimage'))
				$('bgimage').setStyle({width:w+'px',height:h+'px',left:ox+'px',top:oy+'px'})
			if($('bgimage2'))
				$('bgimage2').setStyle({width:w+'px',height:h+'px',left:ox+'px',top:oy+'px'})
			
		}

	})
	
	
	
	
	/*
		A class for displaying rollover images using cropped background images 
		(useful for reducing bandwidth and complexity).
	*/
	Rollover = Class.create({
		initialize:function(el){
			this.el = $(el);
			this.h = [0,0];
			this.o = [0,0];
			this.el.observe('mouseover',this.dohover.bindAsEventListener(this))
			this.el.observe('mouseout',   this.doout.bindAsEventListener(this))
		},
		dohover:function(){
			this.setBackground(this.h[0],this.h[1]);
		},
		doout:function(){
			this.setBackground(this.o[0],this.o[1]);
		},
		setBackground:function(x,y){	
			this.el.style.backgroundPosition = x + 'px ' + y + 'px';
		},
		normal:function(x,y){
			this.o = [x,y];
		},
		hover:function(x,y){
			this.h = [x,y];
		}
	})
	


/* Slider from galatea */


var Slider = Class.create({
	initialize: function(sContent,sTrack,options){
		this.sContent = $(sContent);
		
		this.sTrack = $(sTrack);
		this.sBar = this.sTrack.down();
		
		//sort out the styling
		this.sContent.setStyle({overflow:'hidden',height:'400px'});
		
		this.trackHeight = this.sTrack.getHeight();
		this.barHeight = 0;
		
		this.pos = 0;
		
		this.setBarHeight();
		
		this.updateUI();
		
		
		this.sTrack.observe('mousedown',function(e){
			this.dragStart = Event.pointerY(e);
			this.dragging = true;
			Event.stop(e); // makes the text not select
		}.bindAsEventListener(this))
		
		document.observe('mouseup', function(e) {
	 		if(this.dragging){
				this.dragging = false;
			}
		}.bindAsEventListener(this));
		
		document.observe('mousemove', function(event) {
	 		if(this.dragging){
				p = Event.pointerY(event);
				Event.stop(event);
				
				var dragDelta = p - this.dragStart;
				this.ScrollBy(dragDelta);
				this.dragStart = p;
			}
		}.bindAsEventListener(this));
		
		document.observe('DOMMouseScroll',function (e){
			if(Event.element(e).up('#scrollContent')){
				Event.stop(e);
				this.ScrollBy(e.detail);
			}
		}.bindAsEventListener(this));
		
		document.observe('mousewheel',function (e){
			if(Event.element(e).up('#scrollContent')){
				Event.stop(e);
				this.ScrollBy(-e.wheelDelta / 60);
			}
		}.bindAsEventListener(this));
		
	},
	ScrollBy : function(y){
		this.pos += y;
		this.limitPos();
		this.updatePos();
	},
	between : function(low,value,high){
		return value < low ? low : value > high ? high : value;
	},
	limitPos : function(){
		this.pos = this.between(0,this.pos,this.trackHeight - this.barHeight);
	},
	//content has changed
	newContent:function(){
		this.pos = 0;
		this.dragPoint = 0;
		this.setBarHeight();
		this.updateUI();
	},
	setBarHeight:function(){
		this.prop = (this.sContent.scrollHeight) / this.sContent.getHeight();
		this.barHeight = this.trackHeight/this.prop;
		this.sBar.setStyle({height:this.barHeight+'px'});
	},
	updateUI:function(){
		if(this.prop <= 1){
			this.sTrack.hide();
		} else {			
			this.sTrack.show();
		}
		this.updatePos();
	},
	updatePos:function(){
		this.sTrack.down().setStyle({marginTop:this.pos+'px'})
		this.sContent.scrollTop = this.pos+(this.pos*1.1);
	},
	setScrollArea:function(sContent){
		this.sContent = $(sContent);
		this.sContent.setStyle({overflow:'hidden',height:'400px'});
		this.refresh()
	},
	refresh:function(){
		this.pos = 0
		this.setBarHeight();
		this.updateUI();
	}
	
})
// var scroller = new FA.Slider('scrollContent', 'scrollTrack');

//FOR SHOWING A SUB MENU
function showsection(el,i){
	//select the menu item
	
	$(el).siblings().each(function(s){
		s.removeClassName('selected');
	})
	$(el).addClassName('selected');
	
	
	//show the correct content
	$(el).up().siblings().each(function(s,j){
		if(i == j){
			s.show();
			subcontentscroller.setScrollArea(s);
		} else {
			s.hide();
		}
	});
	
	//update the lightbox
	LB.updateImageList();
}


	
		Event.observe(window,'load',function(){
			if(isIntro){return;}
			
			var MMenu = Class.create(Content,{
				initialize:function($super){
					$super($$('#m > div'),$('mmc'),false,false)
				}
			})
			
			//create the quick links
			var ql = new QL();
			
			
			
			//MENU STUFF
			
			var closeButt = new Rollover('closeMenu');
			closeButt.normal(0,0);
			closeButt.hover(0,-35);
			
			var menuMinimized = false;
			
			$('closeMenu').observe('click',function(){
				if(!menuMinimized){	
					$('mainMenu').morph('margin-top:-154px;height:38px',{duration:0.5});
					new Effect.Opacity('logo',{to:0.001});
					$('m').morph('padding-top:30px;',{duration:0.5});
					$('mainMenuContentHolder').hide();
					closeButt.normal(0,-70);
					closeButt.hover(0,-105);
				} else {
					$('mainMenu').morph('margin-top:20px;height:60px',{duration:0.5});
					new Effect.Opacity('logo',{to:1});
					$('m').morph('padding-top:0px;',{duration:0.5,afterFinish:function(){
						$('mainMenuContentHolder').appear();
					}});
					
					closeButt.normal(0,0);
					closeButt.hover(0,-35);

				}
				
				menuMinimized = (!menuMinimized);
			})
			
			var mShown = false;
			$('m').observe('click',function(e){
				// if(!mShown){
				// alert('x');
					$('mainMenuContent').setStyle('height:0px;')
					$('mainMenuContentFoot').setStyle('height:0px;')
					$('mainMenuContent').morph('height:400px')
					$('mainMenuContentFoot').morph('height:20px')
					mShown = true;
				// }
			})
			
			
			
			//temp (will be in flash)
			// $('skip').observe('click',function(e){
			// 	dragos_towers_v2_DoFSCommand('skip');
			// })
			
			if(window.location.hash === '#x'){
				dragos_towers_v2_DoFSCommand('dont_show')
			}
			
			


				var m = new MMenu();

				bg = new BG(1225,700);

				bg_loop = function(){
						bg.setImage('images/bgs/render01.jpg');
						setTimeout("bg.setImage('images/bgs/render01.jpg')",10000);
						setTimeout("bg.setImage('images/bgs/render02.jpg')",20000);
						setTimeout("bg.setImage('images/bgs/render03.jpg')",30000);
						setTimeout("bg.setImage('images/bgs/render04.jpg')",40000);
						setTimeout("bg.setImage('images/bgs/render05.jpg')",50000);
						setTimeout("bg.setImage('images/bgs/render06.jpg')",60000);
						setTimeout("bg.setImage('images/bgs/render07.jpg')",70000);
						setTimeout("bg.setImage('images/bgs/render08.jpg')",80000);
						setTimeout("bg_loop()",90000)
				}
				bg_loop();

			// })
			
				subcontentscroller = new Slider('mmc', 'scrollTrack');

			
		})
		
		
		
