/*********************************************************/
/* Random Quote Generator version 0.1
/* Created By: Michael Wenzel (wenzel.michael@gmail.com)
/* Created On: October 11th, 2006
/* licensing below...creative commons...
/*********************************************************/

if(!SWPGenerator) var SWPGenerator = {}; 

SWPGenerator.Statistic = Class.create();
SWPGenerator.Statistic.prototype = {
	initialize: function(stats) {
		
		options = Object.extend({
      			objectTitle:	'content_title',
      			objectText:  'content_text',
 						objectNumber: 'content_number',
						objectContainer: 'generator_content_container_inner',
						controlNext: 'control_next',
						controlPrevious: 'control_previous',
						controlPlay: 'control_play',
						controlStop: 'control_stop',
						controlNumber: 'control_index',
						generator: 'generator',
						randomElement: 'random',
						intervalElement: 'interval',
						randomLogo: 'logo_random',
						autoPlay: false,  
						controls: true,
						random:		 false
    				}, arguments[1] || {});
		
		this.objectTitle = $(options.objectTitle);
		this.objectText 	= $(options.objectText);  
		this.objectNumber = $(options.objectNumber);
		this.objectImage 	= $(options.objectImage) || null;
		this.objectContainer = $(options.objectContainer);
		this.controlNext 	= $(options.controlNext) || null;
		this.controlPrevious = $(options.controlPrevious) || null;
		this.controlPlay 	= $(options.controlPlay) || null;
		this.controlStop	= $(options.controlStop) || null;
		this.generator  = $(options.generator);
		this.randomElement = $(options.randomElement) || null;
		this.intervalElement = $(options.intervalElement) || null;
		this.randomLogo		= $(options.randomLogo);
		
		this.content 						= stats;
		this.index 							= this.content.length;
		this.random							= options.random || false;
		this.autoPlay	 			 		= options.autoPlay;		
		this.controls						= options.controls;
		this.playing 						= options.playing || false;
		this.interval 					= options.interval || 5;      
		
		
		var defaultStack = new Array();               
		
		defaultStack.push({
			funktion: function() {
				if(this.objectContainer.visible)
					Effect.Fade(this.objectContainer, {duration: 0.3});
			},
			interval: 0
		});
		
		defaultStack.push({
			funktion: function() { 
				this.objectNumber.innerHTML = this.currentObject.number;
				this.objectTitle.innerHTML = this.currentObject.title;
				this.objectText.innerHTML = this.currentObject.text;  
				Effect.Appear(this.objectContainer, {duration: 3});
			},
			interval: 0.4
		});                   
		
		this.stack = options.stack || defaultStack;
		
		if(this.controls)
			this.initializeListeners();
			
		this.detect();
		
		this.start();
		
	},   
	
	detect: function() {
        var UA = navigator.userAgent;
        this.isKHTML = /Konqueror|Safari|KHTML/.test(UA);
        this.isGecko = (/Gecko/.test(UA) && !this.isKHTML);
        this.isOpera = /Opera/.test(UA);
        this.isMSIE  = (/MSIE/.test(UA) && !this.isOpera);
        this.isMSIE7 = this.isMSIE && !(/MSIE 6\./.test(UA) && !this.isOpera);
    },
	
	initializeListeners: function() {
		if(this.randomElement)
			Event.observe(this.randomElement, 'click', this.toggleRandomValue.bindAsEventListener(this));
			
		if(this.intervalElement)
			Event.observe(this.intervalElement, 'change', this.changeIntervalValue.bindAsEventListener(this));     
			
		if(this.controlNext) {
			Event.observe(this.controlNext, 'mousedown', this.toggleButton.bindAsEventListener(this, true));
			Event.observe(this.controlNext, 'mouseup', this.toggleButton.bindAsEventListener(this, false));
			Event.observe(this.controlNext, 'mouseout', this.toggleButton.bindAsEventListener(this, false));  
			Event.observe(this.controlNext, 'click', this.nextObject.bindAsEventListener(this));
		}
		if(this.controlPrevious) {
			Event.observe(this.controlPrevious, 'mousedown', this.toggleButton.bindAsEventListener(this, true));
			Event.observe(this.controlPrevious, 'mouseup', this.toggleButton.bindAsEventListener(this, false));
			Event.observe(this.controlPrevious, 'mouseout', this.toggleButton.bindAsEventListener(this, false));
			Event.observe(this.controlPrevious, 'click', this.previousObject.bindAsEventListener(this));
		}
		if(this.controlPlay)
			Event.observe(this.controlPlay, 'click', this.play.bindAsEventListener(this));
			
		if(this.controlStop)
			Event.observe(this.controlStop, 'click', this.stop.bindAsEventListener(this));
	},
	
	start: function() {
		if(this.random && this.randomElement)
			this.randomElement.checked = true;
		
		if(this.intervalElement)
			this.intervalElement.selected = this.interval;
			
		if(this.autoPlay)
			this.play();  
		else
			this.nextObject();
	},
	
	play: function() {
		if(this.playing)
			return;
		Element.removeClassName(this.controlStop, 'active');
		Element.addClassName(this.controlPlay, 'active');
		this.nextObject();
		this.playing = setInterval(this.nextObject.bind(this), this.interval * 1000);
	},
	
	stop: function() {
		clearInterval(this.playing);
		Element.removeClassName(this.controlPlay, 'active');
		Element.addClassName(this.controlStop, 'active');
		this.playing = false;
	},                      
	
	toggleButton: function(event, active) {
		event = window.event? window.event.srcElement : event ? event.target : null;
		if(active) {
			Element.addClassName(event, 'active');
		}
		else
			Element.removeClassName(event, 'active');
	},
	
	changeIntervalValue: function(event) {
		event = window.event? window.event.srcElement : event ? event.target : null;
		this.interval = event.value;
		if(this.playing) {
			clearTimeout(this.playing);
			this.playing = setInterval(this.nextObject.bind(this), this.interval * 1000);
		}
	},
	
	toggleRandomValue: function(event) {
		event = window.event? window.event.srcElement : event ? event.target : null;
		this.random = event.checked;                                                    
		
		if(this.randomLogo) 
			(this.random) ? Element.show(this.randomLogo) : Element.hide(this.randomLogo);
		
		
	},
	
	nextObject: function() {
		i = (this.random) ? (this.index = this.getRandomNumber(this.content.length)) : this.nextObjectIndex();
    this.displayContent(this.getObject(i));            

	},
	
	previousObject: function() {
		i = (this.random) ? (this.index = this.getRandomNumber(this.content.length)) : this.previousObjectIndex();
		this.displayContent(this.getObject(i));

		
	},
	
	displayContent: function(object) {
		this.currentNumber = 0;         
		clearTimeout(this.animatedNumberCount)
		this.currentObject = object;
		this.executeAnimationStack(this.stack, object);
		
	},
	
	//returns false if int matches any non-numeric characters
	//otherwise true;
	isNumeric: function(int) {
		return (int.toString().match(/\D/g) == null);
		
		
	},
	
	setRandomObjectPosition: function(element)  {
		y_distance = Math.floor(Math.random() * (parseInt(this.generator.getStyle('height')) - parseInt(element.getStyle('height')))) + 'px';
		x_distance = Math.floor(Math.random() * (parseInt(this.generator.getStyle('width')) - parseInt(element.getStyle('width')))) + 'px';
		
		vertical 	= ['top', 'bottom'];
		horizontal  = ['left', 'right'];   
		
		y = vertical[Math.round(Math.random())];
		x = horizontal[Math.round(Math.random())];
		
		//this.log(x + ':' + x_distance + '::' + y + ':' + y_distance)
		                      
		if(y == 'top') 
			element.setStyle({top: y_distance});
		else 
			element.setStyle({top: null,bottom: y_distance});
			
		if(x == 'left')
			element.setStyle({left: x_distance});
		else 
			element.setStyle({left: null, right: x_distance});
				
	},
	
	animateNumberCount: function(number) {      
		//this.log(this.currentNumber);
		if(this.currentNumber >= number) {
			clearInterval(this.animatedNumberCount);
			return;
		}
		
		increment = Math.ceil(number * .021);
		this.currentNumber += increment;
		
		if(this.currentNumber > number)
			this.currentNumber = number;
			
		this.objectNumber.innerHTML = this.formatNumber(this.currentNumber);
		//this.objectNumber.innerHTML = this.currentNumber;
	},        
	
	formatNumber: function(integer) {
		var number = integer.toString();
		var length = number.length;
		num = '';    
				
		
		for(i = 0; i < length; i++) {
			if(((length - i) % 3) == 0 && i != 0)
				num += ',';
			num += number.charAt(i);
		}                         
		
		return num;
	},
	
	executeAnimationStack: function(stack) {
		stack.each(function(item) {
			setTimeout(item.funktion.bind(this), item.interval * 1000);
		}.bind(this));
		
	},      
	
	changeContent: function(object) {
		this.objectTitle.innerHTML = object.title;
		this.objectText.innerHTML = object.text;
	},
	
	nextObjectIndex: function() {
		this.index++;
		if(this.index > this.content.length - 1)
			this.index = 0;
	 	return this.index;
	},   
	
	previousObjectIndex: function() {
		this.index--;
		if(this.index < 0)
			this.index = this.content.length - 1;
		return this.index;
	},

	getObject: function(i) {   
		return (i >= 0 && i < this.content.length) ? this.content[i] : false;
	},
	
	getRandomNumber: function(size) {
		return Math.floor(Math.random()*size);
	},          
	
	testContent: function() {
		html = [];
		this.content.each(function(object) {
					html.push(object.title + ":" + object.text);
								  });
		new Insertion.Top('content', html.join("<br>"));
	},
	
	log: function(text) {	
		new Insertion.Top('log', "<li>" + text.toString() + "</li>");
	}
}