speedOptions = 100;
speedOpen = 500;
speedClose = 500;
speedFade = 250;
speedScroll = 500;
currentKeywords = '';

/** Pseudo URLs for GA.  Note that panels just log a hit against /[panelID] */
googleAnalyticsPseudoURLs = {
	storeSearch: '/wheretobuy/search',
	storeClickthrough: '/wheretobuy/clickthrough'
};

$JQ = jQuery.noConflict();

// document load
$JQ(document).ready(function() {
	FITFLOP.GENERIC.initialise();
});

// window resize
$JQ(window).resize(function() {
	FITFLOP.GENERIC.resizeFlash();
});


// *******************************************************************************************


FITFLOP = {
};


// *******************************************************************************************


FITFLOP.GENERIC = {

	initialise: function() {
		FITFLOP.GENERIC.movePanelsInSource();
		FITFLOP.GENERIC.resizeFlash();
		FITFLOP.GENERIC.bindDocument();
		FITFLOP.HOME.homeTestimonials();
		FITFLOP.GENERIC.bindAfterContentRefresh();
		if(typeof panelToOpenAtLoad != "undefined") {
			$JQ('a').each(function() {
				var eachElement = $JQ(this);
				if (eachElement.attr('class') == panelToOpenAtLoad) {
					eachElement.trigger('click');
					return false;
				}
			});
		}
		$JQ('p.linkToFrance').click(function(){
			pageTracker._trackPageview('/belgium-to-france');
			pageTracker._trackEvent('belgium', 'belgium-to-france');
		});
		$JQ('p.linkToNetherlands').click(function(){
			pageTracker._trackPageview('/belgium-to-netherlands');
			pageTracker._trackEvent('belgium', 'belgium-to-netherlands');
		});	
		
	},

	bindAfterContentRefresh: function() {
		FITFLOP.GENERIC.handleNewWindowLinks();
		FITFLOP.PAGE.bindInfoPopups();
		FITFLOP.PAGE.bindTitleControls();
		FITFLOP.PAGE.bindImageControls();
		FITFLOP.PAGE.bindTextControls();
		FITFLOP.PAGE.bindProductsControls();
		FITFLOP.PAGE.bindProductControls();
		FITFLOP.PAGE.bindProductImages();
		FITFLOP.STORE.bindFinderSubmit();
		FITFLOP.STORE.bindFinderBack();
		FITFLOP.STORE.bindCountrySelect();
		FITFLOP.STORE.countryChanged();
		FITFLOP.STORE.bindClickthroughLinks();
		FITFLOP.PANEL.bindPanelControls();
		FITFLOP.REFINE.repurposeEnterSubmit();
		FITFLOP.REFINE.bindRefineItems();
		FITFLOP.REFINE.bindPagination();
		FITFLOP.FORM.prepopulateFieldsText('fieldPrepopulate');
		FITFLOP.FORM.panelFieldsForAjaxSubmit();
		FITFLOP.FORM.commentsFieldsForAjaxSubmit();
		FITFLOP.FORM.sendFieldsForAjaxSubmit();
		FITFLOP.FORM.bindCharacterCount();
	},

	movePanelsInSource: function() {
		if ($JQ('.banner .sectionInside .bannerInside').size() > 0) {
			var divToAppend = $JQ('.banner .sectionInside .bannerInside');
		} else {
			var divToAppend = $JQ('.banner .sectionInside');
		}

		var htmlLogin = FITFLOP.GENERIC.getHTML($JQ('#panelLogin'));
		var htmlRegister = FITFLOP.GENERIC.getHTML($JQ('#panelRegister'));
		var htmlSubscribe = FITFLOP.GENERIC.getHTML($JQ('#panelSubscribe'));
		var htmlWrite = FITFLOP.GENERIC.getHTML($JQ('#panelWrite'));
		var htmlStoreFinder = FITFLOP.GENERIC.getHTML($JQ('#panelStoreFinder'));
		var htmlInternational = FITFLOP.GENERIC.getHTML($JQ('#panelInternational'));

		//$JQ('#panelLogin').remove();
		//$JQ('#panelRegister').remove();
		//$JQ('#panelInternational').remove();
		$JQ('#panelSubscribe').remove();
		$JQ('#panelWrite').remove();
		$JQ('#panelStoreFinder').remove();

		//divToAppend.append(htmlLogin);
		//divToAppend.append(htmlRegister);
		//divToAppend.append(htmlInternational);
		divToAppend.append(htmlSubscribe);
		divToAppend.append(htmlWrite);
		$JQ('input.star_preloaded').each(function() {
			var eachElement = $JQ(this);
			eachElement.removeClass('star_preloaded');
			eachElement.addClass('star');
		});
		$JQ('input.star').rating();
		$JQ('input.star').rating('select','5');
		divToAppend.append(htmlStoreFinder);
	},

	moveHTML: function(theElement, targetElement) {
		var theHTML = FITFLOP.GENERIC.getHTML(theElement);
		theElement.remove();
		targetElement.prepend(theHTML);
	},

	getHTML: function(theElement) {
		var HTML = '<div class="' + theElement.attr('class') + '" id="' + theElement.attr('id') + '" style="' + theElement.attr('style') + '">' + theElement.html() + '</div>';
		return HTML;
	},

	bindDocument: function() {
		$JQ(document).unbind('mousedown');
		$JQ(document).bind('mousedown', function(click) {
			var elementClicked = $JQ(click.target);
			var elementTag = elementClicked.attr('tagName').toLowerCase();
			var ignoreClick = 0;
			if ((elementTag == 'a') || (elementClicked.parents('a').size() > 0)) {
				ignoreClick = 1;
			}
			if ((elementClicked.parents('.panelOutside').size() == 0) && (ignoreClick == 0)) {
				FITFLOP.PANEL.panelCloseAll();
			}
			$JQ('.infoPopupInside').fadeOut(speedFade);
		});
	},

	handleNewWindowLinks: function() {
		$JQ('.targetBlank').each(function() {
			$JQ(this).attr('target', '_blank');
		});
	},

	getTotalWidth: function(elementToInspect) {
		var totalWidth = elementToInspect.width();
		totalWidth += parseInt(elementToInspect.css('padding-left').replace('px'), '') + parseInt(elementToInspect.css('padding-right').replace('px'), '');
		totalWidth += parseInt(elementToInspect.css('margin-left').replace('px'), '') + parseInt(elementToInspect.css('margin-right').replace('px'), '');
		if (!isNaN(parseInt(elementToInspect.css('borderLeftWidth').replace('px'), '') + parseInt(elementToInspect.css('borderRightWidth').replace('px'), ''))) {
			totalWidth += parseInt(elementToInspect.css('borderLeftWidth').replace('px'), '') + parseInt(elementToInspect.css('borderRightWidth').replace('px'), '');
		}
		return totalWidth;
	},

	getTotalHeight: function(elementToInspect) {
		var totalHeight = elementToInspect.height();
		totalHeight += parseInt(elementToInspect.css('padding-top').replace('px'), '') + parseInt(elementToInspect.css('padding-bottom').replace('px'), '');
		totalHeight += parseInt(elementToInspect.css('margin-top').replace('px'), '') + parseInt(elementToInspect.css('margin-bottom').replace('px'), '');
		if (!isNaN(parseInt(elementToInspect.css('borderTopWidth').replace('px'), '') + parseInt(elementToInspect.css('borderBottomWidth').replace('px'), ''))) {
			totalHeight += parseInt(elementToInspect.css('borderTopWidth').replace('px'), '') + parseInt(elementToInspect.css('borderBottomWidth').replace('px'), '');
		}
		return totalHeight;
	},

	showAjaxLoading: function() {
		$JQ('.ajaxLoading').css('display', 'inline');
	},

	hideAjaxLoading: function() {
		$JQ('.ajaxLoading').css('display', 'none');
	},

	resizeFlash: function() {
		var browserWidth = $JQ(window).width();
		$JQ('.flash .sectionInside').css('overflow', 'hidden');
		/*if (browserWidth > 995) {
			if (browserWidth > 1280) {
				browserWidth = 1280;
			}
			$JQ('.flash .sectionInside').css('width', browserWidth+'px');
		}*/
		if (browserWidth > 995) {
			browserWidth = 995;
			$JQ('.flash .sectionInside').css('width', browserWidth+'px');
		}
	},

	getBrowserDimensions: function() {
		var browserDimensions = new Array();
		browserDimensions = {
			width: 0,
			height: 0
		}
		// the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
		if (typeof window.innerWidth != 'undefined') {
			browserDimensions.width = window.innerWidth,
			browserDimensions.height = window.innerHeight
		}
		// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
		else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth !='undefined' && document.documentElement.clientWidth != 0) {
			browserDimensions.width = document.documentElement.clientWidth,
			browserDimensions.height = document.documentElement.clientHeight
		}
		// older versions of IE
		else {
			browserDimensions.width = document.getElementsByTagName('body')[0].clientWidth,
			browserDimensions.height = document.getElementsByTagName('body')[0].clientHeight
		}
		return browserDimensions;
	}
};


// *******************************************************************************************


FITFLOP.HOME = {

	homeTestimonials: function() {
		var itemsAvailable = $JQ('.contentHome .quotedRow .quoted').size();
		var itemShowing = 1;
		var rowWidth = 0;
		if (itemsAvailable > 0) {
			$JQ('.contentHome .columnTestimonials .columnBody').scrollTo('0px', speedScroll, {axis:'x'});
			$JQ('.contentHome .quotedRow .quoted').each(function() {
				rowWidth += FITFLOP.GENERIC.getTotalWidth($JQ(this));
			});
			$JQ('.contentHome .quotedRow').css('width', rowWidth + 'px');
			$JQ('.contentHome .quotedRow .quoted').css('display', 'inline');
			if (itemsAvailable > 0) {
				$JQ('.contentHome .columnTestimonials .pagination .next').addClass('active');
			}
		}

		$JQ('.contentHome .columnTestimonials .pagination .previous').unbind('click');
		$JQ('.contentHome .columnTestimonials .pagination .previous').bind('click', function() {
			$JQ('.contentHome .columnTestimonials .pagination .next').addClass('active');
			if (itemShowing > 1) {
				var currentQuoted = $JQ('.contentHome .quotedRow .quoted:nth(' + (itemShowing-1) + ')');
				itemShowing--;
				if (itemShowing == 1) {
					$JQ('.contentHome .columnTestimonials .pagination .previous').removeClass('active');
				}
				$JQ('.contentHome .columnTestimonials .columnBody').scrollTo('-=' + FITFLOP.GENERIC.getTotalWidth(currentQuoted) + 'px', speedScroll, {axis:'x'});
			}
			return false;
		});

		$JQ('.contentHome .columnTestimonials .pagination .next').unbind('click');
		$JQ('.contentHome .columnTestimonials .pagination .next').bind('click', function() {
			$JQ('.contentHome .columnTestimonials .pagination .previous').addClass('active');
			if (itemShowing < itemsAvailable) {
				var currentQuoted = $JQ('.contentHome .quotedRow .quoted:nth(' + (itemShowing-1) + ')');
				itemShowing++;
				if (itemShowing == itemsAvailable) {
					$JQ('.contentHome .columnTestimonials .pagination .next').removeClass('active');
				}
				$JQ('.contentHome .columnTestimonials .columnBody').scrollTo('+=' + FITFLOP.GENERIC.getTotalWidth(currentQuoted) + 'px', speedScroll, {axis:'x'});
			}
			return false;
		});

		$JQ('')
	}

};


// *******************************************************************************************


FITFLOP.PAGE = {

	bindInfoPopups: function() {
		$JQ('.infoPopup > a').unbind('click');
		$JQ('.infoPopup > a').bind('click', function() {
			var infoToOpen = $JQ(this).siblings('.infoPopupInside');
			count = 0;
			$JQ('.infoPopupInside').each(function() {
				var eachElement = $JQ(this);
				count++;
				FITFLOP.PAGE.setZindex(eachElement, 0);
				if (count == $JQ('.infoPopupInside').size()) {
					FITFLOP.PAGE.setZindex(infoToOpen, 1);
					infoToOpen.fadeIn(speedFade);
				}
			});
			return false;
		});

		$JQ('.infoPopupInside a').unbind('click');
		$JQ('.infoPopupInside a').bind('click', function() {
			return false;
		});
	},

	setZindex: function(theElement, elementState) {
		theElement.parents().each(function() {
			var eachElement = $JQ(this);
			if ((eachElement.css('position') == 'relative') && (eachElement.attr('class').indexOf('sectionOutside') < 0)) {
			//if (eachElement.css('position') == 'relative') {
				if (elementState == 1) {
					eachElement.css('zIndex','99999');
				} else {
					eachElement.css('zIndex','0');
				}
			}
		});
	},

	bindTitleControls: function() {
		$JQ('.panelEmbedded .titleToggle').unbind('click');
		$JQ('.panelEmbedded .titleToggle').bind('click', function() {
			var elementClicked = $JQ(this);
			var divToOpen = elementClicked.parent().parent().parent().siblings('.panelBody');
			var imageToToggle = $JQ('img', elementClicked.parent().parent().siblings('.headingRight'));
			FITFLOP.PAGE.actionImageControls(imageToToggle, divToOpen);
			return false;
		});

		$JQ('.resultsHeading .titleToggle').unbind('click');
		$JQ('.resultsHeading .titleToggle').bind('click', function() {
			var elementClicked = $JQ(this);
			var divToOpen = elementClicked.parent().parent().parent().siblings('.resultsList');
			var imageToToggle = $JQ('img', elementClicked.parent().parent().siblings('.headingRight'));
			FITFLOP.PANEL.panelCloseAll();
			FITFLOP.PAGE.actionImageControls(imageToToggle, divToOpen);
			return false;
		});

		$JQ('.commentsHeading .titleToggle').unbind('click');
		$JQ('.commentsHeading .titleToggle').bind('click', function() {
			var elementClicked = $JQ(this);
			var divToOpen = elementClicked.parent().parent().parent().siblings('.commentsList');
			var imageToToggle = $JQ('img', elementClicked.parent().parent().siblings('.headingRight'));
			FITFLOP.PANEL.panelCloseAll();
			FITFLOP.PAGE.actionImageControls(imageToToggle, divToOpen);
			return false;
		});

		$JQ('.addHeading .titleToggle').unbind('click');
		$JQ('.addHeading .titleToggle').bind('click', function() {
			var elementClicked = $JQ(this);
			var divToOpen = elementClicked.parent().parent().parent().siblings('.addList');
			var imageToToggle = $JQ('img', elementClicked.parent().parent().siblings('.headingRight'));
			FITFLOP.PANEL.panelCloseAll();
			FITFLOP.PAGE.actionImageControls(imageToToggle, divToOpen);
			return false;
		});

		$JQ('.sendHeading .titleToggle').unbind('click');
		$JQ('.sendHeading .titleToggle').bind('click', function() {
			var elementClicked = $JQ(this);
			var divToOpen = elementClicked.parent().parent().parent().siblings('.sendList');
			var imageToToggle = $JQ('img', elementClicked.parent().parent().siblings('.headingRight'));
			FITFLOP.PANEL.panelCloseAll();
			FITFLOP.PAGE.actionImageControls(imageToToggle, divToOpen);
			return false;
		});

		$JQ('.groupNews .itemHeading .titleToggle').unbind('click');
		$JQ('.groupNews .itemHeading .titleToggle').bind('click', function() {
			var elementClicked = $JQ('.itemToggle a.openSamePage', $JQ(this).parent().parent().parent().siblings('.itemLink'));
			var divToOpen = elementClicked.parent().parent().siblings('.itemFull');
			FITFLOP.PANEL.panelCloseAll();
			FITFLOP.PAGE.actionTextControls(elementClicked, divToOpen);
			return false;
		});

		$JQ('.groupTestimonials .itemHeading .titleToggle').unbind('click');
		$JQ('.groupTestimonials .itemHeading .titleToggle').bind('click', function() {
			var elementClicked = $JQ('.itemToggle a.openSamePage', $JQ(this).parent().parent().parent().parent().siblings('.itemLink'));
			var divToOpen = $JQ('.itemFull', elementClicked.parent().parent().siblings('.itemRight'));
			FITFLOP.PANEL.panelCloseAll();
			FITFLOP.PAGE.actionTextControls(elementClicked, divToOpen);
			return false;
		});


		$JQ('.groupFaqs .itemHeading .titleToggle').unbind('click');
		$JQ('.groupFaqs .itemHeading .titleToggle').bind('click', function() {
			var elementClicked = $JQ('.headingToggle a.openSamePage', $JQ(this).parent().parent().siblings('.headingRight'));
			if (elementClicked.css('display') == 'none') {
				elementClicked = $JQ('.itemToggle a', $JQ(this).parent().parent().parent().siblings('.itemFull'));
				var openControl = elementClicked.parent().parent().parent().siblings('.itemHeading');
				var divToClose = elementClicked.parent().parent().parent();
				$JQ('.headingRight a.openSamePage', openControl).css('display', 'inline');
				FITFLOP.PANEL.panelCloseAll();
				divToClose.slideUp(speedClose);
			} else {
				var divToOpen = elementClicked.parent().parent().parent().siblings('.itemFull');
				var listsClosed = 0;
				elementClicked.css('display', 'none');
				FITFLOP.PANEL.panelCloseAll();
				$JQ('.groupFaqs .itemFull').each(function() {
					var listItem = $JQ(this);
					var listItemText = $JQ('.headingToggle a.openSamePage', listItem.siblings('.itemHeading'));
					if (listItem.css('display') != 'none') {
						listsClosed = 1;
						listItemText.css('display', 'inline');
						listItem.slideUp(speedClose, function() {
							divToOpen.slideDown(speedOpen, function() {
								var divToScroll = $JQ(this).siblings('.itemHeading');
								$JQ(window).scrollTo(divToScroll, speedScroll);
							});
						});
						return false;
					}
				});
				if (listsClosed == 0) {
					divToOpen.slideDown(speedOpen, function() {
						var divToScroll = $JQ(this).siblings('.itemHeading');
						$JQ(window).scrollTo(divToScroll, speedScroll);
					});
				}
			}
			return false;
		});


		$JQ('.groupPages .itemHeading .titleToggle').unbind('click');
		$JQ('.groupPages .itemHeading .titleToggle').bind('click', function() {
			var elementClicked = $JQ('.headingToggle a.openSamePage', $JQ(this).parent().parent().siblings('.headingRight'));
			if (elementClicked.css('display') == 'none') {
				elementClicked = $JQ('.itemToggle a', $JQ(this).parent().parent().parent().siblings('.itemFull'));
				var openControl = elementClicked.parent().parent().parent().siblings('.itemHeading');
				var divToClose = elementClicked.parent().parent().parent();
				$JQ('.headingRight a.openSamePage', openControl).css('display', 'inline');
				FITFLOP.PANEL.panelCloseAll();
				divToClose.slideUp(speedClose);
			} else {
				var divToOpen = elementClicked.parent().parent().parent().siblings('.itemFull');
				var listsClosed = 0;
				elementClicked.css('display', 'none');
				FITFLOP.PANEL.panelCloseAll();
				$JQ('.groupPages .itemFull').each(function() {
					var listItem = $JQ(this);
					var listItemText = $JQ('.headingToggle a.openSamePage', listItem.siblings('.itemHeading'));
					if (listItem.css('display') != 'none') {
						listsClosed = 1;
						listItemText.css('display', 'inline');
						listItem.slideUp(speedClose, function() {
							divToOpen.slideDown(speedOpen, function() {
								var divToScroll = $JQ(this).siblings('.itemHeading');
								$JQ(window).scrollTo(divToScroll, speedScroll);
							});
						});
						return false;
					}
				});
				if (listsClosed == 0) {
					divToOpen.slideDown(speedOpen, function() {
						var divToScroll = $JQ(this).siblings('.itemHeading');
						$JQ(window).scrollTo(divToScroll, speedScroll);
					});
				}
			}
			return false;
		});

	},

	bindImageControls: function() {
		$JQ('.panelEmbedded .headingRight a').unbind('click');
		$JQ('.panelEmbedded .headingRight a').bind('click', function() {
			var elementClicked = $JQ(this);
			var divToOpen = elementClicked.parent().parent().siblings('.panelBody');
			var imageToToggle = $JQ('img', elementClicked);
			FITFLOP.PAGE.actionImageControls(imageToToggle, divToOpen);
			return false;
		});

		$JQ('.resultsHeading .headingRight a').unbind('click');
		$JQ('.resultsHeading .headingRight a').bind('click', function() {
			var elementClicked = $JQ(this);
			var divToOpen = elementClicked.parent().parent().siblings('.resultsList');
			var imageToToggle = $JQ('img', elementClicked);
			FITFLOP.PANEL.panelCloseAll();
			FITFLOP.PAGE.actionImageControls(imageToToggle, divToOpen);
			return false;
		});

		$JQ('.commentsHeading .headingRight a').unbind('click');
		$JQ('.commentsHeading .headingRight a').bind('click', function() {
			var elementClicked = $JQ(this);
			var divToOpen = elementClicked.parent().parent().siblings('.commentsList');
			var imageToToggle = $JQ('img', elementClicked);
			FITFLOP.PANEL.panelCloseAll();
			FITFLOP.PAGE.actionImageControls(imageToToggle, divToOpen);
			return false;
		});

		$JQ('.addHeading .headingRight a').unbind('click');
		$JQ('.addHeading .headingRight a').bind('click', function() {
			var elementClicked = $JQ(this);
			var divToOpen = elementClicked.parent().parent().siblings('.addList');
			var imageToToggle = $JQ('img', elementClicked);
			FITFLOP.PANEL.panelCloseAll();
			FITFLOP.PAGE.actionImageControls(imageToToggle, divToOpen);
			return false;
		});

		$JQ('.sendHeading .headingRight a').unbind('click');
		$JQ('.sendHeading .headingRight a').bind('click', function() {
			var elementClicked = $JQ(this);
			var divToOpen = elementClicked.parent().parent().siblings('.sendList');
			var imageToToggle = $JQ('img', elementClicked);
			FITFLOP.PANEL.panelCloseAll();
			FITFLOP.PAGE.actionImageControls(imageToToggle, divToOpen);
			return false;
		});
	},

	bindTextControls: function() {
		$JQ('a.openAddComment').unbind('click');
		$JQ('a.openAddComment').bind('click', function() {
			var elementClicked = $JQ(this);
			FITFLOP.PANEL.panelCloseAll();
			FITFLOP.PAGE.actionAddControls(elementClicked);
			return false;
		});

		$JQ('.groupNews .headingToggle a.openSamePage').unbind('click');
		$JQ('.groupNews .headingToggle a.openSamePage').bind('click', function() {
			var elementClicked = $JQ(this);
			FITFLOP.PANEL.panelCloseAll();
			FITFLOP.PAGE.actionCommentsControls(elementClicked);
			return false;
		});

		$JQ('.groupNews .itemToggle a.openSamePage').unbind('click');
		$JQ('.groupNews .itemToggle a.openSamePage').bind('click', function() {
			var elementClicked = $JQ(this);
			var divToOpen = elementClicked.parent().parent().siblings('.itemFull');
			FITFLOP.PANEL.panelCloseAll();
			FITFLOP.PAGE.actionTextControls(elementClicked, divToOpen);
			return false;
		});

		$JQ('.groupPages .headingToggle a.openSamePage').unbind('click');
		$JQ('.groupPages .headingToggle a.openSamePage').bind('click', function() {
			var elementClicked = $JQ(this);
			FITFLOP.PANEL.panelCloseAll();
			FITFLOP.PAGE.actionCommentsControls(elementClicked);
			return false;
		});

		$JQ('.groupTestimonials .headingToggle a.openSamePage').unbind('click');
		$JQ('.groupTestimonials .headingToggle a.openSamePage').bind('click', function() {
			var elementClicked = $JQ(this);
			FITFLOP.PANEL.panelCloseAll();
			FITFLOP.PAGE.actionCommentsControls(elementClicked);
			return false;
		});

		$JQ('.groupTestimonials .itemToggle a.openSamePage').unbind('click');
		$JQ('.groupTestimonials .itemToggle a.openSamePage').bind('click', function() {
			var elementClicked = $JQ(this);
			var divToOpen = $JQ('.itemFull', elementClicked.parent().parent().siblings('.itemRight'));
			FITFLOP.PANEL.panelCloseAll();
			FITFLOP.PAGE.actionTextControls(elementClicked, divToOpen);
			return false;
		});

		$JQ('.groupFaqs .headingToggle a.openSamePage').unbind('click');
		$JQ('.groupFaqs .headingToggle a.openSamePage').bind('click', function() {
			var elementClicked = $JQ(this);
			var divToOpen = elementClicked.parent().parent().parent().siblings('.itemFull');
			var listsClosed = 0;
			elementClicked.css('display', 'none');
			FITFLOP.PANEL.panelCloseAll();
			$JQ('.groupFaqs .itemFull').each(function() {
				var listItem = $JQ(this);
				var listItemText = $JQ('.headingToggle a.openSamePage', listItem.siblings('.itemHeading'));
				if (listItem.css('display') != 'none') {
					listsClosed = 1;
					listItemText.css('display', 'inline');
					listItem.slideUp(speedClose, function() {
						divToOpen.slideDown(speedOpen, function() {
							var divToScroll = $JQ(this).siblings('.itemHeading');
							$JQ(window).scrollTo(divToScroll, speedScroll);
						});
					});
					return false;
				}
			});
			if (listsClosed == 0) {
				divToOpen.slideDown(speedOpen, function() {
					var divToScroll = $JQ(this).siblings('.itemHeading');
					$JQ(window).scrollTo(divToScroll, speedScroll);
				});
			}
			return false;
		});

		$JQ('.groupFaqs .itemToggle a.openSamePage').unbind('click');
		$JQ('.groupFaqs .itemToggle a.openSamePage').bind('click', function() {
			var elementClicked = $JQ(this);
			var openControl = elementClicked.parent().parent().parent().siblings('.itemHeading');
			var divToClose = elementClicked.parent().parent().parent();
			$JQ('.headingRight a.openSamePage', openControl).css('display', 'inline');
			FITFLOP.PANEL.panelCloseAll();
			divToClose.slideUp(speedClose);
			return false;
		});

		$JQ('.groupPages .headingToggle a.openSamePage').unbind('click');
		$JQ('.groupPages .headingToggle a.openSamePage').bind('click', function() {
			var elementClicked = $JQ(this);
			var divToOpen = elementClicked.parent().parent().parent().siblings('.itemFull');
			var listsClosed = 0;
			elementClicked.css('display', 'none');
			FITFLOP.PANEL.panelCloseAll();
			$JQ('.groupPages .itemFull').each(function() {
				var listItem = $JQ(this);
				var listItemText = $JQ('.headingToggle a.openSamePage', listItem.siblings('.itemHeading'));
				if (listItem.css('display') != 'none') {
					listsClosed = 1;
					listItemText.css('display', 'inline');
					listItem.slideUp(speedClose, function() {
						divToOpen.slideDown(speedOpen, function() {
							var divToScroll = $JQ(this).siblings('.itemHeading');
							$JQ(window).scrollTo(divToScroll, speedScroll);
						});
					});
					return false;
				}
			});
			if (listsClosed == 0) {
				divToOpen.slideDown(speedOpen, function() {
					var divToScroll = $JQ(this).siblings('.itemHeading');
					$JQ(window).scrollTo(divToScroll, speedScroll);
				});
			}
			return false;
		});

		$JQ('.groupPages .itemToggle a.openSamePage').unbind('click');
		$JQ('.groupPages .itemToggle a.openSamePage').bind('click', function() {
			var elementClicked = $JQ(this);
			var openControl = elementClicked.parent().parent().parent().siblings('.itemHeading');
			var divToClose = elementClicked.parent().parent().parent();
			$JQ('.headingRight a.openSamePage', openControl).css('display', 'inline');
			FITFLOP.PANEL.panelCloseAll();
			divToClose.slideUp(speedClose);
			return false;
		});
	},

	actionImageControls: function(control, target) {
		if (target.css('display') != 'none') {
			target.slideUp(speedClose);
			FITFLOP.PAGE.changeImageControl(control, 'open');
		} else {
			if (control.parent().parent().parent().attr('class') == 'resultsHeading') {
				var groupsClosed = 0;
				$JQ('.groupResults').each(function() {
					var groupResultItem = $JQ('.resultsList', $JQ(this));
					var groupResultImage = $JQ('.headingRight img', groupResultItem.siblings('.resultsHeading'));
					if (groupResultItem.css('display') != 'none') {
						groupsClosed = 1;
						FITFLOP.PAGE.changeImageControl(groupResultImage, 'open');
						groupResultItem.slideUp(speedClose, function() {
							target.slideDown(speedOpen, function() {
								 $JQ(window).scrollTo(control, speedScroll);
							});
						});
						return false;
					}
				});
				if (groupsClosed == 0) {
					target.slideDown(speedOpen, function() {
						 $JQ(window).scrollTo(control, speedScroll);
					});
				}
			} else {
				target.slideDown(speedOpen, function() {
					 $JQ(window).scrollTo(control, speedScroll);
				});
			}
			FITFLOP.PAGE.changeImageControl(control, 'close');
		}
	},

	actionTextControls: function(control, target) {
		if (target.css('display') != 'none') {
			target.slideUp(speedClose);
			control.html(text[languageCode]['open']);
			$JQ(window).scrollTo(target.siblings('.itemHeading'), speedScroll);
		} else {
			var listParent = control.parent().parent().parent().parent().parent();
			if ((listParent.attr('class').indexOf('groupTestimonials') >= 0) || (listParent.attr('class').indexOf('groupNews') >= 0) || (listParent.attr('class').indexOf('groupPages') >= 0)) {
				var listsClosed = 0;
				$JQ('.itemFull', listParent).each(function() {
					var listItem = $JQ(this);
					if (listItem.css('display') != 'none') {
						listsClosed = 1;
						if (listParent.attr('class').indexOf('groupTestimonials') >= 0) {
							var listItemText = $JQ('a.openSamePage', listItem.parent().siblings('.itemLink'));
						}
						if ((listParent.attr('class').indexOf('groupNews') >= 0) || (listParent.attr('class').indexOf('groupPages') >= 0)) {
							var listItemText = $JQ('a.openSamePage', listItem.siblings('.itemLink'));
						}
						listItemText.html(text[languageCode]['open']);
						listItem.slideUp(speedClose, function() {
							target.slideDown(speedOpen, function() {
								 $JQ(window).scrollTo(target.siblings('.itemHeading'), speedScroll);
							});
						});
						return false;
					}
				});
				if (listsClosed == 0) {
					target.slideDown(speedOpen, function() {
						$JQ(window).scrollTo(target.siblings('.itemHeading'), speedScroll);
					});
				}
			} else {
				target.slideDown(speedOpen, function() {
					 $JQ(window).scrollTo(target.siblings('.itemHeading'), speedScroll);
				});
			}
			control.html(text[languageCode]['close']);
		}
	},

	actionCommentsControls: function(control) {
		if (control.parent().parent().parent().parent().attr('class') == 'itemRight') {
			var closeControl = $JQ('a.openSamePage', control.parent().parent().parent().parent().siblings('.itemLink'));
		} else {
			var closeControl = $JQ('a.openSamePage', control.parent().parent().parent().siblings('.itemLink'));
		}
		var openControl = $JQ('.commentsHeading .headingRight img', control.parent().parent().parent().siblings('.itemFull'));
		var divToOpen = control.parent().parent().parent().siblings('.itemFull');
		var listParent = closeControl.parent().parent().parent().parent().parent();
		FITFLOP.PAGE.changeImageControl(openControl, 'close');
		if ((listParent.attr('class').indexOf('groupTestimonials') >= 0) || (listParent.attr('class').indexOf('groupNews') >= 0) || (listParent.attr('class').indexOf('groupPages') >= 0)) {
			var listsClosed = 0;
			$JQ('.itemFull', listParent).each(function() {
				var listItem = $JQ(this);
				if (listItem.css('display') != 'none') {
					listsClosed = 1;
					if (divToOpen.css('display') != 'none') {
						divToOpen = $JQ('.commentsList', control.parent().parent().parent().siblings('.itemFull'));
						divToOpen.slideDown(speedOpen, function() {
							var divToScroll = $JQ(this).siblings('.commentsHeading');
							$JQ(window).scrollTo(divToScroll, speedScroll);
						});
					} else {
						if (listParent.attr('class').indexOf('groupTestimonials') >= 0) {
							var listItemText = $JQ('a.openSamePage', listItem.parent().siblings('.itemLink'));
						}
						if ((listParent.attr('class').indexOf('groupNews') >= 0) || (listParent.attr('class').indexOf('groupPages') >= 0)) {
							var listItemText = $JQ('a.openSamePage', listItem.siblings('.itemLink'));
						}
						listItemText.html(text[languageCode]['open']);
						listItem.slideUp(speedClose, function() {
							divToOpen.slideDown(speedOpen, function() {
								divToOpen = $JQ('.commentsList', control.parent().parent().parent().siblings('.itemFull'));
								divToOpen.slideDown(speedOpen, function() {
									var divToScroll = $JQ(this).siblings('.commentsHeading');
									$JQ(window).scrollTo(divToScroll, speedScroll);
								});
							});
						});
					}
					return false;
				}
			});
			if (listsClosed == 0) {
				divToOpen.slideDown(speedOpen, function() {
					divToOpen = $JQ('.commentsList', control.parent().parent().parent().siblings('.itemFull'));
					divToOpen.slideDown(speedOpen, function() {
						var divToScroll = $JQ(this).siblings('.commentsHeading');
						$JQ(window).scrollTo(divToScroll, speedScroll);
					});
				});
			}
		} else {
			divToOpen.slideDown(speedOpen, function() {
				var divToOpen = $JQ('.commentsList', control.parent().parent().parent().siblings('.itemFull'));
				divToOpen.slideDown(speedOpen, function() {
					var divToScroll = $JQ(this).siblings('.commentsHeading');
					$JQ(window).scrollTo(divToScroll, speedScroll);
				});
			});
		}

		closeControl.html(text[languageCode]['close']);
	},

	actionAddControls: function(control) {
		var closeControl = control.siblings('a.openSamePage');
		if (control.parent().parent().siblings('.itemRight').size() > 0) {
			var openControl = $JQ('.addHeading .headingRight img', control.parent().parent().siblings('.itemRight'));
			var divToOpen = $JQ('.itemFull', control.parent().parent().siblings('.itemRight'));
		} else {
			var openControl = $JQ('.addHeading .headingRight img', control.parent().parent().siblings('.itemFull'));
			var divToOpen = control.parent().parent().siblings('.itemFull');
		}
		var listParent = closeControl.parent().parent().parent().parent().parent();
		FITFLOP.PAGE.changeImageControl(openControl, 'close');
		var listsClosed = 0;

		$JQ('.itemFull', listParent).each(function() {
			var listItem = $JQ(this);
			if (listItem.css('display') != 'none') {
				listsClosed = 1;
				if (divToOpen.css('display') != 'none') {
					divToOpen = $JQ('.addList', divToOpen);
					divToOpen.slideDown(speedOpen, function() {
						var divToScroll = $JQ(this).siblings('.addHeading');
						$JQ(window).scrollTo(divToScroll, speedScroll);
					});
				} else {
					if (listParent.attr('class').indexOf('groupTestimonials') >= 0) {
						var listItemText = $JQ('a.openSamePage', listItem.parent().siblings('.itemLink'));
					}
					if ((listParent.attr('class').indexOf('groupNews') >= 0) || (listParent.attr('class').indexOf('groupPages') >= 0)) {
						var listItemText = $JQ('a.openSamePage', listItem.siblings('.itemLink'));
					}
					listItemText.html(text[languageCode]['open']);
					listItem.slideUp(speedClose, function() {
						divToOpen.slideDown(speedOpen, function() {
							divToOpen = $JQ('.addList', divToOpen);
							divToOpen.slideDown(speedOpen, function() {
								var divToScroll = $JQ(this).siblings('.addHeading');
								$JQ(window).scrollTo(divToScroll, speedScroll);
							});
						});
					});
				}
				return false;
			}
		});
		if (listsClosed == 0) {
			divToOpen.slideDown(speedOpen, function() {
				divToOpen = $JQ('.addList', divToOpen);
				divToOpen.slideDown(speedOpen, function() {
					var divToScroll = $JQ(this).siblings('.addHeading');
					$JQ(window).scrollTo(divToScroll, speedScroll);
				});
			});
		}
		closeControl.html(text[languageCode]['close']);
	/*


		if ((listParent.attr('class').indexOf('groupTestimonials') >= 0) || (listParent.attr('class').indexOf('groupNews') >= 0) || (listParent.attr('class').indexOf('groupPages') >= 0)) {

		} else {
			alert(3)
			divToOpen.slideDown(speedOpen, function() {
				var divToOpen = $JQ('.commentsList', control.parent().parent().parent().siblings('.itemFull'));
				divToOpen.slideDown(speedOpen, function() {
					var divToScroll = $JQ(this).siblings('.commentsHeading');
					$JQ(window).scrollTo(divToScroll, speedScroll);
				});
			});
		}

		closeControl.html(text[languageCode]['close']);*/
	},

	bindProductsControls: function() {
		$JQ('.groupProducts .listItem').each(function() {
			var currentItem = $JQ(this);
			var defaultProductColour = '';
			if ($JQ('.refineOption #colour').val() != '') {
				defaultProductColour = $JQ('.refineOption #colour').val().toLowerCase();
				defaultProductColour = defaultProductColour.replace(/ /gi, "");
			}
			if ($JQ('.itemIntro>a>img', currentItem).size() > 1) {
				var colourCount = $JQ('.itemIntro>a>img', currentItem).size();
				var defaultSet = 0;
				$JQ('.itemIntro>a>img', currentItem).each(function() {
					var currentImage = $JQ(this);
					var imagePart = currentImage.attr('src').split('/');
					colourCount--;
					if ((defaultProductImages[languageCode][imagePart[imagePart.length-1]]) && (defaultProductColour == '')) {
						currentImage.parents('a').addClass('defaultColour');
						defaultSet = 1;
					}
					if ((colourCount == 0) && (defaultSet == 0)) {
						if (defaultProductColour != '') {
							$JQ('.groupProducts .listItem .itemIntro a').each(function() {
								var eachElement = $JQ(this);
								if (eachElement.attr('id').substring(12) == defaultProductColour) {
									eachElement.addClass('defaultColour');
								}
							});
						} else {
							$JQ('.itemIntro>a:nth(0)', currentItem).addClass('defaultColour');
						}
					}
				});
			} else {
				$JQ('.itemIntro>a', currentItem).addClass('defaultColour');
			}
		});
		$JQ('.groupProducts .listItem h2 a').css('display','block');
		$JQ('.groupProducts .listItem ul.colors a').css('display','block');

		$JQ('.groupProducts .listItem div').unbind('mouseenter');
		$JQ('.groupProducts .listItem div').bind('mouseenter', function() {
			var productEntered = $JQ(this);
			productEntered.removeClass('itemIntro');
			productEntered.addClass('itemFull');
		});

		$JQ('.groupProducts .listItem div').unbind('mouseleave');
		$JQ('.groupProducts .listItem div').bind('mouseleave', function() {
			var productEntered = $JQ(this);
			productEntered.removeClass('itemFull');
			productEntered.addClass('itemIntro');
		});

		$JQ('.groupProducts .listItem ul.colors a').unbind('mouseenter');
		$JQ('.groupProducts .listItem ul.colors a').bind('mouseenter', function() {
			var colorEntered = $JQ(this);
			var urlArray = colorEntered.attr('href').split('#');
			colorEntered.parents('ul').siblings('a').css('display','none');
			$JQ('#' + urlArray[1], colorEntered.parents('.listItem')).css('display','inline');
		});
	},

	bindProductControls: function() {
		$JQ('.product .contentRight .productDetails').each(function() {
			FITFLOP.PAGE.changeImageControl($JQ('.productDescription .detailHeading img'), 'close');
			$JQ('.productDescription .detailBody').slideDown(speedOpen);

			$JQ('.productDetail .detailHeading .headingRight a').unbind('click');
			$JQ('.productDetail .detailHeading .headingRight a').bind('click', function() {
				FITFLOP.PAGE.productDetailToggle($JQ(this));
			});

			$JQ('.productDetail .detailHeading .titleToggle').unbind('click');
			$JQ('.productDetail .detailHeading .titleToggle').bind('click', function() {
				FITFLOP.PAGE.productDetailToggle($JQ('a', $JQ(this).parent().parent().siblings('.headingRight')));
			});
		});
	},

	productDetailToggle: function(elementClicked) {
		var control = $JQ('img', elementClicked);
		var productDetail = elementClicked.parents('.detailHeading').siblings('.detailBody');
		FITFLOP.PANEL.panelCloseAll();
		if (productDetail.css('display') != 'none') {
			FITFLOP.PAGE.changeImageControl(control, 'open');
			productDetail.slideUp(speedClose);
		} else {
			var detailsLeft = $JQ('.productDetail').size();
			var detailToggled = 0;
			$JQ('.productDetail').each(function() {
				detailsLeft--;
				if (($JQ(this) != elementClicked.parents('.productDetail')) && ($JQ('.detailBody', $JQ(this)).css('display') != 'none')) {
					detailToggled = 1;
					FITFLOP.PAGE.changeImageControl($JQ('.detailHeading img', $JQ(this)), 'open');
					$JQ('.detailBody', $JQ(this)).slideUp(speedClose, function() {
						FITFLOP.PAGE.changeImageControl(control, 'close');
						productDetail.slideDown(speedOpen);
					});
				}
				if ((detailsLeft == 0) && (detailToggled == 0)) {
					FITFLOP.PAGE.changeImageControl(control, 'close');
					productDetail.slideDown(speedOpen);
				}
			});
		}
	},

	bindProductImages: function() {
		var productPage = $JQ('.product .contentLeft').size();
		if (productPage == 1) {
			if (location.href.indexOf('#') >= 0) {
				var urlArray = location.href.split('#');
				var colorChoice = urlArray[1];
			} else {
				var colorChoice = '';
			}
			if (colorChoice != '') {
				$JQ('#' + colorChoice).css('display', 'block');
				$JQ('.listItem:nth(0)', $JQ('#' + colorChoice)).css('display', 'block');
			} else {
				$JQ('.product .contentLeft .resultsList:nth(0)').css('display', 'block');
				$JQ('.listItem:nth(0)', $JQ('.product .contentLeft .resultsList:nth(0)')).css('display', 'block');
			}

			$JQ('.product .contentLeft .productColors').css('display', 'block');

			$JQ('.product .contentLeft .itemIntro a').unbind('click');
			$JQ('.product .contentLeft .itemIntro a').bind('click', function() {
				var elementToBind = $JQ(this);
				$JQ(elementToBind.attr('href')).fadeIn(speedFade);
				return false;
			});

			$JQ('.product .contentLeft .itemFull a').unbind('click');
			$JQ('.product .contentLeft .itemFull a').bind('click', function() {
				var elementToBind = $JQ(this);
				$JQ(elementToBind.parents('.itemFull')).fadeOut(speedFade);
				return false;
			});

			$JQ('.product .contentLeft .productThumbnails a').unbind('mouseenter');
			$JQ('.product .contentLeft .productThumbnails a').bind('mouseenter', function() {
				var elementToBind = $JQ(this);
				$JQ(elementToBind.parents('.productThumbnails').siblings('.listItem')).css('display', 'none');
				var elementToShow = $JQ(elementToBind).attr('href');
				$JQ(elementToShow).css('display', 'inline');
			});

			$JQ('.product .contentLeft .productThumbnails a').unbind('click');
			$JQ('.product .contentLeft .productThumbnails a').bind('click', function() {
				return false;
			});

			$JQ('.product .contentLeft .productColors a').unbind('mouseenter');
			$JQ('.product .contentLeft .productColors a').bind('mouseenter', function() {
				var elementToBind = $JQ(this);
				$JQ(elementToBind.parents('.productColors').siblings('.resultsList')).css('display', 'none');
				$JQ('.listItem', $JQ($JQ(elementToBind).attr('href'))).css('display', 'none');
				$JQ('.listItem:nth(0)', $JQ($JQ(elementToBind).attr('href'))).css('display', 'block');
				var elementToShow = $JQ(elementToBind).attr('href');
				$JQ(elementToShow).css('display', 'inline');
			});

			$JQ('.product .contentLeft .productColors a').unbind('click');
			$JQ('.product .contentLeft .productColors a').bind('click', function() {
				return false;
			});
		}
	},

	changeImageControl: function(elementToChange,state) {
		elementToChange.attr('alt', text[languageCode][state]);
		elementToChange.attr('title', text[languageCode][state]);
		if (state == 'close') {
			elementToChange.attr('src', elementToChange.attr('src').replace('_off', '_on'));
		} else {
			elementToChange.attr('src', elementToChange.attr('src').replace('_on', '_off'));
		}
	},

	duplicatePagination:function() {
		if ($JQ('.resultsHeadingTop').size() == 1) {
			theElement = $JQ('.resultsHeadingTop');
			theElement.parents('.groupResults').append(FITFLOP.GENERIC.getHTML(theElement).replace('resultsHeadingTop','resultsHeadingBottom'));
		}
	}
};


// *******************************************************************************************


FITFLOP.REFINE = {

	bindRefineItems: function() {
		if ($JQ('.refineForm').size() > 0) {
			$JQ('.refineForm').parents('.bannerInside').addClass('bannerInsideWithRefine');
			FITFLOP.GENERIC.moveHTML($JQ('.heading'),$JQ('.refineForm'));
		}
		FITFLOP.REFINE.bindRefineOptions();
		var refineCount = 0;
		$JQ('.refineOption').each(function() {
			refineCount++;
			var refineOption = $JQ(this);
			refineOption.attr('id', 'refineOption' + refineCount);
			refineOption.css('display', 'inline');
			if ($JQ('input', refineOption).attr('id') == 'refineKeyword') {
				currentKeywords = $JQ('input', refineOption).val();
			}
		});

		$JQ('#refineSubmit').unbind('click');
		$JQ('#refineSubmit').bind('click', function(e) {
			FITFLOP.REFINE.changeRefineInput();
			return false;
		});

		$JQ('.refineItem').unbind('mouseenter');
		$JQ('.refineItem').bind('mouseenter', function() {
			var refineItem = $JQ(this);
			var refineItemHeight = FITFLOP.GENERIC.getTotalHeight($JQ('.refineItemTop', refineItem));
			refineItem.css('height', refineItemHeight + 'px');
			refineItem.css('overflow', 'hidden');
			$JQ('.refineItemBottom', refineItem).css('display', 'inline');
			refineItemHeight += FITFLOP.GENERIC.getTotalHeight($JQ('.refineItemBottom', refineItem));
			refineItem.animate({
				height:refineItemHeight
			}, speedOptions, function() {
				refineItem.css('height', 'auto');
			});
		});

		$JQ('.refineItem').unbind('mouseleave');
		$JQ('.refineItem').bind('mouseleave', function() {
			var refineItem = $JQ(this);
			var refineItemHeight = FITFLOP.GENERIC.getTotalHeight($JQ('.refineItemTop', refineItem));
			refineItem.animate({
				height:refineItemHeight
			}, speedOptions, function() {
				$JQ('.refineItemBottom', refineItem).css('display', 'none');
				refineItem.css('overflow', 'visible');
				refineItem.css('height', 'auto');
			});
			if ($JQ('input', refineItem).attr('id') == 'refineKeyword') {
				FITFLOP.REFINE.changeRefineInput();
			}
		});
	},

	bindRefineOptions: function() {
		$JQ('.refineTitle a').unbind('click');
		$JQ('.refineTitle a').bind('click', function() {
			$JQ('#refineKeyword').val('');
			$JQ('.refineItem').removeClass('refineSet');
			$JQ('.refineItemTop p').remove();
			$JQ('.boxTerms li').removeClass('active');
			$JQ('.refineItemBottom li').removeClass('active');
			$JQ('.refineItemBottom li.refineDefault').addClass('active');
			$JQ('.refineItemBottom li.refineDefault').each(function() {
				defaultItem = $JQ(this);
				if ($JQ('a', defaultItem).html() != text[languageCode]['all']) {
					defaultItem.parents('.refineItem').addClass('refineSet');
					defaultItem.parents('.refineItemBottom').siblings('.refineItemTop').append('<p>' + $JQ('a', defaultItem).html() + '</p>');
				}
			});
			$JQ('.refineItemBottom input').each(function() {
				var formField = $JQ(this);
				if (formField.attr('type') == 'hidden') {
					formField.val('');
				}
			});
			currentKeywords = '';
			FITFLOP.REFINE.actionRefine($JQ(this).attr('href'));
			return false;
		});

		$JQ('.refineItemBottom a').unbind('click');
		$JQ('.refineItemBottom a').bind('click', function() {
			var elementClicked = $JQ(this);
			if (elementClicked.parent().attr('class').indexOf('active') < 0) {
				var passURL = elementClicked.attr('href');
				if (passURL == '?') {
					passURL += 'refineKeyword=' + $JQ('#refineKeyword').val();
				} else {
					passURL += '&refineKeyword=' + $JQ('#refineKeyword').val();
				}

				var fieldsLeft = $JQ('input', elementClicked.parents('.refineOption').siblings()).size();
				if (fieldsLeft == 0) {
					FITFLOP.REFINE.actionRefine(passURL);
				}
				$JQ('input', elementClicked.parents('.refineOption').siblings()).each(function() {
					var formField = $JQ(this);
					fieldsLeft--;
					if (formField.attr('type') == 'hidden') {
						passURL += '&' + $JQ(this).attr('name') + '=' + $JQ(this).attr('value');
					}
					if (fieldsLeft == 0) {
						passURL = passURL.replace(/ /g, '+');
						passURL = passURL.replace(/&amp;/g, '%26');
						FITFLOP.REFINE.actionRefine(passURL);
					}
				});

				$JQ('li', elementClicked.parents('ul')).removeClass('active');
				if (elementClicked.parents('ul').siblings('ul').size() > 0) {
					if (elementClicked.parents('ul').siblings('ul').attr('class').indexOf('colors') >= 0) {
						$JQ('li', elementClicked.parents('ul').siblings('ul')).removeClass('active');
					}
				}
				if (elementClicked.parents('ul').attr('class').indexOf('colors') < 0) {
					var pText = '<p>' + elementClicked.html() + '</p>';
				} else {
					var pText = '<p>' + $JQ('img', elementClicked).attr('alt') + '</p>';
					$JQ('li', elementClicked.parents('ul').siblings('ul')).removeClass('active');
				}
				elementClicked.parent().addClass('active');
				$JQ('.refineItemTop p', elementClicked.parents('.refineItem')).remove();
				if (elementClicked.html() == text[languageCode]['all']) {
					elementClicked.parents('.refineItem').removeClass('refineSet');
					elementClicked.parents('ul').siblings('input').val('');
				} else {
					elementClicked.parents('.refineItem').addClass('refineSet');
					$JQ('.refineItemTop', elementClicked.parents('.refineItem')).append(pText);
					if (elementClicked.parents('ul').attr('class').indexOf('colors') < 0) {
						elementClicked.parents('ul').siblings('input').val(elementClicked.html());
					} else {
						elementClicked.parents('ul').siblings('input').val($JQ('img', elementClicked).attr('alt'));
					}
				}
			}
			return false;
		});
	},

	changeRefineInput: function() {
		var refineInput = $JQ('#refineKeyword');
		if(currentKeywords != refineInput.val()) {
			currentKeywords = refineInput.val();
			FITFLOP.FORM.getInputsAsParameters(refineInput.parents('form'), function(passedURL) {
				passedURL = passedURL.replace(/ /g, '+');
				passedURL = passedURL.replace(/&amp;/g, '%26');
				FITFLOP.REFINE.actionRefine(passedURL);
			});
			var pText = '';
			$JQ('.refineItemTop p', refineInput.parents('.refineItem')).remove();
			if (refineInput.val() != '') {
				pText = '<p>' + refineInput.val() + '</p>';
				refineInput.parents('.refineItem').addClass('refineSet');
			} else {
				refineInput.parents('.refineItem').removeClass('refineSet');
			}
			$JQ('.refineItemTop', refineInput.parents('.refineItem')).append(pText);
		}
	},

	actionRefine: function(refineURL) {
		if (refineURL == '?') {
			refineURL += "ajax=1"
		} else if (refineURL.indexOf('ajax=1') <= 0) {
			refineURL += "&ajax=1"
		}
		FITFLOP.GENERIC.showAjaxLoading();
		$JQ.get(refineURL, function(data) {
			$JQ('.contentLeft').html(data);
			FITFLOP.GENERIC.bindAfterContentRefresh();
			FITFLOP.GENERIC.hideAjaxLoading();
		});
	},

	bindPagination: function() {
		//FITFLOP.PAGE.duplicatePagination();
		$JQ('ul.pagination a').unbind('click');
		$JQ('ul.pagination a').bind('click', function() {
			var passedURL = $JQ(this).attr('href');
			FITFLOP.REFINE.actionRefine(passedURL);
			return false;
		});
	},

	repurposeEnterSubmit:function() {
		$JQ('.refineForm').unbind('keypress');
		$JQ('.refineForm').bind('keypress', function(e) {
			if (e.keyCode=='13') {
				FITFLOP.REFINE.changeRefineInput();
				return false;
			}
		});
	}
};


// *******************************************************************************************


FITFLOP.PANEL = {

	bindPanelControls: function() {
		$JQ('a').each(function() {
			if ($JQ(this).attr('class').indexOf('open_') >= 0) {
				$JQ(this).unbind('click');
				$JQ(this).bind('click', function() {
					var elementToBind = $JQ(this);
					FITFLOP.PANEL.panelToggle('#' + elementToBind.attr('class').substring(5));
					return false;
				});
			}
			if ($JQ(this).parents('div').attr('class').indexOf('panelClose') >= 0) {
				$JQ(this).unbind('click');
				$JQ(this).bind('click', function() {
					FITFLOP.PANEL.panelMenuToggle();
					var elementToBind = $JQ(this);
					if ($JQ(elementToBind).parents('.panelOutside').attr('class').indexOf('panelFooter') >= 0) {
						FITFLOP.PANEL.footerPanelClose('#' + $JQ(elementToBind).parents('.panelOutside').attr('id'));
					} else {
						$JQ('#' + elementToBind.parents('.panelOutside').attr('id')).slideUp(speedClose, function() {
							$JQ(window).scrollTo(0, speedScroll);
						});
					}
					return false;
				});
			}
		});
	},

	panelToggle: function(panelToActivate) {
		FITFLOP.PANEL.panelMenuToggle(panelToActivate);
		var panelsLeft = $JQ('.panelOutside').size();
		var panelToggled = 0;
		if ($JQ(panelToActivate).css('display') != 'none') {
			if ($JQ(panelToActivate).attr('class').indexOf('panelFooter') >= 0) {
				FITFLOP.PANEL.footerPanelClose(panelToActivate);
			} else {
				FITFLOP.PANEL.panelMenuToggle();
				$JQ(panelToActivate).slideUp(speedClose);
			}
		} else {
			$JQ('.panelOutside').each(function() {
				panelsLeft--;
				if (($JQ(this).attr('id') != panelToActivate.substring(1)) && ($JQ(this).css('display') != 'none')) {
					panelToggled = 1;
					if ($JQ(panelToActivate).attr('class').indexOf('panelFooter') >= 0) {
						if ($JQ(this).attr('class').indexOf('panelFooter') >= 0) {
							FITFLOP.PANEL.footerPanelClose('#' + $JQ(this).attr('id'),function() {
								FITFLOP.PANEL.footerPanelOpen(panelToActivate, function() {
									$JQ(window).scrollTo($JQ(panelToActivate), speedScroll)
									pageTracker._trackPageview('/' + escape(panelToActivate.substring(1)));
								});
							})
						} else {
							$JQ(this).slideUp(speedClose, function() {
								FITFLOP.PANEL.footerPanelOpen(panelToActivate, function() {
									$JQ(window).scrollTo($JQ(panelToActivate), speedScroll)
									pageTracker._trackPageview('/' + escape(panelToActivate.substring(1)));
								});
							});
						}
					} else {
						if ($JQ(this).attr('class').indexOf('panelFooter') >= 0) {
							FITFLOP.PANEL.footerPanelClose('#' + $JQ(this).attr('id'),function() {
								$JQ(panelToActivate).slideDown(speedOpen, function() {
									$JQ(window).scrollTo($JQ(panelToActivate), speedScroll)
									pageTracker._trackPageview('/' + escape(panelToActivate.substring(1)));
								});
							});
						} else {
							$JQ(this).slideUp(speedClose, function() {
								$JQ(panelToActivate).slideDown(speedOpen, function() {
									$JQ(window).scrollTo($JQ(panelToActivate), speedScroll)
									pageTracker._trackPageview('/' + escape(panelToActivate.substring(1)));
								});
							});
						}
					}
				}
				if ((panelsLeft == 0) && (panelToggled == 0)) {
					if ($JQ(panelToActivate).attr('class').indexOf('panelFooter') >= 0) {
						FITFLOP.PANEL.footerPanelOpen(panelToActivate, function() {
								$JQ(window).scrollTo($JQ(panelToActivate), speedScroll)
								pageTracker._trackPageview('/' + escape(panelToActivate.substring(1)));
							});
					} else {
						$JQ(panelToActivate).slideDown(speedOpen, function() {
							$JQ(window).scrollTo($JQ(panelToActivate), speedScroll)
							pageTracker._trackPageview('/' + escape(panelToActivate.substring(1)));
						});
					}
				}
			});
		}
	},

	panelMenuToggle: function(panelToActivate) {
		$JQ('.headerRight li').each(function() {
			if ($JQ('a', $JQ(this)).attr('href') == panelToActivate) {
				$JQ(this).addClass('active');
			} else if (($JQ('a', $JQ(this)).attr('href') == '#panelStoreFinder') || ($JQ('a', $JQ(this)).attr('href') == '#panelSubscribe')) {
				$JQ(this).removeClass('active');
			}
		});

		$JQ('.headerLeft li').each(function() {
			if ($JQ('a', $JQ(this)).attr('href') == panelToActivate) {
				$JQ(this).addClass('active');
			} else if (($JQ('a', $JQ(this)).attr('href') == '#panelStoreFinder') || ($JQ('a', $JQ(this)).attr('href') == '#panelSubscribe')) {
				$JQ(this).removeClass('active');
			}
		});
	},

	panelCloseAll: function() {
		FITFLOP.PANEL.panelMenuToggle();
		var panelsLeft = $JQ('.panelOutside').size();
		$JQ('.panelOutside').each(function() {
			var panelToClose = $JQ(this);
			if (panelToClose.css('display') != 'none') {
				if (panelToClose.attr('class').indexOf('panelFooter') < 0) {
					panelToClose.slideUp(speedClose);
				} else {
					FITFLOP.PANEL.footerPanelClose('#' + panelToClose.attr('id'));
				}
			}
		});
	},

	locationToggle: function(elementClicked) {
		var locationDetail = elementClicked.parents('.locationTitle').siblings('.locationDetails');
		if (locationDetail.css('display') != 'none') {
			//locationDetail.slideUp(speedClose);
			//locationDetail.parents('.locationItem').removeClass('locationItemActive');
		} else {
			var locationsLeft = $JQ('.locationItem').size();
			var locationToggled = 0;
			$JQ('.locationItem').each(function() {
				locationsLeft--;
				if (($JQ(this) != elementClicked.parents('.locationItem')) && ($JQ('.locationDetails', $JQ(this)).css('display') != 'none')) {
					locationToggled = 1;
					$JQ('.locationDetails', $JQ(this)).slideUp(speedClose, function() {
						$JQ(this).parents('.locationItem').removeClass('locationItemActive');
						locationDetail.slideDown(speedOpen, function() {
							$JQ('#locations').scrollTo(locationDetail.parents('.locationItem'), speedScroll);
							locationDetail.parents('.locationItem').addClass('locationItemActive');
						});
					});
				}
				if ((locationsLeft == 0) && (locationToggled == 0)) {
					locationDetail.slideDown(speedOpen, function() {
						$JQ('#locations').scrollTo(locationDetail.parents('.locationItem'), speedScroll);
						locationDetail.parents('.locationItem').addClass('locationItemActive');
					});
				}
			});
		}
	},

	footerPanelOpen: function(panelID, callback) {
		var panelHeight = parseInt($JQ(panelID).height()) + parseInt(1);
		$JQ(panelID).css('height', '0px');
		$JQ(panelID).animate({
			height:panelHeight + 'px',
			top: -1 * panelHeight
		}, speedOpen, function() {
			if (callback) {
				callback.call();
			}
		});
	},

	footerPanelClose: function(panelID, callback) {
		$JQ(panelID).animate({
			height:'0px',
			top: '0px'
		}, speedClose, function() {
			$JQ(panelID).css('display', 'none');
			$JQ(panelID).css('height', 'auto');
			if (callback) {
				callback.call();
			}
		});
	}
};


// *******************************************************************************************


FITFLOP.STORE = {

	map: null,
	geocoder: null,
	searchUrlRoot: '/retail_locator/service.php',
	googleMapsUrlRoot: 'http://maps.google.co.uk/maps',

	bindFinderSubmit: function() {
		$JQ('#panelStoreFinder form').unbind('submit');
		$JQ('#panelStoreFinder form').bind('submit', function() {
			if (FITFLOP.STORE.validateSearch()) {
				FITFLOP.STORE.executeSearch($JQ('#storePostcode').attr('value').toLowerCase() + ', ' + $JQ('OPTION:selected', $JQ('#storeCountry')).text().toLowerCase());
				pageTracker._trackPageview(googleAnalyticsPseudoURLs['storeSearch'] + '/' + escape($JQ('#storeCountry').attr('value').toLowerCase()) + '/' + escape($JQ('#storePostcode').attr('value').toLowerCase()));
			}
			return false;
		});
	},

	bindFinderBack: function() {
		$JQ('#panelStoreFinder_google .panelHeader a').unbind('click');
		$JQ('#panelStoreFinder_google .panelHeader a').bind('click', function() {
			$JQ('#panelStoreFinder_google').slideUp(speedClose, function() {
				$JQ('#panelStoreFinder_find').slideDown(speedOpen);
				FITFLOP.STORE.clearMapPanel();
			});
			return false;
		});
	},

	bindLocationTitles: function() {
		//$JQ('.locationTitle a').unbind('click');
		$JQ('.locationTitle a').bind('click', function() {
			FITFLOP.PANEL.locationToggle($JQ(this));
			return false;
		});
	},

	bindCountrySelect: function() {
		$JQ('#storeCountry').unbind('change');
		$JQ('#storeCountry').bind('change', function () {
			FITFLOP.STORE.countryChanged();
		});
	},

	bindClickthroughLinks: function() {
		$JQ('.storeClickthrough').each(function() {
			$JQ(this).unbind('click');
			$JQ(this).bind('click', function() {
				pageTracker._trackPageview(googleAnalyticsPseudoURLs['storeClickthrough'] + '/' + escape($JQ(this).text()));
				return true;
			});
		});
	},

	countryChanged: function() {
		var countryInfo = FITFLOP.STORE.countries[$JQ('OPTION:selected', $JQ('#storeCountry')).attr('value')];


		if (countryInfo) {
			// Display the prompt text
			$JQ('#storePostcodeLabel').html(countryInfo['enterText'] + '<br />' + countryInfo['exampleText']);

			// Display the online retailers
			var html = '';
			if (countryInfo['onlineRetailers'].length == 0) {
				html += '<p>' + text[languageCode]['noOnlineRetailers'] + '</p>';
			} else {
				html += '<ul>';
				for (var i = 0; i < countryInfo['onlineRetailers'].length; i++) {
					var onlineRetailer = countryInfo['onlineRetailers'][i];
					if (onlineRetailer['blank'] == 0) {
						html += '<li class="normal"><a href="' + onlineRetailer['url'] + '" class="storeClickthrough" title="">' + onlineRetailer['title'] + '</a></li>';
					} else {
						html += '<li><a href="' + onlineRetailer['url'] + '" class="targetBlank storeClickthrough" title="' + text[languageCode]['willOpenInNewWindow'] + '">' + onlineRetailer['title'] + '</a></li>';
					}
				}
				html += '</ul>';
			}

			$JQ('#onlineRetailers').html(html);
			$JQ('#onlineRetailers').css('display','inline');
			$JQ('#onlineRetailersHeader').css('display','inline');
			$JQ('#onlineRetailersImage').css('display','inline');
			FITFLOP.GENERIC.handleNewWindowLinks();
			FITFLOP.STORE.bindClickthroughLinks();
		}
	},

	showErrorMessage: function(errorCount, errorMessage) {
		if (errorCount == 0) {
			errorMessage = '';
		} else if (errorCount == 1) {
			errorMessage = '<p class="textError">' + text[languageCode]['errorMessage'] + '</p><ul class="textError">' + errorMessage + '</ul>';
		} else {
			errorMessage = '<p class="textError">' + text[languageCode]['errorMessages'] + '</p><ul class="textError">' + errorMessage + '</ul>';
		}

		$JQ('#panelStoreFinder_find .panelHeader:nth(0) .textError').remove();
		$JQ('#panelStoreFinder_find .panelHeader:nth(0)').append(errorMessage);
	},

	validateSearch: function() {
		var errorCount = 0;
		var errorText = '';

		if (!$JQ('#storeCountry').attr('value')) {
			errorCount++;
			errorText += "<li>" + text[languageCode]['specifyCountry'] + "</li>";
		}

		if ($JQ('#storePostcode').attr('value') == '') {
			errorCount++;
			errorText += "<li>" + text[languageCode]['specifyCityPostcode'] + "</li>";
		}

		FITFLOP.STORE.showErrorMessage(errorCount, errorText);

		return (errorCount == 0);
	},

	executeSearch: function(address) {
		if ((GBrowserIsCompatible()) && (FITFLOP.STORE.geocoder == null)) {
			FITFLOP.STORE.geocoder = new GClientGeocoder();
		}

		FITFLOP.STORE.geocoder.getLatLng(address, function(latlng) {
			if (!latlng) {
				FITFLOP.STORE.showErrorMessage(1, '<li>' + $JQ('#storePostcode').attr('value') + ', ' + $JQ('OPTION:selected', $JQ('#storeCountry')).text() + ' ' + text[languageCode]['notFound'] + '</li>');
			} else {
				FITFLOP.STORE.showErrorMessage(0);
				FITFLOP.STORE.showResults(latlng);
			}
		});
	},

	showResults: function(latlng) {
		$JQ('#panelStoreFinder_find').slideUp(speedClose, function() {
			$JQ('#panelStoreFinder_google').slideDown(speedOpen, function() {
				$JQ('#searchResultsHeader').html(text[languageCode]['searchResultsFor'] + ' <strong>' + $JQ('OPTION:selected', $JQ('#storeCountry')).text() + ' ' + $JQ('#storePostcode').attr('value') + '</strong>');
				FITFLOP.STORE.createMapObject(latlng);
			});
		});
	},

	createMapObject: function(latlng) {
		if (FITFLOP.STORE.map) {
			FITFLOP.STORE.map.clearOverlays();
		} else {
			FITFLOP.STORE.map = new GMap2(document.getElementById('googleMap'));
			FITFLOP.STORE.map.addControl(new GLargeMapControl());
			FITFLOP.STORE.map.addControl(new GMapTypeControl());
			FITFLOP.STORE.map.setCenter(latlng, 10);
			FITFLOP.STORE.map.checkResize();
		}
		FITFLOP.STORE.loadMarkers(latlng);
	},

	loadMarkers: function(center) {
		var countryInfo = FITFLOP.STORE.countries[$JQ('OPTION:selected', $JQ('#storeCountry')).attr('value')];
		searchUrl = FITFLOP.STORE.searchUrlRoot + '?lat=' + center.lat() + '&lng=' + center.lng();
		var temp=new Array();
		$JQ('.storeCountryCategory:checked').each(
				function(index,value){
					temp.push($JQ(value).attr('value'));
				}
		)
		if(temp.length){
			searchUrl+='&category='+temp.join('|');
		}
		if (countryInfo['km'] == '1') searchUrl += '&km=1';

		GDownloadUrl(searchUrl, function(data) {
			FITFLOP.STORE.map.clearOverlays();
			var xml = GXml.parse(data);
			markers = xml.documentElement.getElementsByTagName('marker');
			if (markers.length == 0) {
				// No results
				$JQ('#panelStoreFinder_google').slideUp(speedClose, function() {
					$JQ('#panelStoreFinder_find').slideDown(speedOpen, function() {
						FITFLOP.STORE.showErrorMessage(1, '<li>'+text[languageCode]['noStockists']+'</li>');
					});
				});
			} else {
				var bounds = new GLatLngBounds();

				for (var i = 0 ; i < markers.length ; i++) {
					var id = 'location_'+markers[i].getAttribute('id');
					var name = markers[i].getAttribute('name');
					var distance = (Math.round(markers[i].getAttribute('distance') * 10) / 10).toString();
					distance += (countryInfo['km'] == '1') ? ' km' : ' m';
					var point = new GLatLng(parseFloat(markers[i].getAttribute('lat')), parseFloat(markers[i].getAttribute('lng')));
					var address = markers[i].getAttribute('address');
					var city = markers[i].getAttribute('city');
					var country = markers[i].getAttribute('country');
					var postCode = markers[i].getAttribute('post_code');
					var telephone = markers[i].getAttribute('telephone');
					var email = markers[i].getAttribute('email');
					var url = markers[i].getAttribute('url');
					var category = markers[i].getAttribute('category');

					FITFLOP.STORE.createLocationItem(id, name, distance, center, point, address, city, country, postCode, telephone, email, url, category);
					var marker = FITFLOP.STORE.createMarker(id, point, distance, name, center, point, address, city, country, postCode, telephone, i);
					bounds.extend(point);
				}

				$JQ('.locationDetails:first').slideDown(speedOpen);
				$JQ('.locationDetails:first').parents('.locationItem').addClass('locationItemActive');
				FITFLOP.STORE.bindLocationTitles();
				FITFLOP.GENERIC.handleNewWindowLinks();
				FITFLOP.STORE.bindClickthroughLinks();
				FITFLOP.STORE.map.setCenter(bounds.getCenter(), FITFLOP.STORE.map.getBoundsZoomLevel(bounds));
			}
		});
	},

	createMarker:function (id, point, distance, name, directionsStart, directionsEnd, address, city, country, postCode, telephone, count) {

		var marker = new GMarker(point);
		$JQ('#'+id+' h3 a').click(function() {
			GEvent.trigger(marker,'click');
		});

		GEvent.addListener(marker, 'click', function() {
			var html = '<h3>'+name+'</h3>';
			html += '<p>' + address + '<br />' + city + ' ' + postCode + '<br />Tel: '+ telephone +'<br /><strong>' + distance +'</strong></p>';
			marker.openInfoWindowHtml(html);

			if ($JQ('#' + id + ' .locationDetails').css('display') == 'none') {
				FITFLOP.PANEL.locationToggle($JQ('#' + id +' h3 a'));
			}
		});

		FITFLOP.STORE.map.addOverlay(marker);

		//detect which item is open and then trigger the marker bubble

		if (count==0) {
			var html = '<h3>'+name+'</h3>';
			html += '<p>' + address + '<br />' + city + ' ' + postCode + '<br />Tel: '+ telephone +'<br /><strong>' + distance +'</strong></p>';
			marker.openInfoWindowHtml(html);
		}
	},

	createLocationItem: function(id, name, distance, directionsStart, directionsEnd, address, city, country, postCode, telephone, email, url, category) {
		var categoryFirstLetters = '';
		if (category) {
			categoryArr = category.split(',');
			for(i=categoryArr.length; i>0; i--) {
				trimmed = $JQ.trim(categoryArr[i-1]);
				categoryFirstLetters += '<span class="categories" title="'+ trimmed +'">' + trimmed.substr(0,1) + '</span>';
			}
		}
		var listItem = '<div class="locationItem" id="' + id + '">';
		listItem += '<div class="locationTitle"><h3><a href="#">' + name + '</a><span class="distance">' + distance + '</span>' + categoryFirstLetters + '</h3></div>';
		listItem += '<div class="locationDetails">';
		listItem += '<div class="locationAddress"><p>' + address + '<br />' + city + ' ' + postCode;
		if (email) listItem += '<br /><a href="mailto:' + email + '">' + email + '</a>';
		if (url) {
			var urlShow = url;
			var urlLink = url;
			var urlStart = url.substring(0,7);
			if (urlStart == 'http://') {
				urlShow = url.substring(7);
			} else {
				urlLink = 'http://' + urlLink;
			}
			listItem += '<br /><a href="' + urlLink + '" class="targetBlank storeClickthrough">' + urlShow + '</a>';
		}
		listItem += '</p></div>';
		// listItem += '<div class="locationCategory"><p>'+category+'</p></div>';
		listItem += '<div class="locationContact"><p>';
		
		if (telephone) {
			listItem += text[languageCode]['tel'] + ': ' + telephone;
		}
		else {
			listItem += '&nbsp;';
		}
		listItem += '</p>';
		listItem += '<p class="locationDirections"><a href="' + FITFLOP.STORE.googleMapsUrlRoot + '?f=d&saddr='+ $JQ('#storePostcode').attr('value') + ','+ $JQ('OPTION:selected', $JQ('#storeCountry')).text() +'&daddr='+ address.replace('<br />',',') +','+ city +','+ postCode + ',' + country +'&hl=' + languageCode + '&geocode=&mra=ls&sll='+ directionsStart.lat() +','+ directionsStart.lng() +'&sspn='+ directionsEnd.lat() +','+ directionsEnd.lng() +'&ie=UTF8" class="targetBlank">' + text[languageCode]['showDirections'] + '</a></p></div>';
		listItem += '</div>';
		listItem += '</div>';
		$JQ('#locations').append(listItem);

		//$JQ('#locations #'+id+' h3 a').bind('mouseup', function() {
		//	GEvent.trigger(marker, 'click');
		//});
	},

	clearMapPanel: function() {
		$JQ('#locations').html('');
	}
};


// *******************************************************************************************


FITFLOP.FORM = {

	bindCharacterCount: function() {
		$JQ('#writeReview').unbind('keydown');
		$JQ('#writeReview').bind('keydown', function(event) {
			element = event.target;
			if ($JQ('#writeReview').val().length >= 1000 && event.keyCode != 8 && event.keyCode != 46) {
				event.stopPropagation();
				return false;
			}
		});

		$JQ('#writeReview').unbind('keyup');
		$JQ('#writeReview').bind('keyup', function() {
			// Truncate to 1000 in case the user has pasted
			if ($JQ('#writeReview').val().length >= 1000) {
				$JQ('#writeReview').val($JQ('#writeReview').val().substr(0, 1000));
			}
			// Report the remaining characters
			$JQ('#charsRemaining').text((1000 - $JQ('#writeReview').val().length) + ' characters remaining');
		});
	},

	panelFieldsForAjaxSubmit: function() {
		$JQ('.panelOutside').each(function() {
			var panelDiv = $JQ(this);
			if (panelDiv.attr('id') != 'panelStoreFinder') {
				var formCount = 0;
				$JQ('form', panelDiv).each(function() {
					formCount++;
					var panelForm = $JQ(this);
					var formID = panelDiv.attr('id') + '_form_' + formCount;
					var formTarget = panelDiv.attr('id') + '_target_' + formCount;
					panelForm.attr('id', formID);
					$JQ('input', panelForm).each(function() {
						var formField = $JQ(this);
						if (formField.attr('type') == 'submit') {
							formField.unbind('click');
							formField.bind('click', function() {
								FITFLOP.FORM.submitForm(formField.attr('id'));
								return false;
							});
						}
					});
					if (panelForm.attr('enctype') == 'multipart/form-data') {
						$JQ('div:nth(0)', panelForm).prepend('<input  name="ajax" type="hidden" value="2" />');
						panelForm.parent().append('<iframe class="iframeHidden" id="' + formTarget + '" name="' + formTarget + '" src="#"></iframe>');
						panelForm.attr('target', formTarget);
					} else {
						$JQ('div:nth(0)', panelForm).prepend('<input name="ajax" type="hidden" value="1" />');
					}
				});
			}
		});
	},

	commentsFieldsForAjaxSubmit: function() {
		var formCount = 0;
		$JQ('.addList form').each(function() {
			formCount++;
			var commentsForm = $JQ(this);
			var formID = 'comments_form_' + formCount;
			commentsForm.attr('id', formID);
			$JQ('input', commentsForm).each(function() {
				var formField = $JQ(this);
				if (formField.attr('type') == 'submit') {
					formField.unbind('click');
					formField.bind('click', function() {
						FITFLOP.FORM.submitFormComments('#' + formID);
						return false;
					});
				}
			});
			$JQ('div:nth(0)', commentsForm).prepend('<input name="ajax" type="hidden" value="1" />');
		});
	},

	sendFieldsForAjaxSubmit: function() {
		var formCount = 0;
		$JQ('.sendList form').each(function() {
			formCount++;
			var sendForm = $JQ(this);
			var formID = 'send_form_' + formCount;
			sendForm.attr('id', formID);
			$JQ('input', sendForm).each(function() {
				var formField = $JQ(this);
				if (formField.attr('type') == 'submit') {
					formField.unbind('click');
					formField.bind('click', function() {
						FITFLOP.FORM.submitFormSend('#' + formID);
						return false;
					});
				}
			});
			$JQ('div:nth(0)', sendForm).prepend('<input name="ajax" type="hidden" value="1" />');
		});
	},

	submitForm: function(submitClicked) {
		var formID = '#' + $JQ('#' + submitClicked).parents('form').attr('id');
		if (FITFLOP.FORM.stopDoubleSubmit(formID)) return;
		if(submitClicked == 'submitLogin') {
			FITFLOP.FORM.submitFormLogin(formID);
		}
		if(submitClicked == 'submitForgot') {
			FITFLOP.FORM.submitFormForgot(formID);
		}
		if(submitClicked == 'submitLogout') {
			FITFLOP.FORM.submitFormLogout(formID);
		}
		if(submitClicked == 'submitRegister') {
			FITFLOP.FORM.submitFormRegister(formID);
		}
		if(submitClicked == 'submitWrite') {
			FITFLOP.FORM.submitFormWrite(formID);
		}
		if(submitClicked == 'submitSubscribe') {
			FITFLOP.FORM.submitFormSubscribe(formID);
		}
		if(submitClicked == 'submitUnsubscribe') {
			FITFLOP.FORM.submitFormUnsubscribe(formID);
		}
	},

	submitFormLogin: function(formID) {

		var errorMessage = '';
		var errorCount = 0;
		var validationResult = '';
		validationResult = FITFLOP.FORM.validateInputText($JQ('#loginUsername'));
		if (validationResult != 1) {
			errorMessage += validationResult;
			errorCount++;
		}
		validationResult = FITFLOP.FORM.validateInputPassword($JQ('#loginPassword'));
		if (validationResult != 1) {
			errorMessage += validationResult;
			errorCount++;
		}
		if (errorCount == 0) {
			FITFLOP.FORM.postInputsAsParameters($JQ(formID), function(passedParameters) {
				passedParameters['submitLogin'] = $JQ('#submitLogin').val();
				var redirect = passedParameters['redirect'];
				FITFLOP.GENERIC.showAjaxLoading();
				$JQ.post($JQ(formID).attr('action').replace("/#message",""), passedParameters, function(data) {
					if (data == 1) {
						pageTracker._trackPageview('/formsubmit/login/');
						FITFLOP.FORM.redirectAfterSuccess(redirect, 'successLogin');
					} else {
						var response = eval('(' + data + ')');
						var errorsLeft = 0;
						$JQ.each(response, function(key, val) {
							errorsLeft++;
						});
						$JQ.each(response, function(key,val) {
							errorsLeft--;
							errorCount++;
							errorMessage += FITFLOP.FORM.createErrorItem(key, val);
							if (errorsLeft == 0) {
								FITFLOP.FORM.showErrorMessage(errorCount, errorMessage, formID);
							}
						});
						FITFLOP.PANEL.bindPanelControls();
						FITFLOP.GENERIC.hideAjaxLoading();
					}
				});
			});
		} else {
			FITFLOP.FORM.showErrorMessage(errorCount, errorMessage, formID);
		}
	},

	submitFormForgot: function(formID) {

		var errorMessage = '';
		var errorCount = 0;
		var validationResult = '';
		validationResult = FITFLOP.FORM.validateInputText($JQ('#loginUsername'));
		if (validationResult != 1) {
			errorMessage += validationResult;
			errorCount++;
		}
		if (errorCount == 0) {
			FITFLOP.FORM.postInputsAsParameters($JQ(formID), function(passedParameters) {
				passedParameters['submitForgot'] = $JQ('#submitForgot').val();
				var redirect = passedParameters['redirect'];
				FITFLOP.GENERIC.showAjaxLoading();
				$JQ.post($JQ(formID).attr('action').replace("/#message",""), passedParameters, function(data) {
					if (data == 1) {
						pageTracker._trackPageview('/formsubmit/forgot/');
						FITFLOP.FORM.redirectAfterSuccess(redirect, 'successForgot');
					} else {
						var response = eval('(' + data + ')');
						var errorsLeft = 0;
						$JQ.each(response, function(key, val) {
							errorsLeft++;
						});
						$JQ.each(response, function(key,val) {
							errorsLeft--;
							errorCount++;
							errorMessage += FITFLOP.FORM.createErrorItem(key, val);
							if (errorsLeft == 0) {
								FITFLOP.FORM.showErrorMessage(errorCount, errorMessage, formID);
							}
						});
						FITFLOP.PANEL.bindPanelControls();
						FITFLOP.GENERIC.hideAjaxLoading();
					}
				});
			});
		} else {
			FITFLOP.FORM.showErrorMessage(errorCount, errorMessage, formID);
		}
	},

	submitFormLogout: function(formID) {
		FITFLOP.FORM.postInputsAsParameters($JQ(formID), function(passedParameters) {
			passedParameters['submitLogout'] = $JQ('#submitLogout').val();
			FITFLOP.GENERIC.showAjaxLoading();
			$JQ.post($JQ(formID).attr('action').replace("/#message",""), passedParameters, function(data) {
				pageTracker._trackPageview('/formsubmit/logout/');
				FITFLOP.FORM.redirectAfterSuccess(passedParameters['redirect'], 'successLogout');
			});
		});
	},

	submitFormRegister: function(formID) {

		var errorMessage = '';
		var errorCount = 0;
		var validationResult = '';
		validationResult = FITFLOP.FORM.validateInputText($JQ('#registerFirst'));
		if (validationResult != 1) {
			errorMessage += validationResult;
			errorCount++;
		}
		validationResult = FITFLOP.FORM.validateInputText($JQ('#registerLast'));
		if (validationResult != 1) {
			errorMessage += validationResult;
			errorCount++;
		}
		validationResult = FITFLOP.FORM.validateInputText($JQ('#registerEmail'));
		if (validationResult != 1) {
			errorMessage += validationResult;
			errorCount++;
		} else {
			validationResult = FITFLOP.FORM.validateInputEmail($JQ('#registerEmail'));
			if (validationResult != 1) {
				errorMessage += validationResult;
				errorCount++;
			}
		}
		validationResult = FITFLOP.FORM.validateInputSelect($JQ('#registerCountry'));
		if (validationResult != 1) {
			errorMessage += validationResult;
			errorCount++;
		}
		/*validationResult = FITFLOP.FORM.validateInputText($JQ('#registerCity'));
		if (validationResult != 1) {
			errorMessage += validationResult;
			errorCount++;
		}*/
		if ($JQ('#registerUserID').size() == 0) {
			validationResult = FITFLOP.FORM.validateInputPassword($JQ('#registerPassword'));
			if (validationResult != 1) {
				errorMessage += validationResult;
				errorCount++;
			}
		}
		if (errorCount == 0) {
			FITFLOP.GENERIC.showAjaxLoading();
			$JQ('form').removeClass('activeForm');
			$JQ(formID).addClass('activeForm');
			pageTracker._trackPageview('/formsubmit/register/');
			$JQ(formID).submit();
		} else {
			FITFLOP.FORM.showErrorMessage(errorCount, errorMessage, formID);
		}
	},

	submitFormWrite: function(formID) {
		var errorMessage = '';
		var errorCount = 0;
		var validationResult = '';

		validationResult = FITFLOP.FORM.validateInputText($JQ('#writeFirst'));
		if (validationResult != 1) {
			errorMessage += validationResult;
			errorCount++;
		}
		validationResult = FITFLOP.FORM.validateInputText($JQ('#writeLast'));
		if (validationResult != 1) {
			errorMessage += validationResult;
			errorCount++;
		}
		validationResult = FITFLOP.FORM.validateInputText($JQ('#writeEmail'));
		if (validationResult != 1) {
			errorMessage += validationResult;
			errorCount++;
		} else {
			validationResult = FITFLOP.FORM.validateInputEmail($JQ('#writeEmail'));
			if (validationResult != 1) {
				errorMessage += validationResult;
				errorCount++;
			}
		}
		validationResult = FITFLOP.FORM.validateInputSelect($JQ('#writeCountry'));
		if (validationResult != 1) {
			errorMessage += validationResult;
			errorCount++;
		}
		validationResult = FITFLOP.FORM.validateInputCheckbox($JQ('#writeAgree'));
		if (validationResult != 1) {
			errorMessage += validationResult;
			errorCount++;
		}
		/*validationResult = FITFLOP.FORM.validateInputText($JQ('#writeCity'));
		if (validationResult != 1) {
			errorMessage += validationResult;
			errorCount++;
		}*/
		validationResult = FITFLOP.FORM.validateInputArea($JQ('#writeReview'));
		if (validationResult != 1) {
			errorMessage += validationResult;
			errorCount++;
		}
		if (errorCount == 0) {
			FITFLOP.GENERIC.showAjaxLoading();
			$JQ('form').removeClass('activeForm');
			$JQ(formID).addClass('activeForm');
			pageTracker._trackPageview('/formsubmit/write-testimonial/');
			$JQ(formID).submit();
		} else {
			FITFLOP.FORM.showErrorMessage(errorCount, errorMessage, formID);
		}
	},

	submitFormImage: function(imageField) {
		formID = '#' + $JQ('#' + imageField.id).parents('form').attr('id');
		$JQ('.submitFileField', formID).val('1');
		FITFLOP.GENERIC.showAjaxLoading();
		$JQ('form').removeClass('activeForm');
		$JQ(formID).addClass('activeForm');
		$JQ('.textError').remove();
		$JQ(formID).submit();
		$JQ('.submitFileField', formID).val('0');
	},

	submitFormComments: function(formID) {
		if (FITFLOP.FORM.stopDoubleSubmit(formID)) return;
		var errorMessage = '';
		var errorCount = 0;
		var validationResult = '';
		var nameToID = {};
		$JQ('*', $JQ(formID)).each(function() {
			var formElement = $JQ(this);
			if ((formElement.attr('name') != '') && (formElement.attr('id') != '')) {
				nameToID[formElement.attr('name')] = '#' + formElement.attr('id');
			}
		});
		validationResult = FITFLOP.FORM.validateInputText($JQ(nameToID['commentName']));
		if (validationResult != 1) {
			errorMessage += validationResult;
			errorCount++;
		}
		validationResult = FITFLOP.FORM.validateInputText($JQ(nameToID['commentEmail']));
		if (validationResult != 1) {
			errorMessage += validationResult;
			errorCount++;
		} else {
			validationResult = FITFLOP.FORM.validateInputEmail($JQ(nameToID['commentEmail']));
			if (validationResult != 1) {
				errorMessage += validationResult;
				errorCount++;
			}
		}
		validationResult = FITFLOP.FORM.validateInputArea($JQ(nameToID['commentComment']));
		if (validationResult != 1) {
			errorMessage += validationResult;
			errorCount++;
		}
		if (errorCount == 0) {
			FITFLOP.FORM.postInputsAsParameters($JQ(formID), function(passedParameters) {
				passedParameters['commentSubmit'] = $JQ(nameToID['commentSubmit']).val();
				var redirect = passedParameters['redirect'];
				FITFLOP.GENERIC.showAjaxLoading();
				$JQ.post($JQ(formID).attr('action').replace("/#message",""), passedParameters, function(data) {
					if (data == 1) {
						pageTracker._trackPageview('/formsubmit/comment/');
						FITFLOP.FORM.redirectAfterSuccess(redirect, 'successComment');
					} else {
						var response = eval('(' + data + ')');
						var errorsLeft = 0;
						$JQ.each(response, function(key, val) {
							errorsLeft++;
						});
						$JQ.each(response, function(key,val) {
							errorsLeft--;
							errorCount++;
							errorMessage += FITFLOP.FORM.createErrorItem(key, val);
							if (errorsLeft == 0) {
								FITFLOP.FORM.showErrorMessage(errorCount, errorMessage, formID);
							}
						});
						FITFLOP.PANEL.bindPanelControls();
						FITFLOP.GENERIC.hideAjaxLoading();
					}
				});
			});
		} else {
			FITFLOP.FORM.showErrorMessage(errorCount, errorMessage, formID);
		}
	},

	submitFormSend: function(formID) {
		if (FITFLOP.FORM.stopDoubleSubmit(formID)) return false;
		var errorMessage = '';
		var errorCount = 0;
		var validationResult = '';
		var nameToID = {};
		$JQ('*', $JQ(formID)).each(function() {
			var formElement = $JQ(this);
			if ((formElement.attr('name') != '') && (formElement.attr('id') != '')) {
				nameToID[formElement.attr('name')] = '#' + formElement.attr('id');
			}
		});
		validationResult = FITFLOP.FORM.validateInputText($JQ(nameToID['sendName']));
		if (validationResult != 1) {
			errorMessage += validationResult;
			errorCount++;
		}
		validationResult = FITFLOP.FORM.validateInputText($JQ(nameToID['sendEmail']));
		if (validationResult != 1) {
			errorMessage += validationResult;
			errorCount++;
		} else {
			validationResult = FITFLOP.FORM.validateInputEmail($JQ(nameToID['sendEmail']));
			if (validationResult != 1) {
				errorMessage += validationResult;
				errorCount++;
			}
		}
		validationResult = FITFLOP.FORM.validateInputText($JQ(nameToID['sendToName']));
		if (validationResult != 1) {
			errorMessage += validationResult;
			errorCount++;
		}
		validationResult = FITFLOP.FORM.validateInputText($JQ(nameToID['sendToEmail']));
		if (validationResult != 1) {
			errorMessage += validationResult;
			errorCount++;
		} else {
			validationResult = FITFLOP.FORM.validateInputEmail($JQ(nameToID['sendToEmail']));
			if (validationResult != 1) {
				errorMessage += validationResult;
				errorCount++;
			}
		}
		if (errorCount == 0) {
			FITFLOP.FORM.postInputsAsParameters($JQ(formID), function(passedParameters) {
				passedParameters['sendSubmit'] = $JQ(nameToID['sendSubmit']).val();
				var redirect = passedParameters['redirect'];
				FITFLOP.GENERIC.showAjaxLoading();
				$JQ.post($JQ(formID).attr('action').replace("/#message",""), passedParameters, function(data) {
					if (data == 1) {
						pageTracker._trackPageview('/formsubmit/send-to-friend/');
						FITFLOP.FORM.redirectAfterSuccess(redirect, 'successSend');
					} else {
						var response = eval('(' + data + ')');
						var errorsLeft = 0;
						$JQ.each(response, function(key, val) {
							errorsLeft++;
						});
						$JQ.each(response, function(key,val) {
							errorsLeft--;
							errorCount++;
							errorMessage += FITFLOP.FORM.createErrorItem(key, val);
							if (errorsLeft == 0) {
								FITFLOP.FORM.showErrorMessage(errorCount, errorMessage, formID);
							}
						});
						FITFLOP.PANEL.bindPanelControls();
						FITFLOP.GENERIC.hideAjaxLoading();
					}
				});
			});
		} else {
			FITFLOP.FORM.showErrorMessage(errorCount, errorMessage, formID);
		}
	},

	submitFormSubscribe: function(formID) {
		var errorMessage = '';
		var errorCount = 0;
		var validationResult = '';
		var nameToID = {};
		nameToID['systemMessage'] = 'systemMessage';
		$JQ('*', $JQ(formID)).each(function() {
			var formElement = $JQ(this);
			if ((formElement.attr('name') != '') && (formElement.attr('id') != '')) {
				nameToID[formElement.attr('name')] = formElement.attr('id');
			}
		});
		validationResult = FITFLOP.FORM.validateInputText($JQ('#subscribeFirst'));
		if (validationResult != 1) {
			errorMessage += validationResult;
			errorCount++;
		}
		validationResult = FITFLOP.FORM.validateInputText($JQ('#subscribeLast'));
		if (validationResult != 1) {
			errorMessage += validationResult;
			errorCount++;
		}
		validationResult = FITFLOP.FORM.validateInputSelect($JQ('#subscribeCountry'));
		if (validationResult != 1) {
			errorMessage += validationResult;
			errorCount++;
		}
		validationResult = FITFLOP.FORM.validateInputText($JQ('#subscribeEmail'));
		if (validationResult != 1) {
			errorMessage += validationResult;
			errorCount++;
		} else {
			validationResult = FITFLOP.FORM.validateInputEmail($JQ('#subscribeEmail'));
			if (validationResult != 1) {
				errorMessage += validationResult;
				errorCount++;
			}
		}
		if (errorCount == 0) {
			FITFLOP.FORM.postInputsAsParameters($JQ(formID), function(passedParameters) {
				passedParameters['subscribeYes'] = 'subscribe';
				passedParameters['country'] = $JQ('OPTION:selected', $JQ('#subscribeCountry')).attr('value');
				var redirect = passedParameters['redirect'];
				FITFLOP.GENERIC.showAjaxLoading();
				$JQ.post($JQ(formID).attr('action').replace("/#message",""), passedParameters, function(data) {
					if (data == 1) {
						pageTracker._trackPageview('/formsubmit/subscribe/');
						FITFLOP.FORM.redirectAfterSuccess(redirect, 'successSubscribe');
					} else {
						var response = eval('(' + data + ')');
						var errorsLeft = 0;
						$JQ.each(response, function(key, val) {
							errorsLeft++;
						});
						$JQ.each(response, function(key,val) {
							errorsLeft--;
							errorCount++;
							errorMessage += FITFLOP.FORM.createErrorItem(nameToID[key], val);
							if (errorsLeft == 0) {
								FITFLOP.FORM.showErrorMessage(errorCount, errorMessage, formID);
							}
						});
						FITFLOP.PANEL.bindPanelControls();
						FITFLOP.GENERIC.hideAjaxLoading();
					}
				});
			});
		} else {
			FITFLOP.FORM.showErrorMessage(errorCount, errorMessage, formID);
		}
	},

	submitFormUnsubscribe: function(formID) {
		var errorMessage = '';
		var errorCount = 0;
		var validationResult = '';
		var nameToID = {};
		nameToID['systemMessage'] = 'systemMessage';
		$JQ('*', $JQ(formID)).each(function() {
			var formElement = $JQ(this);
			if ((formElement.attr('name') != '') && (formElement.attr('id') != '')) {
				nameToID[formElement.attr('name')] = formElement.attr('id');
			}
		});
		validationResult = FITFLOP.FORM.validateInputText($JQ('#subscribeEmail'));
		if (validationResult != 1) {
			errorMessage += validationResult;
			errorCount++;
		} else {
			validationResult = FITFLOP.FORM.validateInputEmail($JQ('#subscribeEmail'));
			if (validationResult != 1) {
				errorMessage += validationResult;
				errorCount++;
			}
		}
		if (errorCount == 0) {
			FITFLOP.FORM.postInputsAsParameters($JQ(formID), function(passedParameters) {
				passedParameters['subscribeNo'] = 'un-subscribe';
				var redirect = passedParameters['redirect'];
				FITFLOP.GENERIC.showAjaxLoading();
				$JQ.post($JQ(formID).attr('action').replace("/#message",""), passedParameters, function(data) {
					if (data == 1) {
						pageTracker._trackPageview('/formsubmit/unsubscribe/');
						FITFLOP.FORM.redirectAfterSuccess(redirect, 'successUnsubscribe');
					} else {
						var response = eval('(' + data + ')');
						var errorsLeft = 0;
						$JQ.each(response, function(key, val) {
							errorsLeft++;
						});
						$JQ.each(response, function(key,val) {
							errorsLeft--;
							errorCount++;
							errorMessage += FITFLOP.FORM.createErrorItem(nameToID[key], val);
							if (errorsLeft == 0) {
								FITFLOP.FORM.showErrorMessage(errorCount, errorMessage, formID);
							}
						});
						FITFLOP.PANEL.bindPanelControls();
						FITFLOP.GENERIC.hideAjaxLoading();
					}
				});
			});
		} else {
			FITFLOP.FORM.showErrorMessage(errorCount, errorMessage, formID);
		}
	},

	stopDoubleSubmit: function(formID) {
		if ($JQ('.ajaxLoading').css('display') == 'inline'
		    && FITFLOP.FORM.stopDoubleSubmitData
		    && FITFLOP.FORM.stopDoubleSubmitData.formID == formID
		    // Block double submit for 10 seconds - long enough to allow for working server, short enough to allow retry after eg connectivity issue
		    && (new Date() - FITFLOP.FORM.stopDoubleSubmitData.time) < 10000) {
			// Could possibly give some feedback here.  Perhaps change text on submit button.
			return true;
		}
		FITFLOP.FORM.stopDoubleSubmitData = {time: new Date(),formID: formID};
		return false;
	},

	ajaxResponseHandler: function(response) {
		var errorMessage = '';
		var errorCount = 0;
		var formID = '#' + $JQ('.activeForm').attr('id');

		if (response == 1) {
			var redirectType = '';
			if ($JQ('#registerUserID', formID).size() > 0) {
				if ($JQ('#registerUserID', formID).val() == '') {
					redirectType = 'successRegister';
				} else {
					redirectType = 'successEdit';
				}
			}
			if ($JQ('#writeUserID', formID).size() > 0) {
				redirectType = 'successWrite';
			}
			$JQ('*', $JQ(formID)).each(function() {
				var formElement = $JQ(this);
				if (formElement.attr('name') == 'redirect') {
					FITFLOP.FORM.redirectAfterSuccess(formElement.val(), redirectType);
					return false;
				}
			});
		}
		//image has been uploaded successfully - now show image
		 if (response.length > 1) {
			$JQ('.thumbnail').attr('src',response);
			FITFLOP.GENERIC.hideAjaxLoading();
		}

		else {
			var nameToID = {};
			nameToID['systemMessage'] = 'systemMessage';
			var elementsLeft = $JQ('*', $JQ(formID)).size();
			$JQ('*', $JQ(formID)).each(function() {
				elementsLeft--;
				var formElement = $JQ(this);
				if ((formElement.attr('name')) && (formElement.attr('id'))) {
					nameToID[formElement.attr('name')] = formElement.attr('id');
				}
				if (elementsLeft == 0) {
					var errorsLeft = 0;
					$JQ.each(response, function(key, val) {
						errorsLeft++;
					});
					$JQ.each(response, function(key, val) {
						errorsLeft--;
						errorCount++;
						errorMessage += FITFLOP.FORM.createErrorItem(nameToID[key], val);
						if (errorsLeft == 0) {
							FITFLOP.FORM.showErrorMessage(errorCount, errorMessage, formID);
						}
					});
					FITFLOP.PANEL.bindPanelControls();
					FITFLOP.GENERIC.hideAjaxLoading();
				}
			});
		}
	},

	getInputsAsParameters: function(formElement, callback) {
		var parameterString = '?';
		var inputsLeft = $JQ('input', formElement).size();
		$JQ('input', formElement).each(function() {
			var currentInput = $JQ(this);
			inputsLeft--;
			parameterString += currentInput.attr('name') + '=' + currentInput.val().replace(' ','+') + '&';
			if (inputsLeft == 0) {
				parameterString = parameterString.slice(0, -1);
				if (callback) {
					callback.call(this, parameterString);
				}
			}
		});
	},

	postInputsAsParameters: function(formElement, callback) {
		var parameterArray = {};
		var inputsLeft = $JQ('input', formElement).size();
		var areasLeft = $JQ('textarea', formElement).size();
		$JQ('input', formElement).each(function() {
			var currentInput = $JQ(this);
			inputsLeft--;
			if (currentInput.attr('type') != 'submit') {
				parameterArray[currentInput.attr('name')] = currentInput.val();
			}
			if (inputsLeft == 0) {
				if (areasLeft > 0) {
					$JQ('textarea', formElement).each(function() {
						var currentArea = $JQ(this);
						areasLeft--;
						parameterArray[currentArea.attr('name')] = currentArea.val();
						if (areasLeft == 0) {
							if (callback) {
								callback.call(this, parameterArray);
							}
						}
					});
				} else {
					if (callback) {
						callback.call(this, parameterArray);
					}
				}
			}
		});
	},

	redirectAfterSuccess: function(redirectURL, redirectType) {
		redirectURL = redirectURL.split('#')[0];
		if (redirectURL == '') {
			redirectURL = '/';
		}
		FITFLOP.GENERIC.hideAjaxLoading();
		var windowHeight = $JQ(window).height();
		var documentHeight = $JQ(document).height();
		if (documentHeight > windowHeight) {
			windowHeight = documentHeight;
		}
		$JQ('body').append('<div class="popupBG"></div>');
		$JQ('.popupBG').css('height', windowHeight + 'px');
		$JQ('body').append('<div class="popupFG" id="popupFG"><h2>' + text[languageCode][redirectType] + '</h2><p class="secondaryButton"><a href="' + redirectURL + '">' + text[languageCode]['continue'] + '</a></p></div>');
		$JQ('#popupFG a').focus();
		$JQ(window).scrollTo('0px', speedScroll);
	},

	validateInputEmail: function(fieldElement) {
		var reg = /^([A-Za-z0-9_\-\.+])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		var emailText = $JQ(fieldElement).val();
		var fieldID = FITFLOP.FORM.getFieldID(fieldElement.attr('id'));
		if(reg.test(emailText) == false) {
			return FITFLOP.FORM.createErrorItem(fieldID, 'emailInvalid');
		} else {
			return 1;
		}
	},

	validateInputText: function(fieldElement) {
		var fieldText = $JQ(fieldElement).val();
		var fieldID = FITFLOP.FORM.getFieldID(fieldElement.attr('id'));
		if (fieldText == '') {
			return FITFLOP.FORM.createErrorItem(fieldID, 'fieldEmpty');
		} else {
			return 1;
		}
	},

	validateInputPassword: function(fieldElement) {
		var fieldText = $JQ(fieldElement).val();
		var fieldID = FITFLOP.FORM.getFieldID(fieldElement.attr('id'));
		if (fieldText.length < 6) {
			return FITFLOP.FORM.createErrorItem(fieldID, 'fieldPassword');
		} else {
			return 1;
		}
	},

	validateInputSelect: function(fieldElement) {
		var fieldText = $JQ(fieldElement).val();
		var fieldID = FITFLOP.FORM.getFieldID(fieldElement.attr('id'));
		if (fieldText == '') {
			return FITFLOP.FORM.createErrorItem(fieldID, 'fieldSelect');
		} else {
			return 1;
		}
	},

	validateInputArea: function(fieldElement) {
		var fieldText = $JQ(fieldElement).val();
		var fieldID = FITFLOP.FORM.getFieldID(fieldElement.attr('id'));
		if (fieldText == '') {
			return FITFLOP.FORM.createErrorItem(fieldID, 'fieldEmpty');
		} else {
			return 1;
		}
	},

	validateInputCheckbox: function(fieldElement) {
		var fieldID = FITFLOP.FORM.getFieldID(fieldElement.attr('id'));
		if (!fieldElement.attr('checked')) {
			return FITFLOP.FORM.createErrorItem(fieldID, 'fieldCheckbox');
		} else {
			return 1;
		}
	},

	getFieldID: function(fieldIDIn) {
		var fieldIDOut = fieldIDIn;
		if (fieldIDOut.indexOf('_') >= 0) {
			fieldIDOut = fieldIDOut.substring(0, fieldIDOut.indexOf('_'));
		}
		return fieldIDOut;
	},

	showErrorMessage: function(errorCount, errorMessage, formID) {
		$JQ('.textError').remove();
		errorMessage = '<ul class="textError">' + errorMessage + '</ul>';
		if (errorCount == 1) {
			errorMessage = '<p class="textError">' + text[languageCode]['errorMessage'] + '</p>' + errorMessage;
		} else {
			errorMessage = '<p class="textError">' + text[languageCode]['errorMessages'] + '</p>' + errorMessage;
		}
		if ($JQ(formID).parent().attr('class') == 'addList') {
			var addHeader = $JQ(formID).parents('.addList').siblings('.addHeading');
			$JQ('.headingLeft', addHeader).append(errorMessage);
		} else if ($JQ(formID).parent().attr('class') == 'sendList') {
			var sendHeader = $JQ(formID).parents('.sendList').siblings('.sendHeading');
			$JQ('.headingLeft', sendHeader).append(errorMessage);
		} else {
			var panelHeader = $JQ(formID).siblings('.panelHeader');
			if ($JQ('.headingLeft', panelHeader).length > 0) {
				$JQ('.headingLeft', panelHeader).append(errorMessage);
			} else {
				panelHeader.append(errorMessage);
			}
			if (panelHeader.parents('.footer').size() > 0) {
				var currentPanel = panelHeader.parents('.panelOutside');
				currentPanel.css('height','auto');
				var currentPanelHeight = parseInt(currentPanel.height()) + parseInt(1);
				currentPanel.css('top',-1 * currentPanelHeight);
			}
		}
	},

	createErrorItem: function(fieldID, messageType) {
		if (fieldID != 'systemMessage') {
			return '<li>' + text[languageCode][messageType].replace('FIELDNAME', '<span>' + text[languageCode][fieldID] + '</span>') + '</li>';
		} else {
			return '<li>' + messageType + '</li>';
		}
	},

	prepopulateFieldsText: function(classPrefix) {
		classPrefix = classPrefix.toLowerCase()
		$JQ('input.fieldText').each(function() {
			var eachElement = $JQ(this);
			if (!eachElement.attr('readonly')) {
				if (eachElement.attr('class').toLowerCase().indexOf(classPrefix) >= 0) {
					var defaultFieldValue = FITFLOP.FORM.getDefaultFieldValue(eachElement, classPrefix)
					eachElement.val(defaultFieldValue);

					eachElement.unbind('focus');
					eachElement.bind('focus', function() {
						if (eachElement.val() == defaultFieldValue) {
							eachElement.val('');
						}
					});

					eachElement.unbind('blur');
					eachElement.bind('blur', function() {
						if (eachElement.val() == '') {
							eachElement.val(defaultFieldValue);
						}
					});
				}
			}
		});
	},

	getDefaultFieldValue: function(theElement, classPrefix) {
		var classNames = theElement.attr('class').split(' ');
		for (var i=0; i<classNames.length; i++) {
			if (classNames[i].toLowerCase().indexOf(classPrefix) >= 0) {
				var defaultFieldValue = classNames[i].substring(classPrefix.length).replace(/_/g, ' ').replace('DOTS', '...');
				break;
			}
		}
		return defaultFieldValue;
	}
};

FITFLOP.COMPETITION = {
    initialize: function() {
	FITFLOP.COMPETITION.setStyles();
    },
    setStyles: function() {
	$JQ('.competitionEntries').find('tr:odd').addClass('odd');
    }
}