if(typeof(MusicBox) == "undefined") {
	var MusicBox = function() {
		this.reloadTimer = null;
		this.listSize = 7;
		
		this.setListSize = function(listSize) {
			this.listSize = listSize;
		}
		
		this.startTimer = function() {
			this.reloadTimer = setTimeout(this.changeReloadImg.bind(this, "on"), 1000 * 10);
		}
		
		this.changeReloadImg = function(flag) {
			var e = $("#musicBoxReloadImg");
			var src = e.attr("src");
			
			if(flag == "on")
				src = src.replace("bo_reload.gif", "bo_reloadOn.gif");
			else
				src = src.replace("bo_reloadOn.gif", "bo_reload.gif");
				
			e.attr("src", src);
		}
		
		this.getList = function(genre) {
			try{ event.returnValue = false;} catch(e){}
			
			if(!genre)
				genre = $("#musicBoxData").attr("genre");
			
			if(this.reloadTimer) {
				clearTimeout(this.reloadTimer);
				this.reloadTimer = null;
			}
			
			this.changeReloadImg("off");
			
			try{
				var url = _staticUrl + "/ajax/json/saycast/index/getMusicBoxList/" + genre + "/" + this.listSize;
				$.getJSON(url, function(data) {
					for(idx in data["result"]) {
						var obj = data["result"][idx];
	
	
						$("#musicBoxData" + idx + "_down").attr("href", "javascript:void(ilike.goDown('"+obj.trackId+"'))");
						$("#musicBoxData" + idx).attr("domainid", obj.domainid);
						$("#musicBoxData" + idx + "_title").text(obj.artist + " - " + obj.title);
						
						var e = $("#musicBoxData" + idx + "_listener");
						e.text(obj["listener"] + e.attr("postfix"));
					}

					$("#musicBoxData").attr("genre", genre);
					if($("#musicBoxCategory").attr("listtype") == "home") {
						// 메인 홈 리스트 형태에 맞게끔
						$("#musicBoxCategory > li").each(function() {
							if($(this).attr("genre") == genre)
								$(this).addClass("selected");
							else
								$(this).removeClass("selected");
						});
					}
					else {
						// 세이캐스트 홈 리스트 형태에 맞게끔
						$("#musicBoxCategory img").each(function() {
							var src = $(this).attr("src");
							
							if($(this).attr("genre") == genre) {
								src = src.replace("Off", "Select");
								src = src.replace("On", "Select");
							}
							else {
								src = src.replace("Select", "Off");
							}
							
							$(this).attr("src", src);
						});
					}
				});
			}
			catch(e)
			{
				$("#virtualFrame").attr("src", _staticUrl + "/main/index/virtualFrame/musicBoxList/" + genre + "/" + this.listSize);
			}
			this.startTimer();
			
			return false;
		}
		
		this.openRadio = function(idx) {
			openSayRadio("station", $("#musicBoxData" + idx).attr("domainid"));
		}
	};
}

if(typeof(g_musicBox) == "undefined") {
	var g_musicBox = new MusicBox();

	$(document).ready(function() {
		g_musicBox.startTimer();
	});
}

