/*
  gallery.js - Videos/Photos gallery script

  Version: 1.2.1
 */

var
	galService = null,
	galDir = "",
	galItems = null,
	galItemNr = 0;
	galOptions = { autoplay: 1 };


// Initialize gallery

function galInit ( service, basedir, options ) {

	if ( typeof(options) == "object" ) galOptions = options;

	// Set service and base directory

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

	// Load data

	galLoad(galOptions.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 = galOptions.useDateNumerator ? galItems[index][3] : 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" ) {

		// Dimension

		var
			vW = galOptions.videoWidth  || 400,
			vH = galOptions.videoHeight || 322;

	        // Load

		swfobject.embedSWF("http://rd.videos.sapo.mz/play?file="+item[0]+"/mov/1","detail",vW+"px",vH+"px","8.0.0","http://imgs.sapo.pt/swf/NOTICIAS2008/swfobject/expressInstall.swf",{},{allowScriptAccess:"allways",quality:"high"},{});

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

		var
			comp = document.getElementById("detail"),
			imageURL = item[0];

		if ( comp == null )
			return 0;

		if ( imageURL.match(/http:\/\/fotos\.sapo\.[a-z]{2,6}/) ) imageURL += "/x435";

		comp.innerHTML = "<img src=\""+imageURL+"\" 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(item[4]);


	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";

}

