var dgRotator = new Class({
	Implements: Options,
	options: {
		'elID': '',
		'contClass': 'dgRotContainer',
		'thumbClass': 'dgRotThumb',
		'rDelay': 5500,
		'rsDelay': 12000,
		'tFade': '.tFade50',
		'tFull': '.tFull100',
		'tShow': '.tShow',
		'tNone': '.tNone',
		'imgpath':'/i/adRotator/',
		'morphDurr':500,
		'stylesArr':'z'
	},
	initialize: function(options){
		this.setOptions(options);
		if(this.options.elID){
			$(this.options.elID).addClass(this.options.contClass);
			this.createContainer(this.options.elID);
			this.createThumbs();
			this.createStyles();
			this.startTimer();
		} else {
			alert('Error in creating dgRotator. No elID provided!')
		}
	},
	createContainer: function(elID){
		var did = elID + "outer";
		var dc = new Element('div',{
									'class':'dgRotOuter',
									'id':did
									});
		dc.inject($(elID),"before");
		this.options.lis = $(this.options.elID).getChildren();
		var tb = new Element('div',{
							 	'class':'dgRotTextBack',
								'id':elID+'tb'
							 });
		for(l=0;l<this.options.lis.length;l++){
			var db = new Element('div',{
										'class':'dgRotThmBk tNone',
										'id': this.options.lis[l].getProperty('id')+'back'
										//'styles':{
										//	'background-image':'url("'+this.options.imgpath+this.options.lis[l].getProperty('id')+'-backimg.jpg")'
										//}
										});
			db.inject(dc);
			db.morph(this.options.tNone,{'duration':1});
			var td = new Element('div',{
								 	'class':'dgRotText',
									'id':this.options.lis[l].getProperty('id')+'text',
									'html':this.options.lis[l].get('html'),
									'styles':{
											
										}
								 });
			td.inject(dc);
			$(this.options.lis[l].getProperty('id')+'text').morph(this.options.tNone);
		}
		tb.inject(dc);
		$(elID).inject(dc);
		$$('.dgRotThmBk')[0].morph(this.options.tShow,{'duration':this.options.morphDurr});
		this.options.cThmb = this.options.lis[0].getProperty('id');
		$(this.options.cThmb).morph(this.options.tShow,{'duration':this.options.morphDurr});
	},
	createThumbs: function(){
		if(this.options.lis){
			for(l=0;l<this.options.lis.length;l++){
				this.options.lis[l].addClass(this.options.thumbClass);
				var lisAhref = this.options.lis[l].getElements('a').getProperty('href');
				this.options.lis[l].empty();
				var newA = new Element('a',{
										'href':lisAhref,
										'class':'dgRotThmA',
										'id':this.options.lis[l].getProperty('id')+'-thumbimg',
										//'styles':{
										//	'background-image':'url("'+this.options.imgpath+this.options.lis[l].getProperty('id')+'-thumbimg.jpg")'
										//},
										'events':{
											'mouseover':function(event){
												event.preventDefault();
												this.clearTimer();
												this.alterFocus($(event.target).getParent().getProperty('id'));
												}.bind(this),
											'mouseout':function(){
												this.restartTimer();
												}.bind(this),
											'click':function(){
												// nothing
												}
										}
							});
				newA.inject(this.options.lis[l]);
				this.options.lis[l];
				$(this.options.lis[l].getProperty('id')).morph(this.options.tFade);
			}
			this.options.cThmb = this.options.lis[0].getProperty('id');
			$(this.options.cThmb).morph(this.options.tFull);
			$(this.options.cThmb+'text').morph(this.options.tShow);
		}
	},
	alterFocus: function(newEl){
		$(this.options.elID).getElementById(newEl).morph(this.options.tFull,{'duration':this.options.morphDurr});
		$(this.options.cThmb).morph(this.options.tFade,{'duration':this.options.morphDurr});

		$(newEl+'back').morph(this.options.tShow,{'duration':this.options.morphDurr});
		$(this.options.cThmb+'back').morph(this.options.tNone,{'duration':this.options.morphDurr});

		$(newEl+'text').morph(this.options.tShow,{'duration':this.options.morphDurr});
		$(this.options.cThmb+'text').morph(this.options.tNone,{'duration':this.options.morphDurr});

		this.options.cThmb = $(this.options.elID).getElementById(newEl).getProperty('id');
	},
	createStyles: function(){
		var sl = eval(advertStyles);
		for(s=0;s<sl.styles.length-1;s++){
			var sellarge = sl.styles[s].id + "back";
			var selthumb = sl.styles[s].id + "-thumbimg";
			$(sellarge).setStyle('background-image','url(' + sl.styles[s][sellarge] + ')');
			$(selthumb).setStyle('background-image','url(' + sl.styles[s][selthumb] + ')');
		}
	},
	startTimer: function(){
		if(this.options.timer){
			$clear(this.options.timer);
		}
		this.options.timer = function(){this.nextAd()}.bind(this).periodical(this.options.rDelay);
	},
	restartTimer: function(){
		this.options.timer = function(){this.startTimer()}.bind(this).periodical(this.options.rsDelay);
	},
	clearTimer: function(){
		$clear(this.options.timer);
	},
	nextAd: function(){
		if( $(this.options.cThmb).getNext('li')){
			this.alterFocus($(this.options.cThmb).getNext('li').getProperty('id'));
		} else {
			this.alterFocus(this.options.lis[0].getProperty('id'))
		}
	}
});
