function droplastchild(i)
{
		i++;
  		document.getElementById("imagebox"+i).removeChild(document.getElementById("imagebox"+i).lastChild);
		document.getElementById("imagebox"+i).firstChild.style.display = "block";
};

function dropfirstchild(i)
{
		i++;
  		document.getElementById("imagebox"+i).removeChild(document.getElementById("imagebox"+i).firstChild);
		document.getElementById("imagebox"+i).lastChild.style.display = "block";
};

function cleartmp(i)
{
	if(showtmplast[i]) droplastchild(i);
	if(showtmpfirst[i]) dropfirstchild(i);
	showtmplast[i] = false;
	showtmpfirst[i] = false;
}	

$(document).ready(function(){
	
    $(".down").click(function(){
        
    	cleartmp(this.id-1);
        now = curpos[this.id-1];
        need = now + 1;

        if(need > maxpos[this.id-1])
		{
			need = 1;
			
			showtmplast[this.id-1] = true;
			tmp = document.createElement("div");
			tmp.id = "tmp"+this.id;
			tmp.innerHTML = document.getElementById("imagebox"+this.id).firstChild.innerHTML; 
			document.getElementById("imagebox"+this.id).appendChild(tmp);
			
		}		
		
        curpos[this.id-1] = need;
     	if(need != now)   	
     	{
     		if(showtmplast[this.id-1])
     		{
    			$("#tmp"+this.id).animate({height:"show"}, 600, "easein");        
    			$("#slide"+this.id+"_"+now).animate({height:"hide"}, 600, "easein");
     		    
     		}
     		else
     		{
    			$("#slide"+this.id+"_"+need).animate({height:"show"}, 600, "easein");        
    			$("#slide"+this.id+"_"+now).animate({height:"hide"}, 600, "easein");
    		}	
    	
    		el = "line"+this.id;
    		document.getElementById(el).innerHTML = labels[this.id][need];
    		el = "info"+this.id;
    		document.getElementById(el).href = links[this.id][need];
    		
    		el = "add"+this.id;
    		document.getElementById(el).name = toursid[this.id][need];
    		
    	}
    	        
   });
    
   $(".up").click(function(){
   
		cleartmp(this.id-1);
		
	    now = curpos[this.id-1];
       	need = now - 1;

        if(need < 1)
		{
			need =  maxpos[this.id-1];

			showtmpfirst[this.id-1] = true;
			tmp = document.createElement("div");
			tmp.id = "tmp"+this.id;
			tmp.innerHTML = document.getElementById("imagebox"+this.id).lastChild.innerHTML; 
			document.getElementById("imagebox"+this.id).insertBefore(tmp, document.getElementById("imagebox"+this.id).firstChild);
			
		}		
       	curpos[this.id-1] = need;       	
		
       	if(need != now)
       	{				   
       		if(showtmpfirst[this.id-1])
     		{
    			$("#tmp"+this.id).animate({height:"show"}, 600, "easein");        
    			$("#slide"+this.id+"_"+now).animate({height:"hide"}, 600, "easein");
     		    
     		}
       		else
       		{
   				$("#slide"+this.id+"_"+need).animate({height:"show"}, 600, "easein");	   
   				$("#slide"+this.id+"_"+now).animate({height:"hide"}, 600, "easein");
			}
			
			el = "line"+this.id;
			document.getElementById(el).innerHTML = labels[this.id][need];
		
    		el = "info"+this.id;
    		document.getElementById(el).href = links[this.id][need];
    		
    		el = "add"+this.id;
    		document.getElementById(el).name = toursid[this.id][need];
    		
   		}
   });
});
