/** 
 * Spelling suggestion, feedback handling
 * jguo@nla.gov.au | June 2009 | v1.2
 */

$(document).ready(function() {   	        

	$("div.inlineContent").empty();

   	var query = $('input#queryKeywords').val();
   	var baseUrl = $('input#baseUrl').val();
   	var loadSpellingSuggestion = $('input#loadSpellingSuggestion').val();
   	
   	if ((query.length > 0) && (loadSpellingSuggestion == 1)) {
		$.ajax({
		    url: encodeURI(baseUrl+'/spelling/check/output/ext-xml/?q='+query),
		    type: 'GET',
		    dataType: 'xml',
		    timeout: 10000,
		    error: function(){
				//don't do anything if the spelling checking service is unavailable
				$('div#spellingSuggestions').css("background","transparent");
				$('div#spellingSuggestions').empty();
		    },
		    success: function(xml) {
		    				    				    	
				var suggestedWords = $(xml).find('suggestion').text();	 			  
				var highlightedWords = $(xml).find('highlight').text();
				var controller = 'default';
				if (location.href.indexOf('search/web') > 0) 
					controller = 'web';			
		    	var suggestedLink = baseUrl+'/search/'+controller+'/?q='+encodeURI(suggestedWords);
				var suggestion = 'Did you mean <a class="suggestion" href="'+suggestedLink+'">'+highlightedWords+'</a> ?';		
				
				if(suggestedWords.length > 0) {        
					$('div#spellingSuggestions').html(suggestion);
					$('div#spellingSuggestions').css("background","transparent");
				} else {
					$('div#spellingSuggestions').html('&nbsp;');
					$('div#spellingSuggestions').css("background","transparent");
				}				
					        
		    }
		    
		});
		
	} else {
	
		$('div.spellingSuggestions').empty();
	
	}
	
	/* toggle result sets */
	
	$("div.sectionCtrl").click(
		function() {
			var baseUrl = $('input#baseUrl').val();
			
			var isHidden = $(this).parents("div.resultSet").find("div.records").is(":hidden");
			
			$(this).parents("div.resultSet").find("div.records").toggle("normal");			
			
			if (isHidden)
				$(this).parents("div.resultSet").find("img.sectionCtrlImg").attr("src", baseUrl+"/static/images/triangle_down.png");
			else
				$(this).parents("div.resultSet").find("img.sectionCtrlImg").attr("src", baseUrl+"/static/images/triangle_up.png");
			return true;
		}
	)
	
	$("a#webSummary").click(
		function() {
			if ($("div#webRecords").is(":hidden")) {
				$("div#webRecords").toggle("normal");
				$("div#webRecords").parents("div.resultSet").find("img.sectionCtrlImg").attr("src", baseUrl+"/static/images/triangle_down.png");
			}				
		}
	)
		
	$("a#catalogueSummary").click(
		function() {
			if ($("div#catalogueRecords").is(":hidden")) {
				$("div#catalogueRecords").toggle("normal");
				$("div#catalogueRecords").parents("div.resultSet").find("img.sectionCtrlImg").attr("src", baseUrl+"/static/images/triangle_down.png");
			}				
		}
	)
	
	$("a#eresourcesSummary").click(
		function() {
			if ($("div#eresourcesRecords").is(":hidden")) {
				$("div#eresourcesRecords").toggle("normal");
				$("div#eresourcesRecords").parents("div.resultSet").find("img.sectionCtrlImg").attr("src", baseUrl+"/static/images/triangle_down.png");
			}				
		}
	)		
	/* activate any inline interactive content. eg feedback form */
	/* currently inactive
	$("span.inlineFeedbackLink").click(
			function(){
				var activeItem = $(this).parents('li.resultItem');
				var urlText = activeItem.find('div.urlText').text();								
				var commentFormContent = '<strong><span id="suggestionInstruction">Suggest this document for review? Please tell us...</span></strong><br />';
					commentFormContent += '<lable>Your suggestion:<lable><br /><textarea id="suggestion" name="suggestion" rows="3" cols="45"></textarea><br />';
					commentFormContent += '<lable>Your email:<lable><br /><input type="text" width="65" name="email" id="email"><br />';
					commentFormContent += '<button>Send suggestion</button>';
				$("div.activeCommentForm").empty();
				$("div.activeCommentForm").removeClass('activeCommentForm');
				activeItem.find("div.inlineContent").addClass('activeCommentForm');
				activeItem.find("div.inlineContent").append('<div class="activeCommentFormContent">'+commentFormContent+'</div>');
				$('div.activeCommentFormContent button').click(function() {
					var suggestion = $("#suggestion").serialize();
					var email = $("#email").serialize();						
					var query = $('#queryKeywords').serialize();
					var ajaxData = suggestion+'&'+email+'&'+'url='+urlText+'&'+query;
    				$.ajax({
						url: baseUrl+'/feedback/send',
						dataType: "html",
						type: 'POST',
						data: ajaxData,
						error: function(){
							$("span#suggestionInstruction").html('Oops, we are having a glitch here, please contact the IT Service Desk.');
						},
						success: function(){		
							$("span#suggestionInstruction").html('Your suggestion has been sent!');
							activeItem.find("div.inlineContent").fadeOut('slow', function() {
								activeItem.find("span.inlineFeedbackLink").html('suggestion sent');
								activeItem.find("span.inlineFeedbackLink").addClass('inlineFeedbackLinkDisabled');	
								activeItem.find("span.inlineFeedbackLink").removeClass('inlineFeedbackLink');									
								$("div.activeCommentForm").empty();
								activeItem.find("div.inlineContent").removeClass('activeCommentForm');	
							});																			
						}
					});
						
  				});				

			}
		);
		*/
	 	
	});
	


