/* CSS3 Slidehsow */

var hovered = false;
var changing = false;
var num_slides = 0;
window.next = 0;

$(document).ready(function() {

num_slides = $('#slideshow-container .slide').size();
window.next = num_slides - 1;
// alert(window.next);
	function onResize() {
		var width = $('#slideshow').width();
		var height = width / 3.33;
		$('#slideshow').css('height', height);
		$('#slideshow .slide').css('height', height);
		if($(window).width() > 798) {
     uncommentF();
		}
	}
	onResize();
	$(window).resize(onResize);
	
	$('#slideshow-container div.slide').each(function(index) {
		$(this).attr('id', 'slide-' + (index + 1));
	});
	
	$('.slide').first().addClass('toggled');
	$('#slideshow-nav a').first().addClass('toggled');

  slideID = setInterval("slideInterval(window.next)",4000);

	$('#slideshow-nav a').click(function() {
    // if(changing) return;
		var index = $(this).attr('rel');
		var slide = $('#slide-' + index);
		if(slide.hasClass('toggled')) return;
		changeToSlide(index);
    // index > 1 ? next = index -1 : next = num_slides;
    clearInterval(slideID); // stop automatic slide movement
	});
});

function changeToSlide(index) {
  // var current = $('#slideshow-container div.slide').last();
  // var current_index = (/\d+/.exec(current.attr('id')))[0];
  var slide = $('#slide-' + index);
  // var current_nav = $('#slideshow-nav a[rel|="' + current_index + '"]');
  var slide_nav = $('#slideshow-nav a[rel|="' + index + '"]');

  $('#slideshow-nav a').removeClass('toggled');
  slide_nav.addClass('toggled');
  $('.slide.toggled').delay(500).removeClass('toggled');
  slide.addClass('toggled');
  // current.before(slide);

}

function slideInterval(next)  {
	if($('#slideshow-container').children().length <= 1) return;
  // if(hovered || changing) return;
  if (next == 0) {
    next = num_slides;
  }
	changeToSlide(next);
  window.next = next - 1;
}

function uncommentF() {
  $('#commented').contents().each(function() {
		if (this.hasChildNodes() ) {
			$(this).uncomment();
		} else if ( this.nodeType == 8 ) {
			// Need to "evaluate" the HTML content,
			// otherwise simple text won't replace
			var e = $('<span>' + this.nodeValue + '</span>');
			$(this).replaceWith(e.contents());
		}
	});
	
}
/* IE Flexible Image fix by Ethan Marcotte - See: http://unstoppablerobotninja.com/entry/fluid-images */

var imgSizer = {
     Config : {
          imgCache : []
          ,spacer : "http://18reasons.org/creamery/wp-content/themes/creamery/images/spacer.gif"
     }
     ,collate : function(aScope) {
          var isOldIE = (document.all && !window.opera && !window.XDomainRequest) ? 1 : 0;
          if (isOldIE && document.getElementsByTagName) {
               var c = imgSizer;
               var imgCache = c.Config.imgCache;
               var images = (aScope && aScope.length) ? aScope : document.getElementsByTagName("img");
               for (var i = 0; i < images.length; i++) {
                    images.origWidth = images.offsetWidth;
                    images.origHeight = images.offsetHeight;
                    imgCache.push(images);
                    c.ieAlpha(images);
                    images.style.width = "100%";
               }
               if (imgCache.length) {
                    c.resize(function() {
                         for (var i = 0; i < imgCache.length; i++) {
                              var ratio = (imgCache.offsetWidth / imgCache.origWidth);
                              imgCache.style.height = (imgCache.origHeight * ratio) + "px";
                         }
                    });
               }
          }
     }
     ,ieAlpha : function(img) {
          var c = imgSizer;
          if (img.oldSrc) {
               img.src = img.oldSrc;
          }
          var src = img.src;
          img.style.width = img.offsetWidth + "px";
          img.style.height = img.offsetHeight + "px";
          img.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizingMethod='scale')"
          img.oldSrc = src;
          img.src = c.Config.spacer;
     }
     // Ghettomodified version of Simon Willison's addLoadEvent() -- http://simonwillison.net/2004/May/26/addLoadEvent/
     ,resize : function(func) {
          var oldonresize = window.onresize;
          if (typeof window.onresize != 'function') {
               window.onresize = func;
          } else {
               window.onresize = function() {
                    if (oldonresize) {
                         oldonresize();
                    }
                    func();
               }
          }
     }
}
