
var flashPath = 'flash/';
var swfInstallFile = flashPath + 'swfobject-2.1/expressInstall.swf';
var flashVersion = '9';
var flashParams = {wmode: 'opaque', quality: 'high', scale: 'noborder'};
var backgroundDivId = 'background';
var backgroundFlashId = 'backgroundSwf';
var imagesPath = 'images/';
var flashContentsPath = flashPath + 'contents/';
var flowPlayerSwf = flashPath + 'flowplayer/flowplayer-3.0.3.swf';

document.write('<style type="text/css">#' + backgroundDivId + ' {display: none;}</style>');

function trace() {
	for (var i = 0; i < arguments.length; i++) {
		if (typeof console != 'undefined' && console.log) {
			console.log(arguments[i]);
		}
	}
}

window.addEvent('domready', function () {
	new SubMenu('nav', {addShadow: false});
	
	var clientSize = document.getSize();
	
	//background
	var bgSrc = getBackgroundSrc();
	if (bgSrc) {
		bgSrc = bgSrc.join(',');
		var backgroundFlash = new Element('div', {id: backgroundFlashId}).injectInside(backgroundDivId);
		swfobject.embedSWF(flashPath + 'flashbg.swf', backgroundFlashId, '100%', '100%', flashVersion, swfInstallFile, {bg: bgSrc, clientWidth: clientSize.x, clientHeight: clientSize.y}, flashParams);
	}
	$(backgroundDivId).setStyles({
		'display': 'block',
		'background': 'none'
	});
	
	//home page power links
	['powerblock1', 'powerblock2'].each(function(p) {
		var powerLink = $$('#pHome #' + p);
		if (powerLink.length > 0) {
			var copy = powerLink[0].get('text');
			powerLink[0].empty();
			new Element('div', {id: p + 'Swf'}).injectInside(powerLink[0]).set('text', copy);
			swfobject.embedSWF(flashContentsPath + p + '.swf', p + 'Swf', '100%', '100%', flashVersion, swfInstallFile, {}, flashParams);
			powerLink[0].set('styles', {
				'text-indent': 0,
				'background-image': 'none'
			});
		}
	});
	
	//flash elements
	swfobject.embedSWF(flashContentsPath + 'floorplans.swf', 'floorPlansSwf', '100%', '100%', flashVersion, swfInstallFile, {}, flashParams);
	swfobject.embedSWF(flashContentsPath + 'access.swf', 'accessSwf', '100%', '100%', flashVersion, swfInstallFile, {}, flashParams);
	
	//flash movie links
	$$('.videoShowcase a').each(function(e) {
		e.addClass('playMovie');
	});
	new MovieGallery('showreelbox');
	
	//terms and conditions
	new ContentFetcher($$('a.fetch'), {
		adoptOnly: 'copybox',
		onlyAdoptChildren: true,
		fixFlash: true, //hide flash (things are getting too slow)
		onReady: function() {
			new Element('a', {
				'class': 'close',
				'href': 'Javascript:;',
				'text': 'close'
			}).injectInside(this.content);
		}
	});
});

function getBackgroundSrc() {
	var backgroundDiv = $(backgroundDivId);
	if (backgroundDiv) {
		if (typeof bgConfig != 'undefined') {
			var paths = [];
			//get body id
			var bodyId = $$('body')[0].get('id');
			var bodyClasses = $$('body')[0].get('class').split(' ');
			var configKeys = [];
			bodyClasses.each(function(e) {
				configKeys.push(bodyId + '.' + e);
			});
			configKeys.push(bodyId);
			for (i = 0; i < configKeys.length; i++) {
				if (configKeys[i] && typeof bgConfig[configKeys[i]] != 'undefined') {
					var values = typeof bgConfig[configKeys[i]] != 'object' ? [bgConfig[configKeys[i]]] : bgConfig[configKeys[i]];
					
					values.each(function(e) {
						path = /\.swf$/.test(e) ? flashContentsPath : imagesPath;
						path += e;
						paths.push(path);
					});
					break;
				}
			}
			if (paths.length) {
				return paths;
			}
		}
		//get background image
		var bgSrc = backgroundDiv.getStyle('background-image');
		var matches = bgSrc.match(/^url\("?(.*)"?\)$/);
		if (matches.length > 1) {
			return [matches[1]];
		}
	}
	return null;
}

var MovieGallery = new Class({
	Implements: Options,
	target: null,
	links: [],
	options: {
		linksClass: 'playMovie'
	},
	initialize: function(target, options) {
		this.target = $(target);
		if (!this.target) return;
		this.setOptions(options);
		this.setLinks();
	},
	setLinks: function() {
		this.links = [];
		var links = $$('a.' + this.options.linksClass);
		links.each(function(e) {
			if (/\.flv$/.test(e.get('href'))) {
				this.setAction(e);
				this.links.push(e);
			}
		}, this);
	},
	setAction: function(link) {
		link = $(link);
		link.addEvent('click', function(evt) {
			evt.stop();
			flowplayer(this.target, {src: flowPlayerSwf, wmode: 'opaque'}, link.get('href'));
			flowplayer(this.target).play();
		}.bindWithEvent(this, link));
	}
});
