
var
	galService = null,
	galDir = "",
	galItems = null,
	galItemNr = 0;


// Initialize gallery

function galInit ( service, basedir, autoplay ) {

	// Set service and base directory

	galService = service;
	if ( basedir != null )
		galDir = '/'+basedir+'/';

	// Load data

	galLoad(autoplay ? galShow : null);

}


// Load gallery items feed

function galLoad ( onLoad ) {

	var
		synd = new SAPO.Communication.Syndication(),
		itemsId = synd.push(galDir+'items.js', {
			objectName:	'obj',
			timeout:	10,

			onComplete:	function(data) { galLoadDone(data); if ( onLoad != null ) onLoad(data) },
			onTimeout:	galLoadError
		} );

	// Get it!

	synd.run(itemsId);

}


// Gallery loaded

function galLoadDone ( items ) {

	if ( items == null )
		return galLoadError();

	// Set items

	galItems = items;

	// Ajust buttons

	galAjustButtons();


	return 1;

}


// Gallery load error

function galLoadError ( ) {

	return false;

}


// Lookup an item specified on URL

function galURLLookup ( ) {

	var
		cur = 0,
		parts,
		ptr;

	// Split location by #

	parts = location.href.split('#');
	ptr = parts[1];

	if ( (ptr != null) && (ptr.length > 0) ) {

		// Number or URL lookup

		if ( ptr.match(/^\d+$/) )
			return ptr;
		else {
			for ( var x = 0 ; x < galItems.length ; x++ ) {
				if ( galItems[x][0].indexOf("/"+ptr) > 0 )
					return x;
			}
		}

	}

	return 0;

}



// Show current item

function galShow ( ) {

	var
		cur = galItemNr || galURLLookup();


	// Validate and show

	if ( (cur < 0) || (cur >= galItems.length) )
		return 0;

	galShowItemIndex(cur);

}


// Show previous item

function galShowPrev ( ) {

	if ( galItemNr < 1 )
		return 0;

	galShowItemIndex(--galItemNr);

}


// Show next item

function galShowNext ( ) {

	if ( galItemNr == galItems.length-1 )
		return 0;

	galShowItemIndex(++galItemNr);

}


// Show an item with a specific index

function galShowItemIndex ( index ) {

	var
		cntEl = document.getElementById('counter');

	if ( cntEl != null )
		cntEl.innerHTML = parseInt(index+1)+"/"+parseInt(galItems.length);

	galItemNr = index;
	galShowItem(galItems[index]);
	galAjustButtons();

}


// Redim photo

function galRedimPhoto ( img ) {

	var
		comp = document.getElementById("detail");

	if ( comp )
		comp.style.height = img.offsetHeight+"px";

}


// Show an item

function galShowItem ( item ) {

	var
		titleEl = document.getElementById('title'),
		descrEl = document.getElementById('descr');
		dateEl = document.getElementById('date');


	if ( item == null )
		return 0;

	if ( galService == "videos" ) {

	        // Load

		var fo = new SWFObject("http://videos.sapo.ao/play?file="+item[0]+"/mov/1", "video", "400", "322", "8");
		fo.addParam("allowScriptAccess", "always");
		fo.addParam("quality", "high");
		fo.write("detail");

	}
	else if ( galService == "photos" ) {

		var
			comp = document.getElementById("detail");

		if ( comp == null )
			return 0;

		comp.innerHTML = "<img src=\""+item[0]+"\" border=\"0\" onload=\"galRedimPhoto(this)\">";

	}

	// Set title and description

	if ( titleEl != null )
		titleEl.innerHTML = item[1];
	if ( descrEl != null )
		descrEl.innerHTML = item[2];
	if ( dateEl != null )
		dateEl.innerHTML = item[3];


	// Generate interaction

	siteInteraction();


	return 1;

}


/*
  Ajust gallery buttons
 */

function galAjustButtons ( ) {

	var
		prev = document.getElementById('galBtnPrev') || document.getElementById('galBtnPrev-inactive'),
		next = document.getElementById('galBtnNext') || document.getElementById('galBtnNext-inactive');

	prev.id = (galItemNr < 1) ? "galBtnPrev-inactive" : "galBtnPrev";
	next.id = (galItemNr >= galItems.length-1) ? "galBtnNext-inactive" : "galBtnNext";

}

