$(function() {
	$('#locale_en').click(function() {
		document.location.href = "/?locale=en";
	});
	
	$('ul.sortable li.sort').hover(
		function() { $(this).addClass('highlight') },
		function() { $(this).removeClass('highlight') }
	);
	
	$('ul.sortable.links').sortable({
    update : function () { 
      $.get('/sort_links?'+$(this).sortable('serialize'));
    }
	});

	$('ul.sortable.related_categories').sortable({
    update : function () { 
      $.get('/sort_related_categories?'+$(this).sortable('serialize'));
    }
	});
	
	// search form post
	$('form div.pagination a').click(function(ev) {
		ev.stopPropagation();
		ev.preventDefault();
		$('#page_search').val($(this).attr('href').split("?page=")[1]);
		$('#search_form').submit();
		return false;
	});
	
	$('.section_disabled input').attr('disabled', true);
	$('.section_disabled select').attr('disabled', true);

	$('#locale_fr').click(function() {
		document.location.href = "/?locale=fr";
	});
	
	$('#document_group_list').hide();
	$('#document_group_list.show').show();
	
	// document type by option selection
	$('#document_document_type_id').change(function() {
		changeDocumentFields($(this).val());
		// remove fieldWithErrors classes and error messages if they exist
		$('div').removeClass('fieldWithErrors');
		$('.errorExplanation').hide();
	});

	var doc_type = '';
	if($('#document_document_type_id').val() == 1) {
		doc_type = 1;
	} else if($('#document_document_type_id').val() == 2) {
		doc_type = 2;
	} 
	changeDocumentFields(doc_type);
	
	// document security by option selection
	$('#document_security').change(function() {
		$('#document_group_list').removeClass('show');
		if($(this).val() == 2) {
			$('#document_group_list').show();
		} else {
			$('#document_group_list').hide();
		}
	});

	//
	// Search-related scripting
	// Disables/enables sections based on criteria preferences
	//
	if($(".section_criteria").length > 0){
		for(var f in ["a","b","c"]){
			$("#section_filter_"+f+" .section_criteria input, #section_filter_"+f+" .section_criteria select, #section_filter_"+f+" .section_criteria textarea").attr("disabled",true);
		}

		$("input[name=filter_a],input[name=filter_b],input[name=filter_c]").change(function(){
			var which_filter = $(this).attr("name");
			if(this.value=="none"){
				$("#section_"+which_filter+" .section_criteria input, #section_"+which_filter+" .section_criteria select, #section_"+which_filter+" .section_criteria textarea").attr("disabled",true);
				$("#section_"+which_filter+" .section_criteria").addClass("section_disabled");
			} else {
				$("#section_"+which_filter+" .section_criteria input, #section_"+which_filter+" .section_criteria select, #section_"+which_filter+" .section_criteria textarea").attr("disabled",false);
				$("#section_"+which_filter+" .section_criteria").removeClass("section_disabled");
			}
		});
	}
	
	
	//
	// Editor's link / image chooser
	//
	$(document).ready(function(){
		if($("#editor_link_listing").length > 0 || $("#editor_image_listing").length  > 0 || $("#search_results").length > 0){
			$(".editor_link").click(function(evt){
				evt.stopPropagation();
				var link;
				if(evt.target.nodeName.toLowerCase()=='img'){
					link = $(this).attr("src");
				} else {
					link = $(this).attr("href");
				}
				window.opener.linkToDocument(link);
				window.close();
				return false;
			});
		}
	});
	
});

// determines which fields to show based on the document type
function changeDocumentFields(val) {
	if(val == 1) {
		// arbitration award
		$('.arb').show();
		$('.arb_wra').show();
		$('.wra').hide();
		$('.other').hide();
	} else if(val == 2) {
		// wra award
		$('.wra').show();
		$('.arb_wra').show();
		$('.arb').hide();
		$('.other').hide();
	} else {
		// other
		$('.other').show();
		$('.wra').hide();
		$('.arb_wra').hide();
		$('.arb').hide();
	}
}

function getAjaxResponse(url, container, item, request_name) {
	if(request_name == "POST") {
		$.ajax({
			type: "POST",
			data: $(item).serialize(),
			url: url,
			cache: false,
			success: function(html){
				$(container).replaceWith(html);
			},
			error: function(html) {
				$(container).replaceWith(html);
			}
		});
	} else {
		$.ajax({
			type: "GET",
			url: url,
			cache: false,
			success: function(html){
				$(container).replaceWith(html);
			},
			error: function(html) {
				$(container).replaceWith(html);
			}
		});
	}
}

function searchDocs(language, search_text) {
	if(search_text.length > 2) {
		getAjaxResponse('/search_name?language=' + language + '&search_text=' + escape(search_text), '#search_' + language + '_doc_results', '', 'GET');
	} else {
		$('#search_' + language + '_doc_results').html('');
	}
}

function searchDocsAdd(search_text) {
	if(search_text.length > 2) {
		getAjaxResponse('/search_name?type=new&search_text=' + escape(search_text), '#search_doc_results', '', 'GET');
	} else {
		$('#search_doc_results').html('');
	}
}

function searchArticles(language, search_text) {
	if(search_text.length > 2) {
		getAjaxResponse('/search_title?language=' + language + '&search_text=' + escape(search_text), '#search_' + language + '_article_results', '', 'GET');
	} else {
		$('#search_' + language + '_article_results').html('');
	}
}