// Add event function
function addEvent(elm, evType, fn, useCapture) {
	if(elm.addEventListener) {
		elm.addEventListener(evType, fn, useCapture);
		return true;
		}
	else if (elm.attachEvent) {
		var r = elm.attachEvent('on' + evType, fn);
		return r;
		}
	else {
		elm['on' + evType] = fn;
		}
	}

// Limit text in textarea
function limitText(limitField, limitCount, limitNum) {
	if (limitField.value.length > limitNum) {
		limitField.value = limitField.value.substring(0, limitNum);
	} else {
		limitCount.value = limitNum - limitField.value.length;
	}
}

// (jQuery)
$(document).ready(function(){

	// initialise drop down menus
	if ( $("ul.sf-menu").length > 0 ) { 
		$(function(){
			$('ul.sf-menu').superfish();
		});  
	}
	
	// Bind facebox links
	$('a[rel*=facebox]').facebox() 
	
	// Open / Close help panel 						   
	$(".helpButton").click(function() {
		$("#content").addClass("helpPanel")
		$('#helpPanel').show('slow')
		return false;
	});

	$("#helpPanelTitle").click(function() {
		$('#helpPanel').hide('slow');
		$("#content").removeClass("helpPanel")
		return false;		
	});		


	// When user hovers over the profile div, fade in the stats paragraph	
	$("#itemGrid ul li p.stats").hide();
	
	$('#itemGrid ul li').hover(
		function(){$(this).children('p.stats').stop(false, true).slideUp().fadeIn(500);},
		function(){$(this).children('p.stats').stop(false, true).slideDown().fadeOut(500);}
	);




	// Profile reminders (GUESTS)
		$("#reminderBtn").children('p').hide();
		// When the reminder button is clicked
		$("#btnReminder").click(function() {
			$("#reminderBtn").children('p').hide();
			// Poll the email validation script if a value has been entered.
			if ( $("#sReminderEmailAddress").val().length > 0 ) {
				
				// Assign values to variables
				sEmailAddress = $("#sReminderEmailAddress").val();
				iProfileID = $("#iProfileID").val();
				iMemberID = $("#iMemberID").val();
				
				// Add reminder
				$.post("/_ajaxRequests/addReminder.cfm", { sString: sEmailAddress, iProfileID: iProfileID, iMemberID: iMemberID },
				function(data){
					// If the response is an error, show message and stop form submission
					if ( (data) == "Error" ) {
						// Show message
						$("#reminderBtn").children('p').removeClass("success").addClass("error").html('Please enter a valid email address.').fadeIn('slow');
						// Stop form submission
						return false;
					}

					if ( (data) == "Duplicate" ) {
						// Show message
						$("#reminderBtn").children('p').removeClass("success").addClass("error").html('You have already submitted a request to be notified about this escort.').fadeIn('slow');
						// Stop form submission
						return false;
					}

					if ( (data) == "Success" ) {
						// Show message
						$("#reminderBtn").children('p').removeClass("error").addClass("success").html('Thanks! A confirmation email has been sent to you. Click the link to activate your reminder.').fadeIn('slow');
						// Stop form submission
						return false;
					}
				});
			return false;
			}
			return false;	
		});

	// Profile reminders (MEMBERS)
		$("#membersReminderBtn").children('p').hide();
		// When the reminder button is clicked
		$("#btnMembersReminder").click(function() {
			// Hide AJAX reponse
			$("#membersReminderBtn").children('p').hide();
			// Assign values to variables
			iMemberID = $("#iMemberID").val();
			iProfileID = $("#iProfileID").val();
			sEmailAddress = "";

			// Add reminder
			$.post("/_ajaxRequests/addReminder.cfm", { sString: sEmailAddress, iProfileID: iProfileID, iMemberID: iMemberID },
			function(data){
				// If the response is an error, show message and stop form submission
				if ( (data) == "Duplicate" ) {
					// Show message
					$("#membersReminderBtn").children('p').addClass("error").html('You have already submitted a request to be notified about this escort').fadeIn('slow');
					// Stop form submission
					return false;
				}

				if ( (data) == "Success" ) {
					// Change page to show the notification has been set.
					$("#escortAway").html('<p><strong>This escort is currently unavailable.</strong></p><p class="smallText bold">You have asked to be notified when she becomes available.<br /><a href="/members/myAccount/profileReminders.cfm" title="Manage your escort notifications">Manage your notifications</a></p>');
					// Remove notification button.
					$("#membersReminderBtn").remove();
					// Stop form submission
					return false;
				}
			});
			return false;
		});
	
	// Use the 'example' plugin on the search box
		$("#frmSearch #sSearchString").example('Escort search...');	


	// Sort order display
	// Hide the list of display orders
		$('#sortOrder ul').hide();

	// Toggle view of display order list
		$('#sortOrder p').click(function() {
			$('#sortOrder ul').toggle('fast');
		});

	// Subscription form - Check it exists first
		if ( $("#frmSubscribeToList").length > 0 ) { 
	
			// Hide subscription response 
			$('#subscribeResponse').hide();
			
			// bind form using ajaxForm 
			$('#frmSubscribeToList').ajaxForm({ 
				// target identifies the element(s) to update with the server response 
				target: '#subscribeResponse', 
		
				beforeSubmit: function() {
					// Hide subscription response 
					$('#subscribeResponse').hide();
				},
					
				// success identifies the function to invoke when the server response 
				// has been received; here we apply a fade-in effect to the new content 
				success: function() { 
					// Show response
					$('#subscribeResponse').fadeIn('slow'); 
				}
			}); 
		}
	

	// Facebox forms
		$('#facebox').livequery(function() {
			// Hook up the login form with livequery, as it's pulled in via the facebox ajax function
			$(this).find('#frmLogin').livequery(function() {
				// This'll set up the ajax form
				$(this).ajaxForm({
					success: function(html) {
						// Now, when the form's submitted, send the response to the facebox div				
						$('#facebox').find('.facebox').html(html);
						// Now, if the login was successful, we need to change some elements on the page.
						$('#loginSuccess').livequery(function() {
							// Search for the notification id and change it.
							$('#notificationsLink').html('&raquo; <a href="/london-escorts/notifications/" title="Tell me about new escorts" rel="facebox">Tell me about new escorts</a>');
							// Attach facebox to the new notification link.
							$('#notificationsLink a').facebox(); 
							// Search for the member block and change this.
							$('#memberPanel').load("/members/_dspMember.cfm" + '?nocache=' + Math.random());
						});
					}
				});
			});

			// Hook up the notifications form with livequery, as it's pulled in via the facebox ajax function
			$(this).find('#frmNotifications').livequery(function() {
				// This'll set up the ajax form
				$(this).ajaxForm({
					success: function(html) {
						// Now, when the form's submitted, send the response to the facebox div	
						$('#facebox').find('#frmNotifications fieldset').hide();
						$('#facebox').find('#frmNotifications').after(html);
					}
				});
			});
		});

	// Hide advanced search form
		$('#searchFormDiv').hide();

	
	// Lightbox plugin
	if ( $("#profileImages").length > 0 ) { 	
		$('#profileImages a.alternativeImage').lightBox({
			imageLoading: '/js/jQuery/lightbox/images/lightbox-ico-loading.gif',
			imageBtnClose: '/js/jQuery/lightbox/images/lightbox-btn-close.gif',
			imageBtnPrev: '/js/jQuery/lightbox/images/lightbox-btn-prev.gif',
			imageBtnNext: '/js/jQuery/lightbox/images/lightbox-btn-next.gif'
		});
	};
	
});

// Horrid legacy javascript
	// Show / Hide Content (Simple Version)
	function toggleContentSimple(el) {
	
	  var myelement = document.getElementById(el);
	  var myimg = document.getElementById("btn-" + el);
	
	  if( !myelement.style.display || myelement.style.display == "none" ) {
		myelement.style.display = "inline";
	  } else {
		myelement.style.display = "none";
	  }
	}
	
	// Show / Hide Content
	function toggleContent(el) {
	
	  var myelement = document.getElementById(el);
	  var myimg = document.getElementById("btn-" + el);
	
	  if( !myelement.style.display || myelement.style.display == "none" ) {
		myelement.style.display = "inline";
		myimg.src= "/img/icons/btnContract.gif";
	  } else {
		myelement.style.display = "none";
		myimg.src= "/img/icons/btnExpand.gif";
	  }
	}

