// Copyright 2006 Internetrix (http://www.internetrix.net)

var connect_image_list;
var connect_image_index = 0;
var response_array = new Array();

function get_connect_gallery(page_id)
{
		new Ajax.Request(
		'/cgi-bin/connect_gallery_xml.pl',
		{
			asynchronous: true, evalScripts: true, method: 'post',
			onSuccess: function(response){ 
							connect_image_list = $A(response.responseXML.getElementsByTagName('item')); 
							get_connect_image(connect_image_list[connect_image_index].firstChild.nodeValue);
						},
			postBody: 'action=list&page_id=' + page_id
		}
	);
	
}


function get_connect_image(id)
{
		new Ajax.Request(
		'/cgi-bin/connect_gallery_xml.pl',
		{
			asynchronous: true, evalScripts: true, method: 'post',
			onSuccess: function(response) {
							response_array[connect_image_index] = response;	
							populate_connect_image(response);
						},
			postBody: 'action=item&id=' + id
		}
	);
}


function populate_connect_image(response){
	
	var image_title = response.responseXML.getElementsByTagName('image_title')[0].firstChild ? response.responseXML.getElementsByTagName('image_title')[0].firstChild.nodeValue : null;
	var connect_gallery_title = $('connect_gallery_title');
	connect_gallery_title.innerHTML = image_title;
	
	var content = response.responseXML.getElementsByTagName('content')[0].firstChild ? response.responseXML.getElementsByTagName('content')[0].firstChild.nodeValue : null;
	var connect_image_content = $('connect_gallery_content');
	connect_image_content.innerHTML = content;

	var image = response.responseXML.getElementsByTagName('image')[0].firstChild ? response.responseXML.getElementsByTagName('image')[0].firstChild.nodeValue : null;
	var connect_image_img_container = $('connect_gallery_image');
	connect_image_img_container.innerHTML = '';

	if (image) { 
		var content_image = document.createElement('img');
		content_image.src = image;
		content_image.width = '306';
		content_image.height = '169';
		content_image.alt = '';
		connect_image_img_container.appendChild(content_image);
	}

	var url = response.responseXML.getElementsByTagName('url')[0].firstChild ? response.responseXML.getElementsByTagName('url')[0].firstChild.nodeValue : null;

	var connect_gallery_case_study_link = $('connect_gallery_case_study_link');
	connect_gallery_case_study_link.href = url;
	var connect_gallery_case_study = $('connect_gallery_case_study');
	connect_gallery_case_study.style.display = url ? '' : 'none'; 

};


function prev_connect_image(){
	connect_image_index = connect_image_index == 0 ? connect_image_list.length - 1 : connect_image_index - 1;
	display_connect_image(connect_image_index);
}

function next_connect_image(){
	connect_image_index = connect_image_index == connect_image_list.length - 1 ? 0 : connect_image_index + 1;
	display_connect_image(connect_image_index);
}

function display_connect_image(idx){
	if (response_array[idx]){
		populate_connect_image(response_array[idx]);
	} else {
		get_connect_image(connect_image_list[idx].firstChild.nodeValue);
	}
}
