window.addEvent('domready', function(){

	var bg = Cookie.read('background'),
		panorama = document.id('panorama');
	
	if (!bg){
		
		var bg = Number.random(1, 30);
		
		Cookie.write('background', bg/*, {duration: 1}*/);

	} else {
		
		if (Number.random(0, 9) == 1) Cookie.dispose('background');
		
	}
	
	if (panorama) panorama.addClass('bg_' + bg);
	
	
	var ticker = document.id('ticker');
	
	if (ticker){
	
		if (bg) Cookie.dispose('background');
	
		var tickText = ticker.getElement('.inner');
	
		new Request.Stocks({
			stocks: ['^SSMI', 'ABBN.VX', 'ATLN.VX', 'ADEN.VX', 'CSGN.VX', 'HOLN.VX', 'BAER.VX', 'LONN.VX', 'NESN.VX', 'NOVN.VX', 'CFR.VX', 'ROG.VX', 'SGSN.VX', 'UHR.VX', 'RUKN.VX', 'SCMN.VX', 'SYNN.VX', 'SYST.VX', 'RIGN.VX', 'UBSN.VX', 'ZURN.VX'],
			onComplete: function(yahoo){
				
				var smi, result = '';
				Array.each(Array.from(yahoo.query.results.quote), function(quote){
					if (quote.Ask) result += '<span>{Name}</span><span>{Ask} CHF</span><span>{ChangeinPercent}</span>'.substitute(quote); // <span>{Change}</span>
					else {
						quote.Trend = (quote.Change < 0) ? 'negative' : 'positive';
						smi = '<span class="{Trend}">{Name}</span><span class="{Trend}">{ChangeinPercent}</span>'.substitute(quote); // <span class="{Trend}">{Change}</span>
					}
				}, this);
				
				
				//result =
				
				tickText.set('html', smi + result);
				
				
				var bodywidth = $(document.body).getSize().x,
					tickerWidth = tickText.getSize().x;
				
				tickText.clone().getChildren().inject(tickText);
				
				tickText.set('tween', {
					duration: 60000,
					link: 'cancel',
					transition: 'linear'
				});
				
				var tickNow = function(){
					tickText.setStyle('left', 0);
					tickText.tween('left', -tickerWidth);
				}
				
				tickNow();
				
				tickNow.periodical(60000);

			},
			onRequest: function(script){
				tickText.set('html', '<span>Loading...</span>');
			}
		}).send();
		
		
	/*
		var bodywidth = document.body.getSize().x;
		
		tickerWidth = tickText.getSize().x;
		
		tickText.clone().getChildren().inject(tickText);
		
		tickText.set('tween', {
			duration: 20000,
			transition: 'linear'
		});
		
		var tickNow = function(){
			tickText.setStyle('left', 0);
			tickText.tween('left', -tickerWidth);
		}
		
		tickNow();
		
		tickNow.periodical(20000);
		*/
		
	}

});


