/***************************************************************
*  Copyright notice
*
*  (c) 2008 Alexander Weiß <alexander.weiss@ub.uni-muenchen.de>
*  All rights reserved
*
*  This script is part of the TYPO3 project. The TYPO3 project is
*  free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 2 of the License, or
*  (at your option) any later version.
*
*  The GNU General Public License can be found at
*  http://www.gnu.org/copyleft/gpl.html.
*
*  This script is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/



/*
	Additional xajax Functionality for dynform plugin
*/



var tablePrefix='tx_dynform_pi1_search_field_table_';
var containerPrefix='tx_dynform_pi1_search_field_';

function tx_dynform_pi1_remove(id){
		
	var e=document.getElementById(tablePrefix + id);
	
	if(e){
		var pos=tx_dynform_pi1_getPos(e);
	
	}
	
	
	var count_element=document.getElementById('tx_dynform_pi1[element_count]');
	var next_container_element=document.getElementById('tx_dynform_pi1[next_container]');
	
	if(pos==0){ //is first search field? then remove the next connector
		//remove connector to next field if existent
		if(count_element && count_element.value>0){

			var nextContainerId = 'tx_dynform_pi1_search_field_' + (pos+1);
			var c = document.getElementById(nextContainerId);

			if(c && c.hasChildNodes()){
				next_id=c.firstChild.id;
				next_id=next_id.substring(next_id.lastIndexOf("_")+1);		
				xajax.remove('tx_dynform_pi1_search_field_connector_'+next_id);
			}
		}
	}
	
	xajax.remove(tablePrefix + id);
	tx_dynform_pi1_moveAll(pos+1);
	if(count_element)count_element.value=count_element.value-1;
	if(next_container_element)next_container_element.value=next_container_element.value-1;
}



function tx_dynform_pi1_getPos(e){
	var container_id=e.parentNode.id;
	var pos = container_id.substr(container_id.lastIndexOf('_')+1);
	return(parseInt(pos));
}

function tx_dynform_pi1_moveAll(pos){
	var c = document.getElementById(containerPrefix+pos);
	while(c){
		var p = document.getElementById(containerPrefix+(pos-1));
		var ch = c.firstChild;	
		if(p && ch){
			c.removeChild(c.firstChild);
			p.appendChild(ch);	
		}
		pos=pos+1;
		c=document.getElementById(containerPrefix+pos);
	}
}


function tx_dynform_pi1_collapseElement(id, buttonLabel, buttonClass, buttonImage, buttonImageClass){
	elem = document.getElementById(id);
	if(elem){
		elem.style.display='none';
		parent=elem.parentNode;
		button=tx_dynform_pi1_makeButton(buttonLabel,buttonClass,buttonImage,buttonImageClass,id);
		if(parent){
			parent.insertBefore(button,elem);
		}	
	}
}

function tx_dynform_pi1_showElement(id){
	Element.hide('mdDescription');
	elem=document.getElementById(id);
	if(elem){
		elem.style.display="block";
		button = document.getElementById(id + '_switchButton');
		if(button)button.style.fontWeight="bold";
		for(i=1;i<arguments.length;i++){
			otherElement=document.getElementById(arguments[i]);
			if(otherElement){
				otherElement.style.display="none";
				button = document.getElementById(arguments[i] + '_switchButton');
				if(button)button.style.fontWeight="normal";
			}
			
		}
	}
}

function tx_dynform_pi1_showSearchForm(){
	tx_dynform_pi1_showElement('tx_dynform_pi1_form','tx_dynform_pi1_search_results','tx_dynform_pi1_search_results_fis','tx_dynform_pi1_detail_view');
	return(false);
}

function tx_dynform_pi1_showResults(){
	tx_dynform_pi1_showElement('tx_dynform_pi1_search_results','tx_dynform_pi1_form','tx_dynform_pi1_search_results_fis','tx_dynform_pi1_detail_view');
	return(false);
}

function tx_dynform_pi1_showDetails(){
	tx_dynform_pi1_showElement('tx_dynform_pi1_detail_view','tx_dynform_pi1_form','tx_dynform_pi1_search_results_fis','tx_dynform_pi1_search_results');
	return(false);
}


function tx_dynform_pi1_clearField(elem,defaultVal){
	if(elem.value==defaultVal)elem.value="";
}

function tx_dynform_pi1_setDefaultOnEmpty(elem,defaultVal){
	if(elem.value=='')elem.value=defaultVal;
}	
function showing(){
document.getElementById('description').style.display='block';
}

function makeSmartList(name){
					s1 = new SmartList({
						baseName:                name,
						itemClass:               "item",
						showFlagCount:           false,
						itemFlagClass:           "flags",
						itemFlagSeparator:       ", ",
						itemChangeEffect:        "Blind", // "FadeAppear", "Blind", "ShrinkGrow", "" 
						itemChangeDuration:      0.5, // seconds for Prototype
						pageChangeEffect:        "FadeAppear", // "FadeAppear", "Blind", "ShrinkGrow", ""
						pageChangeDuration:      0.5, // seconds for Prototype
						numItemsPerPage:         10,
						paginationLeft:          "\u00ab",
						paginationRight:         "\u00bb",
						maxPaginationLinks:      20,
						defaultDropdownOptText:  "All items",
						defaultDropdown2OptText: "All items",
						dd2Flags:                [] // an array. See note below
					});
}