(function($) {
    $.fn.kilnSlider = function(target) {
        var holder = this;
        var content = holder.find('.slider_inner');
        var left = holder.find('.left');
        var right = holder.find('.right');
        var width = 0;
        var imgheight = 0;
        var counter = 0;
        var loaded = 0;
        var single = false;
        content.css('height','400px');
        if (content.find('div').length == 1) {
            single = true;
            left.css('display', 'none');
            right.css('display', 'none');
        }
        left.css('display', 'none');
        right.css('display', 'none');
        content.find('div').each(function() {
            counter++;
            if ($(this).find('img').length) {
                $(this).find('img').css('display','none');
                $(this).find('img').imagesLoaded(function() {
                    loaded++;
                    if ($(this).height() > imgheight) {
                        imgheight = $(this).height();
                        
                    }
                });
                // $(this).find('img').error(function(){
                //   // $(this).hide();
                //   loaded++;
                //   console.log('load failed');
                // })
            } else {
              loaded++;
              if ($(this).height() > imgheight) {
                  imgheight = $(this).height();
              }
            }
        });
        width = counter * 750;
        loadingInterval = setInterval(function() {
            // console.log('loaded: '+loaded+' | counter: '+counter);
            if (loaded >= counter) {
                if (content.find('div').length > 1) {
                    left.css('display', 'block');
                    right.css('display', 'block');
                }
                if (content.find('div img').length) {
                    content.find('div img').css('display','block');
                }
                content.parent().height(imgheight);
                holder.css('height','auto');
                content.find('div').height(imgheight);
                content.width(width);
                clearInterval($(this));
            }
            // content.find('div').each(function() {
            //   $(this).height(imgheight);
            // });
        },
        100);
        var animation = false;
        var current = 1;
        content.click(function() {
            // console.log('clicked content ' + current + " : " + counter);
            if (animation == false) {
                if (current < counter) {
                    animation = true;
                    current += 1;
                    content.animate({
                        left: '-=750'
                    },
                    function() {
                        animation = false;
                    });
                } else {
                    animation = true;
                    current = 1;
                    content.animate({
                        left: '+=' + (width - 750)
                    },
                    function() {
                        animation = false;
                    });
                }
            }
        });
        right.click(function() {
            // console.log('clicked right ' + current + " : " + counter);
            if (animation == false) {
                if (current < counter) {
                    animation = true;
                    current += 1;
                    content.animate({
                        left: '-=750'
                    },
                    function() {
                        animation = false;
                    });
                } else {
                    animation = true;
                    current = 1;
                    content.animate({
                        left: '+=' + (width - 750)
                    },
                    function() {
                        animation = false;
                    });
                }
            }
        });
        left.click(function() {
            // console.log('clicked left ' + counter);
            if(current > 1){
              current -= 1;
              if (content.css('left').replace('px', '') == 0 || content.css('left').replace('px', '') == 'auto') {
                  } else {
                  content.animate({
                      left: '+=750'
                  });
              }
            }
        });
    }
    

})(jQuery);

$(document).ready(function() {
    if ($('#navigation ul li.here').length == 0) {
        $('#navigation ul li:first').addClass('here');
    }
    $('.slider').each(function() {
        $(this).kilnSlider();
    });
});

$.fn.imagesLoaded = function(callback){
  var elems = this.filter('img'),
      len   = elems.length;

  elems.bind('load',function(){
      if (--len <= 0){ callback.call(elems,this); }
  }).each(function(){
     // cached images don't fire load sometimes, so we reset src.
     if (this.complete || this.complete === undefined){
        var src = this.src;
        // webkit hack from http://groups.google.com/group/jquery-dev/browse_thread/thread/eee6ab7b2da50e1f
        // data uri bypasses webkit log warning (thx doug jones)
        this.src = "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==";
        this.src = src;
     }  
  }); 

  return this;
};

