var homeScroller;
var trystatus;
var statusFade;
var newsFade;

Date.prototype.getDayName = function(shortName) {
	   var Days = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
	   if (shortName) {
	      return Days[this.getDay()].substr(0,3);
	   } else {
	      return Days[this.getDay()];
	   }
	}

	Date.prototype.getMonthName = function() {
	   return ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'][this.getMonth()]; 
	}

window.addEvent('load', function() { 
	if($('timedate')) { 
		//class="CoolClock:chunkySwiss:11:noSeconds" id="c6" width="22" height="22" style="width: 22px; height: 22px;"
		//var can = new Element('canvas', {'class': "CoolClock:chunkySwiss:11:noSeconds", 'id': "c6", 'width': "22", 'height': "22", 'style': "width: 22px; height: 22px;"});
		//can.inject($('clock'));
		CoolClock.findAndCreateClocks();
	}
});
window.addEvent('domready', function() {
	homeScroller = new rsScroller({container: 'imagerotatorhome'});
	
	if(trystatus = $('linestatus')) {
		fade = 0;
		if(trystatus.getChildren('div').length > 1) {
		trystatus.getChildren('div').each(function(item) {
			trystatus.setStyles({'position': 'relative', 'height': (item.getSize().y.toInt()+9), 'overflow': 'hidden'});
			item.setStyles({'position': 'absolute'});
			
		});
		fadeStatus(0);
	} }
	if($('newsscroller')) {
		$$('#newsscroller .item').each(function(item) { item.set('opacity', 0).setStyle('position', 'absolute'); });
		changeNews(0);
	}
	$$('#header h1').each(function(item) { item.setStyle('cursor', 'pointer'); item.addEvent('click', function() { window.location = '/'; }); });
	
	
	if($('text_enlarge')) {
		$('text_enlarge').setStyle('cursor', 'pointer').addEvent('click', function() { $(document.body).setStyle('font-size', '14px'); Cookie.dispose('textFont'); Cookie.write('textFont', '14px', {domain: 'luas.ie', path: '/'}); });
		$('text_reduce').setStyle('cursor', 'pointer').addEvent('click', function() { $(document.body).setStyle('font-size', '12px'); Cookie.dispose('textFont'); Cookie.write('textFont', '12px', {domain: 'luas.ie', path: '/'}); });
		if(Cookie.read('textFont')) { $(document.body).setStyle('font-size', Cookie.read('textFont')); }
	}
	
	//Accordion
	//if($$('')) { var myAccordion = new Fx.Accordion(togglers, elements); }
	
	//Clock
	if($('timedate')) {
		var funTime = (function() {
			var today = new Date();
			var h=today.getHours();
			if(h > 11){
				postfix = "PM";
			} else {
				postfix = "AM";
			}
			if(h > 12) {
				var h = h - 12;
			}
			var m=today.getMinutes();
			var s=today.getSeconds();
			var d = today.getDayName();
			var da = today.getDate();
			var ma = today.getMonthName();
			
			if (m < 10){
				m = "0" + m;
			}

			$$('#timedate .date').set('html', d+' '+da+' '+ma+' '+today.getFullYear());
			$$('#timedate .time').set('html', '<span>'+postfix+'</span>'+h+':'+m);
		}).periodical(500);
	}	
});


function changeNews(itemnum) {
  items = $$('#newsscroller .item');
  if(items.length <= itemnum) {
    itemnum = 0;
  }
  items.each(function(item, i) {
  if(i != itemnum) {
    item.tween('opacity', 0);
  }
  });
  items[itemnum].tween('opacity', 1);
  itemnum = itemnum+1;
  newsFade = setTimeout('changeNews('+itemnum+')', 10000);
}

function fadeStatus(way) {
	if(way == 0) {
		red = 0;
		green = 1;
		way = 1;
	} else {
		red = 1;
		green = 0;
		way = 0;
	}
	
	trystatus.getChildren('div').each(function(item, i) {
		if(i == 0) {
			item.tween('opacity', red);
		} else {
			item.tween('opacity', green);
		}
	});
	
	statusFade = setTimeout('fadeStatus('+way+')', 5000);
}

var rsScroller = new Class({
	options: {
		//Basic
		container: false,
		baseClass : 'rsScroller',
		
		//Items
		Items : '.rsScroller-item',
			
			//Inside Items
			Title : 'h4',
			Image : '.rsScroller-image',
			Icon : '.rsScroller-icon',
			Text : 'a',
			Link : 'a',
			Extras : 'span',
		
		//Arrows
		upArrow : {'src': '/assets/graphics/rsScroller/rsUp.jpg', 'styles': {'cursor': 'pointer'}},
		downArrow : {'src': '/assets/graphics/rsScroller/rsDown.jpg', 'styles': {'cursor': 'pointer'}},
		more: '/assets/graphics/rsScroller/rsMore.jpg',
		
		way : 'vertical',
		position : 'right',
		max : 6,
		
		//Clear Item
		clear : new Element('br', {'clear': 'clearfloat'}),
		
		//Styles
		pictureBox : {'position': 'relative', 'float': 'left', 'width': 440, 'overflow': 'hidden'},
		iconBox : {'position': 'relative', 'float': 'right', 'width': 60, 'text-align': 'center'},
		iconWrap : {'width': 60, 'margin-top': 7, 'margin-bottom': 7, 'position': 'relative', 'overflow': 'hidden'},
		icons : {'margin-bottom': 7, 'cursor': 'pointer', 'z-index': 16, 'left': '4px'}
	},
	items : [],
	item : { height : false,
			width : false,
			current : 0
	},
	container : false,
	containerSizes : false,
	
	//Icon Wrap
	iconWrap : false,
	iconMover : false,
	btnUp : false,
	btnDown : false,
	middle : false,
	iconMoverEffect : false,
	//Picture Box
	pictureBox : false,
	iconBox : false,
	iconHeight : 57,
	intervalOb : false,
	intervalTime : 5000,
	/*
	pass the options,
	create html and inject into container
	*/
	initialize: function(options){
		this.setOptions(options);
		this.container = $(this.options.container);
		if(this.container) {
			this.pullItems();
			
			this.container.addEvents({
				'mouseenter': function() { $clear(this.intervalOb); }.bind(this),
				'mouseleave': function() { this.intervalOb = (function() { this.loadNextItem(); }).delay(intervaltime, this); }.bind(this)
			});
		}
	},
	
	pullItems: function() {

		this.setStage();
		//Get all the items in the scroller
		$$('#'+this.options.container+' '+this.options.Items).each(function(el, index) {
			var wrapEl = new Element('div', {'styles': {'position': 'absolute'}});
			wrapEl.wraps(el);
			
			
			//Link of the show
			if(this.options.Link == 'a') {
				var itemLink = (tryfind = el.getElement('a')) ? tryfind.get('href') : false;
			} else {
				var itemLink = (tryfind = el.getElement(this.options.Link)) ? tryfind.get('text') : false;
			}
			el.getElement('a').mask();
			el.addEvent('click', function() { 
				window.location = itemLink;
			});
			
			
			
			//Get the item text
			var itemText = (tryfind = el.getElement(this.options.Text)) ? tryfind.get('html') : false;
			//Title of the item
			var itemTitle = (tryfind = el.getElement(this.options.Title)) ? tryfind.get('html') : itemText;
			if(tryfind = el.getElement(this.options.Title)) { tryfind.setStyle('display', 'none'); }
			
			
			//Get the main image
			var itemImage = (tryfind = el.getElement(this.options.Image)) ? tryfind.clone() : false;
			this.options.icons.top = (index*this.iconHeight);
			
			var itemIcon = (tryfind = el.getElement(this.options.Icon)) ? tryfind.setStyles(this.options.icons) : false;
			
			var myTips = new Tips(itemIcon, {'fixed': true,
					'onShow': function(tip) {
			    		tip.fade('in');
					},
					'onHide': function(tip) {
					    tip.fade('out');
					}
			});

			
			itemIcon.inject(this.iconMover);
			itemIcon.store('item', index);
			itemIcon.store('tip:title', itemTitle);
			itemIcon.setStyle('border', 'solid 1px #FFFFFF');
			itemIcon.setStyle('border-left', 'none');
			itemIcon.addEvents({
					'click' : function() {
						/*fin = itemIcon.retrieve('item');
						this.moveItem(fin);*/
						window.location = itemLink;
					}.bind(this)/*,
					'mouseenter' : function() {
						this.tween('border-color', '#412570');
						this.setStyle('border-left', 'none');
					}, 
					'mouseleave' : function() {
						this.tween('border-color', '#FFFFFF');
						
					}*/
			});
			itemIcon.fireEvent('mouseleave');
			
			//Get Object Extras
			var itemExtras;
			if(itemExtras = el.getElement(this.options.Extras)) {
				itemExtras = itemExtras.get('html');
				itemExtras = eval('new Hash({'+itemExtras+'});');
				wrapEl.store('extras', itemExtras);
			}
			
			//Put them all in an items object
			var nHash = new Hash ({'Title': itemTitle, 
							'Text': itemText, 
							'Link': itemLink,
							'Image': itemImage,
							'Icon': itemIcon,
							'Extras' : itemExtras});
			this.items.push(wrapEl);
			wrapEl.set('opacity', 0);
			wrapEl.inject(this.pictureBox);
			
		}.bind(this));
		this.moveItem(0);
		
		$(this.options.container).setStyles({'margin-left': '0px', 'padding-right': '0px', 'background': 'transparent'});	
	}, 
	
	loadNextItem : function() {
		this.moveItem(this.item.current+1);
	},
	
	moveItem : function(item) {
		$clear(this.intervalOb);
		if(item < 0) {
			item = this.items.length+item;
		} else if (item >= this.items.length) {
			item = item-this.items.length;
		}
		
		this.adjustIcons(item);
		
		this.items.each(function(ela, elaindex) { 
			if(elaindex != item) { 
				ela.tween('opacity', 0); 
			} else {
				nomove = false;
				if(extras = ela.retrieve('extras')) {
					
					if(intervaltime = extras.intervalTime) {
						
						intervaltime = extras.intervalTime;
					} else {
						intervaltime = this.intervalTime;
					}
					
					//Pause the Item
					if(extras.nomove) {
						nomove = true;
					}
				} else {
					intervaltime = this.intervalTime;
				}
				
				if(!nomove) {
					this.intervalOb = (function() { this.loadNextItem(); }).delay(intervaltime, this);
				}
			}
		}.bind(this));
		
		
		this.iconMover.getElements('img').each(function(ela, elaindex) {
			ela.setStyles({'position': 'absolute', 'left': 4});
			if(elaindex != item) {
				offset = elaindex - item;
				 if(offset < 0) {
					offset = this.items.length-item+elaindex;
					mv = (offset*this.iconHeight);
					//ela.retrieve('iconFx').start({'opacity': 0.5});
				} else { 
					mv = (offset*this.iconHeight);
					//ela.retrieve('iconFx').start({'opacity': 0.5});
				 } 
			} else {
				mv = 0;
			}
			ela.setStyle('top', mv);
		}.bind(this)); /**/
		
		
		mv = (item*this.iconHeight)*(-1);
		//this.iconMoverEffect.start({'top': mv});
		
		
		
		this.items[item].tween('opacity', 1);
		this.item.current = item;	
		
	},
	
	adjustIcons : function(item) {
		return true;
	},
	
	setStage : function() {
		//Container Sizes
		this.containerSizes = {'width': 507, 'height': 376, 'x': 507, 'y': 376}; 
		//var newContainer = new Element('div', {'styles': {'width': this.containerSizes.x, 'height': this.containerSizes.y, 'overflow': 'hidden'}});
		var newContainer = new Element('div', {'styles': {'width': 507, 'height': 376, 'overflow': 'hidden'}});
		newContainer.inject(this.container);
		this.container = newContainer;
		
		//Picture Box
		this.pictureBox = new Element('div');
		pboxStyles = this.setDefaultSize(this.options.pictureBox);
		this.pictureBox.setStyles(pboxStyles);
		this.pictureBox.inject(this.container);
		
		//Icon Box
		this.iconBox = new Element('div');
		iconBoxStyles = this.setDefaultSize(this.options.iconBox);
		this.iconBox.setStyles(iconBoxStyles);
		this.iconBox.inject(this.container);
			//Icon Wrap and Arrows
				//Change Height
				icoSt = this.setDefaultSize(this.options.iconWrap);
				icoSt.height -= 24+14;
			this.iconWrap = new Element('div', {'styles': icoSt});
			this.iconMover = new Element('div', {'styles': {'width': icoSt.width, 'height': icoSt.height}})
			//IconMoverFx
			this.iconMoverEffect = new Fx.Morph(this.iconMover, {'link': 'cancel'});
			
			this.btnUp = new Element('img', this.options.upArrow);
			this.btnUp.addEvent('click', function() { this.moveItem(this.item.current+1); }.bind(this));
			
			this.btnDown = new Element('img', this.options.downArrow);
			this.btnDown.addEvent('click', function() { this.moveItem(this.item.current-1); }.bind(this));
			
			this.btnUp.inject(this.iconBox);
			this.iconMover.inject(this.iconWrap);
			this.iconWrap.inject(this.iconBox);
			this.btnDown.inject(this.iconBox);
		
		//Clear Icon
		this.options.clear.clone().inject(this.container);
	},
	
	setDefaultSize : function(itemStyles) {
		if (!itemStyles.height) {
			itemStyles.height = this.containerSizes.y;
		}
		if (!itemStyles.width) {
			itemStyles.width = this.containerSizes.x;
		}
		return itemStyles
	}

});
rsScroller.implement(new Events);
rsScroller.implement(new Options);
