window.addEvent('domready', function() {

	$('listen_on').addEvent('click', function(e) {
		e.stop();
		window.open('player.html','iGolFM','width=300,height=150,resizable=no,scrollbars=no,toolbars=no,location=no,status=no,menubar=no');
	});

	var links = $$('.days li a');	
	var duration = 3; // in seconds
	var current;
	
	var expo = function(x) {
		return Math.pow(2,8*(x-1));
	}
	
	var exponents = new Array();
	for(i=0;i<=100;i++)
	{
		exponents[i] = expo(i/100);
	}

	var animate = function() {
		this.morph({'margin-left': -318});
	};
	
	var height = function(day_id) {
		var new_height = ($('d'+day_id)) ? $('d'+day_id).getChildren().length*52 : 0;
		if (new_height<200) new_height=159;
		$('program_elements').morph({'height': new_height, 'duration': 'short'});
	};
	
	var init = function(day_id) {
		current = day_id;
		var new_height = ($('d'+day_id)) ? $('d'+day_id).getChildren().length*52 : 0;
		if (new_height<200) new_height=159;		
		$('program_elements').setStyle('height', new_height);
		$('l'+current).addClass('selected');
		$$('#d'+current+' li').each(function(el){
				el.setStyle('margin-left', -318);
		});
		$('program_date').set('text', $('l'+current).get('text'));
	}
	
	// initialize
	init ($$('.days .current')[0].get('id').replace('l',''));
	
	links.each(function(link, index) {
		link.addEvent('click', function(e) {
			e.stop();
			var day_id = index+1;

			if (current == day_id) 
			{
				return true;
			}
			
			height(day_id);
			
			$$('#d'+current+' li').each(function(el){
				el.morph({'margin-left': '0', 'duration': 'short'});
			});

			$('l'+current).removeClass('selected');			
			$('l'+day_id).addClass('selected');

			$('program_date').set('text', $('l'+day_id).get('text'));
			
			current = day_id;
			var elements = $$('#d'+day_id+' li');
			var count = elements.length;
			var times = new Array(); 	
			var diff = Math.floor(100/count);
			
			elements.each(function(el,key){		
				animate.delay(((exponents[(key+1)*diff]*duration)/exponents[(count*diff)])*100,el);
			});
		});
	});
	
	var rotatePhotos = function()
	{
		var current = this.getFirst('.current');
		var new_current = (current.getNext()) ? current.getNext() : this.getFirst();
		current.removeClass('current');
		current.set('tween', {duration: 'long'});
		current.fade('out');
		new_current.set('tween', {duration: 'long'});		
		new_current.addClass('current');
		new_current.fade('in');		
	}
	
	var rotateEditors = function()
	{
		var current = this.getFirst('.current');
		var new_current = (current.getNext()) ? current.getNext() : this.getFirst();
		current.removeClass('current');
		new_current.addClass('current');	
	}

	$$('div.photos').each(function(div){
		var images = div.getChildren();
		var count = images.length;
		images[count-1].addClass('current').setStyle('visibility', 'visible');
		if (count<2) return true;		
		rotatePhotos.periodical(5000, div);
	});
	
	var editors = $('onair_editors').getChildren();
	var count = editors.length;
	editors[count-1].addClass('current');				
	if (count>1)
	{
		rotateEditors.periodical(5000, $('onair_editors'));	
	}
	
	
	var initTips = function ()
	{
		// Store titles and text  
		$$('.t').each(function(element,index) {  
			var content = element.get('title').split('::');  
			element.store('tip:title', content[0]);  
			element.store('tip:text', content[1]);  
		});  

		var tips = new Tips($$('.t'), {
			onShow: function(tip) {
					tip.setStyle('z-index',200);
					tip.fade(0.9)
				},
			onHide: function(tip) {tip.fade(0)}
		});
	}
	
	initTips();
	
});

