	$(document).ready(function(){
		
		var clearMePrevious = '';
	
		// clear input on focus
		$('.clearMeFocus').focus(function(){
			if($(this).val()==$(this).attr('title')){
				clearMePrevious = $(this).val();
				$('#search-field').css('color','#000000');
				$(this).val('');
			}
		});
		
		$('.clearEmailFocus').focus(function(){
			if($(this).val()==$(this).attr('title')){
				clearMePrevious = $(this).val();
				$('.clearEmailFocus').css('color','#000000');
				$(this).val('');
			}
		});
		
		$('.clearPasswordFocus').focus(function(){
			$('.clearPasswordFocus').hide();
			$('.clearPasswordBlur').show();
			$('.clearPasswordBlur').focus();
			$('.clearPasswordBlur').css('color','#000000');
		});
		
		// if field is empty afterward, add text again
		$('.clearMeFocus').blur(function(){
			if($(this).val()==''){
				$(this).val(clearMePrevious);
				$('#search-field').css('color','#7c7c7c');
			}
		});
		
		$('.clearEmailFocus').blur(function(){
			if($(this).val()==''){
				$(this).val(clearMePrevious);
				$('.clearEmailFocus').css('color','#7c7c7c');
			}
		});

		$('.clearPasswordBlur').blur(function(){
			if($(this).val()==''){
				$('.clearPasswordBlur').hide();
				$('.clearPasswordFocus').show();
			}
		});
	
		
		// SLIDE TABLES UP AND DOWN
		$('.end-cap a').click(function(evt) {
			
			var element = evt.currentTarget;
			var container = $(element).parent().parent().parent();
			
			$('.table-body', container).slideToggle('fast', function() {
				
				if($('.end-cap a', container).attr('class') == 'down'){
					$('.end-cap a', container).attr('class', 'up');
				} else {
					$('.end-cap a', container).attr('class', 'down');
				}
				
			});
		});
		
		// OPEN SIGNIN BOX
		$('#sign-in').click(function(){
			$('#sign-in-box').slideDown();
			$('#coaches-header-sign-in-box').slideUp();
		});
		
		$('.wrapper-middle').click(function() {
    	$('#sign-in-box').slideUp();
    	$('#coaches-header-sign-in-box').slideUp();
		});

		$('.wrapper-bottom').click(function() {
    	$('#sign-in-box').slideUp();
    	$('#coaches-header-sign-in-box').slideUp();
		});
		
		// OPEN COACHES SIGNIN BOX

		$('#coaches-header-sign-in').click(function(){
			$('#sign-in-box').slideUp();
			$('#coaches-header-sign-in-box').slideDown();
		});

		$('#coaches-sign-in').click(function(){
			$('#coaches-sign-in-box').slideToggle();
		});
		
		$('#coaches-reg-link').click(function() {
			//window.location = coachUrl;
			window.location = "/coaches";
			return false;
		});
		
		//DATE PICKER
		$('#start-date').datepicker();
		$("#start-date").keydown(function(event) {event.preventDefault();});
		$('#end-date').datepicker();
		$("#end-date").keydown(function(event) {event.preventDefault();});

		
		
		// PRETTY PHOTOS
		$("a[rel^='prettyPhoto']").prettyPhoto({theme:'dark_rounded'});
		$("#hidden-page-link").html("<p>Paste link in <b>email</b> or <b>IM</b> to link to this page<br/><input type='text' value='" + window.location + "' style='width:450px;'/><br/><br/>Paste this HTML into your website to link to this page<br/><input type='text' value='<a href=" + window.location +">" + window.location + "</a>' style='width:450px;'/></p>");
		$("#logout-confirmation").dialog({ autoOpen: false });
		$("#logout-button").click(logoutButton_onClick);
		$("#logout-dialog-close-button").click(logoutDialogCloseButton_onClick);
		
		// SCROLL PANE
		
		$('#termsScrollPane').jScrollPane({showArrows:true});
		
		// PROFILE LINKAGES CHECKBOX
		
		$(".profile-link-checkbox").click(profileLinkCheckbox_onClick);
		
	});
	
	
	
	
	
	function profileLinkCheckbox_onClick() {
		$("#UserLinkMultiplePlayersForm").submit();
	}
	
	
	
	
	
	/**
	 * Filter a VarVee table.
	 * @param node
	 * @param tableHash
	 * @return
	 */
      
	function tableFilter(node, tableHash){
		$('body').css('cursor', 'wait');
		
		var id = $(node).attr('id');
		var val =  $(node).val();
		var path = window.location.pathname;
		var parts = path.split('/');
		var found = false;
		var foundActiveTable = false;
		var innerParts = null;
                var urlflag = false; //variable declared to check wheter flag is set or not by icreon on 24/5/11
		
		// LOOP THROUGH ALL OF THE QUERY PARTS AND 
		// SEARCH FOR A PREVIOUS FILTER AND AN ACTIVE
		// TABLE HASH
		//........................................
		for(var k in parts){
			innerParts = parts[k].split(":");
			if(innerParts){
				if(innerParts[0] == id){
					parts[k] = innerParts[0] + ":" + val;
					found = true;
				}
				else if(innerParts[0] == 'page'){
					parts[k] = innerParts[0] + ":1";
				}
				
				if(innerParts[0] == 'activeTable'){
					parts[k] = innerParts[0] + ":" + tableHash;
					foundActiveTable = true;
				}
          // START of code to check whether flag is in url so that url must be different added by icreon on 24/5/11
             if(innerParts[0] == 'flag'){
				urlflag = true;
                                if(innerParts[1] == 1)
                                    {
                                   parts[k] = innerParts[0] + ":2";
                                    }
                                    else
                                   {
                                   parts[k] = innerParts[0] + ":1";
                                   }
				}

              // START of code to check whether flag is in url so that url must be different added by icreon on 24/5/11


			}
		}
		
		if(!found){
			parts[parts.length] = id + ":" + val;
		}
                   // START of code to set flag to get different url added by icreon on 24/5/11
                 if(!urlflag)
                {
                parts[parts.length] = 'flag' + ":1";
                }

                // END of code to set flag to get different url added by icreon on 24/5/11
		if(!foundActiveTable){
			parts[parts.length] = 'activeTable' + ":" + tableHash;
		}
		
		
		window.location = parts.join("/") + "#" + tableHash;
	}


function logoutButton_onClick() {
	$("#logout-confirmation").dialog('open');
}

function logoutDialogCloseButton_onClick() {
	$("#logout-confirmation").dialog('close');
}

