if( typeof at == "undefined" )
   at = {};
if( typeof at.treemotion == "undefined" )
   at.treemotion = {};

at.treemotion.Picasa = {
   // namespaces
   ns_media : "http://search.yahoo.com/mrss/"
,   ns_atom   : "http://www.w3.org/2005/Atom"
}

at.treemotion.Fotoshow = {
   feeds : null
,   container: null
,   feed: null
,   ss: null
,   popupName: 'popup'
,    popupWin : null
,   bigImage : null
,   bigImageUrl: null
,   picasaUrl : null
,   saveHTML : null
,   options : null
,   loadAttempt : 1
,   loadMaxAttempts : 10
,   loadSlideShow: function(feed){
      this.loadAttempt = 1;
      $('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
      //,  linkTarget : google.feeds.LINK_TARGET_BLANK

      //,   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="#" onclick="at.treemotion.Fotoshow.play()">play</a>';
         this.ss.pause(false);
      }
   }
,   play: function(){
      if (this.ss != null) {
           $('fotoshowPause').innerHTML = '<a href="#" 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';
   }
,   loadPicasaLink: function(){
      window.open(this.picasaUrl,'_blank');
   }

,   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');
      this.picasaUrl = entry.link;
   }
,   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="#" 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,params){
      if(typeof params == 'undefined'){
         params = 'width=540,height=480,scrollbars=yes';
      }
      this.popupWin = window.open('http://vhs-bregenz.at/treemotion/resources/js/picasa/popup.php?feed=' +
         encodeURIComponent(url),this.popupName,params);

   }

}

at.treemotion.Galleries = {
   fc : at.treemotion.FeedControl
,   numEntries : 4
,   url : null
,   container : null
,   clickCallback : false
,   showImage : true
,   imgPerRow : 2
,   initialize : function(url,container){
      this.url = url;
      this.container = container;

   }
,   list : function(){
      this.fc.numEntries = this.numEntries;
      this.fc.initialize(this.url,this.container);
      var me = this;
      this.fc.showEntriesCallback = function(control,entry,i){
         var feed = entry.xmlNode.getElementsByTagName('guid')[0].firstChild.data;
         feed = feed.replace(/entry/,'feed');
         var params = {feed : feed};
         var br = '';
         var style =   '';
         if(me.imgPerRow){
            style = ' style="float:left"';
            if((i+1) % me.imgPerRow == 0){
               br = '<br clear="all"/>';
            }
            params['br'] = br;
         }
         params['style'] = style;
         var html = '<div#{style}><a href="#"';
         if(me.clickCallback) {
            html += ' onclick="at.treemotion.Galleries.clickCallback(\'#{feed}\')"';
         }
         html += '>#{title}';
         if(me.showImage){
            var contentNode = google.feeds.getElementsByTagNameNS(
               entry.xmlNode,at.treemotion.Picasa.ns_media, 'thumbnail')[0];
            var tn = contentNode.getAttribute('url');
            html += '<img hspace="1" vspace="1" src="#{src}" alt="image" border="0"/>';
            params['src'] = tn;
         }
         html += '</a></div>#{br}';
         var htmlTpl = new Template(html);
         return htmlTpl.evaluate(params);
      }
      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)
   }
}
