var ytplayer = null;
function vinitpage() {
    if(ticker) clearInterval(ticker);
    $.get('rss/youtube_music.xml?'+new Date().getTime(), function(xml) {
	
	$('#music_body').html('');
	$('#music_list').html('');
	var music_form = 
	    $('<select></select>').appendTo('#music_list')
	    .attr('id', 'videoSelection')
	    .attr('style', 'width:580px; border: 0px solid #fff')
	    .bind('change', loadVideo);
	
	$('#music_body').append("<br><div id='videoDiv'></div><form id='sf'><input id='title' type='text' size='70' value='title'><br><input id='url' type='text' size='70' value='url'><br><input type='button' value='스크랩' onclick='save_youtube()'><input type='button' value='작게' onclick='small_youtube()'></form>");
	
	$(xml).find('item').each( function(idx) {
	    
	    if(idx == 0) {
		loadPlayer($(this).find('id').text(), 'videoDiv');
	    }
	    $('<option></option>').appendTo(music_form)
		.attr('value', $(this).find('id').text())
		.text($(this).find('title').text());
	} );
	
    } );
}

// Loads the selected video into the player.
function loadVideo(vp) {

    var selectBox = document.getElementById("videoSelection");
    var videoID = selectBox.options[selectBox.selectedIndex].value;
    
    ytplayer.loadVideoById(videoID);
}

// This function is called when an error is thrown by the player
function onPlayerError(errorCode) {
    alert("An error occured of type:" + errorCode);
}

function onStateChange(state) {

    if(state == '1') { // playing
	var url = ytplayer.getVideoUrl();
	url = url.replace(/&feature.*/, '');
	document.getElementById('url').value = url;
    }else if(state == '0') { // ended
	ytplayer.playVideo();
    }
}

// This function is automatically called by the player once it loads
function onYouTubePlayerReady(playerId) {

    if(playerId == 'svideoDiv') {
	ytplayer = document.getElementById("svideoDiv_player");
    }else {
	ytplayer = document.getElementById("videoDiv_player");
    }
    ytplayer.addEventListener("onError", "onPlayerError");
    ytplayer.addEventListener("onStateChange", "onStateChange");
}

// The "main method" of this sample. Called when someone clicks "Run".
function loadPlayer(vid, display) {

    // The video to load
    var videoID = vid;
    // Lets Flash from another domain call JavaScript
    var params = { allowScriptAccess: "always", loop: "1" };
    // The element id of the Flash embed
    var atts = { id: display+"_player" };
    
    if(display == 'svideoDiv') {
	width = '20'; height = '10';
    }else {
	width = '435'; height = '311';
    }

    swfobject.embedSWF("http://www.youtube.com/v/" + videoID +
		       "&enablejsapi=1&playerapiid="+display,
                       display, width, height, "8", null, null, params, atts);
}

function save_youtube() {

    if(confirm('저장한다') == true) {
	var sf = document.getElementById('sf');

    $.post('save_youtube.php', { title: sf.title.value, url: sf.url.value },
	   function(data) {
	       $('#log').text($(data).find('result').text());
	   } );
    }
}

function small_youtube() {
    
    if(ytplayer) {
	ytplayer.stopVideo();
	ytplayer.pauseVideo();
	ytplayer.clearVideo();
    }
    loadPlayer('0eHh-BxJTqw', 'svideoDiv');
}