Broadstreet = function()
{
	var public = {
		
		flashSwfFile: 'background.swf',
		
		pageContentTemplate: '/html/#{name}.html',
		
		containerDivId:		'container',
		buttonsDivId:		'buttons',
		contentDivId:		'content',
		flashContainerId:	'flash',
		
		contentFadeDuration: 0.5
		
	};
	
	
	
	/****************************** Private Members: *****************************/
	
	var myFlashIsReady = false;
	
	var myFlashTag, myFlashObject = null;
	
	var myElements = {};
	
	var myFlashUid = "broadstreetBackgroundFlash";
	
	var myPages = $H();
	
	var myContentDivVisible = false;
	
	var myScrollBar = false;
	
	var mySlideshow = false;
	
	/****************************** Private Methods: *****************************/
	
	var myButtonClickHandler = function(e)
	{
		var link = e.element();
		
		var page = $w(link.className)[0];
		
		this.showPage(page);
		
		e.stop();
	}.bindAsEventListener(public);
	
	var myInitialize = function()
	{
		DFTools.console.debug("Broadstreet myInitialize() called.");
		
		myElements.containerDiv = $(public.containerDivId);
		
		myElements.buttonsDiv = $(public.buttonsDivId);
		
		myElements.contentDiv = $(public.contentDivId);
		
		
		
		
		
		myElements.containerDiv.hide();
		myElements.contentDiv.hide();
		
		
		
		myElements.flashContainer = $(public.flashContainerId);
		
		
	
		
		
		var flashpage = window.location.hash.length ? window.location.hash.substr(1) : 'home';

		if(flashpage != 'home') {
			myElements.flashContainer.innerHTML = '<img src="/images/background.jpg" width="980" height="580">';
			Broadstreet.flashReady();
		}
		else {
			
			var hasReqestedVersion = DetectFlashVer(requiredMajorVersion, requiredMinorVersion, requiredRevision);
			
			if (hasReqestedVersion) {
			
		myFlashTag = DFTools.loadFlash({
			id:			myFlashUid,
			codebase:	'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0',
			pluginspace:	'http://www.macromedia.com/go/getflashplayer',
			width:		'980',
			height:		'580',
			src:		public.flashSwfFile,
			movie:		public.flashSwfFile,
			quality:	'high',
			wmode:		'transparent',
//			scale:		'noscale',
			menu:		'false',
			swliveconnect:		'true',
//			allowFullScreen:	'false',
			allowScriptAccess:	'always',
			FlashVars:  'lcId='+myFlashUid
		});
		
		

		myElements.flashContainer.innerHTML = myFlashTag;
		
		
		
		
		myFlashObject = myElements.flashContainer.down('embed');
		
		
		
		DFTools.console.debug("Created flash object",myFlashObject);
			
			} else {  // flash is too old or we can't detect the plugin
				myElements.flashContainer.innerHTML = '<img src="/images/background.jpg" width="980" height="580">';
				
				Broadstreet.flashReady();
				new Effect.Appear(myElements.containerDiv,{
					duration: this.contentFadeDuration
				});
				
			}
		}
		
		myElements.buttonsDiv.select('a').invoke('observe','click',myButtonClickHandler);
		
		
		DFTools.console.debug("Broadstreet myInitialize() finished.");
	}
	
	
	
	
	
	
	
	
	/****************************** Public Methods: ******************************/
	
	public.flashReady = function()
	{
		DFTools.console.debug("Broadstreet.flashReady called.");
		
		myFlashIsReady = true;
		
		new Effect.Appear(myElements.containerDiv,{
			duration: this.contentFadeDuration
		});
		
		this.showPage(window.location.hash.length ? window.location.hash.substr(1) : 'home');
	}.bind(public);
	
	
	public.isReady = function()
	{
		return myFlashIsReady;
	};
	
	public.getFlash = function()
	{
		return myFlashObject;
	}.bind(public);
	
	public.showPage = function(name)
	{
		DFTools.console.log("Broadstreet.showpage ",name);
		
		var page = myPages.get(name);
		
		if(!page)
		{
			page = new Broadstreet.Page(name);
			myPages.set(name,page);
		}
		
		page.display();
	}.bind(public);
	
	public.fadeContentOut = function()
	{
		if(myContentDivVisible)
		{
			new Effect.Fade(myElements.contentDiv,{
				duration: this.contentFadeDuration,
				queue: 'end'
			});
			
			myContentDivVisible = false;
		}
	}.bind(public);
	
	public.fadeAndUpdateContent = function(content)
	{
		if(myContentDivVisible)
		{
			this.fadeContentOut();
			this.setContent.bind(this,content).delay(this.contentFadeDuration);
			this.fadeContentIn.bind(this).delay(this.contentFadeDuration);
		}
		else
		{
			this.setContent(content);
			this.fadeContentIn();
		}
	}.bind(public);
	
	public.setContent = function(html)
	{
		myElements.contentDiv.update(html);
		
		if(mySlideshow) {
			mySlideshow.running = false;
		}
		
		myScrollBar = false;
		mySlideshow = false;
		
		var scrollDiv = myElements.contentDiv.down('div.scroll');
		
		if(scrollDiv)
		{
			var createScrollBar = function() {
				myScrollBar = new DFThemeObject.CustomScrollbar(scrollDiv, {
					cssClassPrefix:	'broadScroll',
					scrollbarWidth: 20,
					arrowImages:	{up: '/images/up.gif', down: '/images/down.gif'},
					maxThumbHeight:	11,
					minThumbHeight: 11
				});
			}
			
			createScrollBar.delay(1);
		}
		
		var ssContainer = myElements.contentDiv.down('div.slideshow'), ssDiv;
		
		if(ssContainer && (ssDiv = ssContainer.down('div')))
		{
			mySlideshow = new JSlideShow(ssDiv,{
				debug: false,
				sourceList: myElements.contentDiv.down('ol.slideshow'),
				delay: 5,
				fadeDuration: 2,
				autoplay: true
			});
			
			ssDiv.select('div').invoke('setStyle',{width:'100%'});
		}
		
		var fixLinks = function()
		{
			var hrefStart = window.location.protocol + '//'+window.location.hostname+'/';
			
			myElements.contentDiv.select('a').each(function(l)
			{
				if(l.href == window.location || l.hasClassName(window.location.hash.substr(1)))
				{
					l.addClassName('active');
				}
				
				if(l.href.substr(0,hrefStart.length) == hrefStart && !l.up('ul.clips'))
				{
					l.observe('click',myButtonClickHandler);
				}
			});
		};
		
		fixLinks.delay(0.5);
		fixLinks.delay(1.5);
	}
	
	public.fadeContentIn = function()
	{
		if(!myContentDivVisible)
		{
			new Effect.Appear(myElements.contentDiv,{
				duration: this.contentFadeDuration,
				queue: 'end'
			});
			
			myContentDivVisible = true;
		}
	}.bind(public);
	
	public.showClip = function(url)
	{
		var player = myElements.contentDiv.down('div.player'),
			clipName = myElements.contentDiv.down('div.clipName');
		
		var clipItem;
		
		if(clipItem = myElements.contentDiv.down('ul.clips li.active'))
		{
			clipItem.removeClassName('active');
		}
		
		if(player)
		{
			player.update('');
			clipName.update('');
			
			var clipNumber = /[0-9]+/.exec(url);
				
			clipNumber = clipNumber[0] ? parseInt(clipNumber[0]) : false;
			
			var clipNameFile = url.gsub(/swf/,'gif');
			
			//if no flash detected load image instead of flash video
			var hasReqestedVersion = DetectFlashVer(requiredMajorVersion, requiredMinorVersion, requiredRevision);
			if (hasReqestedVersion) {
			
			if(url)
			{ 
				DFTools.loadFlash({
					id:			"homepageClip",
					codebase:	'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0',
					pluginspace:	'http://www.macromedia.com/go/getflashplayer',
					width:		'320',
					height:		'240',
					src:		url,
					movie:		url,
					wmode:		'transparent',
					quality:	'high',
					scale:		'noscale',
					salign:		'lt'
				},player);
				
				
				
				clipName.update('<img src="'+clipNameFile+'">');
				
				if(clipNumber && (clipItem = myElements.contentDiv.select('ul.clips li')[clipNumber-1]))
				{
					clipItem.addClassName('active');
				}
			}
			}
			else {
				// Flash player not detected so load images instead
				player.update('<img src="/images/no_flash/demo_reel.png" width="320" height="240">');
				clipName.update('<img src="'+clipNameFile+'">');
			}
		} 
	}.bind(public);
	
	public.playVideo = function(url)
	{
		this.showClip(url);
	}.bind(public);
	
	public.initHomePage = function()
	{
		if(!this.homepageClipClickHandler)
		{
			this.homepageClipClickHandler = function(e)
			{
				var el = e.element();
				
				if(el.tagName.toLowerCase() == "img")
					el = el.up('a');
				
				var href = el.href;
				
				this.showClip(href);
				
				e.stop();
			}.bindAsEventListener(this);
		}
		
		var clips = $$('div#content div.home ul.clips li a');
		clips.each(function(clipLink)
		{
			clipLink.up('li').insert({bottom:new Element('div')});
			clipLink.observe('click',this.homepageClipClickHandler);
		},this);
		this.showClip(clips[0].href);
	}.bind(public);
	
	FastInit.addOnLoad(myInitialize);
	
	return public;
}();

Broadstreet.Page = Class.create({
	initialize: function(name)
	{
		DFTools.console.debug("Creating a new Broadstreet.Page: ",name);
		this.name = name;
		this.content = false;
		this.loaded = false;
	},
	
	getContent: function(callback)
	{
		DFTools.console.debug("Broadstreet.Page.getContent called on ",this);
		if(this.loaded)
		{
			callback(this.content);
		}
		else
		{
			var loadHandler = function(r)
			{
				this.loaded=true;
				this.content=r.responseText;
				callback(r.responseText);
			}.bind(this);
			
			new Ajax.Request(Broadstreet.pageContentTemplate.interpolate(this),{
				onSuccess: loadHandler
			});
		}
	},
	
	display: function()
	{
		DFTools.console.debug("Broadstreet.Page.display called on ",this);
		if(!this.loaded)
		{
			this.getContent(this.display.bind(this));
		}
		else
		{
			Broadstreet.fadeAndUpdateContent(this.content);
			window.location.hash = this.name;
			
			$(Broadstreet.buttonsDivId).select('a').invoke('removeClassName','active');
			
			$(Broadstreet.buttonsDivId).select('a[href='+this.name+']').invoke('addClassName','active');
		}
	}
})

