/*
Copyright (c) 2009, vSolutions. All rights reserved.
http://vsolutions.vn
*/

/*-------------------------------------------------------------------
 General stuff
-------------------------------------------------------------------*/
function gid(name) {
	return document.getElementById(name);
}

function nvl(obj, defaultVal) {
	if(obj) {
		return obj;
	}
	else {
		return defaultVal;
	}
}

function isWhitespace(charToCheck) {
	var whitespaceChars = " \t\n\r\f";
	return (whitespaceChars.indexOf(charToCheck) != -1);
}

function breakNull(s){
 if(s!==null) {return s;}
 else {return "";}

}

function ltrim(str) { 
	for(var k = 0; k < str.length && isWhitespace(str.charAt(k)); k++) {}
	return str.substring(k, str.length);
}
function rtrim_t(str) {
	if(str!==null){
	for(var j=str.length-1; j>=0 && isWhitespace(str.charAt(j)) ; j--) {}
	return str.substring(0,j+1);
	} else {return "";}
}

function trim(str) {
	return ltrim(rtrim_t(str));
}

function getCookie(names)
{
    var info=document.cookie; //alert("info "+info);
    if(info!==null && info!=="")
    {
        var arr=info.split(";");
        for(var i=0;i<arr.length;i++)
        {
             var id_i=arr[i].split("="); 
             if(trim(names)==trim(id_i[0])){
             return unescape(breakNull((id_i[1])));         
             }
        }
    }
    
        return "";
}

/*-------------------------------------------------------------------
Player javascript API
-------------------------------------------------------------------*/
var play_one=false;
var play_random=false;
var current_song=0;
var playlist = new Array();
var playlistChanged = false;

function thisMovie(movieName) {
	return ob_flash;
}

function update_play_position(index,disable_stat) {
	hilightCurrentItem(index);
	current_song=index;
	if(!disable_stat) {
		var track = itemData('', current_song);
		var xmlHttp;
		try
		  {
		  // Firefox, Opera 8.0+, Safari
		  xmlHttp=new XMLHttpRequest();
		  }
		catch (e)
		  {
		  // Internet Explorer
		  try
			{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
			}
		  catch (e)
			{
			try
			  {
			  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			  }
			catch (e)
			  {
			  return false;
			  }
			}
		  }
		  xmlHttp.onreadystatechange=function()
			{
				return true;
			}
		  xmlHttp.open("GET","/service/statistic/statcollector.do?u=2&s=121&c=0&p="+track[2]+"&t=",true);
		  xmlHttp.send(null);
	}
}

function sendEvent(swf,typ,prm) { 
	reloadFile(playlist);
	thisMovie(swf).sendEvent(typ,prm);
	if(typ == 'ITEM') {
		update_play_position(prm);		
	}
}

function listener_model_state(obj) {
	if(obj.newstate == "COMPLETED") {
		if(obj.oldstate == "PLAYING") {
			var play_next = 0;
			if(play_one)
			{ 
				play_next = current_song;
			}
			else if(play_random)
			{ 
				play_next = Math.floor(Math.random()*playlist.length);
			}
			else
			{
				play_next = current_song + 1;
				if(play_next == playlist.length) {
					play_next = 0;
				}
			}		
			
			if(playlistChanged)
				reloadFile(playlist);
			ob_flash.sendEvent('ITEM',play_next);
			update_play_position(play_next);
		}
		else {
			var play_next = current_song;
			if(playlistChanged)
				reloadFile(playlist);
			if(play_next >= 0 && play_next < playlist.length) {
				ob_flash.sendEvent('ITEM',play_next);
			}
		}
	}
}

function listener_view_play(obj) {
	update_play_position(current_song,!obj.state);
}

function listener_view_next() {
	var play_next = current_song + 1;
	if(play_next == playlist.length) {
		play_next = 0;
	}	
	if(playlistChanged)
		reloadFile(playlist);
	update_play_position(play_next);
}

function listener_view_prev() {
	var play_prev = current_song - 1;
	if(play_prev < 0) {
		play_prev = playlist.length - 1;
	}
	if(playlistChanged)
		reloadFile(playlist);
	update_play_position(play_prev);
}

function listener_view_stop() {
	if(playlistChanged)
		reloadFile(playlist);
	update_play_position(current_song,true);
}

function loadFile(swf,obj) {
	playlistChanged = false;
	thisMovie(swf).sendEvent('LOAD', obj);
}

function reloadFile(obj) {
	if(!gid("playlist_reload") || gid("playlist_reload").value != "disable") {
		playlistChanged = false;
		ob_flash.sendEvent('LOAD', obj);		
	}
}

function getLength(swf) { 
	try{
		var len = playlist.length; 
		return len;
	} catch(e) { return 0; }
}

function addItem(swf,obj) {
	if(!gid("playlist_add_item") || gid("playlist_add_item").value != "disable") {
		playlistChanged = true;
		if(playlist.length == 1 && playlist[0].title === "" && playlist[0].file == "nothing.mp3") {
			playlist[0] = obj;
		}
		else {
			playlist = playlist.concat(obj);
		}
		if(playlist.length == 1) {
			reloadFile(playlist);
			thisMovie(swf).sendEvent('ITEM', 0);
			update_play_position(0);
		}
	}
}

function addItem2(swf,obj, reload) {
	if(!gid("playlist_add_item") || gid("playlist_add_item").value != "disable") {
		playlistChanged = true;
		if(playlist.length == 1 && playlist[0].title === "" && playlist[0].file == "nothing.mp3") {
			playlist[0] = obj;
		}
		else {
			playlist = playlist.concat(obj);
		}
		if(playlist.length == 1 && reload) {
			reloadFile(playlist);
			thisMovie(swf).sendEvent('ITEM', 0);
			update_play_position(0);
		}
	}
}

function removeItem(swf,idx) {
	playlistChanged = true;
	var remove_playing_item = (idx==current_song)?true:false;
	if((current_song == (playlist.length-1) && current_song > 0) || current_song > idx) {
		current_song--;
	}
	playlist.splice(idx, 1);
	var empty = false;
	if(playlist.length === 0) {
		playlist = new Array();
		playlist[0] = {tags:"",file:"nothing.mp3",image:"",title:""};
		empty = true;
	}
	if(empty) {
		reloadFile(playlist);
		thisMovie(swf).sendEvent('STOP');
	}
	else if(remove_playing_item) {
		reloadFile(playlist);
		thisMovie(swf).sendEvent('ITEM', current_song);
		update_play_position(current_song);
	}
}

function itemData(swf,idx) { 
	var obj = playlist[idx];
	var title_song=new Array();
	title_song[0]= obj['title'];
	title_song[1]= obj['file'];
	title_song[2]= obj['tags'];
	title_song[3]= obj['image'];
	title_song[4]= obj['link'];
	return title_song;
}

function getValueRadio(name)
{   
	var rad=document.getElementsByName(name);
	for (i=0;i<rad.length;i++){
		if (rad[i].checked===true) {
			return rad[i].value;
		}
	}
}

function setCheckedValue(name,newValue) {
	var rad=document.getElementsByName(name);
	var radioLength=rad.length;
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) {
			radioObj[i].checked = true;
		}
	}
}

function playMode() {
	if(getValueRadio("tuychon")==1)
	{
		play_one=true;
		play_random=false;
	}
	else if(getValueRadio("tuychon")==2)
	{
		play_one=false;
		play_random=true;
	}
	else
	{
		play_one=false;
		play_random=false;
	}
}