var CustomSection = Class.create({
	initialize: function(section)
	{
		section.arrow.observe('section:closing', this.clear.bind(this));
		section.arrow.observe('section:opened', this.reset.bind(this));
		
		document.observe('preBox:show', this.clear.bind(this, true));
		document.observe('preBox:hide', this.reset.bind(this, true));
	},
	
	clearPosition: function(element) {
		element.setStyle({position: ''});
	},
	
	makePositioned: function(element)
	{
		element.setStyle({position: 'relative'});
	},
	
	makeStatic: function(element)
	{
		element.setStyle({position: 'static'});
	},
	
	absolutize: function(element)
	{
		element.setStyle({position: 'absolute'});
	}
});

var BreakingNewsSection = Class.create(CustomSection, {
	v_tabs: null,
	v_contents: null,
	v_content_containers: null,
	
	initialize: function($super, section)
	{
		$super(section);
		
		this.v_tabs = section.content.getElementsByClassName('video-tab');
		this.v_contents = section.content.getElementsByClassName('video-tab-content');
		this.v_content_containers = section.content.getElementsByClassName('video-content-container');
	},
	
	clear: function(ignore)
	{
		if ( ignore == undefined || ( ignore == false && !Prototype.Browser.IE) )
		{
			return;
		}
		
		this.v_tabs.each(this.clearPosition);
		this.v_contents.each(this.clearPosition);
		this.v_content_containers.each(this.clearPosition);
	},
	
	reset: function(ignore)
	{
		if ( ignore == undefined || ( ignore == false && !Prototype.Browser.IE) )
		{
			return;
		}
		
		this.v_tabs.each(this.makePositioned);
		this.v_contents.each(this.absolutize);
		this.v_content_containers.each(this.makePositioned);
	}
});

var MultimediaSection = Class.create(CustomSection, {
	t_content: null,
	t_contents: null,
	
	v_tabs: null,
	v_contents: null,
	v_content_containers: null,
	
	player: null,
	
	initialize: function($super, section)
	{
		$super(section);
		
		this.t_content = section.content.getElementsByClassName('tab-contents');
		this.t_contents = section.content.getElementsByClassName('tab-content');
		
		this.v_tabs = section.content.getElementsByClassName('video-tab');
		this.v_contents = section.content.getElementsByClassName('video-tab-content');
		this.v_content_containers = section.content.getElementsByClassName('video-content-container');
		
		this.player = $('mm-player-flowplayer');
	},
	
	loadPlayer: function(image, a_id)
	{
		if ( !image.hasClassName('video-tab-image-selected') )
		{
			return;
		}
		
		new Ajax.Request('/block.php?b_id=2844&a_id=' + a_id, {
			onSuccess: this.playerLoaded.bind(this)
		});
	},
	
	playerLoaded: function(transport)
	{
		$('mm-player-container').replace(transport.responseText);
		this.setPlayer();
	},
	
	setPlayer: function(count)
	{
		// If the player is loaded set it otherwise wait 2 seconds until it is loaded
		if ( $('mm-player-flowplayer') )
		{
			this.player = $('mm-player-flowplayer');
		}
		else
		{
			count = count || 0;
			if ( count < 40 )
			{
				count++;
				setTimeout(this.setPlayer.bind(this, count), 50);
			}
		}
	},
	
	clear: function(ignore)
	{
		this.player.setStyle({visibility: 'hidden'});
		
		this.t_content.each(this.clearPosition);
		this.t_contents.each(this.clearPosition);
		
		if ( ignore == undefined || ( ignore == false && !Prototype.Browser.IE) )
		{
			return;
		}
		
		this.v_tabs.each(this.clearPosition);
		this.v_contents.each(this.clearPosition);
		this.v_content_containers.each(this.clearPosition);
		
	},
	
	reset: function(ignore)
	{
		this.player.setStyle({visibility: ''});

		this.t_content.each(this.makePositioned);
		this.t_contents.each(this.absolutize);
		
		if ( ignore == undefined || ( ignore == false && !Prototype.Browser.IE) )
		{
			return;
		}
		
		this.v_tabs.each(this.makePositioned);
		this.v_contents.each(this.absolutize);
		this.v_content_containers.each(this.makePositioned);
	}
});

var WorldNewsSection = Class.create(CustomSection, {
	t_content: null,
	t_contents: null,
	
	tabs: null,
	
	map_container: null,
	map: null,
	continents: null,
	
	initialize: function($super, section, tabs)
	{
		$super(section);
		
		this.t_content = section.content.getElementsByClassName('tab-contents');
		this.t_contents = section.content.getElementsByClassName('tab-content');
		
		this.tabs = tabs;
		
		var handler = this.tabShown.bind(this);
		this.tabs.tabs.each(function(tab) { tab.observe('tab:show', handler) } );
		
		this.map_container = $('wn-map');
		this.map = this.map_container.down('img');
		this.continents = $('wn-map').getElementsByClassName('wn-continent');
		
		for ( var i = 0, length = this.continents.length; i < length; i++ )
		{
			if ( i != this.tabs.selected )
			{
				this.continents[i].setStyle({opacity: 0});
			}
		}
	},
	
	tabShown: function(e)
	{
		e = e || window.event;
		
		this.continentEffects(e.memo.old_index, e.memo.new_index);
	},
	
	showTab: function(index)
	{
		if ( index == this.tabs.selected )
		{
			return;
		}
		
		var old_index = this.tabs.selected;
				
		this.tabs.show(index);
		
		this.continentEffects(old_index, index);
	},
	
	continentEffects: function(old_index, new_index)
	{
		if ( Prototype.Browser.IE5_5 || Prototype.Browser.IE6 )
		{
			this.continents[old_index].setStyle({opacity: 0});
			this.continents[new_index].setStyle({opacity: 1});
		}
		else
		{
			new Effect.Fade(this.continents[old_index], {
				duration: 0.3,
				afterFinish: function()
				{
					this.continents[old_index].setStyle({opacity: 0, display: ''});
				}.bind(this)
			});
			new Effect.Appear(this.continents[new_index], {duration: 0.3});
		}
	},
	
	clear: function()
	{
		ignore = false;
		
		this.t_content.each(this.clearPosition);
		this.t_contents.each(this.clearPosition);
		
		if ( ignore == undefined || ( ignore == false && !Prototype.Browser.IE) )
		{
			return;
		}
		
		this.makeStatic(this.map_container);
		this.makeStatic(this.map);
		this.continents.each(this.makeStatic);
	},
	
	reset: function()
	{
		ignore = false;
		
		this.t_content.each(this.makePositioned);
		this.t_contents.each(this.absolutize);
		
		if ( ignore == undefined || ( ignore == false && !Prototype.Browser.IE) )
		{
			return;
		}
		
		this.makePositioned(this.map_container);
		this.absolutize(this.map);
		this.continents.each(this.absolutize);
	}	
});

var ProductListing = Class.create({
	titles: null,
	
	initialize: function()
	{
		var links = $('product-listing-index').select('a');
		this.titles = $('product-listing').select('div');
		
		// Style for Opera
		if ( Prototype.Browser.Opera )
		{
			$('product-listing').setStyle({overflow: 'auto'});
		}
		
		var count = links.length;
		
		if ( count == 0 || count != this.titles.length )
		{
			return;
		}
		
		var link
		for ( var i = 0; i < count; i++)
		{
			link = links[i];
			
			link.writeAttribute({index: i});
			link.observe('click', this.click.bind(this));
		}
	},
	
	click: function(e)
	{
		e = e || window.event;
		
		if( e.target )
		{
			var link = e.target;
		}
		else if( e.srcElement )
		{
			var link = e.srcElement;
		}
		
		if (link.nodeType == 3) // defeat Safari bug, node type 3 = text node
		{
			link = link.parentNode;
		}
		
		var index = link.readAttribute('index');
		var title = this.titles[index];
		
		$('product-listing').scrollTop = title.positionedOffset().top;
	}
});