var adsObj = null;

var aObj = new Class({
		
		actual: 0,
		previous: 0,
		timeout: null,
		
		initialize: function () {
			this.pages = $$('#adBoxes img');
			this.effects = new Fx.Elements(this.pages, { onComplete: this.goWait.bind(this), duration: 1000 } );
			this.goWait();
		},
		
		goNext: function () {
			var used = new Array();
			//alert(adBoxes);
			this.pages.each(function(obj,n){
				//alert(obj.getProperty('src'));
				var key = Math.floor(Math.random()*adBoxes.length);
				while( in_array(key, used) ){
					key = Math.floor(Math.random()*adBoxes.length);
				}
				used[used.length] = key;
				var ai = adBoxes[key];
				obj.setProperty('src','http://www.edgetelecom.co.uk/assets/uploads/174/'+ai.src);
				obj.getParent().setProperty('href',ai.url);
			});
			this.effects.start();
		},
		
		goWait: function () {
			this.timeout = setTimeout( this.goNext.bind( this ), 12000 );
		}
		
});

window.addEvent( 'domready', function () {
	adsObj = new aObj();
} );

function in_array(needle, haystack, argStrict) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // *     example 1: in_array('van', ['Kevin', 'van', 'Zonneveld']);
    // *     returns 1: true
 
    var found = false, key, strict = !!argStrict;
 
    for (key in haystack) {
        if ((strict && haystack[key] === needle) || (!strict && haystack[key] == needle)) {
            found = true;
            break;
        }
    }
 
    return found;
}
