/* create a timeOut function in jQuery */
    jQuery.fn.idle = function(time){
        return this.each(function(){
            var i = $(this);
            i.queue(function(){
                setTimeout(function(){
                i.dequeue();
                }, time);
            });
        });
    };
/**
 * jQuery Plugin Toggle Fade v1.0
 * Requires jQuery 1.2.3 (Not tested with earlier versions).
 * Copyright (c) 2008 Gregorio Magini [gmagini at gmail dot com] 
 * 
 *	@param: Object Array. Arguments need to be in object notation.
 *	Returns: jQuery.
 *	Options:	
 *		speedIn: Sets the speed of the fadeIn effect. Default: "normal".
 *    speedOut: Sets the speed of the fadeOut effect. Default: same as speedIn.
 *
 *	Examples: 
 *    
 *    speedIn and speedOut both "normal":
 *		$("#toggle-link").toggleFade();
 *
 *    speedIn and speedOut both "fast":
 *		$("#toggle-link").toggleFade({ speedIn : "fast");
 *
 *    different settings for speedIn and speedOut:
 *		$("#toggle-link").toggleFade({ speedIn : 800, speedOut : 150 });
 *
 */

(function($) {
  $.fn.toggleFade = function(settings)
  {
  	settings = jQuery.extend(
  		{
        speedIn: "normal",
        speedOut: settings.speedIn
  		}, settings
  	);
  	return this.each(function()
  	{
  	  var isHidden = jQuery(this).is(":hidden");
      jQuery(this)[ isHidden ? "fadeIn" : "fadeOut" ]( isHidden ? settings.speedIn : settings.speedOut);
    });
  };
})(jQuery);
// Removes leading whitespaces
function LTrim( value ) {
	
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
	
}

// Removes ending whitespaces
function RTrim( value ) {
	
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
	
}

// Removes leading and ending whitespaces
function trim( value ) {
	
	return LTrim(RTrim(value));
	
}

function formatCCNumber(){
// replace any white space in the card number
	var newCCnumber = document.theForm.Credit_Card_Number.value;
	re = /[^0-9]/gi;
	newCCnumber = newCCnumber.replace(re, '') ;
	document.theForm.Credit_Card_Number.value = newCCnumber;
}


$(document).ready(function(){
	// replace default value
	
	if(!document.getElementById("adminBody")){
		$('input[type="text"]').focus(function() { 
		
			if (this.value == this.defaultValue){
				this.value = '';
			}
		});
		$('input[type="text"]').blur(function() { 
			if ($.trim(this.value) == ''){
			this.value = (this.defaultValue ? this.defaultValue : '');
		}
		});
	}
	// messagebox 
	if(document.getElementById("messageBox")){
		// fade out
		$('#messageBox').idle(7000).fadeOut('fast');
	}
	if(document.getElementById("headerNav")){
		$('.navWrapper').hover(
		function() { $('.subNavWrapper', this).fadeIn('fast'); },
		function() { $('.subNavWrapper', this).fadeOut('slow'); });
		$('.navWrapper2').hover(
		function() { $('.subNavWrapper2', this).fadeIn('fast'); },
		function() { $('.subNavWrapper2', this).fadeOut('slow'); });
	}
	// paid date field
	if(document.getElementById("paidDateField")){
		// fade out
		$('#paidDateLink').click(function(){$('#paidDateWrapper').toggleFade('Fast');});
	}
	
	// file reg form
	$('.fileRegLink').click(function(){
		var fileReg = "#" + $(this).attr("id").replace("fileRegLink","fileRegWrapper");
		
		$(fileReg).toggleFade('Fast');});
	
	// cart contents
	//if(document.getElementById("cartLink")){
		// fade out
	//	$('#cartLink').click(function(){$('#headercartDisplay').toggleFade('Fast');});
	//	$('#headercartDisplay').mouseout(function(){$('#headercartDisplay').idle(2000).fadeOut('fast');});
	//}
	// what's the security code
	if(document.getElementById("secCodeLink")){
		$('#secCodeLink').hover(
			function() {$('#secCode').fadeIn('fast'); },
			function() {$('#secCode').fadeOut('fast'); });
	}
	// cart contents
	if(document.getElementById("cartLink")){
		$('#cartLink').hover(
			function() {$('#headercartDisplay').fadeIn('fast'); },
			function() {$('#headercartDisplay').idle(1000).fadeOut('fast'); });
	}
	// submit order
	if(document.getElementById("placeorder")){
		$('#placeorder').click(
			function() {$('#processOrder').idle(1000).fadeIn('fast'); 
						$('#placeorder').fadeOut('fast');});
	}
	
	// ratings
	if(document.getElementById("rating")){
		$('input.star').rating();
	}
	

	
	//enable fancybox - http://fancybox.net/api
	if(document.getElementById("historyDiv")){
		$("#historyDiv a").fancybox({
			speedIn			:	500, 
			speedOut		:	200, 
			overlayShow		:	true,
			transitionIn	:	'fade',
			transitionOut	: 	'fade',
			width			: 	800,
			height			:	600,
			overlayOpacity	:	.7,

			onClosed		:	function() {
									if(document.contentForm.historyChange.value != ""){
										document.location.href=location.href;
									}
								}
							
		});
		
	}
	// membership level messages
	if(document.getElementById("joinMemberTypeMessage")){
			$("#mtDayPass").click(
			function() {
				$('.joinMessage').fadeOut('fast');
				$("#dayPassMessage").fadeIn('slow');
			});
			$("#mt1Year").click(
			function() {
				$('.joinMessage').fadeOut('fast');
				$("#oneYearMessage").fadeIn('slow');
			});
			$("#mt2Years").click(
			function() {
				$('.joinMessage').fadeOut('fast');
				$("#twoYearsMessage").fadeIn('slow');
			});
			$("#mt3Years").click(
			function() {
				$('.joinMessage').fadeOut('fast');
				$("#threeYearsMessage").fadeIn('slow');
			});
			$("#mtLife").click(
			function() {
				$('.joinMessage').fadeOut('fast');
				$("#lifeMessage").fadeIn('slow');
			});
			$("#mtCorp").click(
			function() {
				$('.joinMessage').fadeOut('fast');
				$("#corpMessage").fadeIn('slow');
			});
	}
	// this is wrapped so the admin won't give an error
	if(document.getElementById("headerLogo")){
		// login form
		$(".loginLink").fancybox({
			scrolling 		: 'no',
			titleShow		: false,
			width			: 	300,
			height			:	200,
			overlayOpacity	:	.7,
			onComplete		:	function() {
										document.loginForm.user_name.focus();
									}
		});
		
		// more links
		//if(document.getElementByClass("historyDiv")){
			$("a.moreLink").fancybox({
				scrolling 		: 'no',
				titleShow		: false,
				width			: 	700,
				height			:	500,
				overlayOpacity	:	.7
								
			});
			$("a.moreLink2").fancybox({
				scrolling 		: 'no',
				titleShow		: false,
				width			: 	700,
				height			:	500,
				overlayOpacity	:	.7
								
			});
			$("a.moreLink3").fancybox({
				scrolling 		: 'no',
				titleShow		: false,
				width			: 	700,
				height			:	500,
				overlayOpacity	:	.7
								
			});
			$("a.prodLgImgLink").fancybox({
				scrolling 		: 'no',
				titleShow		: true,
				autoScale		: true,
				overlayOpacity	:	.7
								
			});
			
			
			$("a.regCatLink").fancybox({
				scrolling 		: 'no',
				titleShow		: false,
				overlayOpacity	:	.7,
				onClosed		: function(){
										updateTitleList(document.theForm.Member_Categories);
									}
								
			});
			//reel links
			$("a.reelLink").fancybox({
				scrolling 		: 'yes',
				titleShow		: true,
				width			: 	700,
				height			:	500,
				overlayOpacity	:	.7
								
			});
			//work links
			$("a.workLink").fancybox({
				scrolling 		: 'yes',
				titleShow		: true,
				width			: 	700,
				height			:	500,
				overlayOpacity	:	.7
								
			});
			// btn links
			$("a.btnLinkFB").fancybox({
				scrolling 		: 'yes',
				titleShow		: true,
				width			: 	700,
				height			:	500,
				overlayOpacity	:	.7
								
			});
			
			// doc previews
			$("a.docLink").fancybox({
				scrolling 		: 'no',
				titleShow		: true,
				type			: 'iframe',
				width			: 	800,
				height			:	600,
				overlayOpacity	:	.7
								
			});
			
			// comment form links
			$(".commentLink").fancybox({
				scrolling 		: 'no',
				titleShow		: false,
				width			: 	680,
				height			:	220,
				overlayOpacity	:	.7,
				onComplete		:	function() {
											if(document.getElementById("commentItForm")){
												document.commentItForm.Rating_Title.focus();
											}
										}
			});
			$(".commentLink2").fancybox({
				scrolling 		: 'no',
				titleShow		: false,
				width			: 	680,
				height			:	220,
				overlayOpacity	:	.7
			});
			$(".commentLink3").fancybox({
				scrolling 		: 'no',
				titleShow		: false,
				width			: 	300,
				height			:	60,
				overlayOpacity	:	.7
			});
		}
		// reel detail links -- show reel details
		$('.entries a').click(
			function() {
						$('.reelDetails').fadeOut('fast'); 
						// which id did we click on
						var theID = this.id;
						theID = theID.replace("reel_","");
						//alert(theID);
						var divWeWant = "reelDetails_" + theID;
						$('#'+divWeWant).fadeIn('fast');});
	//}
	
	// top nav rollover
	$(".rollover").hover(
	 function()
	 {
	  this.src = this.src.replace("_off","_on");
	 },
	 function()
	 {
	  this.src = this.src.replace("_on","_off")	;
	 }
	);
	
	/**
	 * Character Counter for inputs and text areas
	 */
	$('.word_count').each(function(){
		// get current number of characters
		var length = $(this).val().length;
		// get current number of words
		var length = $(this).val().split(/\b[\s,\.-:;]*/).length;
		// update characters
		$(this).parent().find('.counter').html( length + ' characters entered');
		// bind on key up event
		$(this).keyup(function(){
			// get new length of characters
			var new_length = $(this).val().length;
			// get new length of words
			//var new_length = $(this).val().split(/\b[\s,\.-:;]*/).length;
			// update
			$(this).parent().find('.counter').html( new_length + ' characters entered');
		});
	});
	
	if(document.getElementById("User_Name_J")){
		$('#User_Name_J').focus();
	}
	// advanced search
	if(document.getElementById("advancedSearchLink")){
		$('#advancedSearchLink').click(function() {
		  $('#aSearchDiv').slideToggle('slow', function() {
			// Animation complete.
		  });
		});

	}
	if(document.getElementById("commentIt")){
		// get current number of characters
	   $('#counter').html($('#commentIt').val().length + ' characters');
		
	   $('#commentIt').keyup(function()
	   {
	   // get new length of characters
	   $('#counter').html($(this).val().length + ' characters');
	   });
	}
	// check the discount code amount via ajax -- join now
	if(document.getElementById("Discount_Code")){
		//var dataString = "Discount_Code="+document.theForm.Discount_Code.value;
		
		var dataString = "{ \"Discount_Code\": \"" + document.theForm.Discount_Code.value + "\"}";
		var memFee = document.theForm.memFee.value;
		
		if (dataString != ""){
			$('#btnCheckDiscount').click(function(){
				$.ajax({
				   type: "POST",
				   url: "join-now-get-discount.cfm",
				   data: "Discount_Code=" + document.theForm.Discount_Code.value,
				   dataType: "text",
				   success: function(msg){
				   	//alert( trim(msg) );
				   		if(trim(msg) != "qqwer" && trim(msg) != "zzxcvv" && trim(msg) != ""){
							//var discAmount = "<span class='oldFee'>" + memFee + "</span>";
							//discAmount += "<span class='newFee'>" + msg + "</span>";
							$("#feeAmount").addClass("oldFee");
							$("#newFee").addClass("newFee").text(trim(msg));
							$("#messageBox").css('display', 'block');
							$("#messageBoxHeader").text("Discount Code Accepted");
							$("#messageBoxText").text("Yes, the discount code is valid.");
							$('#messageBox').idle(7000).fadeOut('fast');
							//alert("1");
					 	}
						else if(trim(msg) == "qqwer") {
							//alert("2");
							$("#messageBox").css('display', 'block');
							$("#messageBoxHeader").text("Discount Code Not Valid");
							$("#messageBoxText").text("The discount code you entered is not valid.");
							$('#messageBox').idle(7000).fadeOut('fast');
						}
						else if(trim(msg) == "zzxcvv") {
							//alert("3");
							$("#messageBox").css('display', 'block');
							$("#messageBoxHeader").text("Discount Code Malfunction");
							$("#messageBoxText").text("The discount code did not make it through. Click the \"Proceed \" to get your new amount.");
							$('#messageBox').idle(7000).fadeOut('fast');
						}
				   }
				 });
				
			});
		}
	}
	
	// check the discount code amount via ajax -- class reg
	if(document.getElementById("Discount_Code_Class")){
		//var dataString = "Discount_Code_Class="+document.theForm.Discount_Code_Class.value;
		
		//var dataString = "{ \"Discount_Code_Class\": \"" + document.theForm.Discount_Code_Class.value + "\"}";
		//var memFee = document.theForm.memFee.value;
		
		if (dataString != ""){
			$('#btnCheckDiscount').click(function(){
				$.ajax({
				   type: "POST",
				   url: "class-get-discount.cfm",
				   data: "Discount_Code_Class=" + document.theForm.Discount_Code_Class.value + "&cid=" + document.theForm.Class_ID.value + "&cp=" + document.theForm.Class_Price.value,
				   dataType: "text",
				   success: function(msg){
				   	//alert( trim(msg) );
				   		if(trim(msg) != "qqwer" && trim(msg) != "zzxcvv" && trim(msg) != ""){
							//var discAmount = "<span class='oldFee'>" + memFee + "</span>";
							//discAmount += "<span class='newFee'>" + msg + "</span>";
							$("#feeAmount").addClass("oldFee");
							$("#newFee").addClass("newFee").text(trim(msg));
							// insert new price
							$("#Class_Price_New").val(trim(msg));
							$("#discountMessageBox").css('display', 'block');
							$("#discountMessageBoxHeader").text("Discount Code Accepted");
							$("#discountMessageBoxText").text("Yes, the discount code is valid.");
							$('#discountMessageBox').idle(7000).fadeOut('fast');
							//alert("1");
					 	}
						else if(trim(msg) == "qqwer") {
							//alert("2");
							$("#discountMessageBox").css('display', 'block');
							$("#discountMessageBoxHeader").text("Discount Code Not Valid");
							$("#discountMessageBoxText").text("The discount code you entered is not valid.");
							$('#discountMessageBox').idle(7000).fadeOut('fast');
						}
						else if(trim(msg) == "zzxcvv") {
							//alert("3");
							$("#discountMessageBox").css('display', 'block');
							$("#discountMessageBoxHeader").text("Discount Code Malfunction");
							$("#discountMessageBoxText").text("The discount code did not make it through. Click the \"Proceed \" to get your new amount.");
							$('#discountMessageBox').idle(7000).fadeOut('fast');
						}
				   }
				 });
				
			});
		}
	}
	
	// helpful review
	if(document.getElementById("commentRate")){
			//click on yes
			$('.reviewHelpYes').click(function(){
				var theElement = "#" + $(this).attr("id");
				var theID = $(this).attr("id").replace("reviewHelpYes_","");

				$.ajax({
				   type: "POST",
				   url: "rate-it-helpful.cfm",
				   data: "h=1&id=" + theID + "&type=" + document.reviewHelpForm.type.value,
				   dataType: "text",
				   success: function(msg){
				   	//alert("yes"+ trim(theElement) );
				   		if(trim(msg) != "qqwer" && trim(msg) != ""){
							$(theElement).fadeOut('slow');
							$(theElement).next().fadeOut('slow');
							$("#messageBox").css('display', 'block');
							$("#messageBoxHeader").text("Review Feedback Accepted");
							$("#messageBoxText").text("Thanks for your input about this review.");
							$('#messageBox').idle(7000).fadeOut('fast');
							//alert("1");
					 	}
						else if(trim(msg) == "qqwer") {
							//alert("2");
							$("#messageBox").css('display', 'block');
							$("#messageBoxHeader").text("Review Feedback Not Accepted");
							$("#messageBoxText").text("There was an issue and your feedback was not recorded.");
							$('#messageBox').idle(7000).fadeOut('fast');
						}
				   }
				 });
				
			});
		// click on no
		$('.reviewHelpNo').click(function(){
				var theElement = "#" + $(this).attr("id");
				var theID = $(this).attr("id").replace("reviewHelpNo_","");

				$.ajax({
				   type: "POST",
				   url: "rate-it-helpful.cfm",
				   data: "h=0&id=" + theID + "&type=" + document.reviewHelpForm.type.value,
				   dataType: "text",
				   success: function(msg){
				   	//alert( trim(theElement) );
				   		if(trim(msg) != "qqwer" && trim(msg) != ""){
							$(theElement).fadeOut('slow');
							$(theElement).prev().fadeOut('slow');
							$("#messageBox").css('display', 'block');
							$("#messageBoxHeader").text("Review Feedback Accepted");
							$("#messageBoxText").text("Thanks for your input about this review.");
							$('#messageBox').idle(7000).fadeOut('fast');
							//alert("1");
					 	}
						else if(trim(msg) == "qqwer") {
							//alert("2");
							$("#messageBox").css('display', 'block');
							$("#messageBoxHeader").text("Review Feedback Not Accepted");
							$("#messageBoxText").text("There was an issue and your feedback was not recorded.");
							$('#messageBox').idle(7000).fadeOut('fast');
						}
				   }
				 });
				
			});
	}
	
	// admin items
	if(document.getElementById("hereBeSteps")){
		// fade out
		$('.stepName').click(function(){$(this).next().toggleFade('Fast');});
	}
	
	
});
