/*
randomize.jQuery.js
Code by Chris Watson
Business site: http://www.wattzup.com
Blog: http://www.iknowtec.com
Twitter: @cwatsonknowstec
Realeased under the GNU General Public License http://www.gnu.org/licenses/gpl.html
*/
(function($){
$.fn.randomize = function(options) {
var defaults = {
oddBG: '#000',
oddFG: '#fff',
evenBG: '#fff',
evenFG: '#000',
startButtonClass: 'startBtn',
startButtonTxt: 'Start',
direction: 'bottomToTop',
containerID: 'container',
styleSheet: 'style.css',
};
var options = $.extend(defaults, options);
if (this.length > 1){
this.each(function() { $(this).myPlugin(options) });
return this;
}
obj = $(this);
// Build the necessary elements
obj.wrap('<div id="'+ options.containerID +'">');
$('#' + options.containerID).after('<button class="' + options.startButtonClass + '" >' + options.startButtonTxt + '</button>');
//Set CSS styles
obj.find('span').filter(':odd').css({ 'backgroundColor':options.oddBG,'color':options.oddFG });
obj.find('span').filter(':even').css({ 'backgroundColor':options.evenBG,'color':options.evenFG });
$('head').append('<link rel="stylesheet" href="' + options.styleSheet + '" type="text/css" />');
setVars = function() {
speed = Math.floor(Math.random() * (100 - 50 + 1) + 50);
timeout = Math.floor(Math.random() * (7000 - 2000 + 1) + 2000);
}
$('.' + options.startButtonClass).click(function() {
setVars();
startFunct();
});
loopFunct = function() {
if (options.direction == 'bottomToTop') {
obj.find('span:first-child').appendTo($(obj));
}
else if (options.direction == 'topToBottom') {
obj.find('span:last-child').prependTo($(obj));
}
}
startFunct = function() {
looping = setInterval("loopFunct()",speed);
setTimeout("stopFunct()",timeout);
$('.'+options.startButtonClass).hide();
}
stopFunct = function() {
clearInterval(looping);
resultFunct();
$('.'+options.startButtonClass).show();
}
resultFunct = function() {
if (obj.find('span:nth-child(3) a').length > 0) {
window.location = obj.find('span:nth-child(3) a').attr('href');
}
else {
alert('No Link...');
}
}
};
})(jQuery);