if( typeof at == "undefined" ) at = {};
if( typeof at.treemotion == "undefined" ) at.treemotion = {};

at.treemotion.Fotoshow = {
	feeds : null
,	container: null
,	feed: null
,	ss: null
,	popup: false
,	saveHTML: null
,	options:null
,	loadAttempt:1
,	loadMaxAttempts : 10 
,	loadSlideShow: function(feed){
		$('slideshow').innerHTML = $('slideshowStatus').innerHTML;
		this.options = {
			feedLoadCallback: this.feedLoadCallback.bind(this)
		,	feedProcessedCallback: this.feedProcessedHandler.bind(this)
		,	thumbnailUrlResolver: this.urlResolver.bind(this)
		,	imageClickCallback: this.clickHandler.bind(this)
		,	pauseStateCallback: this.pauseStateCallback.bind(this)
		,	transitionCallback: this.transitionCallback.bind(this)
		,	scaleImages: false //,	centerImage: {ver:true,hor: true}
		,	pauseOnHover: false
		//,	fullControlPanel : true
		//,  	fullControlPanelSmallIcons : true
		//,	scalePauseImage : false
		//,	fullControlPanelCursor : true
		//,	pauseCenterBias : { topBias : -380, leftBias : 0}
		//,	thumbnailSize : GFslideShow.THUMBNAILS_SMALL
		}
		this.feed = feed;
		this.ss = new GFslideShow(feed, "slideshow", this.options);
		if(!this.ss.container) this.destroy();		
	}
,	gotoIndex: function(i){
		this.ss.gotoIndex(i);
		this.pause();
	}
,	pause: function(){
		if(this.ss != null){
			$('fotoshowPause').innerHTML = '<a href="javascript://" onclick="at.treemotion.Fotoshow.play()">play</a>';
			this.ss.pause(false);			
		}
	}
,	play: function(){
		if (this.ss != null) {
		  	$('fotoshowPause').innerHTML = '<a href="javascript://" onclick="at.treemotion.Fotoshow.pause()">pause</a>';
		  	this.ss.resume();
		}
	}
,	next : function(){
		this.pause();
		this.ss.goForward();
	}
,	prev : function(){
		this.pause();
		this.ss.goBackward();
	}
,	loadBigImage: function(){
		this.pause();
		$('pageContent').hide();
		var container = $('pagePopup');
		var td0 = container.down('td',0); // navigate
		var td1 = container.down('td',1); // image
		var td2 = container.down('td',2); // message
		td2.innerHTML = $('status').innerHTML;
		td0.hide();	
		td1.hide();
		td2.show();	
		container.show();
		var image = new Image();
		image.onload = function() {
 			td1.down('img').src = image.src;
			td1.show();	
			td0.show();
	   	td2.hide();
		};
		image.src = this.bigImageUrl + '?imgmax=640';
	}
,	nextBigImage : function(){
		this.next();
		this.loadBigImage();
	}
,	prevBigImage : function(){
		this.prev();
		this.loadBigImage();
	}
,	restore : function(){
		$('pageContent').show();
		$('pagePopup').hide();
	}
,	destroy: function(){
		this.ss = null;
		return;
		// dos not work properly
		if(this.ss != null){
			console.debug(this.ss.container);
			if(typeof this.ss.container == 'object') this.ss.cleanup();		
		}
	}
,	clickHandler: function(entry){
		var tnNode = google.feeds.getElementsByTagNameNS(entry.xmlNode, 
			"http://search.yahoo.com/mrss/", 'content')[0];
		this.bigImage = '<img src="' + tnNode.getAttribute('url') 
			+ '?imgmax=640" onload="at.treemotion.Fotoshow.loadBigImage()"/>';
		this.bigImageUrl = tnNode.getAttribute('url');
	}
,	pauseStateCallback: function(state){
		this.popup = state;
	}
,	feedLoadCallback: function(result){
		if (result.error) {
			if(this.loadAttempt < this.loadMaxAttempts){
				this.loadSlideShow(this.feed);			
			}
			this.ss.container.innerHTML += '<br>Versuch ' + this.loadAttempt++
					+ ' Galerie konnte nicht geladen werden';
		}
	}
,	feedProcessedHandler: function(result){
		if (result.error) {
			alert("feed load failed");
		}
		else {
			var entries = result.feed.entries;
			var html = '<table>';
			for (var i = 0; i < entries.length; i++) {
				var tnNode = google.feeds.getElementsByTagNameNS(entries[i].xmlNode, 
					"http://search.yahoo.com/mrss/", 'thumbnail')[0];
				var align = 'left';
				if (i % 1 == 0) {
					html += '<tr>';
					align = 'center';
				}
				html += '<td align="' + align + '" valign="middle">' 
					+ '<a href="javascript://" onclick="at.treemotion.Fotoshow.gotoIndex(' + i +')">' 
					+ '<img border="0" src="' + tnNode.getAttribute('url') +	'"/></a></td>';
				if (i % 2 == 1) {
					html += '</tr>';
				}
			}
			html += '</table>';
			$("thumbnails").innerHTML = html;
		}
	}
,	urlResolver: function(entry){
		var tnNode = google.feeds.getElementsByTagNameNS(entry.xmlNode, 
			"http://search.yahoo.com/mrss/", 'content')[0];
		return tnNode.getAttribute('url') + '?imgmax=400';
	}
,	transitionCallback: function(){
		if( !$( 'fotoshowCount' ) )
			this.destroy();
		if(this.ss != null && this.ss.entries){
			$('fotoshowCount').innerHTML = this.ss.photo_index + 1 
				+ ' / ' + this.ss.entries.length
			
		}
	}
,	popup : function(url){
		window.open('popup.php?feed=' + encodeURIComponent(url),'popup','width=300,height=400,left=100,top=200');
	
	}
	
}

at.treemotion.Galleries = {
	fc : at.treemotion.FeedControl
,	url : null
,	container : null
,	clickCallback : false
,	initialize : function(url,container){
		this.url = url;
		this.container = container;
	
	}
,	list : function(){
		var me = this;
		this.fc.initialize(this.url,this.container);
		this.fc.showEntriesCallback = function(control,entry,i){
			var feed = entry.xmlNode.getElementsByTagName('guid')[0].firstChild.data;
			feed = feed.replace(/entry/,'feed');
			//console.debug(feed);
			if(me.clickCallback){
				return '<div><a href="javascript://" onclick="at.treemotion.Galleries.clickCallback(\'' + feed + '\')">' + entry.title + '</a></div>';			
			}else{
				return '<div><a target="_blank" href="' + feed + '">' + entry.title + '</a></div>';			
			}
		}
		this.fc.captionCallback = function(control,result){
			return result.title;
		}
		this.fc.showEntries();	
	}
}

at.treemotion.Scroll = {
	interval : 0
,	distance : 10
,	scrollDistance : 5
,	period : 25	
,	container : 'thumbnails'
,	up : function(){
		$(this.container).scrollTop -= this.distance;
		window.clearInterval(this.interval );
		var me = this;
		this.interval = window.setInterval(function(){
			me.scroll(-1 * me.scrollDistance);},me.period);
	}
,	down : function(){
		$(this.container).scrollTop += this.distance;
		window.clearInterval(this.interval );
		var me = this;
		this.interval = window.setInterval(function(){
			me.scroll(me.scrollDistance);},me.period );
	}
,	scroll : function(i){
		$(this.container).scrollTop += i;
	}
,	stop : function(){
		window.clearInterval(this.interval);
	}

}	

at.treemotion.debug = function(msg){
	if(typeof console != "undefined"){
		console.debug(msg)
	}	
}
