var gallery_init_list = new Array();

function gallery_init(init)
{
	init.current_col = 0;
	init.current_slide = 0;
	init.max_col = init.total_cols - init.cols;
	init.hover_prev = false;
	init.hover_next = false;
	gallery_init_list[init.id] = init;
	var id = init.id;
	if(init.slides.length > 0) {
		for(var i = 0; i < init.slides.length; ++i) {
			$('#'+id+' .content').before($(init.slides[i].selector));
		}
		window.setTimeout(function() { gallery_next_slide(id); }, init.slides[0].duration);
	}
	$('#'+id).addClass('gallery');
	$('#'+id+' .viewport-content').css({ width: init.wp_width+'px', height: init.wp_height+'px' });
	if(init.caption) {
		$('#'+id+' ul li img').each(function() {
			if($(this).hasClass("inline_flag")) return;
			var el_name = $('.name', this.parentNode.parentNode);
			//this.alt = el_name.text();
			el_name.remove();
			$(this).imageCaption({
				toCaption: this.id,
				captionTipOpacity: '0.75',
				captionBoxOpacity: '0.75',
				//speedIn: 200,
				//speedOut: 200,
				captionBoxColor: '#16325C',
				captionTipColor: '#16325C'
			});
		});
	}
	$('#'+id+' ul li.unavailable').each(function() {
		var ghost = $("<span></span>");
		ghost.css({
			backgroundColor: 'white', 
			position: 'absolute',
			top: 0,
			left: 0,
			width: init.img_width + "px",
			height: init.img_height + "px",
			//zIndex: 100,
			opacity: 0.75
		});
		$(this).css('position', 'relative').append(ghost);
	});
	if(init.slides.length == 0) {
		gallery_setup_controls(id);
	}
}

function gallery_setup_controls(id)
{
	var init = gallery_init_list[id];
	$('#'+id)
		.append(
			$('<button class=\"gallery-button prev\"></button>')
				.hide()
				.bind('click', function() { gallery_scroll(id, -1); })
				.bind('mouseover', function() { init.hover_prev = true; gallery_scroll(id, -init.steps); })
				.bind('mouseleave', function() { init.hover_prev = false; })
		)
		.append(
			$('<button class=\"gallery-button next\"></button>')
				.hide()
				.bind('click', function() { gallery_scroll(id, 1); })
				.bind('mouseover', function() { init.hover_next = true; gallery_scroll(id, init.steps); })
				.bind('mouseleave', function() { init.hover_next = false; })
		)
		.bind('mouseover', function() {
			if(gallery_test_scroll(id, -1)) $('#'+id+' button.prev').fadeIn(500);
			if(gallery_test_scroll(id, 1)) $('#'+id+' button.next').fadeIn(500);
		})
		.bind('mouseleave', function() { $('#'+id+' button').fadeOut(500); });
}


function gallery_next_slide(id)
{
	var init = gallery_init_list[id];
	var last_one = init.slides.length == init.current_slide + 1;
	var current = $(init.slides[init.current_slide].selector);
	var next = last_one ? $('#' + init.id + " .content") : $(init.slides[init.current_slide+1].selector);
	// Put both the current slide and the next one on top of each other, and hide the next one
	current.css({ position: "absolute", top: 0, left: 0 });
	next.hide().css({ position: "absolute", top: 0, left: 0 });

	next.fadeIn(1000, function() { current.remove(); if(last_one) gallery_setup_controls(init.id) });
}


function gallery_test_scroll(id, steps)
{
	var init = gallery_init_list[id];
	if(init.current_col + steps < 0) return (0 - init.current_col);
	if(init.current_col + steps > init.max_col) return (init.max_col - init.current_col);
	return steps;
}


function gallery_scroll(id, steps)
{
	if((steps = gallery_test_scroll(id, steps)) == 0) return;
	var init = gallery_init_list[id];
	var content = $('#'+id+' .viewport-content');
	
	init.current_col += steps;
	var x = init.current_col * init.step * -1;
	content.animate({ left: x+"px" }, 1000, 'easeInSine', function() {
		if(steps > 0) {
			if(init.current_col == 1) $('#'+id+' button.prev').fadeIn(500);
			if(init.current_col >= init.max_col) $('#'+id+' button.next').fadeOut(500);
		}
		if(steps < 0) {
			if(init.current_col == init.max_col - 1) $('#'+id+' button.next').fadeIn(500);
			if(init.current_col <= 0) $('#'+id+' button.prev').fadeOut(500);
		}
		if(init.hover_prev || init.hover_next) gallery_scroll(id, steps);
	});
}

function gallery_reset_cols(id, cols)
{
	var init = gallery_init_list[id];

	gallery_scroll(id, 0 - init.current_col);
	
	init.total_cols = cols;
	init.max_col = Math.max(0, init.total_cols - init.cols);
	
}

