var winbox = 0;
var scrollbox = 0;
var slideshow = 0;
var slideshow_timer = 15000;
var slideshow_step = 5000;

function getId(id) {
	return document.getElementById(id);
}

function initGalleryScroll(){
	winbox = getId("winbox");
	scrollbox = getId("scrollbox");
	
	winbox_w = (winbox.offsetWidth) ? Number(winbox.offsetWidth) : Number(winbox.clientWidth);
	scrollbox_w = (scrollbox.offsetWidth) ? Number(scrollbox.offsetWidth) : Number(scrollbox.clientWidth);
	reserved = 0;
	speed = 5;
	turbo = 0;
	turbospeed = 15;
	full_w = 0;
	init_gallery_scroll = 1;
}

function scrollToRight(sh){
	if(sh != 1 && slideshow == 1){
		clearInterval(slide_show);
		slideshow = 0;
	}
	
	if (winbox == 0){
		initGalleryScroll();
	}
	
	if(reserved == 0){
		scrollbox_l = Number(winbox.scrollLeft); 
		
		if(scrollbox_l < (scrollbox_w - winbox_w)){
			reserved = 1;
			right_scroll = setInterval("rightScroll()",1);
		} else if (scrollbox_l == (scrollbox_w - winbox_w)){
			reserved = 1;
			turbo = 1;
			full_w = scrollbox_w- winbox_w;
			left_scroll = setInterval("leftScroll()",1);
		}
	}
}

function scrollToLeft(){
	if(slide_show){
		clearInterval(slide_show);
		slideshow = 0;
	}
	if (winbox == 0){
		initGalleryScroll();
	}
	
	if(reserved == 0){
		scrollbox_l = Number(winbox.scrollLeft);
		//alert(scrollbox_l);
		if(scrollbox_l > 0){
			reserved = 1;
			left_scroll = setInterval("leftScroll()",1);
		} else if (scrollbox_l == 0){
			reserved = 1;
			turbo = 1;
			full_w = scrollbox_w - winbox_w;
			right_scroll = setInterval("rightScroll()",1);
		}
	}
}

function rightScroll() {
	var width = (full_w >0) ? full_w : winbox_w;
	var difi = (scrollbox_l + width) - winbox.scrollLeft;
	var s = (turbo == 1) ? turbospeed : speed;
	
	if ( winbox.scrollLeft < (scrollbox_l + width)){
		if(difi <= speed){
			winbox.scrollLeft = winbox.scrollLeft + 1;
		} else {
			winbox.scrollLeft = winbox.scrollLeft + s;
		}
	} else {
		clearInterval(right_scroll);
		full_w = 0;
		turbo = 0;
		reserved = 0;
		if(slideshow == 0){ 
			setTimeout("slideShowGallery()",slideshow_timer);
		}
	}
}

function leftScroll() {
	var width = (full_w >0) ? full_w : winbox_w;
	var difi = winbox.scrollLeft - (scrollbox_l - width);
	
	var s = (turbo == 1) ? turbospeed : speed;
	
	if ( winbox.scrollLeft > (scrollbox_l - width)){
		if(difi <= speed){
			winbox.scrollLeft = winbox.scrollLeft - 1;
		} else {
			winbox.scrollLeft = winbox.scrollLeft - s;
		}
	} else {
		clearInterval(left_scroll);
		full_w = 0;
		turbo = 0;
		reserved = 0;
		if(slideshow == 0){ 
			setTimeout("slideShowGallery()",slideshow_timer);
		}
	}
}

function slideShowGallery(){
	slideshow = 1;
	slide_show = setInterval("scrollToRight(1)",slideshow_step);
}
