var Site = {

	start: function() {
		Site.setupExternalLinks();
		Site.setupToolTips();

		Site.setupLinkedInPopup();
		Site.setupEmailPopup();

		if ($('contact-form')) Site._attachContactRequirements();
		if ($('careers-form')) Site._attachCareersRequirements();
		if ($('more-news-button')) Site._attachMoreNewsRequirements();
		if ($('contact-side')) Site._attachContactSideRequirements();
		if ($$('body.article #content-side').length ||
			$$('body.article-listing #content-side').length) {
			Site._attachArticleSidebarRequirements();
		}

		$$('.enlighten-form').each(function(el){
			Site._attachEnlightenFormRequirements(el);
		});
	},

	setupExternalLinks: function() {
		// Automatically make external links open in new windows
		if (window.location.hostname.indexOf('brightlabs.com.au') >= 0) {
			host = 'brightlabs.com.au';
		} else {
			host = window.location.hostname;
		}

		$$('a').each(function(el){
			if (el.href.indexOf(host) >= 0) return; // internal link
			if (el.href.indexOf('javascript:') == 0) return; // eg. javascript:void(0);

			el.setProperty('target', '_blank');
		});

		// Also set links with class="external", such as the profile PDF
		$$('a.external').each(function(el){
			el.setProperty('target', '_blank');
		});
	},

	setupToolTips: function() {
		// Don't apply tooltips to article page - it overlaps the prev/next links
		if ($$('body.article').length > 0) {
			return;
		}

		$('suckerfish2_menu_0').store('tip:text', 'Return to the homepage');
		$('suckerfish2_menu_1').store('tip:text', 'A bit about our story, team, clients and life on the bright side');
		$('suckerfish2_menu_2').store('tip:text', 'Our services and expertise covered in-depth');
		$('suckerfish2_menu_3').store('tip:text', 'Online Portfolio of work');
		$('suckerfish2_menu_4').store('tip:text', 'Manage your online presence easily');
		$('suckerfish2_menu_5').store('tip:text', 'Join the team at Brightlabs');
		$('suckerfish2_menu_6').store('tip:text', 'Read the latest news from Brightlabs');
		$('suckerfish2_menu_7').store('tip:text', 'What can we do for your business?');

		var menuTips = new Tips($$('.suckerfish2 li'), {
			fixed: true,
			offsets: {x: 11, y: 36 }
		});
	},

	setupLinkedInPopup: function() {
		var elems = $$('#info-linkedin a'); // There should ony be one

		if (elems[0]) {
			elems[0].addEvent('click', function(event) {
				window.open(this.href, 'linkedin', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=600,height=400');
				return false;
			});
		}
	},

	setupEmailPopup: function() {
		var elems = $$('#info-email a'); // There should ony be one

		if (elems[0]) {
			elems[0].addEvent('click', function(event) {
				window.open(this.href, 'email', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=600,height=400');
				return false;
			});
		}
	},

	_attachEnlightenFormRequirements: function(form) {
		// First name
		$(form.elements['cm-name']).setProperty('rel', 'required');
		$(form.elements['cm-name']).setProperty('fieldname', 'Name');
		$(form.elements['cm-name']).setProperty('valmsg', 'Please enter your name.');

		// Email address
		$(form.elements['cm-hiykdr-hiykdr']).setProperty('rel', 'required email');
		$(form.elements['cm-hiykdr-hiykdr']).setProperty('fieldname', 'Email');
		$(form.elements['cm-hiykdr-hiykdr']).setProperty('valmsg', 'Please enter your email address.');

		var default_values = {
			'cm-name': 'Name',
			'cm-hiykdr-hiykdr': 'Email Address'
		};

		// Focus/blur events
		for (field in default_values) {
			$(form.elements[field]).addEvent('focus', function() {
				if (this.value == default_values[this.name]) {
					this.value = '';
				}
			});

			$(form.elements[field]).addEvent('blur', function() {
				if (this.value == '') {
					this.value = default_values[this.name];
				}
			});
		}

		$(form).addEvent('submit', function() {
			// Empty the inputs if they contain the default values
			for (field in default_values) {
				$(form).elements[field].fireEvent('focus');
			}

			// Now validate...
			result = validateForm(this);

			if (result) {
				return true;
			}

			// If the validation failed, set the default values back
			for (field in default_values) {
				$(form).elements[field].fireEvent('blur');
			}
			return false;
		});
	},

	_attachContactSideRequirements: function() {
		// First name
		$('contact-side-name').setProperty('rel', 'required');
		$('contact-side-name').setProperty('fieldname', 'Name');
		$('contact-side-name').setProperty('valmsg', 'Please enter your name.');

		// Email address
		$('contact-side-email').setProperty('rel', 'required email');
		$('contact-side-email').setProperty('fieldname', 'Email');
		$('contact-side-email').setProperty('valmsg', 'Please enter your email address.');

		// Enquiry
		$('contact-side-enquiry').setProperty('rel', 'required');
		$('contact-side-enquiry').setProperty('fieldname', 'Enquiry');
		$('contact-side-enquiry').setProperty('valmsg', 'Please enter your enquiry.');

		var default_values = {
			'contact-side-name': 'Your Name',
			'contact-side-email': 'Email Address',
			'contact-side-enquiry': 'Enquiry'
		};

		// Focus/blur events
		for (field in default_values) {
			$('contact-side').elements[field].addEvent('focus', function() {
				if (this.value == default_values[this.id]) {
					this.value = '';
				}
			});

			$('contact-side').elements[field].addEvent('blur', function() {
				if (this.value == '') {
					this.value = default_values[this.id];
				}
			});
		}

		$('contact-side').addEvent('submit', function() {
			// Empty the inputs if they contain the default values
			for (field in default_values) {
				$('contact-side').elements[field].fireEvent('focus');
			}

			// Now validate...
			result = validateForm(this);

			if (result) {
				return true;
			}

			// If the validation failed, set the default values back
			for (field in default_values) {
				$('contact-side').elements[field].fireEvent('blur');
			}
			return false;
		});
	},

	newsSliders: new Array(),

	_attachMoreNewsRequirements: function() {
		// Site.newsSliders will be an array of sliders - one for each news item
		// except the first ten which are always shown.
		// The first ten don't have class="hidden-article" so they're not included here.

		$$('.hidden-article').each(function(item){
			Site.newsSliders.push(new Fx.Slide(item).hide());
		});

		$('more-news-button').addEvent('click', function() {
			var i = 0;
			var num_done = 0;

			// Loop through Site.newsSliders
			// The loop stops once we've changed ten items or once the end of the array is reached

			while (num_done < 10 && i < Site.newsSliders.length) {
				// Only process sliders that are still closed

				if (!Site.newsSliders[i].open) {
					Site.newsSliders[i].toggle();
					num_done++;
				}
				i++;
			}

			// Update article count
			Site.updateArticleDisplayCount(i+10);

			// Hide More News button if there's no more news
			if (i == Site.newsSliders.length) {
				$('more-news').style.display = 'none';
			}

		});

		// Initialise article count
		Site.updateArticleDisplayCount(10);
	},

	updateArticleDisplayCount: function(num_displayed) {
		$$('.news-count').each(function(el){
			el.innerHTML = 'Displaying '+num_displayed+' of '+(Site.newsSliders.length+10)+' articles';
		});
	},

	_attachContactRequirements: function() {
		$('contact-name').setProperty('rel', 'required');
		$('contact-name').setProperty('fieldname', 'Name');
		$('contact-name').setProperty('valmsg', 'Please enter your name.');

		$('contact-email').setProperty('rel', 'required email');
		$('contact-email').setProperty('fieldname', 'Email');
		$('contact-email').setProperty('valmsg', 'Please enter a contact email address.');

		$('contact-phone').setProperty('rel', 'required');
		$('contact-phone').setProperty('fieldname', 'Phone');
		$('contact-phone').setProperty('valmsg', 'Please enter a contact phone number.');

		$('contact-comments').setProperty('rel', 'required');
		$('contact-comments').setProperty('fieldname', 'Comments');
		$('contact-comments').setProperty('valmsg', 'Please enter your comments or request.');

		$('contact-captcha').setProperty('rel', 'required');
		$('contact-captcha').setProperty('fieldname', 'Verification code');
		$('contact-captcha').setProperty('valmsg', 'Please enter the verification code.');

		$('contact-form').addEvent('submit', function(){
			return validateForm(this);
		});
	},

	_attachCareersRequirements: function() {
		$('careers-name').setProperty('rel', 'required');
		$('careers-name').setProperty('fieldname', 'Name');
		$('careers-name').setProperty('valmsg', 'Please enter your name.');

		$('careers-email').setProperty('rel', 'required email');
		$('careers-email').setProperty('fieldname', 'Email');
		$('careers-email').setProperty('valmsg', 'Please enter a contact email address.');

		$('careers-phone').setProperty('rel', 'required');
		$('careers-phone').setProperty('fieldname', 'Phone');
		$('careers-phone').setProperty('valmsg', 'Please enter a contact phone number.');

		$('careers-comments').setProperty('rel', 'required');
		$('careers-comments').setProperty('fieldname', 'Comments');
		$('careers-comments').setProperty('valmsg', 'Please enter your comments or request.');

		$('careers-captcha').setProperty('rel', 'required');
		$('careers-captcha').setProperty('fieldname', 'Verification code');
		$('careers-captcha').setProperty('valmsg', 'Please enter the verification code.');

		$('careers-form').addEvent('submit', function(){
			return validateForm(this);
		});
	},

	_attachArticleSidebarRequirements: function() {
		// This function makes the sidebar follow you as you scroll

		// IE6 doesn't support this, of course.
		if (Browser.Engine.trident4) {
			return;
		}

		// sidebar_top_offset is the "natural" Y offset of the sidebar
		sidebar_top_offset = $('content-side').getPosition().y;

		// The minimum distance the sidebar can be positioned from the bottom of the page (ie. footer height + some padding)
		sidebar_bottom_offset = 250;

		window.addEvent('scroll', function() {
			// Calculate max top offset
			var max_top_offset = window.getScrollSize().y - sidebar_bottom_offset - ($('content-side').getStyle('height').replace('px','')*1 + 30); // 30px for padding-top

			if (window.getScroll().y >= max_top_offset) {
				// We're looking at the footer and need to stop the sidebar from following
				$('content-side').style.position = 'absolute';
				$('content-side').style.top = max_top_offset+'px';
			} else if (window.getScroll().y > sidebar_top_offset) {
				// We're halfway down, so make the sidebar follow
				$('content-side').style.position = 'fixed';
				$('content-side').style.top = '0';
			} else if (window.getScroll().y <= sidebar_top_offset) {
				// We're near the top, so make the sidebar sit naturally
				$('content-side').style.position = 'absolute';
				$('content-side').style.top = sidebar_top_offset+'px';
			}
		});
	}
};
window.addEvent('domready', Site.start);
