
// install top dropdown
var musicDropdownMoving = false;
function installMusicTypeDropdown()
{
	
	var pos2 = $$('.musicType')[0].positionedOffset();
	var posX = pos2[0];
	var posY = pos2[1];
	
	Event.observe('musicType', 'click', function(event)
	{
		var display = $('itensList').style.display;
		if(display != 'none')
		{
			musicDropdownMoving = true;
			Effect.SlideUp('itensList', { afterFinish: function() { musicDropdownMoving = false; }});
		}
		else
		{
			musicDropdownMoving = true;
			Effect.SlideDown('itensList', { afterFinish: function() { musicDropdownMoving = false; } });
		}
	});
	
	
	$('itensList').style.left = posX + 'px';
	$('itensList').style.top = posY + 23 + 'px';
	
	Event.observe('itensList','mouseleave', function(event)
	{
		if(!musicDropdownMoving)
		{
			musicDropdownMoving = true;
			Effect.SlideUp('itensList', { afterFinish: function() { musicDropdownMoving = false; }});
		}
	});
	
	$$('#itensList a').each(function(e)
	{
		e.observe('click',function()
		{
			if(!musicDropdownMoving)
			{
				$$('#itensList a').each(function(e) { e.removeClassName('selected'); } );
				this.addClassName('selected');
				$('choosenType').innerHTML = this.innerHTML;
				musicDropdownMoving = true;
				Effect.SlideUp('itensList', { afterFinish: function() { musicDropdownMoving = false; }});
			}
		});
	});
}

// install side dropdown
var sideDropdownMoving = false;
function installSideDropdown()
{
	var pos2 = $$('.sideMusicType')[0].positionedOffset();
	var posX = pos2[0];
	var posY = pos2[1];
	
	Event.observe('sideMusicType', 'click', function(event)
	{
		var display = $('sideItensList').style.display;
		if(display != 'none')
		{
			sideDropdownMoving = true;
			Effect.SlideUp('sideItensList', { afterFinish: function() { sideDropdownMoving = false; }});
		}
		else
		{
			sideDropdownMoving = true;
			Effect.SlideDown('sideItensList', { afterFinish: function() { sideDropdownMoving = false; } });
		}
	});
	
	
	$('sideItensList').style.left = posX + 'px';
	$('sideItensList').style.top = posY + 23 + 'px';
	
	Event.observe('sideItensList','mouseleave', function(event)
	{
		if(!sideDropdownMoving)
		{
			sideDropdownMoving = true;
			Effect.SlideUp('sideItensList', { afterFinish: function() { sideDropdownMoving = false; }});
		}
	});
	
	$$('#sideItensList a').each(function(e)
	{
		e.observe('click',function()
		{
			if(!sideDropdownMoving)
			{
				$$('#sideItensList a').each(function(e) { e.removeClassName('selected'); } );
				this.addClassName('selected');
				$('sideChoosenType').innerHTML = this.innerHTML;
				sideDropdownMoving = true;
				Effect.SlideUp('sideItensList', { afterFinish: function() { sideDropDownMoving = false; }});
			}
		});
	});
}



// install info tooltip
function installInfoTooltip()
{	
	
	Event.observe('imgInfo','mouseenter', function(event)
	{
		$$('.titRel')[0].style.zIndex = 1000;
		$('favInfo').appear();
	});
	
	Event.observe('imgInfo','mouseleave', function(event)
	{
		$('favInfo').fade({duration: 1, from: 1, to : 0, afterFinish: function() { $$('.titRel')[0].style.zIndex = 100; }});
	});
}

// navigation
var _hlActive = 1;
var _hlWidth = 598;

function installNavigation()
{
	
	Event.observe('prev','click', function(event)
	{
		goPrevHighlight(1)
	});
	
	Event.observe('next','click', function(event)
	{
		goNextHighlight(1)
	});
	
	$$('.pages a').each(function(s, index)
	{
		s.observe('click',function()
		{
			goToPage(index + 1);
		});
	});
}

var sliderMoving = false;
function goNextHighlight(difference)
{
	if(_hlActive == 3 || sliderMoving)
		return;
	
	sliderMoving = false;
	var distance = 598;
	if(!difference)
		_hlActive += 1;
	else
	{
		distance = 598 * difference;
		_hlActive += difference;
	}
	
	new Effect.Move('highlightSlider', {
				x: -distance,y :0,
				afterFinish: function()
				{
					$$('.pages a').each(function(s) {
						s.removeClassName('on');
					});
					$$('.pages a')[_hlActive-1].addClassName('on');
					sliderMoving = false;
				}
	});
}

function goPrevHighlight(difference)
{
	if(_hlActive == 1 || sliderMoving)
		return;
	
	sliderMoving = true;
	var distance = 598;
	if(!difference)
		_hlActive -= 1;
	else
	{
		distance = 598 * difference;
		_hlActive -= difference;
	}
	
	new Effect.Move('highlightSlider', {
				x: distance,y:0,
				afterFinish: function()
				{
					$$('.pages a').each(function(s) {
						s.removeClassName('on');
					});
					$$('.pages a')[_hlActive-1].addClassName('on');
					sliderMoving = false;
				}
	});
}

function goToPage(index)
{
	if(index < _hlActive)
	{
		goPrevHighlight((index - _hlActive) * - 1);
	}
	else if(index > _hlActive)
	{
		goNextHighlight(index - _hlActive);
	}
}


// install country dropdown
var countryMoving = false;
function installCountryDropdown()
{
	var pos2 = $$('.pickCountry')[0].positionedOffset();
	var posX = pos2[0];
	var posY = pos2[1];
	
	Event.observe('pickCountry', 'click', function(event)
	{
		var display = $('countryList').style.display;
		if(display != 'none')
		{
			countryMoving = true;
			Effect.SlideUp('countryList', { afterFinish: function() { countryMoving = false; }});
		}
		else
		{
			countryMoving = true;
			Effect.SlideDown('countryList', { afterFinish: function() { countryMoving = false; } });
		}
	});
	
	
	$('countryList').style.left = posX + 'px';
	$('countryList').style.top = posY + 23 + 'px';
	
	Event.observe('countryList','mouseleave', function(event)
	{
		if(!countryMoving)
		{
			countryMoving = true;
			Effect.SlideUp('countryList', {afterFinish: function() { countryMoving = false; }});
		}
	});
	
	$$('#countryList a').each(function(e)
	{
		e.observe('click',function()
		{
			if(!countryMoving)
			{
				$$('#countryList a').each(function(e) { e.removeClassName('selected'); } );
				this.addClassName('selected');
				$('choosenCountry').innerHTML = this.innerHTML;
				countryMoving = true;
				Effect.SlideUp('countryList', { afterFinish: function() { countryMoving = false; }});
			}
		});
	});
}

var continentMoving = false;
function installContinentDropdown()
{
	
	
	var pos2 = $$('.pickContinent')[0].positionedOffset();
	var posX = pos2[0];
	var posY = pos2[1];
	
	Event.observe('pickContinent', 'click', function(event)
	{
		var display = $('continentList').style.display;
		if(display != 'none')
		{
			continentMoving = true;
			Effect.SlideUp('continentList', { afterFinish: function() { continentMoving = false; }});
		}
		else
		{
			continentMoving = true;
			Effect.SlideDown('continentList', { afterFinish: function() { continentMoving = false; } });
		}
	});
	
	$('continentList').style.left = posX + 'px';
	$('continentList').style.top = posY + 23 + 'px';
	
	Event.observe('continentList','mouseleave', function(event)
	{
		if(!continentMoving)
		{
			continentMoving = true;
			Effect.SlideUp('continentList', { afterFinish: function() { continentMoving = false; }});
		}
	});
	
	$$('#continentList a').each(function(e)
	{
		e.observe('click',function()
		{
			if(!continentMoving)
			{
				$$('#continentList a').each(function(e) { e.removeClassName('selected'); } );
				this.addClassName('selected');
				$('choosenContinent').innerHTML = this.innerHTML;
				continentMoving = true;
				Effect.SlideUp('continentList', { afterFinish: function() { continentMoving = false; }});
			}
		});
	});
	
	
}

function installTopPlaceholder()
{
	Event.observe('searchByName','focus', function(event)
	{
		if(this.value == 'Nome da Rádio ou Programa')
		{
			this.value = '';	
		}
	});
	
	Event.observe('searchByName','blur', function(event)
	{
		if(this.value == '')
		{
			this.value = 'Nome da Rádio ou Programa';	
		}
	});
}


function installSidePlaceholder()
{
	
	Event.observe('sideSearchByName','focus', function(event)
	{
		if(this.value == 'Nome da Rádio ou Programa')
		{
			this.value = '';	
		}
	});
	
	Event.observe('sideSearchByName','blur', function(event)
	{
		if(this.value == '')
		{
			this.value = 'Nome da Rádio ou Programa';	
		}
	});
}



function installDirectoryListener()
{
	Event.observe('btnDirByCountry', 'click', function(event)
	{
		var display = $('dirByType').style.display;
		if (display == 'block' || display == '')
		{
			$('btnDirByType').removeClassName('on');
			$('btnDirByCountry').addClassName('on');
			$('dirByCountry').show();
			$('dirByType').hide();
		}
	});
	
	Event.observe('btnDirByType', 'click', function(event)
	{
		var display = $('dirByCountry').style.display;
		if (display == 'block' || display == '')
		{
			$('btnDirByCountry').removeClassName('on');
			$('btnDirByType').addClassName('on');
			$('dirByType').show();
			$('dirByCountry').hide();
		}
	});
}


//install area pickers
function installAreaListener()
{
	$$('.areaChooser a').each(function(s, index)
	{
		s.observe('click',function()
		{	
			$$('.areaChooser a').each(function(e) { e.removeClassName('on'); });
			this.addClassName('on');	
			$$('.areaList').each(function(e) { e.hide(); });
			$$('.areaList')[index].show();
		});
	});
}


function installRadioSearch()
{
	$('radio-search').observe('submit',function(e) {
		Event.stop(e);
	
		var tmp = this.serialize(true);
		location.href = '/radio/?q=' + tmp.q;
	});
	
	$('radio-search-cat').observe('click',function(e) {
		Event.stop(e);
	
		$$('#itensList a').each(function(e) { 
			if (e.classNames().any(function(n) { return 'selected' == n; })) {
				location.href = '/radio/' + e.readAttribute('id');
			}
		});
	});
	
}


function installSideRadioSearch()
{
	$('radio-side-search').observe('submit',function(e) {
		Event.stop(e);
	
		var tmp = this.serialize(true);
		location.href = '/radio/?q=' + tmp.q;
	});
	
	
	$('radio-side-search-cat').observe('click',function(e) {
		Event.stop(e);
	
		$$('#sideItensList a').each(function(e) { 
			if (e.classNames().any(function(n) { return 'selected' == n; })) {
				//location.href = '/radio/' + e.readAttribute('id');
				var tmp = e.readAttribute('id').split('-');
				tmp.shift();
				location.href = '/radio/' + tmp.join('-');
			}
		});
	});
}



function installRadioFavorites()  {
	
	$$('a.star').each(function(star) { 
		
		star.observe('click',function(e) {
			Event.stop(e);
			var tmp = $(this).readAttribute('href').split('#');
			
			new Ajax.Request('/radio/?favorite=' + tmp[1], {
				  method: 'get',
				  onSuccess: function(transport) {
					if(transport.responseJSON) {
						if (transport.responseJSON.action) {
							if (transport.responseJSON.action == 'add') {								
								this.addClassName('starOn');
								
								var html = '<div class="destaque300-1 right-fav">'+
									       '<a href="/radio/'+ transport.responseJSON.station.id +'">'+ transport.responseJSON.station.name +'</a><a href="#'+ transport.responseJSON.station.id +'" title="Remover" class="remover">Remover</a>'+
									       '</div>';
								
								$('side-radio-favorites').insert ({'bottom': html}); 

								$$('#side-radio-favorites a.remover').each(function(a) { 
									a.observe('click', removeRadioFavorite);
								});								
							} else {
								this.removeClassName('starOn');
								
								$$('#side-radio-favorites div').each(function(d, index) {
									if (index == transport.responseJSON.station) {
										d.remove();
										throw $break;
									}
								});
							}
						} else {
							return false;
						}
					} else {
						return false;
					}
				  }.bind(this)
			});
		});
	});
	
	
	removeRadioFavorite = function(e) {
		Event.stop(e);

		var tmp = $(this).readAttribute('href').split('#');
		
		new Ajax.Request('/radio/?favorite=' + tmp[1], {
			  method: 'get',
			  onSuccess: function(transport) {
				if(transport.responseJSON) {
					if (transport.responseJSON.action) {
						if (transport.responseJSON.action == 'remove') {
							var href = this.readAttribute('href');
							this.up().remove();
							
							$$('a[href="' + href + '"]').each(function(a) { 
								if (a.hasClassName('starOn'))
									a.removeClassName('starOn');
							});
						}
					} else {
						return false;
					}
				} else {
					return false;
				}
			  }.bind(this)
		});
	}
	
	$$('#side-radio-favorites a.remover').each(function(a) { 
		a.observe('click', removeRadioFavorite);
	});
}


function installRadioPlayer() {
	$$('a.radioplay').each(function(a) {
		if (a.readAttribute('href')) {
			a.observe('click', function(e) {
				Event.stop(e);
				var element = Event.element(e);
				if (element.hasClassName('bbc') == true) {
					newwindow = window.open(this.readAttribute('href') + '?mrec=off', 'radioplayer', 'width=760,height=470');
				} else {	
					
					var guid = this.readAttribute('href').match(/s[0-9]{1,}/);

					switch(guid[0]) {
						case 's18720':
							newwindow = window.open('http://rfm.sapo.pt/playerstream_fl.aspx','player','toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width=810, height=500');
							break;
						case 's81784':
							newwindow = window.open('http://rfm.sapo.pt/player_80s_fl.aspx','player','toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width=810, height=500');
							break;
						case 's25179':
							newwindow = window.open('http://rr.sapo.pt/PlayerStream_fl.aspx','player','toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width=680, height=625');
							break;
						case 's6745':
							newwindow = window.open('http://megahits.sapo.pt/playerstream_fl.aspx','player','toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width=850, height=420');
							break;
						case 's107245':
							newwindow = window.open('http://radiosim.sapo.pt/playerstream.aspx','player','toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width=850, height=420');
							break;
						default:
							newwindow = window.open(this.readAttribute('href'), 'radioplayer', 'width=960,height=470');
					}
				}
				newwindow.focus();
			});
		}
	});
}

