$(document).ready(function() {
	/* added zebra effect for tables */
	if ($('table.annualreport')!=null){
		$('table.annualreport tbody tr:even').addClass('even');
	}
	if ($('table.download_list')!=null){
		$('table.download_list tbody tr:odd').addClass('even');
	}
	
	/* added hover effects on buttons */
	addButtonEffect('input.submit', 'submit_hover', 'submit_down');	
	addButtonEffect('input.reset', 'reset_hover', 'reset_down');
	addButtonEffect('input.search_go', 'go_hover', 'go_down');
	
	/*change printfriendly link */
	addPrintfriendly();
});

function addButtonEffect (element, hoverClass, downClass ){
	$(element).hover(
		function() {
			$(this).addClass(hoverClass);
		}, function () {
			$(this).removeClass(hoverClass);
		}		
	)
	
	$(element).mousedown(
		function() {
			$(this).addClass(downClass);
		}	
	)

	$(element).mouseout(
		function () {
			$(this).removeClass(downClass);
		}		
	)
}

function addPrintfriendly() {
	var windowlink = $(location).attr('href');
	printlink = windowlink.replace( 'show.do', 'printfriendly.do' );
	$('#printlink').attr("href", printlink);
}

function popLegal(lnk) {
	showPopup(lnk,'LegalNotice',500,400,true); // for backwards compatibility
}

function rephraseQuery(searchform) {
	var origQuery = $('#search_query').val();
	var newQuery = "";
	var combi = $('#search_combination').val();
	var section =$('#search_section').val(); 
	$('#search_query').val(rephraseBasicQuery(origQuery, combi, section));
	return true;
}

function rephraseNextPreviousQuery(searchform) {
	var origQuery = $('#search_query').val();
	var newQuery = "";
	var combi = $('#search_combination').val();
	var section =$('#search_section').val(); 
	$('#search_query').val(rephraseBasicQuery(origQuery, "", section));
	return true;
}

function rephraseBasicQuery(query, combination, title) {
	var origQuery = query;
	var newQuery = "";
	var combi = combination;
	var section = title;
	while ( origQuery.indexOf(" ") == 0 ) {
		origQuery = origQuery.replace(" ", "");
	}
	while ( origQuery.lastIndexOf(" ") == origQuery.length - 1 ) {
		origQuery = origQuery.substring(0, origQuery.length - 1);
	}
	if ( combi == "2") {
		while ( origQuery.indexOf(" ") != -1 ) {
			if ( newQuery != null ) {
				newQuery = newQuery + origQuery.substring(0, origQuery.indexOf(" ")) + " AND ";
			} else {
				newQuery = origQuery.substring(0, origQuery.indexOf(" ")) + " AND ";
			}
			origQuery = origQuery.substring(origQuery.indexOf(" ") + 1);
			while ( origQuery.indexOf(" ") == 0 ) {
				origQuery = origQuery.replace(" ", "");
			}
		}
		newQuery = newQuery + origQuery;
	} else if ( combi == "3" ) {
		newQuery = '\"' + origQuery + '\"';
	} else {
		newQuery = origQuery;
	}
	if ( section != "" ) {
		newQuery = "title:" + '\"' +  section + '\"' + " AND " + newQuery;
	}
	return newQuery;
}

function ShowIt(explanation, e)
{
	var x;
	var y;

	if (!e) var e = window.event;
	//Determine position
	if (e.pageX || e.pageY) {
		x = e.pageX;
		y = e.pageY;
	}
	else if (e.clientX || e.clientY) {
		x = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
		y = e.clientY + document.body.scrollTop	+ document.documentElement.scrollTop;
	} 

	//Set position
	x = x + 10;
	y = y + 5;
	
	//Create element and attach to html
	$('<div/>')
	       .attr('id', 'TheTip')
	       .css('top', y)
	       .css('left', x)
	       .html( explanation)
	       .appendTo('body');	
}

function LoseIt() {
	$('#TheTip').remove();
}

/* function to show and hide a hidden div */
function showInternalPopup( id ) {
	if (id!=null && id!=''){
		var containerid = '#Internal_Popup_container_' + id;
		// Remove persist when forms are used in popup
		$(containerid).modal({persist:true});
	} 
};

function hideInternalPopup( id ){ 
	if (id!=null && id!='') {
		var containerid = '#Internal_Popup_container_' + id;
		$.modal.close();	
	}		
}

/*
 * Function to rewrite form values. This causes multiselects and checkboxes to submit ok under
 * the names/values array approach. 
 * Each input must be immediately preceded by its names input.
 * A group of radio buttons must be preceded with a group element with the names input.
*/
function rewriteValues(fname) {
	var myform = $('#'+fname+'');
	var prevHiddenElement = null;
	
	$(myform).find(':input').each( function() {
	    if ($(this).attr('id')!= null){
		    if ($(this).attr('id').indexOf('form_' + $(this).attr('name'))>-1){
		    	$(this).attr('name', 'values');		    	
	    	}
	    }
	    
	    if ($(this).attr('name')!= 'values' & $(this).attr('name')!= 'names'){
    		if ($(this).attr('type')=='checkbox'){
    			prevelt = $(this).prev('input[type=hidden]');
    			if ($(this).is(':checked')){
					prevelt.attr('value', 'Selected');
    			} else {
 					prevelt.attr('value', 'Not selected');
    			}
    		}
    		if ($(this).attr('type')=='radio'){   			
    			if ($(this).is(':checked')){
     				prevHiddenElement.attr('value', $(this).attr('value'));
    			}
    		}
	    }
		if ($(this).is(':hidden')){
			prevHiddenElement = $(this);    			
		}
	       
	});

	var hiddenConfirmation = document.createElement('input');
	hiddenConfirmation.setAttribute('id', 'hiddenConfirmation');
	hiddenConfirmation.setAttribute('name', 'hiddenConfirmation');
	hiddenConfirmation.setAttribute('type', 'hidden');
	hiddenConfirmation.setAttribute('value', 'retlifmaps');
	$(myform).append(hiddenConfirmation);
    	
	return true;
}

function showPopup(lnk, title, width, height, resizable) {
	window.open(lnk, title,'width='+width+',height='+height+',resizable='+(resizable?'yes':'no')+',directories=no,location=no,menubar=no,scrollbars=yes,status=no,toolbar=no');
}
