/**
 *
 *	Warburtons Save tons on Lunch 
 *	http://savetonsonlunch.co.uk
 *	
 *	@page 		/club/
 *	@since		07/07/2009
 *	@author		philthompson
 *	@copyright	LOVE Creative
 */
 
 
	/*
		enforceMaxlength
		Items may have maxlengths assigned to them - if that's the case,
		delete anything that goes over this maxlength
	*/
	function enforceMaxlength(){
	
		var safariEmptyValue = 524288;
		var firefoxEmptyValue = -1;
		
		//$("form input[type='text'], form textarea").each(function(){
		$("form textarea").each(function(){
			var maxLength = parseInt($(this).attr("maxlength"));
			if(!isNaN(maxLength) && maxLength != firefoxEmptyValue && maxLength != safariEmptyValue && maxLength > 0){	
				//alert($(this).attr("id") + " " + maxLength);

				var counter = '<div class="counter"><em>' + maxLength + '</em> characters left</div>';
				$(this).parent().append(counter);
				$(this).parent().find('.counter em').html(maxLength - $(this).val().length);
			}
		});
		
	
		//$("form input[type='text'], form textarea").keyup(function(){
		$("form textarea").keyup(function(){
			/* get the maximum length the element can be from maxlenegth attribute */
			var maxLength = parseInt($(this).attr("maxlength"));
			/* max length isn't present on this item */
			if(isNaN(maxLength)){ 
				return false;
			} else if(maxLength > 0){					
					/* get current value of element*/
					var new_value = $(this).val();
					/* get new value (existing value minus the extra characters*/
					//new_value = new_value.substr(0, maxLength);
					/* put new value into element */
					//$(this).val(new_value);
					$(this).parent().find('.counter em').html(maxLength - new_value.length);
					
					// append/remove a class if the text is too long
					if(new_value.length > maxLength && !$(this).parent().find('.counter em').hasClass('counter-error')){
						$(this).parent().find('.counter em').addClass('counter-error');
					} else{
						$(this).parent().find('.counter em').removeClass('counter-error');
					}
					
			}
		});
		
	}

 
/* ajaxPagination */
function ajaxPagination(){
	$("#TipsAJAX .Pagination div.Arrow a").click(function(){
	
		$(this).unbind();
		
		// hide loading holder
		$("#TipsAJAXLoading").remove();
		
		// id attribute on body tag
		var body_id = $("body").attr("id");
		
		// href value of clicked link
		var URL = $(this).attr("href");
		
		// Load in the new page
		loadByAjax(URL);
		
		// stop link completing
		return false;
		
	});
}

/**
 *	loadByAjax
 *	Use AJAX to grab new content and load it into place
 *	@param string URL 	/news/2009/06
 *	@param string hash	#page2
 */
function loadByAjax(URL, hash){


	/* hide current content and show loading graphic */
	$("#TipsAJAX *").css('visibility','hidden');
	
	$("#TipsAJAX").prepend('<div id="TipsLoading"><div>Loading&hellip;</div></div>');

	// Make AJAX call to set URL
	// then if we're successful show the new content else
	// unhide the old stuff
	$("#ajax-pagination").load(URL + ' #TipsAJAX', '', 
		function (responseText, textStatus, XMLHttpRequest){

			if(textStatus == 'success'){
				// AJAX call was successful so, remove previous content
				// then put new contents in its place
				$("#Content #TipsAJAX *").remove();
				var new_content = $("#ajax-pagination").html();
		  		$('#TipsAJAX').html(new_content);
		 		$('#ajax-pagination *').remove();	
		
		  		// re-run function to allow future AJAX calls
		  		ajaxPagination();
		  	} else{
		  		// AJAX call failed so restore old content
		  		$('#TipsLoading').remove();
		  		$("#TipsAJAX *").not('input[type="hidden"]').css('visibility','visible');
		  		return true;
		  	}
		  	
		}
	);
	
	// stop link completing
	return false;
}



/**
 *	initClub
 *	functions that run on page load
 */
function initClub(){
	// make a container for AJAX content
	$("#FooterBackground").append('<div id="ajax-pagination"></div>');
	ajaxPagination();
	
	
	// change default thickbox settings
	$('a.thickbox').each(function(){
		var thickboxHref = $(this).attr('href');
		thickboxHref = thickboxHref.replace('?width=525&height=491', '?width=758&height=634');
      	$(this).attr('href',thickboxHref);
    });
	
	
	// Hide add form on load...
	// unless the right hash tag is in place...
	// user could've bookmarked the form (in theory)
	var windowHash = window.location.hash;
	if(windowHash.length == 0 || windowHash != '#AddTipForm'){
		$("#AddTipForm").hide();
	} else{
		$("#AddLink").addClass('active');
		$(".Pagination-Top").hide();
	}
	
	
	// Show hide form on click add link
	$("#AddLink").click(function(){
	
		if($(this).hasClass('active')){
			// Show			
			$(this).removeClass('active');
			$("#AddTipForm").hide();
			$(".Pagination-Top").show();
		} else{
			// Hide
			$(this).addClass('active');
			$(".Pagination-Top").hide();
			$("#AddTipForm").show();
		}

	});
	
	hideForm();
	
	
	// Show/hide recipe on clicking the title
	$("#Recipe h3").click(function(){
		
		if($(this).hasClass('active')){
			// hide
			$(this).removeClass('active');
			$("#RecipeContent").hide();
		} else{
			// show
			$(this).addClass('active');
			$("#RecipeContent").show();
		}
	});
}

function hideForm(){

	$('#AddTipForm .Fields div').css('visibility','visible');
	$('#FormLoading').hide();
	$('#AddTipForm div.Field')
	// Show hide form on click cancel button
	$("#CancelTip").click(function(){
		$("#AddLink").removeClass('active');
		$("#AddTipForm").hide();
		$(".Pagination-Top").show();
		return false;
	});
	
	// Show add form on sidebar widget link clicks
	$("#ClubWinner a, #ClubWinnerExtended a").click(function(){
		if(!$("#AddLink").hasClass("active")){
			$("#AddLink").removeClass('active');
			$("#AddTipForm").show();
			$(".Pagination-Top").hide();
		}
	});
}


function hideFields(){
	$('#AddTipForm .Fields div').css('visibility','hidden');
	$('#FormLoading').show();
}
 
 
$(document).ready(function(){
	initClub();	
});