/*
	Site:		CheethamBell JWT
	File:		/assets/scripts/global.js
	Author:		Andrew Disley, Simplified http://simplified.co.uk/ for CheethamBell JWT
	Copyright:	2009 Andrew Disley, Simplified http://simplified.co.uk/ / CheethamBell JWT
	Version:	2009-09-16
----------------------------------------------- */

/*	Global Configuration
----------------------------------------------- */
var $j = jQuery;
var isIE6 = false;
var isSafari = false;

/*	Global Literal Object: Site-wide functions
----------------------------------------------- */
var Global = {

	/* Utility: Browser Tests / Specific Fixes/Hooks */
	utilBrowserTests : function() {

		/* Detect if User Agent is IE6 using object detection, apply fix for background flicker bug */
		if (typeof document.body.style.maxHeight == 'undefined') { isIE6 = true; try { document.execCommand('BackgroundImageCache', false, true); } catch(e) { } }

		/* Detect if User Agent is Safari, add class '.safari' to div#Container */
		if ($j.browser.safari) { isSafari = true; $j('div#Container').addClass('safari'); }

	},

	/* Initialise */ 
	init : function() {

		/* Class Context */
		var cc = this;

		/* Configuration */

		/* jQuery Object References to the Elements this speeds up the DOM */

		cc.utilBrowserTests();

	}

};

/*	Component: .c-listing
----------------------------------------------- */
var CListing = {

	/* jQuery Object References to the Elements this speeds up the DOM */
	jOECListing: null,

	buildCListing : function() {

		/* Class Context */
		var cc = this;

		$j('li p', cc.jOECListing).wrap('<div></div>');

		$j('li', cc.jOECListing).each(function() {

			var jThis = $j(this);
			var jThisP = $j(this).find('p');
			var jThisDiv = $j(this).find('div');
			var jThisSIB = $j('<span class="s-i-b"></span>').css({height: jThisP.height(), paddingTop: '15px'});

			if (!$j.browser.msie) {
				jThisSIB.css('opacity', 0.8);
			}

			jThisP.before(jThisSIB);
			jThisDiv.css({height: jThisP.height(), paddingTop: '15px'});

			jThisP.show();
			jThisSIB.show();

			jThis.hover(function() {
				jThis.fadeTo(400, 1.0);
				jThisDiv.slideDown(300);
			}, function() {
				jThis.fadeTo(500, 0.3);
				jThisDiv.slideUp(300);
			});

		});

		$j('li:not(:first)', cc.jOECListing).fadeTo(300, 0.3);
		$j('li:not(:first) div', cc.jOECListing).hide().slideUp();

	},

	/* Initialise */
	init : function() {

		/* Class Context */
		var cc = this;

		/* jQuery Object References to the Elements this speeds up the DOM */
		cc.jOECListing = $j('.c-listing');

		cc.buildCListing();

	}

};

/*	Component: .c-flowplayer
----------------------------------------------- */
var CFlowplayer = {

	/* Initialise */
	init : function() {
		$j('.c-flowplayer').flowplayer('/media/flowplayer-3.1.3.swf', {
			plugins: {
				controls: {
					url: '/media/flowplayer.controls-3.1.3.swf',
					wmode: 'transparent',
					autoHide: 'always',
					backgroundColor: '#222222',
					backgroundGradient: [0.6,0.3,0,0,0],
					borderRadius: '15',
					bufferColor: '#000000',
					buttonColor: '#000000',
					buttonOverColor: '#E32BC4',
					durationColor: '#FFFFFF',
					height: 24,
					progressColor: '#E32BC4',
					timeColor: '#01DAFF',
					timeBgColor: '#555555',
					tooltipColor: '#222222',
					tooltipTextColor: '#FFFFFF',
					/* Disable all buttons then enable ones we want */
					all:false,
					play: true,
					mute: true,
					scrubber:true
				}
			}
		});

	}

};


/*	HOMEPAGE CLICK TO HIDE
----------------------------------------------- */

				$j(".shift").click(function() 
				{
					$j("#introOverlay")
					.animate({height: 0}, 800,"linear",function()
					{
						$j(this).remove();
                                                $j("p.skipLink").fadeOut("slow");
					})
				});






			
			
			

/*	Form Validation
----------------------------------------------- */
var FormValidation = {

	testField : function(value,pattern) {

		var regExp = new RegExp("^"+pattern+"$","");
		return regExp.test(value);

	},

	/* Initialise */
	init : function() {

		var cc = this;

		$j('form').submit(function() {

			var jForm = $j(this);
			var jFormValName = $j("#name").val();
			var jFormValEmail = $j("#email").val();
			var jFormValMessage = $j("#message").val();
			var jFormErrors = false;
			var jFormErrorsMessage = $j('<div class="c-form-error c-notice"><h3>There was a problem:</h3></div>');
			var jFormErrorsMessageUL = $j('<ul></ul>');

			if (!jFormValName) {
				jFormErrors = true;
				jFormErrorsMessageUL.append('<li>Please enter your name.</li>');
			}

			if (!jFormValEmail) {
/*
				jFormErrors = true;
				jFormErrorsMessageUL.append('Please enter an e-mail address.');
*/
			} else {

				if (!cc.testField(jFormValEmail,'.+@.+\..+')) {
					jFormErrors = true;
					jFormErrorsMessageUL.append('<li>The e-mail address you entered is not valid.</li>');
				}

			}

			if (!jFormValMessage) {
				jFormErrors = true;
				jFormErrorsMessageUL.append('<li>Please enter a message.</li>');
			}

			if (jFormErrors) {
				$j('.c-form-error').remove();
				jFormErrorsMessage.append(jFormErrorsMessageUL);
				$j('legend', jForm).after(jFormErrorsMessage);
				return false;
			}

		});

	}

};

/*	Form Validation
----------------------------------------------- */
var FormValidationInline = {

	testField : function(value,pattern) {

		var regExp = new RegExp("^"+pattern+"$","");
		return regExp.test(value);

	},

	/* Initialise */
	init : function() {

		var cc = this;

		$j('form').submit(function() {

			var jForm = $j(this);
			var jFormFieldName = $j("#name");
			var jFormFieldEmail = $j("#email");
			var jFormFieldMessage = $j("#message");
			var jFormErrors = false;
			var jFormErrorsMessage = $j('<h2 class="form-row-error-title">Please correct the highlighted problems before clicking submit again</h2>');

			/* Remove old error class and messages */
			jFormFieldName.removeAttr('style');
			jFormFieldEmail.removeAttr('style');
			jFormFieldMessage.removeAttr('style');
			$j('.form-row-error-title, .form-row-error').remove();

			if (!jFormFieldName.val()) {
				jFormErrors = true;
				jFormFieldName.css({backgroundColor: '#F7C5E0', border: '1px solid #E63F97'}).after('<span class="form-row-error">Please tell us your name</span>');
			}

			if (!jFormFieldEmail.val()) {
/* if e-mail is required uncomment this section
				jFormErrors = true;
				jFormFieldEmail.css({backgroundColor: '#F7C5E0', border: '1px solid #E63F97'}).after('<span class="form-row-error">Please enter your e-mail address</span>');
*/
			} else {

				if (!cc.testField(jFormFieldEmail.val(),'.+@.+\..+')) {
					jFormErrors = true;
					jFormFieldEmail.css({backgroundColor: '#F7C5E0', border: '1px solid #E63F97'}).after('<span class="form-row-error">Please enter a valid e-mail address</span>');
				}

			}

			if (!jFormFieldMessage.val()) {
				jFormErrors = true;
				jFormFieldMessage.css({backgroundColor: '#F7C5E0', border: '1px solid #E63F97'}).after('<span class="form-row-error">Please enter a message</span>');
			}

			if (jFormErrors) {
				$j('legend', jForm).after(jFormErrorsMessage);
				return false;
			}

		});

	}

};

/*	DOM Ready events
----------------------------------------------- */
$j(function() {

	/* Initialise Global */
	Global.init();

	if ($j('.c-listing').length) {
		CListing.init();
	}

	if ($j('.c-flowplayer').length) {
		CFlowplayer.init();
	}

	if ($j('form').length) {
		FormValidationInline.init();
	}


});



