﻿
function CJL_CookieUtil(name, duration, path, domain, secure)
{
   this.affix = "";
   
   if( duration )
   {   	  
      var date = new Date();
	  var curTime = new Date().getTime();

	  date.setTime(curTime + (1000 * 60 * duration));
	  this.affix = "; expires=" + date.toGMTString();
   }
   
   if( path )
   {
      this.affix += "; path=" + path;
   }
   
   if( domain )
   {
      this.affix += "; domain=" + domain;
   }

   if( secure )
   {
      this.affix += "; secure=" + secure;
   }
   
      
   function getValue()
   {
      var m = document.cookie.match(new RegExp("(" + name + "=[^;]*)(;|$)"));

      return m ? m[1] : null;   
   }
   
   this.cookieExists = function()
   {
      return getValue() ? true : false;
   }
      
   this.expire = function()
   {
      var date = new Date();
	  date.setFullYear(date.getYear() - 1);
	  document.cookie=name + "=noop; expires=" + date.toGMTString(); 
   }
        
   this.setSubValue = function(key, value)
   {
      var ck = getValue();

      if( /[;, ]/.test(value) )
      {
         //Mac IE doesn't support encodeURI
		 value = window.encodeURI ? encodeURI(value) : escape(value);
      }

      
      if( value )
      {
         var attrPair = "@" + key + value;

         if( ck )
         {
             if( new RegExp("@" + key).test(ck) )
	         {
		        document.cookie =
				   ck.replace(new RegExp("@" + key + "[^@;]*"), attrPair) + this.affix;
	         }
	         else
	         {
		        document.cookie =
				   ck.replace(new RegExp("(" + name + "=[^;]*)(;|$)"), "$1" + attrPair) + this.affix;
	         }
         }
         else
         {
	        document.cookie = name + "=" + attrPair + this.affix;
         }
      }
      else
      {      
	     if( new RegExp("@" + key).test(ck) )
	     {
	        document.cookie = ck.replace(new RegExp("@" + key + "[^@;]*"), "") + this.affix;
	     }
      }
   }

      
   this.getSubValue = function(key)
   {
      var ck = getValue();

      if( ck )
      {
         var m = ck.match(new RegExp("@" + key + "([^@;]*)"));

	     if( m )
	     {
	        var value = m[1];

	        if( value )
	        { 
	           //Mac IE doesn't support decodeURI
			   return window.decodeURI ? decodeURI(value) : unescape(value);
	        }
	     }
      }
   }
}

/*------------------------------------------------------\
	CODE PLAYLIST SONG
\------------------------------------------------------*/
var c_name	= "t2vnlist";
var __cookie = new CJL_CookieUtil(c_name,36000,'/');
	var split_1	= '|'; //-- Ngăn cách giữa các cặp giá trị tạo nên một bài hát
	var split_2	= '~'; //-- Ngăn cách giữa tên bài hát và ID bài hát
	var playlistWindow = null;
	var playlistRowHeight = 20;

function addToPlaylist(song_id, song_name)
	{
	
		//-- Srote via cookie
		//
		c = __cookie.getSubValue(c_name);

		if(!c) c = "";
		else
		{
			//-- Kiem tra xem bai hat nay da co trong playlist chua
			//
			cc = c.split(split_1);

			for(i = 0; i < cc.length; i++)
			{
				if(cc[i])
				{
					s = cc[i].split(split_2);
					if( s[0] == song_id )
					{
						alert('Bài này đã có trong playlist rồi');
						return false;
					}
				}
			}
		}
		
		c = split_1 + song_id + split_2 + song_name + c;
		__cookie.setSubValue(c_name, c);
		alert('Bài hát '+song_name+' đã được thêm vào danh sách nhạc phẩm của bạn');
		//sendRequest('playlist/1','playlist');
		showPlaylist(link_playlist);
	}

function listenadd(song_id, song_name)
	{
	
		//-- Srote via cookie
		//
		c = __cookie.getSubValue(c_name);

		if(!c) c = "";
		else
		{
			//-- Kiem tra xem bai hat nay da co trong playlist chua
			//
			cc = c.split(split_1);

			for(i = 0; i < cc.length; i++)
			{
				if(cc[i])
				{
					s = cc[i].split(split_2);
					if( s[0] == song_id )
					{
					parent.document.getElementById("t2playlistall").src=link_playlistlisten+'/'+song_id+',/'+song_id;
						return false;
					}
				}
			}
		}
		
		c = split_1 + song_id + split_2 + song_name + c;
		__cookie.setSubValue(c_name, c);
		
		//sendRequest('playlist/1','playlist');
		parent.document.getElementById("t2playlistall").src=link_playlistlisten+'/'+song_id+',/'+song_id;
		showPlaylist(link_playlist);

	}
function listenlist(list_id,song_id) {
parent.document.getElementById("t2playlistall").src=link_playlistlisten+'/'+list_id+'/'+song_id;
}
function showPlaylist(page) {
parent.document.getElementById("t2playlist").src=link_playlist;
}
function playplaylist(song_id) {
parent.document.getElementById("t2playlistall").src=link_playlistlisten+'/'+song_id+',/'+song_id;
}

function removeFromPlaylist(song_id,page)
{
	c = __cookie.getSubValue(c_name);
	var nc = '';
	
	if(!c) return false;
	else
	{
		//-- Kiem tra xem bai hat nay da co trong playlist chua
		//
		cc = c.split(split_1);

		for(i = 0; i < cc.length; i++)
		{
			if(cc[i])
			{
				s = cc[i].split(split_2);
				if( s[0] != song_id )
				{
					nc = nc + split_1 + s[0] + split_2 + s[1];
				}
			}
		}
		__cookie.setSubValue(c_name, nc);
		alert('Bài hát này đã được xóa');
		self.location = link_playlist+page;
		showPlaylist(page);
		}	
}
function emptyPlaylist()
	{
		if( confirm('Bạn có chắc chắn muốn xoá không?') )
		{
			__cookie.setSubValue(c_name, null);
			alert('Danh sách các bài hát trong playlist đã được xoá toàn bộ!');
			showPlaylist(link_playlist);
		}
	}

