/**
 * @author Damien Churchill
 * @version 0.1
 */
var Straplines = new Class({
	
	Binds: ['rotate'],

	initialize: function(div) {
		this.div = div;
		
		// ensure that all overflowing elements are hidden.
		this.div.setStyle('overflow', 'hidden');
		
		// see how much space we have to use, we need this when adding
		// testimonials.
		this.size = this.div.getSize();
		
		// empty the div as we don't care about whats in there at the
		// moment.
		this.div.empty();
		
		// create an array so we have easy access to our straplines
		// elements
		this.straplines = new Array();
		
		// this effects
		this.effects = new Array();
		
		// set the currentIndex to 0
		this.currentIndex = 0;
	},
	
	addStrapline: function(html) {
		// create a container element for the straplines to ensure that it
		// has a wrapper element
		var elm = new Element('div', {
			html: html,
			style: 'height: ' + this.size.y + 'px; width: ' + this.size.x + 'px;'
		});
		
		// add the element to the easy access array
		this.straplines.push(elm);
		
		// add the created element to the straplines div
		this.div.grab(elm);
		
		// create a slide effect for the element, naming the div as the wrapper.
		this.effects.push(new Fx.Tween(elm, {property: 'margin-top'}));
		
		// return this to allow for chaining
		return this;
	},
	
	addCompanyReg: function(html) {
		// create a container element for the straplines to ensure that it
		// has a wrapper element
		var elm = new Element('div', {
			html: html,
			style: 'height: ' + this.size.y + 'px; width: ' + this.size.x + 'px;'
		});
		
		// add the element to the easy access array
		this.straplines.push(elm);
		
		// add the created element to the straplines div
		this.div.grab(elm);
		
		// create a slide effect for the element, naming the div as the wrapper.
		this.effects.push(new Fx.Tween(elm, {property: 'margin-top'}));
		
		// return this to allow for chaining
		return this;
	},
	
	addDomain: function(html) {
		// create a container element for the straplines to ensure that it
		// has a wrapper element
		var elm = new Element('div', {
			html: html,
			style: 'height: ' + this.size.y + 'px; width: ' + this.size.x + 'px;'
		});
		
		// add the element to the easy access array
		this.straplines.push(elm);
		
		// add the created element to the straplines div
		this.div.grab(elm);
		
		// create a slide effect for the element, naming the div as the wrapper.
		this.effects.push(new Fx.Tween(elm, {property: 'margin-top'}));
		
		// return this to allow for chaining
		return this;
	},
	
		addWelcome: function(html) {
		// create a container element for the straplines to ensure that it
		// has a wrapper element
		var elm = new Element('div', {
			html: html,
			style: 'height: ' + this.size.y + 'px; width: ' + this.size.x + 'px;'
		});
		
		// add the element to the easy access array
		this.straplines.push(elm);
		
		// add the created element to the straplines div
		this.div.grab(elm);
		
		// create a slide effect for the element, naming the div as the wrapper.
		this.effects.push(new Fx.Tween(elm, {property: 'margin-top'}));
		
		// return this to allow for chaining
		return this;
	},
	
	
	rotate: function() {
		// set up a few local vars that are required in the clean up func
		var elm = this.straplines[this.currentIndex];
		
		// start scrolling the visible element out of display.
		this.effects[this.currentIndex++].start(-this.size.y).chain(function() {
			// remove the div from the dom
			elm.dispose();
			
			// reset the margin-top style.
			elm.setStyle('margin-top', 0);
			
			// re-add the element to the dom so it ends up at the end of the div.
			this.div.grab(elm);
		}.bind(this));
		
		// if we've reached our limit then reset the currentIndex
		if (this.currentIndex >= this.straplines.length) this.currentIndex = 0;
	},
	
	start: function(interval) {
		// don't want to start calling rotate again so just
		// return if it we are running
		if (this.running) return;
		
		// check to see if we have a passed in interval
		interval = $pick(interval, 10000)
		
		// use the periodical extension that mootools adds to start the
		// rotate function calling every 7 seconds or the passed in interval.
		this.running = this.rotate.periodical(interval);
	},
	
	stop: function() {
		if (!this.running) return;
		$clear(this.running);
	}
});

document.addEvent('domready', function(e) {
	var straplines = new Straplines($('straplines'));
	straplines
		.addStrapline("<p>over <b>1 million</b> users</p>")
		.addStrapline("<p>over <b>200,000</b> Company Formations</p>")
		.addStrapline("<p>over <b>3 million</b> products</p>")
		.start();
});

document.addEvent('domready', function(e) {
	var straplines = new Straplines($('CompanyReg'));
	straplines
		.addCompanyReg("<a href='http://companyregistration.uk-plc.net'>Company Formation with @UK Company Registration</a>")
		.addCompanyReg("Check to see if your Company name is available")
		.addCompanyReg("Looking to form a Company")
		.start();
});

document.addEvent('domready', function(e) {
	var straplines = new Straplines($('DomainName'));
	straplines
		.addDomain("<a href='http://uk-plc.net/domains.htm'>Domain name with @UK PLC Domain name Registration</a>")
		.addDomain("<b>&pound;2.99 / yr</b> for <b>UK</b> domains<br/><b>&pound;9.00 / yr</b> for <b>.com, .org, .net</b>")
		.addDomain("Check to see if your Domain name is available")
		.start();
});

document.addEvent('domready', function(e) {
	var straplines = new Straplines($('welcome'));
	straplines
		.addWelcome("Welcome to @UKPLC, the UK&#39;s no. 1 marketplace with over 1 million users")
		.addWelcome("Leading Company Registration Agents with over 200,000 Company Formations")
		.addWelcome("Welcome to World&#39;s First Green Marketplace")
		.start();
});
