//the box with the advanced search must have id advSearchDiv 
//the click event is on advSearchToggle
Event.observe( window, 'load', function(){
  Event.observe( $('advSearchToggle'), 'click', function( e ){
  		verifyToggleText();
  		Effect.toggle($('advSearchDiv'), 'slide' , { duration: 1.0 });
  		Event.stop( e );
  }); 
});

Event.observe(window, 'load', function(){
	Event.observe($('searchForm'), 'submit', function( e ){
		if ($('advSearchDiv').style.display=='none') {
			return true;
		}
		addHiddenInputToForm($('searchForm'),"country",$('country').value);
		addHiddenInputToForm($('searchForm'),"provider",$('providerfield').value);
		addHiddenInputToForm($('searchForm'),"tag",$('tag').value);
		addHiddenInputToForm($('searchForm'),"sortby",$('sortby').value);
		addHiddenInputToForm($('searchForm'),"resultsPerPage",$('resultsPerPage').value);
		addHiddenInputToForm($('searchForm'),"rememberResultsPerPage",$('rememberResultsPerPage').value);
	});
});

function verifyToggleText(){
	if ($('advSearchDiv').style.display=='none'){
  		$('advancedSearchText').style.display="none";
  		$('basicSearchText').style.display="inline";
	}else{
  		$('advancedSearchText').style.display="inline";
  		$('basicSearchText').style.display="none";
	}
}

function addHiddenInputToForm(form,inputname,value){
		var inputfield = document.createElement('input');
		inputfield.name = inputname;
		inputfield.value = value;
		inputfield.type = "hidden";
		form.appendChild(inputfield);
}

//Tag info slider
Event.observe( window, 'load', function(){
  Event.observe( $('tagCloudToggle'), 'click', function( e ){
  		verifyTagToggleText();
  		Effect.toggle($('tagInfoDiv'), 'slide', null );
  		Event.stop( e );
  }); 
});

function verifyTagToggleText(){
	if ($('tagInfoDiv').style.display=='none'){
  		$('tagsComplet').style.display="none";
  		$('tagsShort').style.display="inline";
	}else{
  		$('tagsComplet').style.display="inline";
  		$('tagsShort').style.display="none";
	}
}

//More info slider
Event.observe( window, 'load', function(){
  Event.observe( $('moreAbout'), 'click', function( e ){
   		Effect.Appear('moreInfoDiv1', {duration:2.0});
   		Effect.Appear('moreInfoDiv2', {duration:2.0});
  		Effect.Shrink('moreAboutComplet', {duration:1.5});
  		Event.stop( e );
  }); 
});


function showAdvancedStuff( e ) {
  var opt = {
   method: 'get',
   asynchronous: true,
   //I think we don't need any?
   //parameters: '${backendParameters}&text='+$('newtag').value+'&author=${sessionScope.username}',
   
   onSuccess: function( t ) {
    $('advSearchWidgets').innerHTML = t.responseText;
    
    //use the old effect
    Effect.toggle( $( 'add_tag_layer' ), "appear" );
   },
   

   //improve
   onFailure: function( t ) {
    alert('Couldn\'t load form: ' + t.statusText);
    Effect.toggle( $( 'add_tag_layer' ), "appear" );
   }
  }
  //take from specific html bit to have URL rewriting in place!
  url = $('advSearchDivURL').innerHTML;
  new Ajax.Request(url, opt );
}