var currentPhoto = 0;
var currentVideo = 0;
var currentYouTube = 0;
var timer;
var sliding = false;
var slidingMain = false;
var view = 'image';
var fo;
var f;
var downX;
var scrollX;
var iconWidth = 103;
var mainPhotoWidth = 575;

Event.observe(window, 'load', function(){
	var drag1 = new Draggable('mainPhotoList', {snap: function(x, y, draggable) { return [0, 0]; }});
	Draggables.addObserver({
		onStart: function(eventName, draggable, event)
		{
			downX = event.clientX;
			scrollX = draggable.element.scrollLeft;
		},
		onDrag: function(eventName, draggable, event)
		{
			draggable.element.scrollLeft = scrollX + downX - event.clientX;
		},
		onEnd: function(eventName, draggable, event)
		{
			if (event.clientX > downX)
			{
				f--;
				if (f < 1) f = 1;
				OpenPhoto(f);
			}
			else
			{
				f++;
				var length = 0;
				for (var i in photos)
				{
					length++;
				}
				if (f > length) f = length;
				OpenPhoto(f);
			}
		}
	});

	FixPageArrows(document.getElementById('photoList'));
	var length = 0;
	for (var i in videos)
	{
		length++;
	}
	OpenPhoto(1);
	f = 1;
	});

function NextPhoto()
{
	if (view == 'image')
		OpenPhoto(currentPhoto + 1);
	if (view == 'video')
		OpenVideo(currentVideo + 1);
	else if (view == 'youtube') 
		OpenYouTube(currentYouTube + 1);
}

function PrevPhoto()
{
	if (view == 'image')
		OpenPhoto(currentPhoto - 1);
	else 
	if (view == 'video')
		OpenVideo(currentVideo - 1);
	else if (view == 'youtube') 
		OpenYouTube(currentYouTube - 1);

}

function OpenPhoto(i)
{
	if (slidingMain)
		return;

	view = 'image';
	i = parseInt(i);

	var prevPhoto = document.getElementById('prevPhoto');
	var nextPhoto = document.getElementById('nextPhoto');

	if (photos[i])
	{
		f = i;
		loading = document.getElementById('loadingImage');
		loading.style.visibility = "visible";

		var video = document.getElementById('playerDiv');
		video.innerHTML = "";
		video.style.display = "none";
		var photo = document.getElementById('mainPhotoList');
		photo.style.display = "block";
//		var title = document.getElementById('photoTitle');

		slidingMain = true;
		ScrollSmoothMain(document.getElementById('mainPhotoList'), (i-1)*mainPhotoWidth - photo.scrollLeft);

//		title.innerHTML = photos[i]['Title'];
		currentPhoto = i;

/*		if (!photos[i - 1])
			prevPhoto.style.visibility = "hidden";
		else
			prevPhoto.style.visibility = "visible";

		if (!photos[i + 1])
			nextPhoto.style.visibility = "hidden";
		else
			nextPhoto.style.visibility = "visible";*/
	}
	else
	{
//		prevPhoto.style.visibility = "hidden";
//		nextPhoto.style.visibility = "hidden";
	}
}

function OpenYouTube(i)
{
	if (slidingMain)
		return;

	view = 'youtube';
	i = parseInt(i);

	var prevPhoto = document.getElementById('prevPhoto');
	var nextPhoto = document.getElementById('nextPhoto');

	i = parseInt(i);

	var prevPhoto = document.getElementById('prevPhoto');
	var nextPhoto = document.getElementById('nextPhoto');

	if (youtube[i])
	{
		var photo = document.getElementById('mainPhotoList');
		photo.style.display = "none";

		var video = document.getElementById('playerDiv');
		video.innerHTML = youtube[i]['Video'];
		video.style.display = "block";

		currentYouTube = i;
	}
}

function OpenVideo(i)
{
	view = 'video';
	i = parseInt(i);

	var prevPhoto = document.getElementById('prevPhoto');
	var nextPhoto = document.getElementById('nextPhoto');

	if (videos[i])
	{
		var photo = document.getElementById('mainPhotoList');
		photo.style.display = "none";

		var video = document.getElementById('playerDiv');
		video.innerHTML = "";
		video.style.display = "block";

		fo = new SWFObject("/hadassah/module/photogallery/template/hadassah/flash/flvplayer.swf?file="+videos[i]['Video']+"&autoStart=false", "movie_player"+i, ""+mainPhotoWidth, "314", 7, "#FFFFFF");
		// Timeout is needed because in IE sometimes player is not loaded without timeout
		timer = setTimeout("ShowPlayer()", 50);

//		var title = document.getElementById('photoTitle');
//		title.innerHTML = videos[i]['Title'];

		currentVideo = i;

		if (!videos[i - 1])
			prevPhoto.style.visibility = "hidden";
		else
			prevPhoto.style.visibility = "visible";

		if (!videos[i + 1])
			nextPhoto.style.visibility = "hidden";
		else
			nextPhoto.style.visibility = "visible";
	}
	else
	{
		prevPhoto.style.visibility = "hidden";
		nextPhoto.style.visibility = "hidden";
	}
}

function ShowPlayer()
{
	fo.write("playerDiv");
}

function ScrollSmooth(div, direction, left)
{
	var step = 6;
	if (left > 0)
	{
		if (direction > 0)
			div.scrollLeft = div.scrollLeft + step;
		else
			div.scrollLeft = div.scrollLeft - step;

		left = left - step;
		timer = setTimeout("ScrollSmooth(document.getElementById('photoList'), "+direction+", "+left+")", 10);
	}
	else
	{
		FixPageArrows(div);
		sliding = false;
	}
}

function ScrollSmoothMain(div, shift)
{
	var step = 100;
	if (shift != 0)
	{
		if (shift > 0)
		{
			if (step > shift)
			{
				div.scrollLeft = div.scrollLeft + shift;
				shift = 0;
			}
			else
			{
				div.scrollLeft = div.scrollLeft + step;
				shift = shift - step;
			}
		}
		else
		{
			if (step > Math.abs(shift))
			{
				div.scrollLeft = div.scrollLeft + shift;
				shift = 0;
			}
			else
			{
				div.scrollLeft = div.scrollLeft - step;
				shift = shift + step;
			}
		}

		timer = setTimeout("ScrollSmoothMain(document.getElementById('mainPhotoList'), "+shift+")", 10);
	}
	else
	{
		slidingMain = false;
		loading = document.getElementById('loadingImage');
		loading.style.visibility = "hidden";
	}
}

function FixPageArrows(div)
{
	var length = 0;
	for (var i in photos)
	{
		length++;
	}
	var fullWidth = length*iconWidth;
/*	var prevPage = document.getElementById('prevPage');
	var nextPage = document.getElementById('nextPage');

	if (div.scrollLeft + div.clientWidth >= fullWidth)
		nextPage.style.visibility = "hidden";
	else
		nextPage.style.visibility = "visible";

	if (div.scrollLeft == 0)
		prevPage.style.visibility = "hidden";
	else
		prevPage.style.visibility = "visible";
*/
}

function NextPage()
{
	if (!sliding)
	{
		sliding = true;
		ScrollSmooth(document.getElementById('photoList'), 1, iconWidth);
	}
}

function PrevPage()
{
	if (!sliding)
	{
		sliding = true;
		ScrollSmooth(document.getElementById('photoList'), -1, iconWidth);
	}
}
