jQuery(document).ready(function($) {
	
	$('#x').click(function() {
		$('#memdevcp').slideToggle();
	})
	
	if($("acronym[title],abbr[title], a.tip").length) {
		$("acronym[title],abbr[title]").tooltip({
			 extraClass : 'accr',
			 top: 14
		});
	}
	
	if($("a.tip").length) {
		$("acronym[title],abbr[title], a.tip").tooltip({
			 showURL: false
		});
	}
	
	/*
	if($("acronym[title],abbr[title], a.tip").length) {
		$("acronym[title],abbr[title], a.tip").tooltip({
			position: 'bottom',
			relative: false
			//offset: [0 , 120]
		});
	}*/
	
	if($('#suggest').length) {
		var suggest = $('input[id="suggest"]').suggest({
			className: 'simple',
			url: '/includes/js/response.php?request=suggest'
		});
	}
	
	// Side Menu
	$('#side-menu ul li ul').hide();
	$('#side-menu ul li a').hover(function() {
		$(this).parent().children('ul').slideDown('normal');
	});
	
	$('#homepage-search, #sidebar-search, #adv-search').submit( function(e) {
		var searchInput = $('#suggest');
		if((searchInput.val() == 'Enter your postcode, town or county'
			|| searchInput.val() == 'Enter postcode, town or county'
			|| searchInput.val().length === 0
			) && $('[name="session_type"]').val() != 'online'
		) {
			alert('Please enter your postcode, town or county.');
			e.preventDefault();
		}
	});
	
	$('#sidebar-search.advanced').submit(function(e) {
		if(!$('#suggest').val().length && !$('[name=service]').val().length && !$('[name=approach]').val().length && (!$('[name=keyword]').val().length || $('[name=keyword]').val() == 'Enter keyword') && $('[name=session_type]').val() == 'online'
			) {
			alert('Please select at least one further search option.');
		}
		e.preventDefault();
	});
	
	$('#search-form').submit( function(e) {
		var searchInput = $('#search-input');
		if(searchInput.val().length === 0 || searchInput.val() == 'Search entire site') {
			alert('Please enter something in the search box.');
			e.preventDefault();
		}
	});
	
	$(".submit-confirm").click(function(e) {
		if(confirm('Are you sure?') != true) {
			e.preventDefault();
		}
	});
	
	// Registration Form - Allow navigation away from page on Continue button
	$('.registration-continue').click(function() {
		needtoconfirm = false;
	});
	$('.complete-app').click(function() {
		needtoconfirm = false;
	});
	$('.preview-profile-trigger').click(function(e) {
		e.preventDefault();
		var url = $(this).attr('href');
		previewprofile(url);
	});
	
	var tinymce_settings_global = {
		script_url : '/includes/js/tiny_mce/tiny_mce.js',
		width: 675,
		theme : "advanced",
		cleanup_on_startup : true,
		button_tile_map : true,	
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "left",
		theme_advanced_statusbar_location : "none",
		theme_advanced_buttons1 : "bold,italic,underline,strikethrough,sup,|,justifyleft,justifycenter,justifyright,justifyfull,|,removeformat,styleselect,formatselect",
		theme_advanced_buttons3: '',
		theme_advanced_path : true,
		plugins : "paste",
		gecko_spellcheck : true,
		remove_trailing_nbsp : true,
		paste_auto_cleanup_on_paste : true,
		convert_urls: false
	};
	
	if( $('textarea.tinymce-admin').length) {	
		wysiwyg = $('textarea.tinymce-admin').tinymce(tinymce_settings_global);
	}
	
	var tinymce_settings_blog = {
		script_url : '/includes/js/tiny_mce/tiny_mce.js',
		width: 675,
		theme : "advanced",
		cleanup_on_startup : true,
		button_tile_map : true,	
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "left",
		theme_advanced_statusbar_location : "none",
		theme_advanced_buttons1 : "bold,italic,underline,strikethrough,sup,|,justifyleft,justifycenter,justifyright,justifyfull,|,removeformat,styleselect,formatselect",
		theme_advanced_buttons3: "tablecontrols,|,preview",
		theme_advanced_path : true,
		plugins : "paste,table,preview",
		gecko_spellcheck : true,
		remove_trailing_nbsp : true,
		paste_auto_cleanup_on_paste : true,
		convert_urls: false,
		content_css : "/includes/css/tinymce_custom.css"
	};
	
	if( $('textarea.tinymce-blog').length) {	
		wysiwyg = $('textarea.tinymce-blog').tinymce(tinymce_settings_blog);
	}
	
	$('#eventscal').dialog({
		autoOpen: false,
		resizable: false,
		draggable: false,
		width: 860,
		height: 690
	})
	$('.eventscal-trigger').click( function(event) {
		event.preventDefault();
		event.stopImmediatePropagation();
		
		$('#eventscal').dialog('open');
	});
	
	$('.verification-section').each( function(){	
		if(!$(this).is(':checked')){
			$(this).parent().next().hide();
		}
	});
	
	$('.verification-section').change( function() {
		var slider = $(this).attr('id');
		$('.verification-section').each( function() {
			if($(this).attr('id')==slider){
				$(this).parent().next().css('width', 585).slideDown(500, function(){
					$('html,body').animate({scrollTop: $('.numberCircleGrey').offset().top});
				}).css('width', 'auto');
				$(this).prev('label').addClass('checked');			
			} else {
				$(this).parent().next().css('width', 585).slideUp(500).css('width', 'auto');
				$(this).prev('label').removeClass('checked');
			}
		});	
	});
	
	$('.bodyproof').change( function() {
		if($(this).is(':checked')){
			var body = $(this).val();	
			var nextdiv = $(this).next().next('.bodyproofcat').html();
			if(!nextdiv){
				$(this).next('label').after('<div class="bodyproofcat" style="display: none;"></div>');
			}
			var bodyproofcat = $(this).next().next('.bodyproofcat');
			$.get('ajax.html', { action: 'get_body_categories', body: body }, function(data){
				if(data){
					var list = '<ul style="list-style: none; margin: 0 0 0 5px;">';
					$.each( data, function(i) {
						var category = String(this);
						if(category){
							list += '<li style="clear:left;"><input type="checkbox" style="float: left;" name="proof['+body+']['+i+']" /><span style="padding-left: 2px; float: left; width: 140px; font-size: 12px;">'+category+'</span></li>';
						}
					});
					list += '</ul>';
					bodyproofcat.append(list);
					bodyproofcat.slideToggle();
				}
			});
		} else {
			var nextdiv = $(this).next().next('.bodyproofcat');
			if(nextdiv.html()){
				$(nextdiv).slideToggle(function(){
					$(nextdiv).remove();
				});
			}
		}
	});
	
	$('label').disableSelection();
	
});

function previewprofile(url){
	$("body").css("overflow", "hidden");
	var $preview = $('#preview');
	$preview.dialog('destroy');
	var $curwidth = $(window).width()-20;
	if($curwidth > 1100){
		$curwidth = 1080;
	}
	var $curheight = $(window).height()-20;	
	if($curheight > 600){
		$curheight = 580;
	}	
	$preview.dialog({
		width: $curwidth,
		height: $curheight,
		draggable: false,
		title: 'Preview of your Profile',
		modal: true,
		resizable: false,
		create: function() {
			$("body").css("overflow", "hidden"); 
			$("#wrapper").css("height", "120px");
			$("#wrapper").css("width", "200px");
			$("#wrapper").css("overflow", "hidden");
			$("#footer").css("height", "100px");	
			$("#footer").css("width", "200px");	
			$("#footer").css("overflow", "hidden");	
		},
		open: function() { 
			
			// Detect ie6, and add special preview warning
			if($.browser.msie && $.browser.version=="6.0"){
				$preview.html('<iframe src="' + url + '" frameborder=0 width="' + ($curwidth-30) +'" height="' + ($curheight-90) + '" marginheight="0" marginwidth="0" scrolling="0"></iframe></div>');
				$preview.prepend('<div class="preview-bar"><strong>This is a Preview - This profile is Not Live</strong></div>');
			}
			else{
				$preview.html('<iframe src="' + url + '" frameborder=0 width="' + ($curwidth-30) +'" height="' + ($curheight-60) + '" marginheight="0" marginwidth="0" scrolling="0"></iframe></div>');
			}
			//
			$('.ui-widget-overlay').bind('click', function(){ $preview.dialog('close'); });
		},
		beforeClose: function() { 
			$("body").css("overflow", "visible"); 
			$("#wrapper").css("height", "100%");
			$("#wrapper").css("width", "992px");
			$("#wrapper").css("overflow", "visible");
			$("#footer").css("height", "100%");
			$("#footer").css("width", "980px");
			$("#footer").css("overflow", "visible");
		}
	});

}

/* Old functions */
function checkForOther(obj) { 
	if (!document.layers) { 
		var txt = document.getElementById("otherTitle"); 
		if (obj.value == "awaiting_go_ahead") { 
			txt.style.display = "inline"; 
			// gives the text field the name of the drop-down, for easy processing
		} else { 
			txt.style.display = "none"; 
			txt.name = ""; 
		} 
	} 
}

function checkForm(form) {
    // check form input values

    form.javButton.disabled = true;
    form.javButton.value = "Please wait...";
    return true;    
}
