var url = 'http://difusorafm.com.br/';
$(document).ready(function(){
	$('ul#lislider').ready(function(){
		$('ul#lislider li').hide();
		
		var total = ($('ul#lislider li').length - 1);
		var curr = 0;
		var timer = 5000;
		
		function display(i){
			$('#boxes span').removeClass('sel');
			$('#d'+(i+1)).addClass('sel');
			$('ul#lislider li').fadeOut('fast');
			$('ul#lislider li:eq('+i+')').fadeIn('fast');
			$('d'+(i+1)).toggleClass('sel');
		}
		
		function slider(){
			display(curr);
			curr++;
			if(curr > total){
				curr = 0;
			}
		}
		
		if(total >= 1){
			for(c=(total+1); c>0; c--){
				$('<span/>', {
					id: "d"+c,
					text: c,
					mouseover: function(){
						clearInterval(start);
						$('#boxes span').removeClass('sel');
						$(this).addClass('sel');
						display($(this).attr('id').replace('d','') - 1);
					},
					mouseout: function(){
						clearInterval(start);
						start = setInterval(slider, timer);
					}
				}).appendTo('#boxes');
			}
			
			var start = setInterval(slider, timer);
			
			$('ul#lislider').bind({
				mouseover: function(){
					clearInterval(start);
				},
				mouseout: function(){
					clearInterval(start);
					start = setInterval(slider, timer);
				}
			});
		}
		display(curr);
		curr++;
	});
	$('#now_playing').ready(function(){
		function pulsar(){
			$('#now_playing').load(url + 'playing');
		}
		pulsar();
		playing = setInterval(pulsar, 10000);
	});
	$('#info_player').ready(function(){
		function info_player(){
			$('#info_player').load(url + 'player/info');
		}
		info_player();
		player = setInterval(info_player, 10000);
	});
	$('.player').popupWindow({
		height: 260,
		width: 460,
		top: 100,
		left: 100
	});
	
	$('.rating a').click(function(event) {
		event.preventDefault();
		el = $(this).parent('li').parent('ul').parent('div').attr('id');
		$.get(this, function(result){
			$('#'+el).html(result);
			//alert(result);
		});
	});
	
	function openPhoto(photo){
		$('.gallery').empty();
		$('.gallery').addClass('loading');
		var img = new Image();
		$(img)
		.load(function () {
		  $(this).hide();
		  $('.gallery')
		    .removeClass('loading')
		    .append(this);
		  $(this).fadeIn();
		})
		.error(function () {
		})
		.attr('src', photo);
	};
	
	$('.clk').click(function(event){
		event.preventDefault();
		var gallery = jQuery.url.setUrl($(this).attr('href')).segment(1);
		var photo = jQuery.url.setUrl($(this).attr('href')).segment(2);
		var n_url = url + 'img/photos/' + gallery + '/' + photo + '.jpg';
		openPhoto(n_url);
	});
	
	$('.miniplayer').attr('rel', function(){
		$(this).flash({
			swf: url + 'swf/miniplayer.swf',
			width: 25,
			height: 25,
			wmode: 'transparent',
			flashvars: {
				sound_file: $(this).attr('rel')
			}
		});
	});
});