$(document).ready(function(){
	$(".gallery_image").map(function(){
	    //alert($(this).width());
		//alert($(this).parent().get(0).tagName);
		$(this).parent().wrap("<div class=gallery_wrap style=\"width: "+$(this).width()+"px;\"></div>"); //bug in IE
	    //$(this).wrap("<div class=gallery_wrap style=\"width: "+$(this).width()+"px;\"></div>"); //bug in IE
		//$(this).wrap('<div class=gallery_wrap style="width: 160px;"></div>');
	});
	
	
	
	$(".gallery_scroll_area").each(function (i, o){
		//if (i==0) return;
		table = o.parentNode.parentNode.parentNode;
		div = $(o).children("div");
		width = $(this).innerWidth()-2; 
		//width = div.innerWidth()-2; 
		div.width(width).show();
		//alert("td.innerWidth="+$(this).innerWidth()+"\ndiv.innerWidth="+div.innerWidth()+"\ndiv.outerWidth="+div.outerWidth()+"\ndiv.width="+div.width()+" scrollLeft="+div.scrollLeft());
		lastimg = div.find("img:last");
		//alert(lastimg.css("margin-left")); //alert(lastimg[0].style.marginLeft); 
		//alert("\nlastimg.css.left="+lastimg.css("left")+"\nlastimg[0].offsetLeft="+lastimg[0].offsetLeft+"\nlastimg.position.left="+lastimg.position().left+"\nlastimg.offset.left="+lastimg.offset().left+"\nlastimg.width="+lastimg.width()+"\nlastimg.innerWidth="+lastimg.innerWidth()+"\nlastimg.outerWidth="+lastimg.outerWidth()+"\nlastimg[0].width="+lastimg[0].width+"\nlastimg[0].offsetWidth="+lastimg[0].offsetWidth+"\nlastimg[0].clientWidth="+lastimg[0].clientWidth+"\n"+lastimg[0].src);
		marginleft = lastimg.css("margin-left"); if (marginleft) marginleft = new Number(marginleft.slice(0, marginleft.indexOf("px")));
		marginright = lastimg.css("margin-right"); if (marginright) marginright = new Number(marginright.slice(0, marginright.indexOf("px")));
		totalwidth = lastimg[0].offsetLeft + lastimg[0].offsetWidth + marginright;
		
		table.previmg = $(this).siblings(".gallery_scroll_prev").find("img")[0];
		table.nextimg = $(this).siblings(".gallery_scroll_next").find("img")[0];
		
		table.marginleft = marginleft;
		table.marginright = marginright;
		table.totalwidth = totalwidth;
		//alert("width="+width+" totalwidth="+totalwidth);
		if (width<totalwidth){
			div.width(1);
			$(o).siblings("td").show();
			width = $(this).innerWidth()-2;
			div.width(width);
			
			insert = $(o.parentNode.parentNode.parentNode).find(".gallery_scroll_dots_insert"); //$(o).parents("table").find(".gallery_scroll_dots_insert");
			insert.append('<div class="gallery_scroll_dots"></div>');
			table.dots = insert.find(".gallery_scroll_dots");
			pages = Math.ceil(totalwidth/width);
			for (i=0; i<pages; i++){
				table.dots.append('<a href="#" class="gallery_scroll_dot" scrollTo="'+(i*width)+'"><img src="/modules/gallery/dot'+(i>0?"_off":"")+'.png"></a>');
			}
		}
		table.width = width;
	});

	$(".gallery_scroll_dot").click(function (){
		table = this.parentNode.parentNode.parentNode.parentNode.parentNode;
		if (table.scrolling) return false; 
		//alert($(table).find(".gallery_scroll_area").children("div").length);
		div = $(table).find(".gallery_scroll_area").children("div");
		//alert(div[0].tagName);
		var scrollTo = $(this).attr("scrollTo");
		gallery_scrollTo(div, scrollTo);
		return false;
	});
	
	$(".gallery_scroll_next, .gallery_scroll_prev").click(function (){
		var table = this.parentNode.parentNode.parentNode;
		if (table.scrolling){
			//alert("Stop touching me!");
			return false; 
		}
		var div = $(this).siblings(".gallery_scroll_area").children("div"); //alert(div[0].tagName);
		var next = $(this).hasClass("gallery_scroll_next"); //which button is pressed
		var width = div.innerWidth(); 
		var marginleft = table.marginleft;
		var marginright = table.marginright;
		var totalwidth = table.totalwidth;
		if ((next && div.scrollLeft()+width+1<totalwidth) || (!next && div.scrollLeft()>0)){ //check, maybe already at max position
			//alert("Scrolling "+(next?"next":"prev")+"\ndiv.scrollLeft()="+div.scrollLeft()+"\nwidth="+width+"\ntotalwidth="+totalwidth);
			var scrollTo = next ? div.scrollLeft()+width : div.scrollLeft()-width; //alert(scrollTo);
			//alert(div);
			imgs = div.find("img");
			if (next)
				for (i=0; i<imgs.length; i++){ //round scrolling position
					if (imgs[i].offsetLeft+imgs[i].offsetWidth+marginright > scrollTo){
						scrollTo = imgs[i].offsetLeft-marginleft;
						break;
					}
				}
			else
				for (i=imgs.length-1; i>=0; i--){ //round scrolling position
					if (imgs[i].offsetLeft-imgs[i].offsetWidth-marginleft < scrollTo){
						//alert("offsetLeft="+imgs[i].offsetLeft+"\noffsetWidth="+imgs[i].offsetWidth+"\n"+lastimg[0].src+"\n"+marginright);
						//alert(imgs[i].offsetLeft+imgs[i].offsetWidth+marginright);
						scrollTo = imgs[i].offsetLeft-marginleft;
						break;
					}
				}
			gallery_scrollTo(div, scrollTo);
		}
		
		return false;
	});
	
});

function gallery_scrollTo(div, scrollTo){
	if (scrollTo==div.scrollLeft()) return;
	var table = div[0].parentNode.parentNode.parentNode.parentNode;
	//alert(table.nextimg); return;
	table.scrolling = true; //semaphore

	if (table.dots){
		$(table.dots).find("img").each(function (i, img){
			img.src="/modules/gallery/dot_off.png";
		});
	}
	
	var next = scrollTo >= div.scrollLeft();
	div.animate(
		{"scrollLeft": scrollTo}, "slow", "swing",
		function() {
			//alert("width="+table.width+"\ntotalwidth="+table.totalwidth+"\ndiv.scrollLeft()="+div.scrollLeft()+"\nnext="+next);
			if (next && div.scrollLeft()+table.width>=table.totalwidth){//disable next button
				table.nextimg.src = "/modules/gallery/next_off.png";
			}
			if (!next && div.scrollLeft()<=0){//disable next button
				table.previmg.src = "/modules/gallery/prev_off.png";
			}
			if (next) table.previmg.src = "/modules/gallery/prev.png";
			if (!next) table.nextimg.src = "/modules/gallery/next.png";

			adots = $(table.dots).find("a");
			for (i=0; i<adots.length; i++){
				//alert($(this).scrollLeft());
				if ($(adots[i]).attr("scrollTo")>=$(this).scrollLeft()){
					$(adots[i]).children("img")[0].src="/modules/gallery/dot.png";
					break;
				}
			}
			
			table.scrolling = false;
		}
	);
}
