
var action_offset=65;
var auto_step=1;

var current_image={};
var prev_image={};
var next_image={};
var image_id="";

var displayImage=function(dir){
	var obj=current_image;
	if(dir=="prev"){if($(obj).prev().html()){obj=$(obj).prev();}else{obj=$("a:last",$(obj).parent());}
	}else if(dir=="next"){if($(obj).next().html()){obj=$(obj).next();}else{obj=$("a:first",$(obj).parent());}}
	var img=new Image();
	img.src=$(obj).attr("image");
	$("#bar_text").html('<img id="loading" src="'+loading.src+'">');
	$(img).load(function(){
		$("#preview_content").html('<img src="'+$(obj).attr("image")+'">');
		$("#bar_text").html($(obj).attr("title")+". "+$(obj).attr("alt"));
	});
	current_image=obj;
}

var closeImage=function(){$("#preview").hide("slow");}

var img1=new Image();
img1.src="/img/arrow_left_on.png";
var img2=new Image();
img2.src="/img/arrow_right_on.png";
var img3=new Image();
img3.src="/img/close_on.gif";
var loading=new Image();
loading.src="/img/loading.gif";

$("#prev").mouseover(function(){
	var back=$(this).css("background-image");
	$(this).css("background-image",back.replace("off","on"));
}).mouseout(function(){
	var back=$(this).css("background-image");
	$(this).css("background-image",back.replace("on","off"));
}).click(function(){
	displayImage("prev");	
});

$("#next").mouseover(function(){
	var back=$(this).css("background-image");
	$(this).css("background-image",back.replace("off","on"));
}).mouseout(function(){
	var back=$(this).css("background-image");
	$(this).css("background-image",back.replace("on","off"));
}).click(function(){
	displayImage("next");
});

$("#close div:first").mouseover(function(){
	var back=$(this).css("background-image");
	$(this).css("background-image",back.replace("off","on"));
}).mouseout(function(){
	var back=$(this).css("background-image");
	$(this).css("background-image",back.replace("on","off"));
}).click(function(){
	closeImage();
});

	var pos=0;
	var last_x=0;
	var x_over=0;
	
	var w_list=$("#imagelist").width();
	var list_pos=$("#imagelist").offset();

$('#imagelist div.imagescroll').bind("mouseleave",function(e){
	last_x=0;x_over=0;pos=0;
}).bind("mouseenter",function(e){
	var mover=$("div.imagecontainer",$(this));
	w_list=$("#imagelist").width();
	list_pos=$("#imagelist").offset();
}).mousemove(function(e){
	if(!last_x) last_x=e.pageX;	if(!x_over) x_over=e.pageX;
	var diff=e.pageX-last_x;
	var mover=$("div.imagecontainer",$(this));
	var w_mover=$(mover).width()-10;
	if(w_mover>w_list){
		var step=diff*w_mover/(w_list*2);
		var x_mover=parseInt($(mover).css("left"));
		if(x_mover-step>=0){pos=0;}else if(x_mover-step<=-(w_mover-w_list)){pos=-(w_mover-w_list);
		}else{
			if(e.pageX<list_pos.left+action_offset&&diff<0){
				//if(!moving){
					//autoMove(mover,1); 
				//}
			}else if(e.pageX>list_pos.left+w_list-action_offset&&diff>0){
				//if(!moving){
					//autoMove(mover,-1); 
				//}
			}else{
				stopMove();
			}
			pos=x_mover-step;
		}
		moverx=pos;
		//alert(moverx);

		//alert($(mover).css("left"));
		$(mover).css("left",moverx);
		last_x=e.pageX;
	}
});
var movingEnd=function(){};
var move_object={};
var move_step=0;
var moving=false;
var move_shift=0;
var max_speed=1;
var moverx=0;
var stopMove=function(){moving=false;}
var autoMove=function(obj,dir,callback){
	moving=true;
	move_object=obj;
	move_shift=0;
	move_step=Math.round(dir)*auto_step;
	if(Math.abs(move_step)>max_speed) move_step=move_step*max_speed/Math.abs(move_step);
	//alert(move_step);
	movingEnd=callback;
	moveIt();
}
var mt;
var moveIt=function(){
	if(moving){
		//alert(moverx);
		if(!moverx) move_object.x=parseInt($(move_object).css("left"));
		var x_mover=moverx;
		var w_mover=$(move_object).width()-10;
		
		if(move_shift+x_mover+move_step<0&&move_shift+x_mover+move_step>-(w_mover-w_list)){
		}else if(move_shift+x_mover+move_step>0){
			var a=$('a',move_object).last();
			$(a).prependTo($(move_object));
			x_mover-=$(a).width()+parseInt($(a).css("margin-right"));
		}else{
			var a=$('a',move_object).first();
			$(a).appendTo($(move_object));
			x_mover+=$(a).width()+parseInt($(a).css("margin-right"));
		}
		moverx=x_mover+move_step;
		//alert($(move_object).css("left"));
		$(move_object).css("left",moverx);
		clearTimeout(mt);
		mt=setTimeout(moveIt,50);
	}
}


var index_image=$('#flash').css("background-image");
var index_title=$('#flash h2').text();
var t;

$('#imagelist a').each(function(){
}).click(function(e){
}).mouseover(function(){
	automoving=false;
	$('#flash').css("background-image","url("+$(this).attr("image")+")");
	$('#flash h2').text($(this).attr("alt"));
});
$('#imagelist').mouseleave(function(){
	automoving=true;
	clearTimeout(t);
	t=setTimeout(autoShow,10000);
	cycleMoving();
});

var show_image=false;
if(!show_image) show_image=$('div.imagecontainer a',imageline);
var showdir=1;
var autoShow=function(){
	if(automoving){
		$('#flash').css("background-image","url("+$(show_image).attr("image")+")");
		$('#flash h2').text($(show_image).attr("alt"));
		clearTimeout(t);
		t=setTimeout(autoShow,10000);
		if(showdir>0){
			if($(show_image).next("a").attr('image')){
				show_image=$(show_image).next("a");
			}else{
				showdir=-showdir;
				show_image=$(show_image).prev("a");
			}
		}else{
			if($(show_image).prev("a").attr('image')){
				show_image=$(show_image).prev("a");
			}else{
				showdir=-showdir;
				show_image=$(show_image).next("a");
			}
		}
	}
}
var imageline=$("div.imagecontainer","#imagelist");
var autodir=1;
var automoving=true;

var cycleMoving=function(){
	if(automoving){
		autodir=-1;
		autoMove(imageline,autodir,cycleMoving);
	}
}

t=setTimeout(autoShow,10);
cycleMoving();



