
var FC = {
	
	vars: {
		el: 'ul.rating',
		roles: ['Administrator',
		        'Chairman',
		        'Child Protection Officer',
		        'Club Main Contact',
		        'Clubmark Lead',
		        'Coach',
		        'Committee Member',
		        'Competition Organiser',
		        'Head Coach',
		        'Junior Contact',
		        'Match Secretary',
		        'Mini Tennis Contact',
		        'Secretary',
		        'Strength And Condition Coach',
		        'Treasurer',
		        'Veterans Contact',
		        'Volunteer Coordinator'],	
		shortRole: '',
		currentRoles: '',
		newRoles: '',
		rolesObject: {
			memberId: '',
			clubId: '',
			memberRoles: []
		},
		rolesURL: '/Places-To-Play/Roles-Listener/?p2p=',	//destination for roles ajax panel data
		countdownMax: '200'										
	},

	showHideHelp: function(){
		//$('.js-helptext').hide();
		
		$('.js-helptext .inner .padding').append('<p class="floatright"><a href="#" class="hidehelp">Hide help</a></p>');
		
		$('.help a').bind('click', function(){
			var boxToOpen = "#" + $(this).attr('class');
			$(boxToOpen).slideDown(250);
			return false;
		});
		$('.helplink a').bind('click', function(){
			var boxToOpen = "#" + $(this).attr('class');
			$(boxToOpen).slideDown(250);
			return false;
		});
		$('.hidehelp').bind('click', function(){
			$(this).parents('.js-helptext').slideUp(250);
			return false;
		});
		
		
	},
	
	ratingMouseOver: function(){
		$(FC.vars.el).find("label").each(function(){
			$(this).bind("mouseover", function(){
				if($(this).hasClass('open')){
					$(this).find('img').attr({
						src: "/images/clubs/star-over.gif"
					})
				};
				$(this).parent().prevAll().each(function(){
					if ($(this).find("label").hasClass('open')) {
						$(this).find('img').attr({
							src: "/images/clubs/star-over.gif"
						});
					}
				});
			});				
		});
	},
	
	ratingFocus: function(){
		$(FC.vars.el).find("input").each(function(){
			$(this).bind("focus", function(){
				if($(this).siblings("label").hasClass('open')){
					$(this).siblings("label").find('img').attr({
						src: "/images/clubs/star-over.gif"
					})
				};
				$(this).parent().prevAll().each(function(){
					if ($(this).find("label").hasClass('open')) {
						$(this).find('img').attr({
							src: "/images/clubs/star-over.gif"
						});
					}
				});
			});				
		});
	},
	
	ratingMouseOut: function(){
		$(FC.vars.el).find("label").each(function(){
			$(this).bind("mouseout", function(){
				if($(this).hasClass('open')){
					$(this).find('img').attr({
						src: "/images/clubs/star-off.gif"
					})
				};
				$(this).parent().prevAll().each(function(){
					if ($(this).find("label").hasClass('open')) {
						$(this).find('img').attr({
							src: "/images/clubs/star-off.gif"
						});
					}
				});
			});
		});
	},
	
	ratingBlur: function(){
		$(FC.vars.el).find("input").each(function(){
			$(this).bind("blur", function(){
				if($(this).siblings("label").hasClass('open')){
					$(this).siblings("label").find('img').attr({
						src: "/images/clubs/star-off.gif"
					})
				};
				$(this).parent().prevAll().each(function(){
					if ($(this).find("label").hasClass('open')) {
						$(this).find('img').attr({
							src: "/images/clubs/star-off.gif"
						});
					}
				});
			});
		});
	},
			
	
	addRating: function(){
		
		// .club ul.rating li input{position:absolute;left:-9999px;top:-9999px;}
		$(FC.vars.el).find('input').css('position', 'absolute').css('left', '-9999em');
		// <img src="/images/clubs/star-off.gif" width="16" height="15" alt="1 star" />
		$(FC.vars.el).find("label").each(function(intIndex){
			$(this).empty().append($('<img src="/images/clubs/star-off.gif" width="16" height="15" alt="' + (intIndex + 1) + ' star" />'))
		})
		
		FC.ratingMouseOver();
		FC.ratingFocus();
		FC.ratingMouseOut();
		FC.ratingBlur();
		
		$(FC.vars.el).find("label").bind("click", function(){
			$(this).removeClass('open');
			
			$(this).parent().prevAll().find("label").removeClass('open');
			$(this).parent().prevAll().find('img').attr({
				src: "/images/clubs/star-on.gif"
			})
			
			$(this).parent().nextAll().find("label").addClass('open');
			$(this).parent().nextAll().find('img').attr({
				src: "/images/clubs/star-off.gif"
			})
			
			$(this).find('img').attr({
				src: "/images/clubs/star-on.gif"
			})
			
			$(this).next().attr("checked", true);
			
			return false;
		})
		
		$(FC.vars.el).find("input").bind("change", function(){
			$(this).siblings("label").removeClass('open');
			
			$(this).parent().prevAll().find("label").removeClass('open');
			$(this).parent().prevAll().find('img').attr({
				src: "/images/clubs/star-on.gif"
			})
			
			$(this).parent().nextAll().find("label").addClass('open');
			$(this).parent().nextAll().find('img').attr({
				src: "/images/clubs/star-off.gif"
			})
			
			$(this).siblings("label").find('img').attr({
				src: "/images/clubs/star-on.gif"
			})
			
			return false;
		})
		
	},
	
	initMembersList: function(){
		FC.membersList();
	},
	
	createMembersList: function(){
				
		for (var i = 0; i < FC.vars.roles.length; i++) {
			
			FC.vars.shortRole = FC.vars.roles[i].replace(/ /g, '');
			var currentRoles = $('.roleslist').parent().siblings('.roles').text().replace(/ /g, '');
			var checker = "";
			if (currentRoles.indexOf(FC.vars.shortRole) != -1)
			{
			    checker = 'checked="checked"';
			}
			
			($('<div></div>').addClass('field').addClass('clear')
				.append($('<input type="checkbox"' + checker + '></input>').attr('name', FC.vars.shortRole).attr('id', FC.vars.shortRole))
				.append($('<label></label>').attr('for', FC.vars.shortRole).text(FC.vars.roles[i]))
			)
			.insertBefore($('.roleslist .close'));
		}
	},
	
	updateMembersList: function(){
		FC.vars.currentRoles = $('.roleslist').parent().siblings('.roles').text().replace(/ /g, '').toLowerCase();		
		$('.roleslist input').each(function(){
			if(FC.vars.currentRoles.indexOf($(this).attr('id')) > -1){
				$(this).attr('checked', 'checked');
			}
		})
	},
	
	newRoles: function(){
		FC.vars.newRoles = ''
		$('.roleslist input:checked').each(function(){
			FC.vars.newRoles = FC.vars.newRoles + $(this).siblings('label').text() + ', '
		});
		if(FC.vars.newRoles == ''){
			FC.vars.newRoles = ' '
		}else{
			FC.vars.newRoles = FC.vars.newRoles.substring(0, FC.vars.newRoles.length - 2)	
		}
			
		$('.roleslist').parent().siblings('.roles').text(FC.vars.newRoles);
	},
	
	membersList: function(){
		$('.memberslist').find('.update > a').bind('click', function(){
			$(this).parents('tr').addClass('selected');
			($('<div></div>').addClass('roleslist').css('display', 'none')
				.append($('<div></div>').addClass('roles-top'))
				.append($('<div></div>').addClass('roles-content')
					.append($('<div></div>').addClass('padding')
						.append($('<fieldset></fieldset>')
							.append($(this).parent().siblings('roles').find('span'))
							.append($('<a></a>').addClass('close').attr('href', '#').text('Save').bind('click', function(){
								this.style.cursor = 'wait';
								FC.submitMembersList()
								return false;
							}))
						)
					)
				)
				.append($('<div></div>').addClass('roles-bottom'))
			)
			.appendTo($(this).parent());
			$('.memberslist').find('.update > a').removeAttr('href').unbind('click');
			$('.memberslist').find('.update > a > img').attr('src', '/images/clubs/btn-update-disabled.gif');
			FC.createMembersList();
			FC.updateMembersList();
			$(this).siblings('.roleslist').slideDown(250);
			$(this).remove();
			return false;
		})
	},
	
	resetMembersList: function(){
		$('.roleslist').slideUp(250, function(){
			($('<a></a>').attr('href', '#')
				.append($('<img></img>').attr('height', '20').attr('width', '63').attr('alt', 'Update').attr('src', '/images/clubs/btn-update.gif'))
			).prependTo($(this).parent());
			$(this).remove();
			$('.memberslist').find('tr').removeClass('selected');
			$('.memberslist').find('.update > a').attr('href', '#');
			$('.memberslist').find('.update > a > img').attr('src', '/images/clubs/btn-update.gif');
			FC.membersList();
		});
	},
	
	submitMembersList: function(){
		document.body.style.cursor = 'wait';
		FC.vars.rolesObject.memberId = $('.roleslist').siblings()[0].value;
		FC.vars.rolesObject.clubId = $('.roleslist').siblings()[1].value;
		FC.vars.rolesObject.memberRoles = []
		$('.roleslist input:checked').each(function(){
			FC.vars.rolesObject.memberRoles.push($(this).attr('id'));
		})
		//submit data here
		$.post(FC.vars.rolesURL + FC.vars.rolesObject.clubId, { memberId: FC.vars.rolesObject.memberId, memberRole: FC.vars.rolesObject.memberRoles, clubId: FC.vars.rolesObject.clubId  }, function(){
			FC.newRoles();
			FC.resetMembersList();
			document.body.style.cursor = 'auto';
		});
	},
	
	imageViewer: function(){
		$('.imageviewer > li > a').bind('click', function(){
        	__url = $(this).attr('href');
			__img = new Image();
			$('.imageviewer > li.main > span').empty().addClass('loading');
        	$(__img).load(function () {
            $(this).css('display', 'none');
            $(this).hide();
            $('.imageviewer > li.main > span').removeClass('loading').append(this);
            $(this).fadeIn(500);
	        }).error(function () {
	            $('.imageviewer > li.main > span').text('The image could not be loaded')
	        }).attr('src', __url);
			return false;
	    });

	},
	
	confirm: function(){
		$("a.confirm").bind('click', function() {
			link = $(this).attr('href');
			text = $(this).text().toLowerCase();
			FC.confirmAction(text, link, function(r) {
				if(r){
					window.location.href = link
				} else {
					//do nothing
				};
 			});
			return false;
 		});
	},
	
	confirmAction: function(text, link, callback){
		
		try {
			if (typeof document.body.style.maxHeight === "undefined") {//if IE 6
				$("body", "html").css({
					height: "100%",
					width: "100%"
				});
				$("html").css("overflow", "hidden");
				if ($('.hideSelect').length == 0 ) {//iframe to hide select elements in ie6
					$("body").append("<iframe class='hideSelect'></iframe><div class='overlay'></div><div class='popup'><div class='popup-top'><div class='popup-middle'><h3>Are you sure?</h3><p class='popup-text'></p></div></div><div class='popup-bottom clear'></div></div>");
				}
			}
			else {//all others
				if (document.getElementById("TB_overlay") === null) {
					$("body").append("<div class='overlay'></div><div class='popup'><div class='popup-top'><div class='popup-middle'><h3>Are you sure?</h3><p class='popup-text'></p></div></div><div class='popup-bottom clear'></div></div>");
				}
			}
			
			if (FC.detectMacXFF()) {
				$(".overlay").addClass("overlayMacFFBGHack");//use png overlay so hide flash
			}
			else {
				$(".overlay").addClass("overlayBG");//use background and opacity
			}
			
			$(".popup-text").text('This will permanently remove the relationship between this BTM and your club.');
			
			$(".popup-bottom").append('<a href="#" class="popup-no floatleft"><img height="28" width="121" alt="No, don\'t do this" src="/images/clubs/btn-no-dont-do-this.gif"/></a><a href="#" class="popup-yes floatright"><img height="28" width="98" alt="Yes, I\'m sure" src="/images/clubs/btn-yes-im-sure.gif"/></a>');
			
			$(".popup-yes").click( function() {
				if (typeof document.body.style.maxHeight == "undefined") {//if IE 6
					$(".hideSelect").remove();
					$("body","html").css({height: "auto", width: "auto"});
					$("html").css("overflow","");
				}
				$(".popup").remove();
				$(".overlay").remove();
				callback(true);
				return false;
			});
			$(".popup-no").click( function() {
				if (typeof document.body.style.maxHeight == "undefined") {//if IE 6
					$(".hideSelect").remove();
					$("body","html").css({height: "auto", width: "auto"});
					$("html").css("overflow","");
				}
				$(".popup").remove();
				$(".overlay").remove();
				callback(false);
				return false;
			});
			$(".popup-yes").focus();

		} 
		catch (e) {
			
		}
		
	},
	
	detectMacXFF: function() {
		var userAgent = navigator.userAgent.toLowerCase();
		if (userAgent.indexOf('mac') != -1 && userAgent.indexOf('firefox')!=-1) {
			return true;
		}
		
	},
	
	courtsTooltip: function(){
		
		$('.courts img').each(function(){
			var altAtt = $(this).attr('alt');
			$(this).tooltip({
				track: true, 
	    		delay: 0, 
	    		showURL: false,
				bodyHandler: function() { 
			        return altAtt; 
			    },
				extraClass: "tool-tip",
				top: -55, 
	    		left: -150                                      
			})
		})
	},
	
	countdown: function(){
	    var limit = $('textarea.countdown').attr('limit');
		var currentLength = $('textarea.countdown').val().length;
		$('textarea.countdown').parents('.field').find('.counttext').append($('<span>&nbsp;- you have <span class="countvalue">' + (limit - currentLength) + '</span> characters left</span>'));
		$('textarea.countdown').bind('keydown keypress keyup', function(){
			if ($(this).val().length > limit){
				$(this).val($(this).val().substring(0, limit));
			}else {
				$('.countvalue').text(limit - $(this).val().length);
			}
		})
	}
	
//	countdown: function(){
//		$('textarea.countdown').parents('.field').find('.counttext').append($('<span> - you have <span class="countvalue">' + FC.vars.countdownMax + '</span> characters left</span>'));
//		$('textarea.countdown').bind('keydown keypress keyup', function(){
//			if ($(this).val().length > FC.vars.countdownMax){
//				$(this).val($(this).val().substring(0, FC.vars.countdownMax));
//			}else {
//				$('.countvalue').text(FC.vars.countdownMax - $(this).val().length);
//			}
//			
//		})
//	}
}

$(document).ready(function(){	
						  				 
	//		if(jQuery.browser.safari) && (jQuery.browser.version >= 418){alert('hello');}			   
						   						   
	// calling the pagination on the photo gallery page
	//if($(".galleryPagination").length){paginate();}	
	if($("ul.rating").length){FC.addRating();}
	if($('.memberslist').length){FC.initMembersList();}
	if($('.js-helptext').length){FC.showHideHelp();}
	if($('.imageviewer').length){FC.imageViewer();}
	if($('a.confirm').length){FC.confirm();}
	if($('.courts').length){FC.courtsTooltip();}
	if($('textarea.countdown').length){FC.countdown();}
}); 

