﻿var closeButton = $('<a href="#" class="close">close</a>');

/*$(window).load(function()	{
	$('#my_account.loggedout li#my_account_login  a').click();
});*/

$(function()	{

/* ============== GENERIC ============== */

	$('body').removeClass('js-disabled').addClass('js-enabled');

	if ($.browser.safari && $.browser.version<=530) {// adds additional 1px to safari browsers as alignment was off for nav
		$('#mast .nav').css('paddingTop','63px');
	}

	$('a.thickbox').each(function()	{
		href = $(this).attr('href');
		// *************
		
		// *************
		$(this).attr('href', href.replace(/\?/, "?js=yes&"));
	});
//	$('iframe').each(function()	{
//		this.src = this.src.replace(/\?/, "?js=yes&").replace(/\.aspx$/g, ".aspx?js=yes");
//	});
	
	// png fix init /* COMMON */
	if ($.browser.msie && $.browser.version<7) {
		$.ifixpng.pixel = '/magazine/graphics/pixel.gif';
		$('img[@src$=.png],input[@src$=.png]').not('.nofix').ifixpng();
	}

	$('#main input.text, #inform input.infsmall, #inform input.infmedium, #inform input.inflarge').focus(function() {
		$(this).css({backgroundColor:'#ffffb7', border:'1px solid #000'})
	}).blur(function(){
		$(this).css({backgroundColor:'#fff', border:'1px solid #bfbfbf'})
	});

	$('input.text[title]').setFormInstructions();
	
	$('.factfile').wrapInner('<div class="factfile_sleeve"></div>');

	/* nav 2nd level alignment */
	/* Comented out in order to fix 715
	//realignNav();
 
	// button hover
	$('input.button,img.button').hover(buttonOver,buttonOut);

	// hiding initial values
	$('input[value="Enter keyword here"]').one('click',function(){this.value='';});

//	/* search type panel */
//	$('#mast>form .search_type').prepend('<a href="#" class="close">close</a>').children('a.close').click(function(){$(this.parentNode).fadeOut()});
//	$('#mast_search').focus(function(){$('#mast>form .search_type').fadeIn()});
//	$('#mast_search_submit').focus(function(){$('#mast>form .search_type',this).fadeOut()});

//	/* nav border colour animations */
//	$('.nav li:not(.on)>a').hover(
//		function()	{	navBorder($(this), '#959595');	},
//		function()	{	navBorder($(this), '#FFFFFF');	});
//	$('.nav>ul>li.on>a').hover(
//		function()	{	navBorder($(this), '#000000');	},
//		function()	{	navBorder($(this), '#FFFFFF');	});
//	$('.nav>ul>li>ul>li.on>a').hover(
//		function()	{	navBorder($(this), '#4F4D45');	},
//		function()	{	navBorder($(this), '#FFFFFF');	});

	/* 'my account' boxes */ /* COMMON */
	$('#my_account.loggedout li#my_account_login  a').click();


	// avatar popup functionality
	$('div.avatar').hover(function(){$(this).addClass('over').children('.avatar_options').show();},function(){$(this).removeClass('over').children('.avatar_options').fadeOut();});





	/*Events in a month*/
	$('.extras_box.events ul').each(function(){$(this).children('li:gt(19)').css('display','none');});
	$('.extras_box.events a.viewmore').bind('click',{initialNumber:'4'},facetViewMore);
	$('.extras_box.events a.viewfewer').bind('click',{initialNumber:'20'},facetViewLess);

	// search results page
	$('#search_results div.browse_section>div.facet_set>ul').each(function(){$(this).children('li:gt(8)').css('display','none')});
	$('#search_results div.browse_section>div.facet_set>a.viewmore').bind('click',{initialNumber:'9'},facetViewMore);
	$('#search_results div.browse_section>div.facet_set>a.viewfewer').bind('click',{initialNumber:'9'},facetViewLess);
//	$('#search_results div.browse_section>div.facet_set>ul').not('#search_results div.browse_section>div.Month>ul').each(function(){$(this).children('li:gt(8)').css('display','none')});
	$('#search_results div.browse_section>div.Month>ul').each(function(){$(this).children('li:gt(8)').css('display','block')});

	// search results tabs
	$('#jobssearchresults ul.refine_tabs>li>a, #search_results ul.refine_tabs>li>a').click(searchResultsTabs);
	$('#search_results div.browse_section').not(':first').hide();

	// search results area select
	$('#search_results div.box_category select').change(function(){$(this).parents('form').submit()});

	// shows save search
	$('.saveSearch a').click(function(){
		if($('.sections.save_search').is(':hidden')) { $('.sections.save_search').slideDown('slow');}
		else { $('.sections.save_search:visible').slideUp('slow');}
		return false;
	});

	// Jobs - save search
	$('.saveButton_container .button').click(function(){$('.sections.save_search:visible').slideUp('slow');return false;});

	/* popup panels */
	$('.shareArticle>a, .saveSearchParagraph>a').click(openpopupPanel);
	if ($('#map_postcode>a').length > 0){
	    $('#map_postcode>a').click(function(){
		    $('.jobSearchPostcodePopup').fadeIn();
		    return false;
	    });
	}
	if($('.sendtoFriend').length>0){
		$('.sendtoFriend:eq(0)').prependTo($(document.body));
		$('#wrapper .sendtoFriend').each(function(){$(this).removeClass('sendtoFriend');});
	}
	$('.popupPanel_sleeve').prepend(closeButton);
	$('.close').click(closepopUpPanel);
	$('.sharewithFriend a').click(function(){
		var target = $('.sendtoFriend');		
		var xPos = $(this).offset().left-target.width()+$(this).width();
		var yPos = $(this).offset().top;
		var bodyHeight = $(document.body).height();
		var targetHeight = target.height()
		//stop it from going beneath the body
		if ((yPos + targetHeight) > bodyHeight)
			yPos = bodyHeight - targetHeight - 50;
		if(target.css('display') == 'block'){
			target.fadeOut();
		}
		else{
			target.fadeIn().css({zIndex:1000,top:yPos+20,left:xPos});
		}
		return false;
										   
	});

	// resource hub form
	$('.selectSubjects_sleeve input').click(function(){$(this).parent().parent().toggleClass('selected');});

	// tes newspaper
	$('.testabs li a').click(function(){$(this).parent().addClass('on').siblings().removeClass('on');});

	// forum topics
	$('.subjects_container .subjects_sleeve').prepend(closeButton);
	$('#browseStaffroom .button').click(function(){
		if($('.subjects_container').css('display') == 'block')	$('.subjects_container').fadeOut();
		else	$('.subjects_container').fadeIn();
		return false;
	});
	$('.subjects_sleeve .close').click(hideForum);

	// friends a-z
	$('.recipient .button').click(function(){
	$('.subjects_container').fadeIn();return false;});

	// myhome tabs
	$('.hometabsContainer .tc2').hide();
	$('.testabs li a',this).click(function(){
		$(this).parents('.testabs').siblings('.tc2').show().siblings('.tc1').hide();
		$('#nt').val("notice");
		return false;
	});
	$('.testabs li.on a',this).click(function(){
		$(this).parents('.testabs').siblings('.tc1').show().siblings('.tc2').hide();
		$('#nt').val("event");
		return false;
	});

	// forum post list styling - should be in a forum-specific JS file or keyed off id?
	$('ul.ForumPostList>li:odd').addClass('odd');

	// hides rss feeds except for first block 
	$('#myhome .sections .browse_section:gt(0) .heading').addClass('closed');
	$('#myhome .sections .browse_section:gt(0) .facet_set').hide();

	//$('#calendar .resourcesOptionsContainer').addClass('open');
	$('#calendar .resourcesOptionsSleeve').prepend(closeButton).children('a.close').click(function(){$(this).parents('.resourcesOptionsContainer').fadeOut(); return false;})
	$('#calendar h1 .button').click(function(){$(this).parent().siblings('.resourcesOptionsContainer').fadeIn();});
	$('#myalerts .delete').click(function()	{
		if(confirm('Are you sure you want to delete this job alert?'))
			return true;
		else
			return false;
	});  
    

	// my favourites - add note
	$('.addNote_container form').hide();
	$('.addNote_container .addNote',this).click(function(){
		$(this).siblings('form').show().end().hide();
	});
	$('.addNote_container .cancelNote',this).mousedown(function(){
		$(this).parents('form').hide().siblings('.addNote').show();
	});
	$('.addNote_container .saveNote',this).mousedown(function(){

		var textAreaVal = $(this).parent().parent().parent().find('textarea').val()
		var textAreaId =$(this).parent().parent().attr("id");
//		if (addNote(textAreaVal, textAreaId)) {//added be developer pw
		return addNote(textAreaVal, textAreaId)
		$(this).parents('form').hide().next().text(textAreaVal).show();
//		}
//		//else {
//		//$(this).parents('form').show();
//		//}
	});

	// my messages, sort
	//$('#myinbox table th').addClass('asc');
	$('#myinbox table th span').click(function(){
		if ($(this).parent().hasClass('asc')) { $(this).parent().removeClass('asc').addClass('des'); }
		else { $(this).parent().siblings('th').removeClass('asc').removeClass('des').end().addClass('asc').removeClass('des'); }
	});

	$('#myresources .delete a').click(function()	{
		if(confirm('Are you sure you wish to delete this resource?'))	{
			// this bit changes the link into the 'deleted' text
			$(this).parent().removeClass('delete').addClass('deleted').empty().text('Deleted');
			// Do something here....
			/*
				REDIRECT TO ANOTHER PAGE....
				window.location = '/home.aspx';
				
				SEND AN ID VIA AJAX
				$.post('deleteresource.ajax.aspx', {
					'id': 21754
				});
			*/
			//DEVELOPERS CODE ... THIS WILL NOT BE AJAXY CALL
			return true;
		}
		else
		return false;
	});
	
	//formSprite deleteButton
	$('#myresources .deleteButton').click(function()	{
		if(confirm('Are you sure you wish to delete this resource?'))	{

			$(this).parent().removeClass('deleteButton').addClass('deleted').empty();//.text('Deleted');
		
			return true;
		}
		else
		return false;
	});

	// rss feeds which may not have a text in a link which is then hidden
	$('.RSSFeeds .facet_set li h3 a').each(function(){
		if($(this).is(':empty')) {$(this).parent('h3').hide()}
	});

	//tabbed table zebra striping
	$('.tabbedTable tbody tr').removeClass('odd | even');
	$('.tabbedTable tbody tr:odd').addClass('odd');
	// styles for communtity server tabs
	$('div.CommonPaneTabSet table tr').children('td:last').find('.CommonPaneTab2').css('width','135px'); //adds additional width to the last tab item
	$('div.CommonPane>div>div:first').css('overflow','hidden'); // hides scrollbar for cs

});

function buttonOver() {
	if ($(this).is(':not(.over)')); {
		if ($.browser.msie && $.browser.version<7) { $(this).iunfixpng(); }
		this.src.match(/_over\..../) ? null : this.src = this.src.replace(/\.(...)$/,'_over.$1');
		if ($.browser.msie && $.browser.version<7) { $(this).ifixpng(); }
		$(this).addClass('over');
	}
}
function buttonOut() {
	if ($(this).is('.over')); {
		if ($.browser.msie && $.browser.version<7) { $(this).iunfixpng(); }
		this.src = this.src.replace(/_over\.(...)$/,'.$1')
		if ($.browser.msie && $.browser.version<7) { $(this).ifixpng(); }
		$(this).removeClass('over');
	}
}

function navBorder(el, colour) {
	el.find('>span').css('border-bottom-color', colour);
}

/* realign nav to be centered below parent, unless that'd push it off the right of the menu (in which case align right) */
function realignNav() {
	var secondLevel = $('.nav>ul>li.on>ul');
	if(secondLevel.length > 0) {
		if(secondLevel.parent().get('0').offsetLeft + secondLevel.parent().width()/2 + secondLevel.width()/2 <= 965) {
			secondLevel.css('right',parseInt(secondLevel.css('right')) - secondLevel.width()/2 + secondLevel.parent().width()/2);
		}
		else {
			secondLevel.css('right',secondLevel.parent().get('0').offsetLeft + secondLevel.parent().width() - 965)
		}
	}
}


function hideForum() {
	$(this).parents('.subjects_container').fadeOut('slow'); return false;
}

function searchResultsTabs() {
	if($(this.parentNode).is(':not(.on)')) {
		var index = $(this.parentNode.parentNode).children().index(this.parentNode);
		$(this.parentNode.parentNode).children().removeClass('on').eq(index).addClass('on');
		$(this).parents('div.refine').find('div.browse_section').hide().eq(index).show();
	}
	return false;
}

function facetViewMore(event) {
	$(this).prevAll('ul').children('li').show();
	$(this).hide().siblings('.viewfewer').show().css('display','block'); return false;
}
function facetViewLess(event) {
	$(this).prevAll('ul').children('li').slice(event.data.initialNumber).hide();
	$(this).hide().siblings('.viewmore').show().css('display','block'); return false;
}

function openpopupPanel(){
	$(this.parentNode.parentNode).find('.popupPanel').fadeOut();
	if($(this).siblings('.popupPanel').css('display') == 'block')	{
		$(this).siblings('.popupPanel').fadeOut();
	}
	else	{
		$(this).siblings('.popupPanel').fadeIn().css('z-index','1000');
	}
	return false;
}
function closepopUpPanel(){
	$(this).parents('.popupPanel').fadeOut('slow');
	return false;
}

//Developers code -- Add a note
function addNote(theNoteText, theNoteId)
{  
	var theForm = $('#' + theNoteId);
	
	errorText = validateNoteText(theNoteText); //validateNoteForm(theForm); 
	if (errorText == "")
	{
		 controlName = "FavouriteStoryNoteCT";
		 handlerName =    getHandlerName(controlName);
		 var handlerDataArray = getHandlerDataArray(controlName);
		 handlerDataArray = updateArrayValue(handlerDataArray, "mynote",theNoteText);           
		 handlerDataArray = updateArrayValue(handlerDataArray, "FavouriteCode",theNoteId);           
		 processedHandlerData = writeoutArray(handlerDataArray);           
		 invokeActionProvider(starSelectCallback, processedHandlerData,  handlerName);
		 return false;
	}
	else  
	{
		  theForm.find(".errormessage").html(errorText);
		  return false;
	}
}

function validateNoteText(textBox)
{
    errorText = "";
    if (textBox.length < 10)
    {
        errorText = "Note text must be longer than 10 characters";
    }
    if (textBox.length > 300)
    {
        errorText = "Note text must be shorter than 300 characters";
    }
    return errorText;
 
}
