function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}


function toggleSlider(domid) {
  if ($(domid).visible()) {
    Effect.SlideUp(domid);
  } else {
    Effect.SlideDown(domid);
  }
}

// function autolink(s) 
// {   
//   var hlink = /\s(ht|f)tp:\/\/([^ \,\;\:\!\)\(\"\'\<\>\f\n\r\t\v])+/g;
//   return (s.replace (hlink, function ($0,$1,$2) { 
//     s = $0.substring(1,$0.length); 
//     // remove trailing dots, if any
//     while (s.length>0 && s.charAt(s.length-1)=='.') {s=s.substring(0,s.length-1);}
//     // add hlink
//     return " " + s.link(s); 
//   }));
// }
// 
// function load_facebook() {
//   new Ajax.Request('http://tupelohoneycafe.com/facebook', {
//     onSuccess: function(response) {
//       html = "Looks like Facebook isn't available right now. For our latest, check <a href='http://www.facebook.com/#!/pages/Asheville-NC/Tupelo-Honey-Cafe/183504398021'>Tupelo Honey Cafe</a> on Facebook.";
//       posts = JSON.parse(response.responseText);
//       if (posts) { 
//         html = "<ul class='posts'>\n";
//         posts.data.each(function(item,index){
//           html += "<li>" + item.message + "</li>"; 
//         });
//         html += "</ul>";
//       }
//       $("facebook-posts").innerHTML = autolink(html);
//     },
//     onFailure: function(response) {
//       html = "Looks like Facebook isn't available right now. For our latest, check <a href='http://www.facebook.com/#!/pages/Asheville-NC/Tupelo-Honey-Cafe/183504398021'>Tupelo Honey Cafe</a> on Facebook.";
//       $("facebook-posts").innerHTML = autolink(html + " <!-- " + response.responseText + " --> ");
//     }
//   });  
// }
// 
// function load_tweets() {
//   new Ajax.Request('/twitter', {
//     onSuccess: function(response) {
//       tweet = JSON.parse(response.responseText);
//       tweet_text = tweet.text + "<br/>";
//       if (tweet.in_reply_to_screen_name) {
//         tweet_text = tweet_text + " (in reply to @" + tweet.in_reply_to_screen_name + ")"
//       }
//       $("tupelo-tweets").innerHTML = autolink(tweet_text);
//       twttr.anywhere(function (T) {T.hovercards();T.linkifyUsers()});
//     },
//     onFailure: function(response) {
//       $("tupelo-tweets").innerHTML = "Looks like Twitter isn't available right now. For our latest, check <a href='http://twitter.com/tupelohoneycafe.com'>@tupelohoneycafe</a> on Twitter.<!-- " + response.responseText + " -->";
//     }
//   });  
// }


SlideShow = Class.create({
  initialize: function() {
    this.root = $('home_image');
    this.interval_id = null;
    if (!this.root) {alert('There is no viewport. Add a #home_image div to your HTML document to use this slide show.');}
    document.observe("dom:loaded", function() {
      this.interval_id = setInterval(this.poll, 8000);
    }.bind(this));
  },
  
  poll: function() {
    var current = $$('#home_image img.current').first();
    if (current) {
      var next = current.nextSiblings().first();
      if (!next) {next = $$('#home_image img').first();}

      var current = $$('#home_image img.current').first();
      if (!current || !next || current.identify() == next.identify()) {return;}
      
      current.setStyle('z-index:0');
      next.setStyle('z-index:1');

      current.fade({duration:2.0});
      next.appear({duration:3.0});

      next.addClassName('current');
      current.removeClassName('current');

      if (this.interval_id != null) {
        clearInterval(this.interval_id);
        this.interval_id = setInterval(this.poll, 8000);
      }    
    }
  }
});


