var delay;
var list = new Array;
var pics = new Array;
var buttons = new Array;

/*-------------------------------------------------------------------------------------------*/

window.onload = function() {
	if ($('galleryScroll')) {
		galleryInit('galleryScroll', 'galleryControl');
	}
}

function galleryInit(gallery, controls) {

pics = $(gallery).getElementsBySelector('img');
buttons = $(controls).getElementsBySelector('img');

buttons[0].style.visibility = 'hidden';
url = buttons[0].alt;
buttons[0].alt = 'left';

	new Ajax.Request(url+"?oh-noes-cache-haet-me=" + Math.random(), {
		method: 'get',
		onSuccess: function(req) {
			list = req.responseText.evalJSON();
			if (list.length<=pics.length) {
				buttons[1].style.visibility = 'hidden';
			}	
			for (i=0; i<pics.length; i++) {
				pics[i].src = list[i].image;
				pics[i].alt = list[i].link;
			}
		
			pics.each(function(p) {
				Event.observe(p, 'click', function(e) {
					new_window(Event.element(e).alt);
				});
			});	
		} 
	});
}

function scrollGallery(dir) {

if (!stop()) {
		for (i=0; i<list.length; i++) {
			if ('http://' + location.host + list[i].image==pics[0].src) {
				for (j=0; j<pics.length; j++) {
					pics[j].src = list[i+j+dir].image;
					pics[j].alt = list[i+j+dir].link;
				}
				stop();
				break;
			}
		}
	}

function stop() {
	if (pics[(pics.length-1)*(dir+1)/2].src=='http://' + location.host+list[(list.length-1)*(dir+1)/2]['image']) {
		buttons[(buttons.length-1)*(dir+1)/2].style.visibility = 'hidden';
		return true;
	} else {
		buttons[(buttons.length-1)*Math.abs((dir-1)/2)].style.visibility = 'visible';
		return false;
	}
}

}

function new_window(url)  {
	var width = 600;
	var height = 400;
    var window_left   = 0;
    var window_top    = 0;
    var window_dimensions = "height=" + height +
                            ", width=" + width; +
                            ",left=" + window_left +
                            ",top=" + window_top;

    var wnd = window.open(url, 'secondWindow','resizable=yes,status=no,scrollbars=yes,fullscreen=1');
    wnd.focus();
}

/*------------------------------------------------*/

function scrollText(dir) {
/*
sw = document.getElementById("scrollWrapper");
sc = document.getElementById("scrollContent");
step = 1;

if (sc.style.top == "") {
	sc.style.top = "0px";
}

window.clearInterval(delay);

function stopCheck() {
if (Math.abs(((dir+1)/2)*parseInt(sc.style.top)) + Math.abs(((1-dir)/2)*(sc.clientHeight - sw.clientHeight - Math.abs(parseInt(sc.style.top))))<step) {
return true;
}
}

function move() {
		if (!stopCheck()) {
				sc.style.top = (parseInt(sc.style.top) + dir*step).toString() + "px";
			}
		} 

if (dir!==0) {
	delay = window.setInterval(move, 100);
}
*/
}


/*---------------------------------------------------------------------------------------------*/


