
function initInterface()
{
	$("list_type1").checked = true;
	
	if ( IsDoubleCacheMode() )
	{
		$("cb_doublecache").checked = true;
	}
	
	if ( getMylist().length > 0 )
	{
		$("list_type2").checked = true;
		$("mylist_value").value = getMylist();
		onListRadioModified();
	}
	
	var quality = getQuality();
	
	if ( quality == "noscreen" )
	{
		$("no_screen").checked = true;
	}
	else if ( quality == "controlonly" )
	{
		$("control_only").checked = true;
	}
	else if ( quality == "low" )
	{
		$("low_quality").checked = true;
	}
	else if ( quality == "big" )
	{
		$("big_screen").checked = true;
	}
	else
	{
		$("normal_quality").checked = true;
	}
	
	if ( quality == null || quality == "" || quality == "normal" )
	{
		$("show_detail_control").checked = false;
	}
	else
	{
		$("show_detail_control").checked = true;
	}
	detailControlUI();
}

/// list radio 버튼에 따라서 목록이나, 버튼을 disable, enable 시킨다.
function onListRadioModified()
{
	var listType2 = $("list_type2");
	var mylistDiv = $("list_control_mylist");
	var listButton = $("mylist_button"); 
	
	if ( listType2 == null || mylistDiv == null || listButton == null )
	{
		statusMsg("list_type is null");
		return;
	}
	
	if ( listType2.checked )	/// 내 목록이 체크되어 있으면,
	{
		mylistDiv.style.visibility = 'visible';
		mylistDiv.style.position = 'relative';
	}
	else /// 최신가요 목록이 체크되어 있으면,
	{
		mylistDiv.style.visibility = 'hidden';
		mylistDiv.style.position = 'absolute';

		if ( listRequestType == "mylist" )
		{
			/// 이전에 내 목록이 로딩되어 있었으면, 자동으로 최신가요 목록을 불러온다.
			var newURL = makeNewURL("mylist", "");
			location.href = newURL;
		}
	}
}

function getUrlVars()
{
    var vars = [], hash;
    
    /// url 중에서 첫번째 '?' 이후만 가져온다.
    var findIndex = window.location.href.indexOf('?');
    
    if ( findIndex == -1 )
    {
    	return vars;
    }
    
    var params = window.location.href.slice(findIndex + 1);
    
    /// GET 변수 중에 # 은 제거한다.
    params = params.replace("#", "");
    
    /// '&' 을 기준으로 split 한다.
    var hashes = params.split('&');
    
    for(var i = 0; i < hashes.length; i++)
    {
    	/// '=' 를 기준으로 key 와 value 를 나눈다.
	    hash = hashes[i].split('=');
	    vars.push(hash[0]);
	    vars[hash[0]] = hash[1];
    }
    return vars;
}

function GetMaxSongLimit()
{
    urlVars = getUrlVars();
    
    var inputValue = urlVars["maxsonglimit"];

    return parseInt(inputValue);
}

function getCacheMode()
{
    urlVars = getUrlVars();
    
    var inputValue = urlVars["cache"];

    switch ( inputValue )
    {
    case "doublecache":
    	return "doublecache";
    	
    }
    return "normal";
}

function getMylist()
{
    urlVars = getUrlVars();
    
    var inputValue = urlVars["mylist"];

    if ( inputValue && inputValue.length > 0 )
    {
    	return inputValue;
    }
    return "";
}

function IsDoubleCacheMode()
{
	if ( getCacheMode() == "doublecache" )
	{
		return true;
	}
	return false;
}

function IsRepeatChecked()
{
    var repeatControl = document.getElementById("repeat");

    /// repeat 가 check 되어 있으면 다시 플레이
    if ( repeatControl != null && repeatControl.checked == true )
    {
    	return true;
    }
    return false;
}

/// 플레이 정보(검색한 가수명과 노래제목) 표시
function SetPlayInfo(str)
{
	var playInfoDiv = document.getElementById('playInfo');
	
	if ( playInfoDiv )
	{
		playInfoDiv.innerHTML = str;
	}
}

/// 플레이 중인 비디오의 정보(실제 플레이 중인 비디오 정보)
function SetVideoInfo(str)
{
	var contentDiv = document.getElementById('content');
	
	if ( contentDiv )
	{
		contentDiv.innerHTML = str;
	}
}

/// 다음 곡 플레이 정보(검색한 가수명과 노래제목) 표시
function SetNextSongInfo(str)
{
	var nextPlayInfoDiv = document.getElementById('NextSongInfo');
	
	if ( nextPlayInfoDiv )
	{
		nextPlayInfoDiv.innerHTML = str;
	}
}

var bufferAnimationArray = [ "---", "^--", "-^-", "--^" ];
var bufferAnimationIndex = 0;

var lastBufferByte = 0;

function SetBufferingInfo(player)
{
	var bufferingDiv = document.getElementById('bufferingInfo');
	
	if ( bufferingDiv )
	{
		var loaded = parseInt(player.getVideoBytesLoaded()/1024);
		var total = parseInt(player.getVideoBytesTotal()/1024);

		var percentage = "";
		
		if ( loaded < 0 || total <= 0 )
		{
			percentage = "...";
		}
		else
		{
			percentage = parseInt(loaded/total * 100);
		}
		
		var bufferInfo = "";
		
		if ( lastBufferByte != player.getVideoBytesLoaded() )
		{
			lastBufferByte = player.getVideoBytesLoaded();
			bufferAnimationIndex = (bufferAnimationIndex + 1) % 4;
		}
		else
		{
			bufferAnimationIndex = 0;
		}
		bufferInfo += bufferAnimationArray[bufferAnimationIndex];
		bufferInfo += " ";
		
		bufferInfo += loaded + " / " + total + " KBytes (" + percentage + "%)";;

		if ( IsReadyNextSong(player) )
		{
			bufferInfo += " ready ok";
		}
		else
		{
			bufferInfo += " not ready yet";
		}
		
		bufferingDiv.innerHTML = bufferInfo;
	}
}

function EditMyList()
{
	var listValue = $("mylist_value");
	
	if ( listValue == null )
	{
		statusMsg("listValue is null");
		return;
	}
	
	statusMsg("len : " + listValue.value.length);
	
	/*
	if ( listValue.value.length != 23 )
	{
		alert("적절한 구글 문서의 키를 넣어주세요. 도움말을 참고하세요.");
		return;
	}
	*/
	
	url = "http://spreadsheets.google.com/ccc?key=" + listValue.value;
	
	window.open(url);
}

function newXMLHttpRequest()
{
	var ret = null;
	
	try
	{
		ret = new XMLHttpRequest();
	}
	catch ( e )/// ie 6 에서는 exception 발생
	{
		//alert("Internet 6.0 이하 버젼을 사용할 경우 제대로 동작하지 않습니다. 7.0 이상을 사용하시거나, 파이어폭스, 크롬 웹 브라우저를 사용해보세요.");
		
		try
		{
			ret = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch ( e2 )
		{
			alert("지원되지 않는 브라우저입니다.");
		}
	}
	
	if ( ret == null )
	{
		alert("XML 요청 실패");
	}
	
	return ret;
}

function detailControlUI()
{
	var cb = $("show_detail_control");
	
	if ( cb && cb.checked == true )
	{
		//$("detail_control").style.height = "65px";
		$("detail_control").style.visibility = 'visible';
		$("detail_control").style.position = 'relative';
	}
	else
	{
		//$("detail_control").style.height = "1px";
		$("detail_control").style.visibility = 'hidden';
		$("detail_control").style.position = 'absolute';
	}
}

function IsRandom()
{
	var cb = document.getElementById("random");
	
	if ( cb && cb.checked == true )
	{
		return true;
	}
	return false;
}

function onChangeVideoSize()
{
	var newURL = "";
	if ( $("no_screen").checked )
	{
		newURL = makeNewURL("fmt", "noscreen");
	}
	else if ( $("control_only").checked )
	{
		newURL = makeNewURL("fmt", "controlonly");
	} 
	else if ( $("low_quality").checked )
	{
		newURL = makeNewURL("fmt", "low");
	} 
	else if ( $("normal_quality").checked )
	{
		newURL = makeNewURL("fmt", "");
	} 
	else if ( $("big_screen").checked )
	{
		newURL = makeNewURL("fmt", "big");
	} 
	location.href = newURL;
}

function requestMaxsonglimit()
{
	var limitValue = $("maxsonglimitinput").value;
	
	location.href = makeNewURL("maxsonglimit", limitValue);
}

