

/* Highlight current section on the main menu */
function highlightNav(){

	var $path = window.location.pathname; //Get the full path of the current page, including page name
	var $dirpath = unescape($path.substring(0,($path.lastIndexOf("/")) + 1)); //Get just the directory path of the current page

	if(!$dirpath) {$dirpath = '/';} //Default to the home page
	
	/* Check each main menu navigation option to see if it is the link to the root of the current section  */
	$('ul#mainMenu li a').each(function(){
		var $href = $(this).attr('href');
		if ($href == $dirpath) {
			$(this).addClass('selected'); // if this is the root page of the current section then highlight it 
		} else {
			$(this).removeClass('selected'); // otherwise, ensure that it isn't highlighted
		}	
	});
};


function isScrolledIntoView(elem)
{
    var docViewTop = $(window).scrollTop();
    var docViewBottom = docViewTop + $(window).height();

    var elemTop = $(elem).offset().top;
    var elemBottom = elemTop + $(elem).height();
	
	return ((elemBottom >= docViewTop) && (elemTop <= docViewBottom));
}



function scrollToId(id){
   if (! isScrolledIntoView($("#"+id))){
   		window.scrollTo(0,$("#"+id).offset().top);
   }
   
}



function facebookConnect(){

	$('#fbConnectLoader').show();	
	var $iframe = $('#facebookConnectFrame');
	$iframe.attr('src', '/fb-connect/index.html');
	
}






$(document).ready(function(){
	


	/* tinyMCE */
	/* **************************************************************************************** */
	
	$('textarea.tinymce').tinymce({
		// Location of TinyMCE script
		script_url : '/ui/js/tiny_mce/tiny_mce.js',		 
		
		theme : "advanced",		
		plugins : "paste, linkautodetect",
		theme_advanced_buttons1 : "bold,italic,separator,bullist,numlist,link,unlink",
		theme_advanced_buttons2 : "",
		theme_advanced_buttons3 : "",
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "left",
		
		content_css : "/ui/css/text.css"
	
	});
	
	
	
	
		
	/* **************************************************************************************** */
	/* END: tinyMCE */
	
	
	
	
	/* tinyMCE Groups */
	/* **************************************************************************************** */
	$('textarea.groupEditBody').tinymce({
			// Location of TinyMCE script
			script_url : '/ui/js/tiny_mce/tiny_mce.js',		 
			
			width: "670px",
			height: "400px",
			
			theme : "advanced",		
			plugins : "paste, linkautodetect",
			theme_advanced_buttons1 : "bold,italic,separator,bullist,numlist,link,unlink",
			theme_advanced_buttons2 : "",
			theme_advanced_buttons3 : "",
			theme_advanced_toolbar_location : "top",
			theme_advanced_toolbar_align : "left",
			
			content_css : "/ui/css/text.css"
		
		});
	/* **************************************************************************************** */
	/* END: tinyMCE */
	
	
	
	// Hide the password forms in all login boxes
	$(".forgottenpasswordform").hide();
	$(".passwordSentConfirmation").hide();
	
	/* Click event for the window */
	$(document).click(function() { 
	 	
		/* If there is a click outside of the dropdown panel and it is being displayed then hide it and revert the button to normal */
		if ($('#dropdownLoginPanel').hasClass('activated')) {			
			$('#signinButtonHeader').removeClass('activated'); //Revert button to normal state
			$('#dropdownLoginPanel').removeClass('activated'); //Hide the panel
		}
		
		
		if ($('div#wrapperInviteFacebookFriends').is(':visible')) {
			$('a#buttonInviteFacebookFriends').toggleClass('close');
			$('div#wrapperInviteFacebookFriends').toggle();
		}
		
		if ($('div#wrapperSignInFacebook').is(':visible')) {
			$('a#buttonSignInFacebook').toggleClass('close');
			$('div#wrapperSignInFacebook').toggle();
		}
		
		
		
		$(document).trigger('hideCluetip');

	 });
	 
	 /* Zebra stripe listings */
	$(".listingResults li:even").addClass("alt");

	 
	 
	/* HEADER */
	/* *************************************************************************************** */
	
	/* Highlight current section on the main menu */
	highlightNav();	

	
	/* Click event for header sign-in button */
	$('#signinButtonHeader').click(function(e){
		
		e.preventDefault(); //Stop the link from changing the page
		
		$('#signinButtonHeader').toggleClass('activated'); //Alernate button between dropdown display state and normal state
		$('#dropdownLoginPanel').toggleClass('activated'); //Alernate panel between dropdown display state and normal state
		
		e.stopPropagation(); //prevent the clck from bubbling back up to the document level - where it would close the menu
		
	});
	
	
	/* Click event for the dropdown login panel */
	$('#dropdownLoginPanel').click(function(e){ 
	     e.stopPropagation(); //prevent the clck from bubbling back up to the document level - where it would close the menu
	 });


	/* *************************************************************************************** */
	/* END: HEADER */
	
	




	
	/* Sign in feature functions */
	/* *************************************************************************************** */


	function signInFormAJAX(form, section) {

		var $uniqueformname = form.id.replace('LoginForm',''); //get the identifier of the specific login form being used 
		
		var emailval = $("#" + $uniqueformname + "email").val();  //get the email address entered
		var pwordval = $("#" + $uniqueformname + "password").val();	  //get the password entered
		var rememberval = $("#" + $uniqueformname + "rememberme").val(); //get the remember tick box
		
		/*alert("Email: " + emailval + "\nPass: " + pwordval + "\nRem: " + rememberval);*/
		
		
		$.post("/user/sign-in-ajax.html", {
		    email: emailval,
		    password: pwordval,
			rememberme: rememberval
		}, function(data){

			
			/*alert(data);*/
			
			if(data != 0){

				
				$("#userStatusWrapper").fadeOut('fast').load('/user/user-panel.html', function(){$("#userStatusWrapper").fadeIn('slow')});
				$('#' + form.id).children('.errorPanel').hide();
				
				if(section == "contribute"){		
						
						$contributionWrapper = $('#' + form.id).parents('.contributionWrapper');
	
						$contributionWrapper.children('.loginPanel').hide('fast');
						$contributionWrapper.children('.contributionPanel').removeClass('notloggedin');
						$contributionWrapper.children('.contributionPanel').removeClass('notInvited');
						$contributionWrapper.find('.articleTitle').focus();
						
				}
				
				if(section == "header" || section == "signin" || section == "signup"){		
						window.location = '/user/';
				}
				
				if(section == "notice"){		
						$('#noticeloginWrapper').fadeOut('fast');
						$('#noticeAdd').fadeIn('fast');
				}
				
				if(section == "adbuilder"){		
						$('#adbuilderloginWrapper').fadeOut('fast');
						$('#tryNowWrapper').fadeIn('fast');
				}
				
				
				if(section == "photos"){		
						$('#photosloginWrapper').fadeOut('fast');
						$('#photoUploadWrapper').fadeIn('fast');
				}
					
			}
			else {
				$('#' + form.id).children('.errorPanel').fadeIn('fast');
			}
			
		});
		
		return false;
	};
	
	
	function passwordFormAJAX(form, section) {
			
		var $uniqueformname = form.id.replace('PasswordForm',''); //get the identifier of the specific login form being used 
		var emailval = $("#" + $uniqueformname + "passwordemail").val();  //get the email address entered
		
		$.post("/user/forgotten-password-ajax.html", {
		    email: emailval
		}, function(data){
			if(data == 1){
				$('#' + form.id).children('.errorPanel').hide();	
				$("#" + $uniqueformname + "EmailForPassword").html(emailval);
				
				$("#" + form.id).parents('.signinfeature').children('.signinform').find('.email').val(emailval);
				$("#" + form.id).fadeOut('fast');
				$("#" + $uniqueformname + "PasswordSentConfirmation").fadeIn('fast');
				
			}
			else {
				$('#' + form.id).children('.errorPanel').fadeIn('fast');			
			}	
		});
		
		return false;
	};
	
	
	/* ************************************************************************** */
	/* Validation for the header login form */
	/* ignoreTitle prevents an IE GoogleToolbar bug on error messages */ 
	$('#headerLoginForm').validate({
		ignoreTitle:true, submitHandler: function(form){
			signInFormAJAX(form, "header");
 		}
	});
	
	
	$('#articleLoginForm').validate({
		ignoreTitle:true, submitHandler: function(form){
			signInFormAJAX(form, "contribute");
		}
	});
	

	$('#commentLoginForm').validate({
 		ignoreTitle:true, submitHandler: function(form){
			signInFormAJAX(form, "contribute");
 		}
	});
	
	$('#noticeLoginForm').validate({
 		ignoreTitle:true, submitHandler: function(form){
			signInFormAJAX(form, "notice");
 		}
	});
	
	
	
	$('#adbuilderLoginForm').validate({
 		ignoreTitle:true, submitHandler: function(form){
			signInFormAJAX(form, "adbuilder");
 		}
	});
	
	$('#photosLoginForm').validate({
 		ignoreTitle:true, submitHandler: function(form){
			signInFormAJAX(form, "photos");
 		}
	});
	
	
	$('#signinLoginForm').validate({
 		ignoreTitle:true, submitHandler: function(form){
			signInFormAJAX(form, "signin");
 		}
	});
	
	
	
	$('#signupLoginForm').validate({
 		ignoreTitle:true, submitHandler: function(form){
			signInFormAJAX(form, "signup");
 		}
	});
	





	/* ************************************************************************** */
	/* Validation for the forgotten password form */
	/* ignoreTitle prevents an IE GoogleToolbar bug on error messages */ 
	$('#headerPasswordForm').validate({ignoreTitle:true,
 		submitHandler: function(form) {
   			passwordFormAJAX(form);
 		}
	});
	
	$('#articlePasswordForm').validate({ignoreTitle:true,
 		submitHandler: function(form) {
   			passwordFormAJAX(form);
 		}
	});
	
	$('#commentPasswordForm').validate({ignoreTitle:true,
 		submitHandler: function(form) {
   			passwordFormAJAX(form);
 		}
	});
	
	$('#noticePasswordForm').validate({ignoreTitle:true,
 		submitHandler: function(form) {
   			passwordFormAJAX(form);
 		}
	});
	
	
	$('#signinPasswordForm').validate({ignoreTitle:true,
 		submitHandler: function(form) {
   			passwordFormAJAX(form);
 		}
	});
	
	$('#adbuilderPasswordForm').validate({ignoreTitle:true,
 		submitHandler: function(form) {
   			passwordFormAJAX(form);
 		}
	});
	
	$('#photosPasswordForm').validate({ignoreTitle:true,
 		submitHandler: function(form) {
   			passwordFormAJAX(form);
 		}
	});
	
	
	$('#signupPasswordForm').validate({ignoreTitle:true,
 		submitHandler: function(form) {
   			passwordFormAJAX(form);
 		}
	});
	
	
	
	/* Click event for forgotten password link	*/
	$('.forgottenPasswordLink').click(function(e){
		e.preventDefault(); //Stop the link from changing page
		$(this).parents('.signinform').hide();
		$(this).parents('.signinfeature').children('.forgottenpasswordform').show().find('input:[name=email]').focus();		
	});
	
	$('.returnToLoginLink').click(function(e){
		e.preventDefault(); //Stop the link from changing page
		$(this).parents('.passwordSentConfirmation').hide();
		$(this).parents('.forgottenpasswordform').hide();
		
		var $signinform = $(this).parents('.signinfeature').children('.signinform');
		$signinform.children('.errorPanel').hide();
		
		$signinform.show();	
	});

	

	/* *************************************************************************************** */	
	/* END: Sign in feature functions */
	
	
	
	
	
	
	
	
	
	
	/* Sign Up / Edit */
	/* *************************************************************************************** */	
	
	$("#currentuserSettingsForm").validate({
	  rules: {
	    password: "required",
	    passwordagain: {
	      equalTo: "#password"
	    }
	  }
	});
	
	$("#newuserSettingsForm").validate({
	  rules: {
	    password: "required",
	    passwordagain: {
	      equalTo: "#password"
	    }
	  }
	});
	
	/* *************************************************************************************** */	
	/* Sign Up / Edit */
	
	
	
	
	
	
	
	/* Facebook Invite and Sign-In */
	/* ************************************************************************************** */
	
	$('a#buttonInviteFacebookFriends').click(function(e){
		
		e.preventDefault(); //Stop the link from changing page
		$(this).toggleClass('close');		
		$('div#wrapperInviteFacebookFriends').toggle();
		
		e.stopPropagation(); //prevent the clck from bubbling back up to the document level - where it would close the popup
	});
	
	
	
	$('a#buttonInviteFacebookFriends2').colorbox({inline:true, href:"#fbInviteWrapper"});

	
	
	
	$('a#buttonSignInFacebook').click(function(e){		
		e.preventDefault();
		facebookConnect();
		e.stopPropagation(); //prevent the clck from bubbling back up to the document level - where it would close the popup	
			
	});
	
	$("a#followOnFBUser").attr("href", $("a#followOnFB").attr("href"));
	$("a#followOnTwitterUser").attr("href", $("a#followOnTwitter").attr("href"));


	/* *************************************************************************************** */	
	/* END: Facebook Invite */
	
	
	
	
	
	/* Contribution Panel */
	/* *************************************************************************************** */
	

	function showContribution(invitationID){
		
		var activeInvitation = $("#" + invitationID);
		
		activeInvitation.fadeOut("fast");
		
		$contributionPanel = activeInvitation.parents('.contributionWrapper').children('.contributionPanel');
		$loginPanel = activeInvitation.parents('.contributionWrapper').children('.loginPanel');
		
		
		if ($contributionPanel.hasClass('loggedin')) {
			$contributionPanel.removeClass('notInvited');
			$contributionPanel.find('.articleTitle').focus();
		}
		else {
			$loginPanel.fadeIn('fast');
			$loginPanel.find('.email:first').focus();
		}
	};
	
	
	// Invitation box click
	$('.contributionInvitation').click(function(e){
		e.preventDefault();		
		showContribution($(this).attr("id"));
	});
	
	// Contribute button click
	$('.contributeActionButton').click(function(e){
		e.preventDefault();
		showContribution($(this).siblings("div.contributionWrapper").children(".contributionInvitation").attr("id"));
	});
	

	
	$('.addPhotoButton').click(function(e){
		e.preventDefault();
		
		
		
		
		$(this).parents('.contributionform').find('.photoAddItem:hidden:first').fadeIn("fast");
		$(this).parents('.contributionform').find('.photoAddCaption:hidden:first').fadeIn("fast");
		
		$(this).html("Add another photo");
		
		
		
		
		if ($(this).parents('.contributionform').find('.photoAddItem:hidden').length == 0){
			$(this).hide("fast");
		}

		$(this).parents('.contributionform').find('.mediaCopyright').fadeIn("fast");
		
		
		
	});
	
	$('.addVideoButton').click(function(e){
		e.preventDefault();
		$(this).hide("fast");
		$(this).parents('.contributionform').find('.videoAddItem').fadeIn("fast");
		
		$(this).parents('.contributionform').find('.mediaCopyright').fadeIn("fast");
	});
	
	
	$('.previewButton').click(function(e){
		
		e.preventDefault();

		/* force update from tinymce*/
		tinyMCE.triggerSave();

		// Get Contribution Form object
		$contributionForm = $(this).parents('.contributionform');	
		
		// Get Contribution Preview Panel object
		$contributionPreview = $contributionForm.find('.contributionPreview');			
		
		$contributionPreview.find('.contributePreviewTitle').html($contributionForm.find('.articleTitle').val());
		$contributionPreview.find('.contributePreviewBody').html($contributionForm.find('.articleBody').val());
		
		
		if($contributionForm.find('.contributePhoto').val() && $contributionForm.find('.contributeVideo').val()){
			$contributionPreview.find('.contributePreviewPhotoVideo').show();
		}
		else{
			if($contributionForm.find('.contributePhoto').val()){
				$contributionPreview.find('.contributePreviewPhoto').show();
			}
			if($contributionForm.find('.contributeVideo').val()){
				$contributionPreview.find('.contributePreviewVideo').show();
			}
		}	
		
		if($contributionForm.valid()) {
			$contributionForm.find('.contributionCreate').fadeOut("fast");
			$contributionForm.find('.contributionPreview').fadeIn("fast");
			
			// Make sure that the preview panel is visible when the change is made
			// Examples of this may be - a user who has added a picture, creating a long edit form
			actionButtonID = $contributionForm.parents(".contributeToolWrapper").children('.mainHeadline').attr("id");
			scrollToId(actionButtonID);
		}

	});
	
	
	$('.changeButton').click(function(e){
		
		e.preventDefault();
		
		$contributionForm = $(this).parents('.contributionform');
		
		$contributionForm.find('.contributionPreview').hide("fast");
		$contributionForm.find('.contributePreviewPhoto').hide();
		$contributionForm.find('.contributePreviewVideo').hide();
		$contributionForm.find('.contributePreviewPhotoVideo').hide();
		
		$contributionForm.find('.contributionCreate').fadeIn("fast");
		
		// Make sure that the edit panel is visible when the change is made
		// Examples of this may be - a user who has added a lot of text that makes the preview panel vertically tall
		actionButtonID = $contributionForm.parents(".contributeToolWrapper").children('.mainHeadline').attr("id");
		scrollToId(actionButtonID);
	});
	
	
	$('#articleContributionForm').validate({
			ignoreTitle:true,
			
			rules: {
			    photo: {
			      accept: "jpeg|jpg|gif|png"
			    },	
				photo2: {
			      accept: "jpeg|jpg|gif|png"
			    },
				photo3: {
			      accept: "jpeg|jpg|gif|png"
			    },			
			    copyright_agree: {
		      		required: function(element) {
						return $("#articleContributePhoto").val() != '';
		      		}
   				 }
			}
		});
		

	$('#commentContributionForm').validate({
			ignoreTitle:true,
			
			rules: {
			    photo: {
			      accept: "jpeg|jpg|gif|png"
			    },			
			    copyright_agree: {
		      		required: function(element) {
						return $("#commentContributePhoto").val() != '';
		      		}
   				 }
			}
		});
		
		
	
	$('#articleContributionForm').validate({
			ignoreTitle:true,
			
			rules: {
			    photo: {
			      accept: "jpeg|jpg|gif|png"
			    },	
				photo2: {
			      accept: "jpeg|jpg|gif|png"
			    },
				photo3: {
			      accept: "jpeg|jpg|gif|png"
			    },			
			    copyright_agree: {
		      		required: function(element) {
						return $("#articleContributePhoto").val() != '';
		      		}
   				 }
			}
		});
		
	
	
	
	$('#photoUploadForm').validate({
		ignoreTitle:true,
		
		submitHandler: function(form) {
   			$('#photoUploadProgressMessage').show();
			form.submit();
 		},
		
		rules: {
		    photo: {accept: "jpeg|jpg|gif|png"},
			photo2: {accept: "jpeg|jpg|gif|png"},
			photo3: {accept: "jpeg|jpg|gif|png"},
			photo4: {accept: "jpeg|jpg|gif|png"},
			photo5: {accept: "jpeg|jpg|gif|png"},
			photo6: {accept: "jpeg|jpg|gif|png"},
			photo7: {accept: "jpeg|jpg|gif|png"},
			photo8: {accept: "jpeg|jpg|gif|png"},
			photo9: {accept: "jpeg|jpg|gif|png"},
			photo10: {accept: "jpeg|jpg|gif|png"},
			photo11: {accept: "jpeg|jpg|gif|png"},
			photo12: {accept: "jpeg|jpg|gif|png"},
			photo13: {accept: "jpeg|jpg|gif|png"},
			photo14: {accept: "jpeg|jpg|gif|png"},
			photo15: {accept: "jpeg|jpg|gif|png"},
			photo16: {accept: "jpeg|jpg|gif|png"},
			photo17: {accept: "jpeg|jpg|gif|png"},
			photo18: {accept: "jpeg|jpg|gif|png"},
			photo19: {accept: "jpeg|jpg|gif|png"},
			photo20: {accept: "jpeg|jpg|gif|png"},			
		    copyright_agree: {
	      		required: function(element) {
					return $("#articleContributePhoto").val() != '';
	      		}
				 }
		}
	});
		
	
	
	/* Group edit forms */
	
	/*$('.groupEditForm').validate({ignoreTitle:true,});*/
	
	
	
	
	/* **************************************************************************************** */
	/* END: Contribution Panel */


	
	/* Comment Carousel */
	/* **************************************************************************************** */
	
	//$('.commentContainer a').text('link'); //replace any link text with a generic label - to ensure no long addresses skew the height of the box.
	
	/*var data = $('.commentBody').text().split(" ");
    for (var i = 0; i < data.length; i++) {
		if (data[i].length >= 20) {
			alert(data[i]);
		}
	}*/
	
	
	
	
	$('.commentWindow').parent().before('<div id="commentPager">'); //Add the markup for the pager in on-the-fly - the plugin will fill it
	
	// fade the comments in first
	$('.commentWindow li:first').fadeIn(1000, function() {
       // cycle the comments using the cycle plugin
	    $('.commentWindow').cycle({ 
		    fx:     'scrollLeft', 
		    speed:  'slow', 
		    timeout: 5000, 
		    pager:  '#commentPager',
			cleartype: !$.support.opacity
		});
    });
	
	
	

	
	
	
	
	/* **************************************************************************************** */
	/* END: Comment Carousel */
	
	
	
	
	
	
	
	
	/* Article */
	/* **************************************************************************************** */
	
		// Make the first paragraph of an article bold, if there is more than one paragraph
		if ($('.articleBody p').size() > 1) {
			$('.articleBody p').first().css('font-weight', 'bold');
		};
		
		 $(function() {
	        $('a.lightBoxApply').lightBox();
	    });

	/* **************************************************************************************** */
	/* END: Article */
	
	
	
	
	/* Tabs */
	/* **************************************************************************************** */
	
	function tabDisplay(e, linkObject){
		e.preventDefault();
		$('.tabSetContent').hide();
		$('.tabMenu li').removeClass('selected');		
		$(linkObject).parent().addClass('selected');
	}
	
	
	
	/* User Profile */
	
	$('#userProfileHomeTabButton').click(function(e){
		tabDisplay(e, this);
		$('#welcomePanel').show();		
		$('.settingsupdateConfirmation').hide();
	});
	
	
	$('#userProfileContributeTabButton').click(function(e){
		tabDisplay(e, this);
		$('#contrbutePanel').show();		
		$('.settingsupdateConfirmation').hide();
	});
	
	
	$('#userProfileSettingsTabButton').click(function(e){
		tabDisplay(e, this);
		$('#settingsPanel').show();
	});
	
	
	$('#userProfileGroupsTabButton').click(function(e){
		tabDisplay(e, this);
		$('#groupsPanel').show();
	});
	
	
	

	/* Public Profile */
	
	$('#publicProfileCommentsTabButton').click(function(e){
		tabDisplay(e, this);
		$('#publicProfileCommentsPanel').show();
	});
	
	$('#publicProfileNewsTabButton').click(function(e){
		tabDisplay(e, this);
		$('#publicProfileNewsPanel').show();
	});
	
	$('#publicProfileMemoriesTabButton').click(function(e){
		tabDisplay(e, this);
		$('#publicProfileMemoriesPanel').show();
	});
	
	$('#publicProfileGroupsTabButton').click(function(e){
		tabDisplay(e, this);
		$('#publicProfileGroupsPanel').show();
	});
	
	$('#publicProfileNoticesTabButton').click(function(e){
		tabDisplay(e, this);
		$('#publicProfileNoticesPanel').show();
	});

	
	function showPublicPanels (){
		/* Make sure that the first active tab in public profile is visible, this will mke it work even if a user hasn't posted any comments, which is the default first tab */
		$('#publicProfileTabs li:first').addClass('selected');
		var $panelName = $('#publicProfileTabs li:first a').attr('id').replace('TabButton','');	
		$('#' + $panelName + 'Panel').show();
	}
	
	//showPublicPanels();
	
	

	/* **************************************************************************************** */
	/* END: Tabs */
	
	
	/* User profile */
	/* **************************************************************************************** */
	
	// The add photo invitation button when a user has not uploaded a photo
	$('.userPhotoActionButton').click(function(e){
		e.preventDefault();
		$('.tabSetContent').hide();
		$('.tabMenu li').removeClass('selected');		
		$('#userProfileSettingsTabButton').parent().addClass('selected');	
		$('#settingsPanel').show();
		
		//Highlight (short fading spotlight) the photo selection area on the form 
		$('#photoSelection').addClass("oneSecondSpot");
		$('#photoSelectionMessage').addClass("oneSecondSpot");
		$('#currentPhotoPreview').addClass("oneSecondSpot");

		$('#photoSelection').removeClass("oneSecondSpot", 6000);
		$('#photoSelectionMessage').removeClass("oneSecondSpot", 6000);
		$('#currentPhotoPreview').removeClass("oneSecondSpot", 6000);
	});
	
	
	//Highlight (short fading spotlight) the photo selection area on the form 
	$('.contributionInvitationzzz').addClass("oneSecondSpot");		
	$('.contributionInvitationzzz').removeClass("oneSecondSpot", 3000);
	
	
	
	$(".groupEditList li a").click(function(e){

		e.preventDefault();
		$('.groupEditList li').removeClass('selected');
		
		$(this).parent().addClass('selected');
		var $panelName = this.id.replace('groupEditLink','groupEditPanel');	

		$('.groupEditPanel').hide();		
		$('#' + $panelName).show();	


		
	});
	
	
		/* AJAX delete group image */
		/* *************************************************************************************** */
	
		$('.deleteGroupImage').click(function(e){
	
			e.preventDefault();
			
			var $answer = confirm("Press OK to delete this image, otherwise press cancel");
		    if ($answer) {
			
				var $imageContainer = $(this).parent('li');
				var $triggerLink = $(this)
				
				$(this).addClass('inprogress');
				$("*").css("cursor", "progress");
				
				$.post($triggerLink.attr("href"), {
					deleteimage: $triggerLink.attr("id")
				}, function(data){
				
					if (data != 0) {
						$imageContainer.fadeOut();
						$("*").css("cursor", "auto");
						$(this).removeClass('inprogress');
					}
					else {
						$("*").css("cursor", "auto");
						$(this).removeClass('inprogress');
					}
					
				});
			}
			
			
	
	
		});
		
		/* *************************************************************************************** */
		/* END: AJAX delete group image */

	
	
	
	// Remove confirmation panel after five seconds	
	setTimeout(function(){
  			$("div.settingsupdateConfirmation").fadeTo("slow", 0.1, function () {
 		 			$("div.settingsupdateConfirmation").slideUp("fast", function(){
						$("div.settingsupdateConfirmation").remove();
					});
     	 		});
		}, 5000);
	
	/* **************************************************************************************** */
	/* END: User profile */
	
	
	
	/* Tooltips */
	/* **************************************************************************************** */
	
		$('.clueTipLink').cluetip({activation: 'click', sticky: true, width: 400, closePosition: 'title'});
		$('.clueTipLinkFixedHeight').cluetip({activation: 'click', sticky: true, width: 400, height: 400, closePosition: 'title'});		
	
	/* **************************************************************************************** */
	/* END: Tooltips */
	
	
	
	/* Expandable content */
	/* **************************************************************************************** */
	$('.expandlink').click(function(e){
		e.preventDefault();		
		$('.expandable').fadeIn('fast');
		$('.beneathExpand').addClass('clearSpace');
	});
	
	$('a.revealTrigger').click(function(e){
		e.preventDefault();		
		
		// First check for direct siblings
		$detailPanel = $(this).siblings('div.fadeReveal');
		if ($detailPanel.length == 0){ // If no direct siblings, check on the parent level
			$detailPanel = $(this).parents().siblings('div.fadeReveal');
		}
				
		
		
		// hide all fade reveal panels
		//$('div.fadeReveal').fadeOut('fast');
		
		if ($detailPanel.is(":hidden")){
			$detailPanel.fadeIn('fast');
		}
		else {
			$detailPanel.fadeOut('fast');
		}
		
	});

	/* **************************************************************************************** */
	/* END: Expandable content */
	
	
	
	


	
	/* Clickable paragraphs */
	/* **************************************************************************************** */
	$('.mainArticleSummary p').click(function(){
			$link = $(this).siblings('h1').children('a').attr("href");
			window.location = $link;
	});
	
	$('.subArticleSummary p').click(function(){
			$link = $(this).siblings('h2').children('a').attr("href");
			window.location = $link;
	});
	/* **************************************************************************************** */
	/* END: Clickable paragraphs */







	// Contribution panel Show automatically - if a condition is true
	
	setTimeout(function(){
			if ($('#addAction').html() == 'Add'){
				showContribution("articleContributionInvitation");
			}}, 1000);
	



	// Set the login panel dynamically, for the cached home pages that don't set the user status panel on the server side
	// Ensure that the sign-in buttons will only be replaced if the fetched data is for a logged in user. Otherwise the inserted content will be the sign-in buttons but they won't be bound to events 	
	$.get("/user/user-panel.html?time=" + Math.floor(Math.random()*501), 
		function(data){
	     if (data.indexOf('userProfilePanel') >= 0 ){
			$("#userStatusWrapper").html(data).fadeIn('slow');
		 } 
	   }	
	);

	
})

