// collapse div with form
$(document).ready(function(){
													 
	// só páginas interiores
	if ($('#settings_display').length){
		
		//Hide (Collapse) the toggle containers on load
		$("#settings_form").hide(); 
	
		//Switch the "Open" and "Close" state per click
		$("div.settings_toggle_trigger").toggle(function(){
			$(this).addClass("active");
			}, function () {
			$(this).removeClass("active");
		});
	
		//Slide up and down on click
		$("div.settings_toggle_trigger").click(function(){
			$(this).next("#settings_form").slideToggle("slow");
		});

	}

	
	// legendas nas imagens
	/*$('div.text p img').each(function(){
			var aText=$(this).attr("alt");
			if(aText.length>=1){ // only do this for <img> which have alt="" tags 
					$(this).wrap("<div class=\"imgwrapper\"><\/div>").after("<span class=\"legenda\">"+aText+"</span>");
			}
	});*/

	
	//autoblur nos <A>
	$('a').click(function() {
		this.blur();
	});
	

	//popup help
	var $popSearchHelp = $('<div></div>')
		.html('<ul><li>The query is not case or accentuation sensitive, hence, the term «espana» will produce the same results as the term «ESpañA»</li><li>Boolean operators are case sensitive, hence the term «AND» will be handled as a Boolean operator and the term «and» will handled as a regular string</li><li>You can group terms with paranthesis. Like «(diabetes AND elderly)»</li><li>The search within specific fields is allowed. Example: «[TI]» for Title or «[AU]» for Author</li></ul><p>Need more help with building your search querys?<br />Try these pages: <a href="/cgi-bin/content.pl?nid=EkZuuklZpErEEgeSSY"><strong>Simple searches</strong></a> and <a href="/cgi-bin/content.pl?nid=EkZuuklZFZFyekiaHF"><strong>Search with fields</strong></a>')
		.dialog({
			autoOpen: false,
			modal: true,
			width: 600,
			title: 'Quick search help'
	});
	$('a.popSearchHelp').click(function() {
		$popSearchHelp.dialog('open');
	});


	// reset form
	$('a.resetQuery').click(function() {
		$('input.QstrResetMark').val("");
	});
	
	// submit form
	$('a.submitQuery').click(function() {
	  $('#formpesquisa').submit();
	});
	
	
	// user login
	var $popLogin = $('<div></div>')
		.html('<form id="loginform" name="loginform" method="post" action="cgi-bin/login.pl"><table width="200" border="0" cellspacing="0" cellpadding="10"><tr><td align="right">Username:</td><td><input type="text" name="username" id="username" /></td></tr><tr><td align="right">Password:</td><td><input type="password" name="password" id="password" /><!-- br><a href="#">Forgot your Password?</a --></td></tr><tr><td align="right">&nbsp;</td><td><input type="submit" name="button" id="button" value="Submit" /></td></tr></table></form>')
		.dialog({
			autoOpen: false,
			modal: true,
			width: 270,
			title: 'Log-in'
	});
	$('a.userLogin').click(function() {
		$popLogin.dialog('open');
	});
	var $popWhy = $('<div></div>')
		.html('Registered users can have querys running automatically every month and getting the results by email.')
		.dialog({
			autoOpen: false,
			modal: true,
			width: 400,
			title: 'Why register'
	});
	$('a.userWhy').click(function() {
		$popWhy.dialog('open');
	});


	if ($('.toggle_container').length){
		//Hide (Collapse) the toggle containers on load
		$(".toggle_container").hide(); 
	
		//Switch the "Open" and "Close" state per click then slide up/down (depending on open/close state)
		$("h2.trigger").click(function(){
			$(this).toggleClass("active").next().slideToggle("slow");
		});
	}
	
	if ($('form.submitform').length){
		$("form.submitform").validationEngine()
	}

	// submit form
	$('#testquery1').click(function() {
		if ($('#query1').val()) {
		  window.open( 'cgi-bin/search.pl?qstr='+$('#query1').val() );
		  return false;
		}
		else {
		  alert('your query is empty');
		}
	});
	$('#testquery2').click(function() {
		if ($('#query2').val()) {
		  window.open( 'cgi-bin/search.pl?qstr='+$('#query2').val() );
		  return false;
		}
		else {
		  alert('your query is empty');
		}
	});
	$('#testquery3').click(function() {
		if ($('#query3').val()) {
		  window.open( 'cgi-bin/search.pl?qstr='+$('#query3').val() );
		  return false;
		}
		else {
		  alert('your query is empty');
		}
	});
});


function findFilename( origem, destino ) {
  var elem, str;
  if( document.getElementById ) // this is the way the standards work
    elem = document.getElementById( origem );
  else if( document.all ) // this is the way old msie versions work
      elem = document.all[origem];
  else if( document.layers ) // this is the way nn4 works
    elem = document.layers[origem];

  if (elem.value) {
	str = elem.value.substr(elem.value.lastIndexOf('\\')+1);
	document.getElementById( destino ).value = str;
  }
}
