
// initialise all the script objects
$(document).ready(function() { 
	headify.init('h1.js', 'div.content');	// relocate H1 element in page
	handleFormElems();			// add some events to forms to remove default text on focus
	normal.init();				// handle normalising elements in the page
	lightBoxEnable.init();		// add lightbox functionality to selected items
	roundButtonEdges.init();	// add rounded edges to buttons that require it
	linkAncestor.init();		// add links to ancestor elements
	vidContainer.init();		// video player
	regForm.init();				// registration form actions and validation
});

// initialise flash game
$(document).ready(function() {
	if ($("#flashGame").length > 0) { 
		var flashvars = {}, params = { wmode:"opaque" }, attributes = {};
		swfobject.embedSWF("/flash/fat_hog.swf", "flashGame", "630", "384", "9.0.0", false, flashvars, params, attributes);
	}
});


var vidContainer = {

	contElem:'div.vidContainer',
	vidElem:'vid', // the id of the vid target minus the css selector
	arr:new Array(),
	
	init:function(){
		if(($(this.contElem).length < 1) || ($('#'+this.vidElem).length < 1)) return;
		
		if($(this.contElem + ' li').length > 0) {
			
			this.buildVideoArray();
			this.displayVideo(0, false);
		} else { // show alternative view if there is only one video
			this.displayVideoAlt();
		}
	},
	
	buildVideoArray:function(){
		$(vidContainer.contElem + ' li').each(function(s, e) {
			vidContainer.arr[s] = {
				num:s,
				type:'video',
				vidURL:$(e).find('a').attr('href'),
				imgURL:$(e).find('img').attr('src'),
				caption:$(e).find('span').html()
			}
			
			$(e).find('a').click(function(el) {
				vidContainer.displayVideo(s, true);
				this.blur();
				el.preventDefault();
			});
			
		});	
	},
	
	displayVideo:function(num, aStart){
		if(!num) { num = parseInt(0, 10); }
		if(!aStart) { aStart = false; }

		var flashvars = {
			file: vidContainer.arr[num].vidURL,
			image: vidContainer.arr[num].imgURL,
			autostart:aStart
		};
		var params = { wmode:'transparent', allowfullscreen:'true', allowscriptaccess:'always' };
		var attributes = {
			name:vidContainer.vidElem
		};
		swfobject.embedSWF("/javascript/player.swf", vidContainer.vidElem, "490", "412", "9.0.0", false, flashvars, params, attributes);
	},
	
	displayVideoAlt:function(){
		if($(this.contElem + ' a').length > 0) {
			var flashvars = {
				file: $(this.contElem + ' a').eq(0).attr('href'),
				image: $(this.contElem + ' a img').eq(0).attr('src'),
				autostart:false
			};
			var params = { wmode:'transparent', allowfullscreen:'true', allowscriptaccess:'always' };
			var attributes = {
				name:vidContainer.vidElem
			};
			swfobject.embedSWF("/javascript/player.swf", vidContainer.vidElem, "630", "412", "9.0.0", false, flashvars, params, attributes);			
		}
	}
}


// find a container, match a link within and apply it to an ancestor (usually the same element as the container)
var linkAncestor = {
	arr:{
		bob:{
			container:'div.sidebarMod div.bob li',
			link:'a.goLink',
			target:'li'
		}, // best of breed large panels
		bobWide:{
			container:'div.mainWide div.bob li',
			link:'a.goLink',
			target:'li'
		}, // best of breed large panels
		homePromo:{
			container:'div.promo',
			link:'a',
			target:'div.promo'			
		}
	},
	init:function(){
		for(var sIndex in linkAncestor.arr) {
			// check the container exists
			if($(linkAncestor.arr[sIndex].container).length > 0) {
				$(linkAncestor.arr[sIndex].container).each(function(s, e) {
					// check the link exists
					var eLink = $(e).find(linkAncestor.arr[sIndex].link);
					if(eLink.length > 0) {
						// check the ancestor exists
						var eAncestor = eLink.parents(linkAncestor.arr[sIndex].target);
						if(eAncestor.length > 0) {
							eAncestor.addClass('makeLink');
							if(eLink.attr('title') != '') { eAncestor.attr({title:eLink.attr('title')}); }
							eAncestor.click(function(el){
								window.location = eLink.attr('href');
								el.preventDefault();
							});
						}
					}
				});
			}
		}	
	}
}

var roundButtonEdges = {
	btnTrgt:'p.roundedButton a',
	init:function(){
		if($(this.btnTrgt).length < 1) return;
		$(this.btnTrgt).each(function(s, e) {
			
			// fix for stupid IE6 bug with absolute position bottom being 1px out depending on whether the container is an odd or even height!
			elHeight = $(e).height();
			if(elHeight % 2 == 0) { $(e).height(elHeight + 1); }
			
			var spans = '';
			if($(e).parent().hasClass('tr')) {
				spans += '<span class="tr" style="top: 0; left: ' + (parseInt($(e).parent().width(), 10)-5) + 'px;">&nbsp;</span>';
			}
			if($(e).parent().hasClass('br')) {
				spans += '<span class="br" style="bottom: 0; left: ' + (parseInt($(e).parent().width(), 10)-5) + 'px;">&nbsp;</span>';
			}
			if($(e).parent().hasClass('tl')) {
				spans += '<span class="tl" style="top: 0; left: 0;">&nbsp;</span>';
			}
			if($(e).parent().hasClass('bl')) {
				spans += '<span class="bl" style="bottom: 0; left: 0;">&nbsp;</span>';
			}
			$(e).append(spans);
		});
	}
}

// takes an input header element, looks for that and tries to prepend it to an ancestor
var headify = {
	init:function(elem, prnt){
		$(elem).eq(0).addClass('moved'); // this is only added so we can target it with SIFR later if required
		if($(elem).length < 1) return;
		var el = $(elem).eq(0).parents(prnt);
		el.prepend($(elem).eq(0));
	}
}

function handleFormElems(){
	if($('#searchField').length > 0) {
		$('#searchField').focus(function(){ 
			if(this.value == 'Search the site') { this.value = ''; } });
		$('#searchField').blur(function(){ if(this.value == '') { this.value = 'Search the site'; } });
	}
}

// looks for elements to normalise in terms of height - currently doesn't handle nested elements!
var normal = {
	nPrnt:'.normaliser', 	// normalise parent group 
	nNode:'.normalise', 	// normalise node
	
	init:function(){
		$(normal.nPrnt).each(function(s, e){
			var normalHeight = 0;
			$(e).find(normal.nNode).each(function(x, el){
				if(parseInt($(el).height(), 10) > normalHeight) { 
					normalHeight = parseInt($(el).height(), 10); 
				}
			});
			$(e).find(normal.nNode).height(normalHeight);
		});
	}
}

var lightBoxEnable = {
	grouping:'.lbox',
	item:'a[rel="imageshow"]',
	init:function() {
		if($(lightBoxEnable.grouping).length < 1) return;
		$(lightBoxEnable.grouping).each(function(s, e) {
			$(e).find('a[rel="imageshow"]').colorbox({transition:'fade', speed:500, current:''});
		});
	}
	
}

var regForm = {
	
	formID:'register',
	
	init:function(){
		if($('#'+regForm.formID).length < 1) return;
		regForm.doVadlidate();
		// allow html link to do the form submission - might want to change this to a submit button?
		$('#'+regForm.formID).find('p.roundedButton').click(function(e) {
			$('#'+regForm.formID).submit();
			e.preventDefault();
		});
	},
	
	doVadlidate:function(){
		$('#'+regForm.formID).validate({
			//set the rules for the field names
			rules: {
				title: "required",
				forename: {
					required: true, 
					maxlength: 100
				},
				surname: {
					required: true,
					maxlength: 100
				},
				username: {
					required: true,
					email: true,
					maxlength: 100
				},
				password: {
					required: true,
					minlength: 6,
					maxlength: 100
				},
				confirmpassword: {
					required: true,
					minlength: 6,
					equalTo: "#password"
				},
				optin_t1_m1: "required",
				agree_t_cs: "required"
			},
			//set messages to appear inline
			messages: 
			{
				title: "Please select your title",
				forename: 
				{ 
					required: "Please enter your forename",
					maxlength: "Your forename must be less than 100 characters"
				},
				surname:
				{ 
					required: "Please enter your surname",
					maxlength: "Your surname must be less than 100 characters"
				},
				username: 
				{
					required: "Please enter your email address",
					email: "Please enter a valid E-mail address",
					maxlength: "Your E-Mail must be less than 100 characters"
				},
				password: 
				{
					required: "Please enter your password",
					minlength: "Your password must be at least 6 characters long",
					maxlength: "Your password must be less than 100 characters"
				},
				confirmpassword: 
				{
					required: "Please confirm your password",
					minlength: "Your password must be at least 6 characters long",
					equalTo: "Please enter the same password as above"
				},
				optin_t1_m1: "To register successfully you will need to be able to receive email notifiations sent from Fat Hog",
				agree_t_cs: "You must acknowledge that you have read and understood the Privacy Policy and Terms &amp; Conditions to register with us."
			},
			
			errorPlacement: function(error, element) {
				// do some custom rules on the error message based on type...
				var prnt = element.parent().get(0).tagName.toUpperCase();
				if(prnt == 'TD' || prnt == 'LABEL') {
					element.parents('div.fields').append(error);
				} else {
					element.parent().append(error);
				}
			}
		});	
				
		// check if confirmation password is still valid after password changed
		$("#password").blur(function() {
			$("#confirmpassword").valid();
		});
	}
}











	