var tempdata = [];
window.addEvents({
	'domready': function() {

		var waitTime = 3; //Wait time in seconds untill the banner will switch to the next one
		
		$('newsmenu').getChildren()[1].addClass('next');
		//News block
		$$('#newsmenu li a').each(function(li, num) {
			li.addEvent('click', function(e) {
				if (e) {
					e = new Event(e).stop();
					$$('#newsmenu li').removeClass('next');
					if (this.getParent().getNext()) this.getParent().getNext().addClass('next');
					$('viewall').setStyle('visibility', 'hidden');
				}
	
				$$('#newsmenu li').removeClass('active');
				$$('#newsmenu li')[num].addClass('active');
				$('IE_is_a_LOL').empty().addClass('loading');
				
				var href = li.get('href');
				if (!tempdata[num]) {
					new Request({
						url: '/spr_data.php',
						method: 'post', 
						data: {'link': href, 'lang_id': global.lang.id, 'lang_prefix': global.lang.prefix}, 
						onSuccess: function(r) {
							$('IE_is_a_LOL').removeClass('loading');
							r = r.split('|||');
							//Save the response in an array so that we don't need to do an ajax request all the time
							tempdata[num] = [r[0], r[1]];
							if (tempdata[num][1] != '') {
								$('viewall').setStyle('visibility', 'visible').set('href', tempdata[num][1]);
							}
							$('IE_is_a_LOL').set('html', tempdata[num][0]);					
						}
					}).send();
				} else {
					var d = tempdata[num][0];
					$('IE_is_a_LOL').set('html', d).removeClass('loading');
					if (tempdata[num][1] != '') {
						$('viewall').setStyle('visibility', 'visible').set('href', tempdata[num][1]);
					}
				}
			});
		});
		$$('#newsmenu li a')[0].fireEvent('click');
		//End
		//var bannerBox = new Fx.Scroll('introscroll');
		//var pos = 0;
		if (global.version == 0) {
						
			var banner_i = 1, state = false;	
			$$('#picture li').each(function(li, num) {
				li.addEvent('click', function(e) {
					if (e) {
						banner_i = (num == $('picture').getElements('li').length-1) ? 0 : num+1;
						var e = new Event(e).stop();
						changer = $clear(changer);
					} else { banner_i++; }
					
					$('picture').getElements('li').removeClass('active');			
					this.addClass('active');
					
					var image = li.getFirst().get('rel');
					var href = li.getFirst().get('href');
					var title = li.getFirst().get('title');
					var target = li.getFirst().get('target');
					
					var el = $('pictures').getFirst().getFirst();
					var link = $('pictures').getFirst();
					var tween = new Fx.Tween(el, {
						onStart: function() {
							link.set({'href': href, 'target': target});
						},
						onComplete: function() {
							el.set({'src': image, 'alt': title, 'title': title});
							el.fade('in');
							state = false;
						}
					});
					if (!state) {
						state = true;
						tween.start('opacity', 0);	
					}
				})
			});
			
			if ($$('#picture li').length > 1) {
				var changer = function() {
					var el = $$('#picture li')[banner_i];
					el.fireEvent('click');	
					if (banner_i == $$('#picture li').length) banner_i = 0;
				}.periodical(waitTime * 1000);
			}
		}
	},
	
	'load': function() {
	
		$('pagecontent').setStyle('height', $('startcontent').getStyle('height').toInt()-41 + 'px');
		
		if (global.version == 0) {
			Element.Events.mousehold = {
				base: "mousedown",
				condition: function(event) {
					event = new Event(event);
					if (event.relatedTarget != this && !this.hasChild(event.relatedTarget)) {
						var mousedown = $time(); //checker
						var ms = 100; //time in ms to react to 'hold' effect
						
						var cleanup = function() {
							timer = $clear(timer);
							this.removeEvent("mouseleave", cleanup);
						}.bind(this);
						
						var held = function() {
							this.fireEvent('mousehold', event);
						}.bind(this);
						
						this.addEvent("mouseup", cleanup);					
						
						var timer = function(){
							if (mousedown + ms < $time()) held();
						}.periodical(ms);						
					}
				}
			};
		
			/*var init_max = 200;		
			var waitTime = 1; //Wait time in seconds untill the banner will switch to the next one
			var el = $$('#introscroll ul')[0];
			el.setStyle('right', 0);
			var moveFx = new Fx.Tween(el, {duration: 'short'});
			var wrapperWidth = $('introscroll').getStyle('width').toInt()
			var total = 20;
			var direction = 0;
			$$('#introscroll ul li a img').each(function(el) {
			    total += el.offsetWidth;
			})
			
			var slider = {
				'right': function(e) {
					var max = init_max;
					var pos = el.getStyle('right').toInt();
					if (pos + max >= total-wrapperWidth) {
						max = (total-wrapperWidth) - pos;
					}
		
					var left = (total-wrapperWidth)-max;
					//var left = (wrapperWidth-pos-max)-max;
					
					if (left < 100) {
						max = max+left;						
					} 
					
					moveFx.start('right', pos, pos+max);
					if (e) $clear(scrollUpper);
				},
				
				'left': function(e) {
					
					var max = init_max;
					var pos = el.getStyle('right').toInt();
					if ((pos - max) < 0) {
						max = pos;
					}
					
					var left = pos-max;
					if (left < 100) {
						max = max+left;
					} 
					
					moveFx.start('right', pos, pos-max);
					if (e) $clear(scrollUpper);
				}
			}
			
			$('scrollnavright').addEvents({'mousehold': slider.right, 'mousedown': slider.right});		
			$('scrollnavleft').addEvents({'mousehold': slider.left, 'mousedown': slider.left});	
			if (total-wrapperWidth > 50) {
				var scrollUpper = function() {
					var pos = el.getStyle('right').toInt()
					if (direction == 0) {
						$('scrollnavright').fireEvent('mousedown');
						if (wrapperWidth-total+pos == 0) { direction = 1 }
					}
					
					if (direction == 1) {
						$('scrollnavleft').fireEvent('mousedown');
						if (pos == 0) { direction = 0 }
					}
				}.periodical(waitTime * 1000);	
			}*/
		}
	}
});
