//Timer Plugin
function Timer(callback, delay) {
var timerId, start, remaining = delay;
this.pause = function() {
window.clearTimeout(timerId);
remaining -= new Date() - start;
};
this.resume = function() {
start = new Date();
timerId = window.setTimeout(callback, remaining);
};
this.restart = function() {
window.clearTimeout(timerId);
start = new Date();
timerId = window.setTimeout(callback, delay);
};
this.resume();
}
//Shuffle plugin
(function($){
$.fn.shuffle = function() {
return this.each(function(){
var items = $(this).children();
return (items.length)? $(this).html($.shuffle(items)): this;
});
}
$.shuffle = function(arr) {
for(
var j, x, i = arr.length; i;
j = parseInt(Math.random() * i),
x = arr[--i], arr[i] = arr[j], arr[j] = x
);
return arr;
}
})(jQuery);
//highest child height plugin
(function($){
$.fn.extend({
getHeighestChildElement: function(selector) {
if(selector == null){
selector = $(this).children()[0].nodeName.toLowerCase();
}
var buffer = 0;
$(this).children(selector).each(function(index, element) {
if( $(this).height() > buffer){
buffer = $(this).outerHeight();
}
});
return buffer;
}
});
})(jQuery);
//Widget Plugin
$.widgets = {
types : {
slideshow : "slideshow",
random : "random"
},
activeControllers: [],
enable : function(type){
if(type == this.types.random){
this.enableRandomWidget();
}
if(type == this.types.slideshow){
this.enableSlideshowWidget();
}
},
enableRandomWidget: function(){
if($(".randomWidget").length < 2){
$(".randomWidget").randomWidget();
}else{
$(".randomWidget").each(function(index, element) {
$(this).randomWidget();
});
}
},
enableSlideshowWidget: function(){
if($(".slideshowWidget").length < 2){
$(".slideshowWidget").slideshowWidget();
}else{
$(".slideshowWidget").each(function(index, element) {
$(this).slideshowWidget();
});
}
}
};
(function($){
$.fn.extend({
randomWidget: function(options) {
var controller = {};
var defaults = {
count : 1,
elementTarget: "div"
}
options = $.extend(defaults, options);
$(this).children(options.elementTarget).each(function(index, element) {
$(this).css("display", "none");
});
console.log( $(this));
if(options.count == 1){
$($(this).children(options.elementTarget)[Math.floor($(this).children(options.elementTarget).length*Math.random())]).css("display", "block");
}else{
$(this).shuffle();
for(var i = 0; i < options.count; i++){
$($(this).children(options.elementTarget)[i]).css("display", "block");
}
}
}
});
})(jQuery);
(function($){
$.fn.extend({
slideshowWidget: function(options) {
var controller = null;
var defaults = {
random : false,
elementTarget: "div"
}
options = $.extend(defaults, options);
if(options.random == true){
$(this).shuffle();
}
var slides = $(this).children(options.elementTarget);
var numSlides = slides.length;
var currentSlide = 0;
//set height
$(this).height($(this).getHeighestChildElement(options.elementTarget));
//hide all except first
$(this).children(options.elementTarget).each(function(index, element) {
if(index == 0){
$(this).css("display", "block");
}else{
$(this).css("display", "none");
}
});
function nextSlide(){
$(slides[currentSlide]).fadeOut(1000, fadeInNextSlide);
}
function fadeInNextSlide(){
currentSlide++;
if(currentSlide >= numSlides){
currentSlide = 0;
}
$(slides[currentSlide]).fadeIn(1000, controller.restart);
}
if(numSlides > 1){
controller = new Timer(function() {nextSlide();}, 12000);
}
}
});
})(jQuery);










$(document).ready(function(e) {
    
	$.widgets.enable($.widgets.types.slideshow);
	
	
});






