var scripttag = document.getElementsByTagName('script')[0];
wwwbase = scripttag.src.match(/(.*)public\/js.*/)[1];

var ScrollingContainer = Class.create();
ScrollingContainer.prototype = {
	scroller_id: null,
	container: null,
	up: null,
	down: null,
	current_index: null,
	elements_count: 0,
	elements: [],
	top: 0,
	initialize: function(container, options)
	{
		this.container = container;
		this.options = {
			max_visible_elements: 5
		};
		Object.extend(this.options, options || {});
		this.elements = this.container.childElements();
		this.elements_count = this.elements.length;
		this.current_index = this.options.max_visible_elements >= this.elements_count ? this.elements_count - 1 : this.options.max_visible_elements - 1;

		this.scroller_id = 'scr_' + (new Date()).getTime() + "" + Math.round(Math.random() * 1000000);
		var el = new Element('div');
		if(this.elements_count > this.options.max_visible_elements)
		{
			with(el)
			{
				this.down = (new Element('a', {href: 'javascript:;'})).addClassName('down').update('DOWN');
				insert(this.down);
				this.up = (new Element('a', {href: 'javascript:;'})).addClassName('up').addClassName('inactive').update('UP');
				insert(this.up);
			}
		}

		if(this.up)	this.up.observe('click', this.slide_up.bind(this));
		if(this.down) this.down.observe('click', this.slide_down.bind(this));
		this.container.insert({before: (new Element('div', {id: this.scroller_id})).addClassName('updown').insert(el)});
	},

	slide_up: function(e)
	{
		if(this.options.max_visible_elements <= this.current_index)
		{
			this.elements[this.current_index - this.options.max_visible_elements].show();
			this.elements[this.current_index].hide();
			this.current_index--;
		}
		this.setActiveInactiveArrows();
	},

	slide_down: function(e)
	{
		if(this.current_index + 1 >= this.options.max_visible_elements && this.elements[this.current_index + 1])
		{
			this.elements[this.current_index - this.options.max_visible_elements + 1].hide();
			this.elements[this.current_index + 1].show();
			this.current_index++;
		}
		this.setActiveInactiveArrows();
	},

	setActiveInactiveArrows: function()
	{
		if(!this.down) return;

		if(this.elements.length-1 == this.current_index) {
			this.down.addClassName('inactive');
		} else {
			this.down.removeClassName('inactive');
		}

		if(this.options.max_visible_elements > this.current_index) {
			this.up.addClassName('inactive');
		} else {
			this.up.removeClassName('inactive');
		}
	}
};

document.observe('dom:loaded', function(){

	if(Prototype.Browser.IE && !$$('div').first().id)
	{
		var clone = $$('link').first().cloneNode(true);
			clone.href = clone.href.toString().replace(/css.*/, 'css/fixes/alternative.css');
		$$('link').last().insert({after: clone});
	}

	/*$$('ul.scrolling').each(function(el){
		var el_title = el.readAttribute('title');
		if(el_title) {
			new ScrollingContainer(el, {max_visible_elements: parseInt(el_title)});
		} else {
			new ScrollingContainer(el);
		}
	});*/

	$$('table.program tbody > tr:nth-child(odd)').invoke('addClassName', 'even');

	$$('ul.tabs a').each(function(el){
		el.observe('click', function(e){
			var el = Event.element(e);
			switch($$('ul.tabs a').indexOf(el))
			{
				case 1:
					$(el.parentNode.parentNode).addClassName('second');
				break;
				default:
					$(el.parentNode.parentNode).removeClassName('second');
				break;
			}
		});
	});

	/*if ($('channel_changer')) {
		$('channel_changer').observe('change', MAG.Programme.changeChannel.bind(MAG.Programme));
	}*/

	if($('rating')) new Control.Rating('rating', {input: 'idrated', multiple: true});

	if($('terms_of_use_text') && $('terms_of_use'))  {
		$('terms_of_use').observe('click', function() {
			new Effect.Appear('terms_of_use_text');
			this.stopObserving('click');
		});
	}
});

Event.observe(window, 'load', function(e)
{
	if(Prototype.Browser.IE)
	{
		var max_h = null, els = [];
		$$('#links li').each(function(el, key){
			if(key%4 == 0)
			{
				if(els) $A(els).invoke('setStyle', {height: max_h + 'px'});
				max_h = 0;
				els = [];
			}
			max_h = Math.max(max_h, el.offsetHeight);
			els.push(el);
		});
	}

	if ($('channel_changer')) {
		$('channel_changer').observe('change', MAG.Programme.changeChannel.bind(MAG.Programme));
	}

	$$('ul.scrolling').each(function(el){
		var el_title = el.readAttribute('rel'); 
		if(el_title) { 
			new ScrollingContainer(el, {max_visible_elements: parseInt(el_title)});
		} else {
			new ScrollingContainer(el);
		}
	});

});

