var showtmplast = false;
var showtmpfirst = false;
		
function droplastchild()
{
  		document.getElementById("imagecontainer").removeChild(document.getElementById("imagecontainer").lastChild);
		document.getElementById("imagecontainer").firstChild.style.display = "block";
};

function dropfirstchild()
{
  		document.getElementById("imagecontainer").removeChild(document.getElementById("imagecontainer").firstChild);
		document.getElementById("imagecontainer").lastChild.style.display = "block";
};		
function cleartmp()
{
	if(showtmplast) droplastchild();
	if(showtmpfirst) dropfirstchild();
	showtmplast = false;
	showtmpfirst = false;
	
}	

$(document).ready(function(){
   $(".right").click(function(){
        
    	cleartmp();
        now = curpos;
        need = now + 1;

        if(need > maxpos)
		{
			showtmplast = true;
			need = 1;
			tmp = document.createElement("div");
			tmp.id = "tmp";
			tmp.innerHTML = document.getElementById("imagecontainer").firstChild.innerHTML; 
			document.getElementById("imagecontainer").appendChild(tmp);
		}		
		
		curpos = need;
        
     	if(need != now)
     	{
     		if(showtmplast)
     		{
     		    $("#slide_"+now).animate({right:"hide"}, 1000, "easein");     		
     		    $("#tmp").animate({width:"show"}, 1000, "easein");
     		}
     		else
     		{
     			$("#slide_"+now).animate({right:"hide"}, 1000, "easein");     		
     			$("#slide_"+need).animate({width:"show"}, 1000, "easein");
     		}
    		
    	}
    	        
   });
    
   $(".left").click(function(){

   		cleartmp();
	    now = curpos;
       	need = now - 1;

        if(need < 1)
		{
			need =  maxpos;
			showtmpfirst = true;
			tmp = document.createElement("div");
			tmp.id = "tmp";
			tmp.innerHTML = document.getElementById("imagecontainer").lastChild.innerHTML; 
			document.getElementById("imagecontainer").insertBefore(tmp, document.getElementById("imagecontainer").firstChild);
			
			
		}		
       	curpos = need;       	
       	
         	
       	if(need != now)
       	{
       		if(showtmpfirst)
     		{
     			$("#slide_"+now).animate({width:"hide"}, 1000, "easein");     		
     		    $("#tmp").animate({right:"show"}, 1000, "easein");
     		}
     		else
     		{
   				$("#slide_"+now).animate({width:"hide"}, 1000, "easein");
				$("#slide_"+need).animate({right:"show"}, 1000, "easein");   				       		   	   				
   			}
		}
	
   });
});   
