var d=2; //duration factor
var tl=w*d; //time delay left
var tr=0; //time delay right

hw=w+1000;
$("#galleryholder").css("width",hw+"px");

$("#gallery_prev").mouseover(function () {
		
		moveright();
		
}).mouseout(function () {

		stopmove(1);

});

$("#gallery_next").mouseover(function () {
		
		moveleft();
		
}).mouseout(function () {

		stopmove(2);

});

$('body').bind('keydown',function(e){ 

	if(e.which==37){
	
		moveright();
	
	}else if(e.which==39){
	
		moveleft();
	
	}

}).bind('keyup',function(e){ 
	
	if(e.which==37){
	
		stopmove(1);
	
	}else if(e.which==39){
	
		stopmove(2);
	
	}

});  

function moveleft(){

		$("#galleryholder").animate(
		{marginLeft:"-"+w+"px"},
		{
		duration:tl,
		easing:"linear",
		step:keyframe
		});

}

function moveright(){
		
		$("#galleryholder").animate(
		{marginLeft:"0px"},
		{
		duration:tr,
		easing:"linear",
		step:keyframe
		});

}

function stopmove(x){
		
		$("#galleryholder").stop(true,false);
		
		var ml=$("#galleryholder").css("marginLeft");
		ml=ml.replace("px","");
		ml=parseInt(ml);
		
		if(x==2) ml=ml-50;
		else ml=ml+50;
		
		if(ml>0) ml=0;
		
		mx=w*-1;
		if(ml<mx) ml=mx;
		
		moveto(ml);
		
}

function moveto(mt){
	
		$("#galleryholder").animate(
		{marginLeft:mt+"px"},
		{
		duration:150,
		easing:"swing",
		step:keyframe
		});
	
}

ml=0;
function keyframe(){
	
	var ml=$("#galleryholder").css("marginLeft");
	
	ml=ml.replace("px","");
	
	ml=parseInt(ml);		
	ml=ml*-1;
	
	tl=(w-ml)*d;
	tr=ml*d;
	
	
}
