$().ready(function() {
	//	Fire up Cufon
//	Cufon('.cufon');
//	Cufon('.wonkyButton');
	
	$("a[href^='http']").not("[href*='"+window.location.host+"']").attr('target','_blank').click(function() {		if (typeof(pageTracker) !== 'undefined') {			pageTracker._trackPageview(this.href);		}	});
	//	handle showing and hiding the default value for the search form
	var defaultSearchValue = 'Enter keywords';
	var field = $('#CustomSearchForm_SearchForm_Search');
	if (field.val() == defaultSearchValue) {
		field.addClass('defaultValue');
	}
	$(field).focus(function() {
				if ($(this).val() == defaultSearchValue) {
					$(this).val('').removeClass('defaultValue');
				}
			})
			.blur(function() {
				if ($(this).val() == '') {
					$(this).val(defaultSearchValue).addClass('defaultValue');
				}
			});
	
	/*
		Handle the animated mouseovers for section heading images
			- preload the hover image
			- attach hover event handler
	*/
	if (typeof hoverTitleData == 'object') {
		var imgCache = [];
		for (var id in hoverTitleData) {
		//	var cacheImage = document.createElement('img');
		//	cacheImage.src = hoverTitleData[id];
		//	imgCache.push(cacheImage);
			var image = $('#'+id);
			$(image).data('originalSrc', $(image).attr('src')).hover(function() {
				$(this).attr('src', hoverTitleData[$(this).attr('id')]);
			}, function() {
				$(this).attr('src', $(this).data('originalSrc'));
			});
		}
	}
	
	$('#SideMenu a').hover(function() {
		$(this).css('color', '#FFFFFF');
		Cufon(this);
	}, function() {
		$(this).css('color', '#808080');
		Cufon(this);
	});
	

	//shareForm.init();
});
/*
var shareForm = {
	container: false,
	errorContainer: false,
	responseContainer: false,
	valid: true,
	message: '',
	inputs: false,
	init: function() {
		shareForm.container = $('#SendLinkContainer');
		shareForm.errorContainer = $('#Form_SendLinkForm_error');
		shareForm.inputs = $('#Form_SendLinkForm :input');
		shareForm.responseContainer = $('#SendLinkResponse');
		$('#ShareLink').click(function() { shareForm.toggle(); });
		$('#ShareFormClose').click(function() { shareForm.toggle(); });

		$('#Form_SendLinkForm').submit(function() {
			shareForm.valid = true;
			shareForm.message = '';
			
			
			shareForm.inputs.each(function() {
				if (!shareForm.valid) {
					return;
				}
				var inputName = $(this).attr('name');
				var rules = shareLinkValidation.rules[inputName];
				if (rules == undefined) {
					return;
				}
				
				if (rules.required) {
					if ($(this).val() == '') {
						shareForm.message = shareLinkValidation.messages[inputName].required;
						shareForm.valid = false;
						return;
					}
				}
				if (rules.email) {
				
				
					if ( /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i.test($(this).val()) == false) {
						shareForm.message = shareLinkValidation.messages[inputName].email;
						shareForm.valid = false;
						return;
					}
				}
			});
			
		
			if (!shareForm.valid) {
				shareForm.setErrorMessage(shareForm.message);
				return false;
			}
			
			
			
			var form = this;
			
			var formAction = $(form).attr('action');
			var serialisedForm = $(form).serialize() + '&action=' + formAction + '&ajax=1';
			
			
			shareForm.setErrorMessage('Sending...');

			// Submit the form via ajax
			$.ajax({
				url: formAction,
				type: 'POST',
				data: serialisedForm,
				success: function(response) {
					
					shareForm.setErrorMessage('');
					if (response == 1) {
						var className = 'success';
						var message = 'Email Sent';
						shareForm.inputs.val('');
						$('#SendLinkResponse').click(function() { shareForm.toggle(); });
					} else {
						var className = 'failure';
						var message = 'Sending Failed';
						
						$('#SendLinkResponse').click(function() {
							if (jQuery.browser.msie) {
								$(this).css('display', 'none'); 
							} else {
								$(this).fadeOut('fast');
							}
						});
						
					}
					
					if (jQuery.browser.msie) {
						shareForm.responseContainer.css('display', 'none');
						shareForm.responseContainer.html(message).removeClass('failure').removeClass('success').addClass(className).css('display', 'block');
					} else {
						shareForm.responseContainer.fadeOut('fast', function() { 
							shareForm.responseContainer.html(message).removeClass('failure').removeClass('success').addClass(className).fadeIn('fast'); 
						});
						
					}
					
				}
			});
			
			return false;
		});
		
	},
	toggle: function() {
		if (jQuery.browser.msie) {
			shareForm.container.toggle();
		} else {
			shareForm.container.animate({opacity: 'toggle'}, 400);
		}
		shareForm.errorContainer.html('');
		shareForm.responseContainer.css('display', 'none');
	},
	
	setErrorMessage: function(message) {
		if (jQuery.browser.msie) {
			shareForm.errorContainer.html(message).css('display', 'block');
		} else {
			shareForm.errorContainer.fadeOut('fast', function() { $(this).html(message).fadeIn('fast'); });
		}
	}
	
}

*/

var slideshow = {
	container: false,
	preloaded: [],
	init: function(options) {
		var settings = {
			before: slideshow.onBefore,
			next: '#VideoNextButton',
			prev: '#VideoBackButton',
			resize: 0
		};
		settings = $.extend(settings, options);
		slideshow.container = $('#Slideshow');
		var outerContainer = $('#SlideshowContainer');
		if (settings.resize) {
			$('.slide', slideshow.container).each(function() {
				if ($(this).height() > outerContainer.height()) {
					outerContainer.css('height', $(this).height()+'px');
				}
			});
		}
		
		
		slideshow.container.cycle(settings);
		slideshow.preload();
	},
    onBefore: function(curr, next, opts) {
		var bgImage = $('img', this).attr('src');
		if (!slideshow.preloaded[bgImage]) {
			slideshow.container.cyclePause = 1;
			var img = new Image(); 
			img.src = bgImage;
			$(img).bind('load', function() { 
				slideshow.preloaded[bgImage] = true;
				slideshow.container.cyclePause = 0;
			});
		}       
    },
	preload: function() {
		var slides = slideshow.container.children();
		$(slides).each(function() {
			var bgImage = $('img', this).attr('src');
			var img = new Image(); 
			img.src = bgImage; 
			$(img).bind('load', function() { 
				slideshow.preloaded[bgImage] = true;
			}); 
		});
	}
};


var carousel = {
	carousel: false,
	init: function(options) {
		var settings = {
		};
		settings = $.extend(settings, options);
		carousel.container = $('#Carousel');
		
		swfobject.registerObject("Carousel", "9.0.115", "themes/makeyourmark/flash/expressinstall.swf");
		
		/*
		var flashVars = {
		};
		var params = {
			'wmode': 'transparent',
			'allowscriptaccess': 'sameDomain'
		};
		var attributes = {
		};
		
		swfobject.embedSWF(settings.file, 'Carousel', 639, 340, '9', 'themes/makeyourmark/flash/expressinstall.swf', flashVars, params, attributes);
	*/
	}
};



var videoPage = {
	videos: null,
	currentIndex: 0,
	init:function(inputVideos, startID) {
		var startIndex = 0;
		for (var i = 0, j = inputVideos.length; i < j; i++) {
			var video = inputVideos[i];
			$('a[href='+video.Link+']')
				.addClass('videoLink')
				.data('videoIndex', i)
				.data('videoID', video.ID)
				.click(function() {
					videoPage.play($(this).data('videoIndex'), true);
					return false;
				});
			if (video.ID == startID) {
				startIndex = i;
			}
		}
		videoPage.backButton = $('#VideoBackButton');
		videoPage.backButton.click(function() {
			if (!$(this).hasClass('disabled')) {
				videoPage.previous();
			}
			return false;
		});
		videoPage.nextButton = $('#VideoNextButton');
		videoPage.nextButton.click(function() {
			if (!$(this).hasClass('disabled')) {
				videoPage.next();
			}
			return false;
		});
		videoPage.videos = inputVideos;
		videoPage.play(startIndex, false);
	},
		
	play:function(index, autoplay) {
		var video = videoPage.videos[index];
		var flashVars = {
			'itemId': video.File,
			'goPlay': autoplay
		};
		var params = {
			'wmode': 'transparent',
			'allowscriptaccess': 'sameDomain'
		};
		var attributes = {
		};
		
		var startPlaying = true;
		
		var holder = $('#VideoHolder');
		if (holder.hasClass('bigVideoPlayer')) {
		//	swfobject.embedSWF('themes/makeyourmark/flash/mym-player-480x370.swf', 'Video', 480, 370, '9', 'themes/makeyourmark/flash/mym-player-480x370.swf', flashVars, params, attributes);
			swfobject.embedSWF('themes/makeyourmark/flash/mym-player-480x370.swf', 'Video', 480, 320, '9', 'themes/makeyourmark/flash/mym-player-480x370.swf', flashVars, params, attributes);
		} else {
			swfobject.embedSWF('themes/makeyourmark/flash/mym-player.swf', 'Video', 428, 241, '9', 'themes/makeyourmark/flash/expressinstall.swf', flashVars, params, attributes);
		}
		$('.videoTitleHolder').html(video.Title);
		
		
		videoPage.currentIndex = index;
		// update the next/prev buttons as needed
		if (index == 0) {
			videoPage.backButton.addClass('disabled');
		} else {
			videoPage.backButton.removeClass('disabled');
		}
		if (index == videoPage.videos.length-1) {
			videoPage.nextButton.addClass('disabled');
		} else {
			videoPage.nextButton.removeClass('disabled');
		}
		// track view with analytics
		if (typeof(pageTracker) !== 'undefined') {
			pageTracker._trackPageview(video.File);
		}
	},
	next: function() {
		var nextIndex = videoPage.currentIndex+1;
		if (typeof videoPage.videos[nextIndex] != undefined) {
			videoPage.play(nextIndex, true);
		}
	},
	previous: function() {
		var nextIndex = videoPage.currentIndex-1;
		if (typeof videoPage.videos[nextIndex] != undefined) {
			videoPage.play(nextIndex, true);
		}
	}
}



// menu for IE
sfHover = function() {
	var sfEls = document.getElementById("MainMenu").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() { this.className+=" hover"; }
		sfEls[i].onmouseout=function() { this.className=this.className.replace(new RegExp(" hover\\b"), ""); }
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);

