window.addEvent('domready', function() {
	var sponsor = new Sponsors({element:'sponsors-logos', timer:5});
});

var Sponsors = new Class({
	Implements: [Events, Options],
	options:{
		element:'my-element',
		alt:true,
		alt_val:"sponsor",
		timer:5,
		speed:5,
		count:5,
		transition:Fx.Transitions.Quart.easeOut
	},
	initialize: function(options){
		this.setOptions(options);
		this.element = this.options.element;
		$(this.options.element).set('tween',{
			duration:(this.options.speed*100),
			transition:this.options.transition,
			link:'cancel'
		});
		
		arySponsors = this.getSponsors();
		
		//this.cycleSponsors();
		this.cycleSponsors.periodical(this.options.timer * 1000, this);
		
	},
	getSponsors: function(){
		
		var filename = siteurl+'/sponsors';
		//var filename = site_url+'data.php';
		//$(this.options.element).set('html', '<img style="margin-left:130px;" src="'+site_url+'images/ajax-loader.gif">');
		var req = new Request.JSON({url:filename, 
			onSuccess: function(jsonObj) {
				arySponsors = jsonObj;
				//alert(jsonObj);
			},
			onFailure: function() {
				alert('The request failed.');
			}
		});
		
		req.send();
	},
	cycleSponsors: function(){
		this.fader(this.options.element,0);
		content_html = '<ul id="sponsors">';
		
		n = arySponsors.length;
		//alert(n);
		m = (index == 4) ? index + this.options.count : 4;
		for(i = index; i < m; i++){
			val = (this.options.alt == true) ? arySponsors[i].title : 'Sponsor';
			content_html += '<li><a href="'+arySponsors[i].url+'"><img src="'+arySponsors[i].src+'" border="0" alt="'+val+'" target="_blank" /></a></li>';

			
		}
		index = ((index+this.options.count) >= n) ? 0 : index+4;
		content_html += "</ul>";
		
		timer_01 = (function() { $(this.options.element).set('html', content_html); }).delay(500, this);
		timer_02 = (function() { this.fader(this.options.element,1); }).delay(1000, this);
	},
	fader: function(element, opacity){
		$(element).get('tween').start('opacity',opacity);
	}
});