var num_work;
var work_ids;
var cur_index;
var array_index=0;
var cur_id;

function init_gallery() {
    // VARIABLES 
    num_work=document.hidden_vals.num_works.value;
    work_ids=document.hidden_vals.work_ids.value;
    work_ids = work_ids.split('-');
    cur_index=document.hidden_vals.cur_index.value;
    // PREV EN LINK TRIGGEREN
    var prev_link = document.getElementById('prev_work');
    var next_link = document.getElementById('next_work');
    prev_link.onclick=function() {
        show_prev_gal_item();
        return false;
    }
    next_link.onclick=function() {
        show_next_gal_item();
        return false;
    }
}
function show_prev_gal_item() {
    if(cur_index==1) {
        cur_index = num_work;
    } else {
        cur_index--;
    }
    array_index = cur_index;
    cur_id = work_ids[array_index];
    var load_file="_inc/load_ajax.php?part=artist_werk&id="+cur_id;
    sendRequest(load_file, handleRequest);
}
function show_next_gal_item() {
    if(cur_index==num_work) {
        cur_index = 1;
    } else {
        cur_index++;
    }
    array_index = cur_index;
    cur_id = work_ids[array_index];
    var load_file="_inc/load_ajax.php?part=artist_werk&id="+cur_id;
    sendRequest(load_file, handleRequest);
}

/* -----------------------------------------------------
    CALLBACK FUNCTIONS
 -----------------------------------------------------*/
function handleRequest(req) {
    var html_to_show = req.responseText;
	var container = document.getElementById('artist_werk_focus');
	container.innerHTML = html_to_show;
}

/* -----------------------------------------------------
    XMLHTTP Functions
    As found on Quirksmode. You hero.
 -----------------------------------------------------*/

function sendRequest(url,callback,postData) {
	var req = createXMLHTTPObject();
	if (!req) return;
	var method = (postData) ? "POST" : "GET";
	req.open(method,url,true);
	req.setRequestHeader('User-Agent','XMLHTTP/1.0');
	if (postData)
		req.setRequestHeader('Content-type','application/x-www-form-urlencoded');
	req.onreadystatechange = function () {
		if (req.readyState != 4) return;
		if (req.status != 200 && req.status != 304) {
		//	alert('HTTP error ' + req.status);
			return;
		}
		callback(req);
	}
	if (req.readyState == 4) return;
	req.send(postData);
}

function XMLHttpFactories() {
	return [
		function () {return new XMLHttpRequest()},
		function () {return new ActiveXObject("Msxml2.XMLHTTP")},
		function () {return new ActiveXObject("Msxml3.XMLHTTP")},
		function () {return new ActiveXObject("Microsoft.XMLHTTP")}
	];
}

function createXMLHTTPObject() {
	var xmlhttp = false;
	var factories = XMLHttpFactories();
	for (var i=0;i<factories.length;i++) {
		try {
			xmlhttp = factories[i]();
		}
		catch (e) {
			continue;
		}
		break;
	}
	return xmlhttp;
}