var main = {
	init:function() {
		// Start the marquee, flash, and nav
		nav.start();
		marquee.start();
		flash.start();
		links.start();
		links.firstLoad();
		forms.load('');
	}
}

var tracker = {
	add:function(page) {
		pageTracker._trackPageview('/' + page);
	}
}

var forms = {
	load:function(page) {
		if (/contact_us.html/.test(window.location.pathname) || page == 'contact_us.html') forms.contact();
		else if (/events_form.html/.test(window.location.pathname) || page == 'events_form.html') forms.events();
	},
	contact:function() {
		$('submit_contact_form').removeEvents('click').addEvent('click', function() {
			var good = '';
			$$('.required').each(function(el) {
				if (el.get('value') == '') good = el.get('rel');
			});
			
			if (good) alert('Warning: The ' + good + ' field is required.');
			else {
				// Make sure the emails match, and are valid
				var email = $('email').get('value');
				var confirm = $('confirmemail').get('value');
				
				if (!forms.validateEmail(email)) {
					alert('Your email address is not valid.');
					return false;
				} else if (email != confirm) {
					alert('Your emails do not match.');
				}
				
				$('loader').removeClass('displaynone');
				new Request({
					url: $('contact_form').get('action'),
					noCache: true,
					onComplete:function() {
						$('contact_us').set('html', this.response.text);
					}
				}).post($('contact_form'));
			}
			return false;
		});
	},
	events:function() {
		$('submit_events_form').removeEvents('click').addEvent('click', function() {
			var good = '';
			$$('.required').each(function(el) {
				if (el.get('value') == '') good = el.get('rel');
			});
			
			if (good) alert('Warning: The ' + good + ' field is required.');
			else {
				// Make sure the emails match, and are valid
				var email = $('email').get('value');
				
				if (!forms.validateEmail(email)) {
					alert('Your email address is not valid.');
					return false;
				}
				
				new Request({
					url: $('events_form').get('action'),
					noCache: true,
					onComplete:function() {
						$('events').set('html', this.response.text);
					}
				}).post($('events_form'));
			}
			return false;
		});
	},
	validateEmail:function(email) {
	   return (email.indexOf(".") > 2) && (email.indexOf("@") > 0);
	}
}

var slideshow = {
	start:function(title, first, dir, data) {
		$('content').set('html', '').grab(
			new Element('h1', {
				'html': title
			})
		).grab(
			new Element('div', {
				'id': 'show',
				'class': 'slideshow'
			}).grab(
				new Element('img', {
					src: first,
					alt: ''
				})
			)
		);
		
		ss = new Slideshow.Push('show', eval(data), {
			controller: true,
			hu: dir,
			duration: 1000,
			delay: 4500
		});
	}
}

var links = {
	start:function() {
		// Any link clicked with the class 'internal'
		
		$$('a.internal').removeEvents('click').addEvent('click', function(e) {
			if (e) e.stop();
			var page = this.get('href');
			var rel = this.get('rel');
			var pdf = this.hasClass('pdf');
			
			if (/^http/.test(page)) page = page.replace(/^.*\/([\w\.\_\-]+)$/, '$1');
			
			//loading.start();
			new Request.HTML({
				url: 'pageserver.php',
				noCache: true,
				data: {
					'page': page,
					'pageonly': 'true',
					'rel': rel
				},
				onComplete: function() {
					var startLinks = true;
					var extraTracker = '';
					if (pdf) {
						$('content_box').addClass('pdf');
						$('content').set('html', '');
						$('pdf_box').set('html', this.response.text).removeClass('displaynone');
					} else {
						$('content_box').removeClass('pdf');
						$('pdf_box').addClass('displaynone').set('html', '');
						
						var data = this.response.text.split('<|>');
						// If the page was Slideshows.php, the Rel wasn't blank, and the received data isn't blank, do a slideshow!
						if (page == 'slideshows.php' && rel != '' && data[3] != '') {
							extraTracker = '?' + rel;
							slideshow.start(data[0], data[1], data[2], data[3]);
							startLinks = false;
						} else {
							$('content').set('html', this.response.text);
							if (rel == 'Contact') forms.contact();
							else if (rel == 'EventsForm') forms.events();
							
							flash.player_check(page);
						}
					}
					
					tracker.add(page + extraTracker);
					
					//loading.stop();
					// there might be links in the loaded content, so rebuild this event
					if (startLinks) {
						links.start();
						forms.load(page);
					}
				}
			}).get();
		});
		
		$$('a.new_window').removeEvents('click').addEvent('click', function(e) {
			if (e) e.stop();
			var page = this.get('href');
			window.open(page);
			return false;
		});
	},
	firstLoad:function() {
		var page = window.location.href.replace(/^.*\/([\w\.\_\-]+)$/, '$1');
		
		flash.player_check(page);
		
		if (page == 'market_outlook.html') $('market_outlook').fireEvent('click');
		
		$('add_to_favorites').addEvent('click', function() {
			if (window.sidebar) { // Firefox
				window.sidebar.addPanel(document.title, location.href, '');
			} else if (window.external) { // IE
				window.external.AddFavorite(location.href, document.title);
			}
		});
	}
}

var loading = {
	start:function() {
		$(document.body).addClass('loading');
	},
	stop:function() {
		$(document.body).removeClass('loading');
	}
}

var nav = {
	start:function() {
		var menu = new MenuMatic({
			id: 'nav'
		});
	}
}

var stock, sports, testimonials, news; // Globals for Marquee

var marquee = {
	start:function() {
		var options = {
			marHeight: 20,
			marWidth: '120%',
			steps: 2,
			speed: stockBarSpeed,
			direction: 'left',
			pauseOnOver: true,
			pauseOnContainerOver: true,
			subId: 'stock-mooquee'
		}
		stock = new mooquee($('stock_marquee'), options);
		options.speed = sportsBarSpeed;
		options.subId = 'sports-mooquee';
		sports = new mooquee($('sports_marquee'), options);
		options.speed = testimonialsSpeed;
		options.marHeight = 320;
		options.marWidth = 180;
		options.direction = 'top';
		options.subId = 'testimonials-mooquee';
		testimonials = new mooquee($('testimonials'), options);
		options.marHeight = 214;
		options.speed = newsSpeed;
		options.subId = 'news-mooquee';
		news = new mooquee($('news'), options);
	}
}

var logoflash, flashnextlogo, mainaudio, secondaryaudio, video; // Globals for Flash

var flash = {
	custom:'',
	start:function() {
		// Top Flash
		logoflash = new SWFObject('swf/logo.swf', 'toplogo', '750', '50', '8', '#082C7B');
		logoflash.addParam('quality', 'high');
		logoflash.write('flash_top_logo');
		
		// Flash next to the logo
		flashnextlogo = new SWFObject('swf/flash_next_logo.swf', 'flashnextlogo', '395', '85', '8');
		flashnextlogo.addParam('quality', 'high');
		flashnextlogo.addParam('wmode', 'transparent');
		flashnextlogo.write('flash_next_logo');
		
		// Right side audio player (seperate from the others, as they play in the pages)
		mainaudio = new SWFObject('swf/audioplayer.swf', 'mainaudio', '170', '25', '8', '');
		mainaudio.addParam('quality', 'high');
		mainaudio.addParam('menu', 'false');
		mainaudio.addParam('wmode', 'transparent');
		mainaudio.addParam('FlashVars', 'playerID=1&amp;' + flash.custom + 'soundFile=audio/Bayer Financial Group - Main.mp3');
		mainaudio.write('main_audio');
	},
	player_check:function(page) {
		flash.audio_player(page);
		flash.video_player(page);
	},
	audio_player:function(page) {
		var file = '';
		
		if (page == 'events.html') file = 'Bayer Financial Group - Straight and True Men 30';
		else if (page == 'structured_planning.html') file = 'Bayer Financial Legal 062608';
		else if (page == 'business_planning.html') file = 'Bayer Financial Business 062608';
		else if (page == 'financial_planning.html') file = 'Bayer Financial Personal 062608';
		else return false;
		
		secondaryaudio = new SWFObject('swf/audioplayer.swf', 'secondaryaudio', '170', '31', '8', '');
		secondaryaudio.addParam('quality', 'high');
		secondaryaudio.addParam('menu', 'false');
		secondaryaudio.addParam('wmode', 'transparent');
		secondaryaudio.addParam('FlashVars', 'playerID=2&amp;' + flash.custom + 'soundFile=audio/' + file + '.mp3');
		secondaryaudio.write('secondary_audio');
	},
	video_player:function(page) {
		var file = '';
		
		if (page == 'video_bayer_baseball_2009.html') file = 'Bayer Baseball 2009';
		else if (page == 'video_bayer_golf_fall_2008.html') file = 'Bayer Financial Group Fall Golf Outing 2008';
		else return false;
		
		video = new SWFObject('swf/videoplayer.swf', 'video', '384', '288', '8', '');
		video.addParam('quality', 'high');
		video.addParam('wmode', 'transparent');
		//video.addParam('flashvars', 'file=../video/' + file + '.flv');
		video.addVariable('file', '../video/' + file + '.flv');
		video.write('video');
	}
}

window.addEvent('domready', main.init);
