/*
 * ユーザーインタフェース
 */
var window = Titanium.UI.createWindow({});

var feedsTable = Titanium.UI.createTableView({});

var playingPanel = Titanium.UI.createView({
	bottom:-65,
	width:'97%',
	height:65,
	opacity:0.95,
	borderRadius:4,
	backgroundColor:'#333'
});

var playingTitle = Titanium.UI.createLabel({
	top:4,
	left:6,
	right:4,
	height:14,
	color:'#fff',
	wordWrap:false,
	font:{fontSize:12},
});

var playingProgress = Titanium.UI.createProgressBar({
	top:20,
	width:'60%',
	height:32,
	min:0,
	max:100,
	value:0,
	visible:true,
	color:'#fff',
	font:{fontSize:12, fontFamily:'Helvetica'},
	style:Titanium.UI.iPhone.ProgressBarStyle.PLAIN
});

var stopPlaying = Titanium.UI.createButton({
	top:20,
	width:36,
	height:36,
	left:24,
	backgroundImage:'',
	image:'stop.png'
});

var playingElapsed = Titanium.UI.createLabel({
	top:21,
	right:8,
	width:48,
	height:32,
	color:'#fff',
	font:{fontSize:12, fontFamily:'Helvetica'},
	text:'00:00'
});

window.add(feedsTable);
playingPanel.add(playingTitle);
playingPanel.add(stopPlaying);
playingPanel.add(playingProgress);
playingPanel.add(playingElapsed);
window.add(playingPanel);

function createFeedsTableViewRow(item, image){
	var row = Titanium.UI.createTableViewRow({height:64});	
	if(image){
		var thumbnail = Titanium.UI.createImageView({
			left:4,
			width:40,
			height:40,
			image:image.url
		});
		row.add(thumbnail);
	}

	var title = Titanium.UI.createLabel({
		left:50, 
		right:40,
		top:4, 
		height:24,
		font:{fontSize:15},
		text:item.title
	});
			
	var summary = Titanium.UI.createLabel({
		left:50, 
		right:40,
		bottom:4, 
		height:30,
		wordWrap:true,
		color:'#666',
		text:item.summary, 
		font:{ fontSize:12 }
	});
		
	var playing = Titanium.UI.createImageView({
		image:'nowplaying.png',
		width:32,
		height:'100%',
		right:6,
		opacity:0.1
	});
		
	row.add(title);
	row.add(summary);		
	row.add(playing);
	row.duration = item.duration;
	row.feedtitle = item.title;
	row.url = item.enclosure.url;
	row.playing = playing;	

	return row;
}

var playingIndicator;
function setPlayingIndicator(nextPlayingIndicator) {
	if(playingIndicator){
		playingIndicator.opacity = 0.1;
	}
	
	playingIndicator = nextPlayingIndicator;
	
	if(playingIndicator){
		playingIndicator.opacity = 1.0;
	}
}

function showPlayingPanel(args){
	playingPanel.animate({bottom:8, duration:400});
	playingTitle.text = args.feedtitle;
	playingProgress.max = args.duration;
	playingProgress.value = 0;
}

function hidePlayingPanel(){
	playingPanel.animate({bottom:-65, duration:400});
}

function updatePlayingProgress(progress){
	var minute = parseInt(progress / 60, 10);
	var second = parseInt(progress % 60, 10);

	playingElapsed.text = ("00"+minute).slice(-2) + ":" + ("00"+second).slice(-2);
	playingProgress.value = progress;
}

/*
 * YQLでpodcastのRSSフィードを取得
 */
var url = "http://www.cbc.ca/podcasting/includes/andthewinneris.xml";
Titanium.Yahoo.yql('select * from xml where url="'+url+'"', function(result){
	if(!result.success || !result.data 
		|| !result.data.rss || !result.data.rss.channel){
		alert("RSSフィードの取得に失敗しました");
	}
	
	var item = result.data.rss.channel.item;
	var image = result.data.rss.channel.image;
	var data = [];
	for(var i = 0; i < item.length; i++){
		if(image && image.length > 0){
			data[i] = createFeedsTableViewRow(item[i], image[0]);
		}else{
			data[i] = createFeedsTableViewRow(item[i]);
		}
	}	
	feedsTable.data = data;
});

var player;
/*
 * ストリーミング開始
 */
feedsTable.addEventListener('click', function(e){
	if(player){
		player.stop();
		player = null;
	}	
	player = Titanium.Media.createAudioPlayer({url:e.rowData.url});
	player.addEventListener('progress', function(e) { 
		updatePlayingProgress(e.progress); 
	});
	player.start();
	
	setPlayingIndicator(e.rowData.playing);
	showPlayingPanel(e.rowData);
});

/*
 * ストリーミング停止
 */
stopPlaying.addEventListener('click', function(e){
	player.stop();
	
	setPlayingIndicator();
	hidePlayingPanel();
});

// open window
window.open();