// JavaScript Document

var playListCookie = "";
var playedListCookie = "";

function playListInit() {
	playListCookie = getCookie("PLAYLISTID");
	playedListCookie = getCookie("PLAYEDLISTID");
	
	playListCookie = (playListCookie) ? playListCookie : "";
	playedListCookie = (playedListCookie) ? playedListCookie : "";
	
	if(playListCookie == null || playListCookie == "undefined") {
		playListCookie = "";
		return;
	}
	

	var array = playListCookie.split(",");
	var count = array.length;
	
	if(playListCookie == "") {
		count = 0;	
	}

	if(count) {
		var html = "(" + count + ")";
		document.getElementById("shinyvPlayListCountHTML").innerHTML = html;
	}


}


function addToPlayedList(id) {
	var id = parseInt(id);
	if(!id) {
		return;
	}
	
	playedListCookie = playedListCookie.toString();	
	var array = playedListCookie.split(",");
	array = popToArray(array, id);
	var count = array.length;
	//如果多与三十个就去掉最先的视频
	if(count>30){
		//大于30个就踢出最先看的
		array = playedListPop(array);
	}
	playedListCookie = arrayToString(array);
	setCookie("PLAYEDLISTID", playedListCookie, 365);
}
function delToPlayedList(id) {
	var id = parseInt(id);
	if(!id) {
		return;
	}
	playedListCookie = playedListCookie.toString();
	var array = playedListCookie.split(",");
	array = delToArray(array, id);
	var count = array.length;
	playedListCookie = arrayToString(array);

	setCookie("PLAYEDLISTID", playedListCookie, 365);

	document.getElementById("shinyvPlayListCountHTML").innerHTML = "(" + count + ")";
}

function addToPlayList(id) {
	var id = parseInt(id);
	if(!id) {
		return;
	}

	playListCookie = playListCookie.toString();
	var array = playListCookie.split(",");
	array = popToArray(array, id);
	var count = array.length;
	playListCookie = arrayToString(array);

	setCookie("PLAYLISTID", playListCookie, 365);

	document.getElementById("shinyvPlayListCountHTML").innerHTML = "(" + count + ")";
}

function delToPlayList(id) {
	var id = parseInt(id);
	if(!id) {
		return;
	}

	playListCookie = playListCookie.toString();
	var array = playListCookie.split(",");
	array = delToArray(array, id);
	var count = array.length;
	playListCookie = arrayToString(array);

	setCookie("PLAYLISTID", playListCookie, 365);

	document.getElementById("shinyvPlayListCountHTML").innerHTML = "(" + count + ")";
}

//放入数组，如果有相同的值，则不放入
function popToArray(array, value) {
	array = array.reverse();

	var count = array.length;

	if(count == 1 && !array[0]) {
		array = new Array();
	}

	for(var i = 0; i < count; i++){
		if(array[i] == value){
			return array;
		}
	}

	array.push(value);

	return array;
}

function delToArray(array, value) {
	array = array.reverse();

	var count = array.length;

	var result = new Array();

	for(var i = 0, j =0 ; i < count; i++){
		if(array[i] != value){
			result[j] = array[i];
			j++;
		}
	}

	return result;
}

function arrayToString(array) {
	var result = "";
	var count = array.length
	for(var i = 0; i < count; i++){
		if(i ==0) {
			result = array.pop();
		}
		else {
			result += "," + array.pop();
		}
	}

	return result;
}

function getCookie( name ) {
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
		return null;
	}
	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ';', len );
	if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
}

function setCookie( name, value, expires ) {
	var today = new Date();
	today.setTime( today.getTime() );
	if ( expires ) {
		expires = expires * 1 * 3600 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	document.cookie = name+'='+escape( value ) +
	( ( expires ) ? ';expires='+expires_date.toGMTString() : '' ); //+ //expires.toGMTString()
	//( ( path ) ? ';path=' + path : '' ) +
	//( ( domain ) ? ';domain=' + domain : '' ) +
	//( ( secure ) ? ';secure' : '' );
}

function deleteCookie( name, path, domain ) {
	if ( getCookie( name ) ) document.cookie = name + '=' +
	//( ( path ) ? ';path=' + path : '') +
	//( ( domain ) ? ';domain=' + domain : '' ) +
	';expires=Thu, 01-Jan-1970 00:00:01 GMT';
}

function displayCount(HTMLId) {
	playListCookie = getCookie("PLAYLISTID");
	var count = 0;
	if(!(playListCookie == null || playListCookie == "undefined")) {
		var array = playListCookie.split(",");
		count = array.length;
	}

	document.getElementById(HTMLId).innerHTML = count;


}
function playedListPop( array ){
	array.reverse();
	array.pop(); 
	array.reverse();
	return array;
}
function displayPlayedCount(HTMLId) {
	playedListCookie = getCookie("PLAYEDLISTID");
	var count = 0;
	if(!(playedListCookie == null || playListCookie == "undefined")) {
		var array = playedListCookie.split(",");
		count = array.length;
	}

	document.getElementById(HTMLId).innerHTML = count;


}

function clear() {
	setCookie("PLAYLISTID", playListCookie, -1);
}

function clearPlayed() {
	setCookie("PLAYEDLISTID", playedListCookie, -1);
}

function getParameter( sProp ) {
         var re = new RegExp( sProp + "=([^\&]*)", "i" );
         var a = re.exec( document.location.search );
         if ( a == null )
                 return null;
         return a[1];
}

function delForPlayed() {
		playListCookie = playListCookie.toString();
		playedListCookie = playedListCookie.toString();
		
		var array = playListCookie.split(",");
		var arrayPlayed = playedListCookie.split(",");
		var count = arrayPlayed.length;
		array = array.reverse();
		
		for(var i = 0; i < count; i++) {
			array = delToArray(array, arrayPlayed[i]);
		}
		
		var count = array.length;
		playListCookie = arrayToString(array);
		
		setCookie("PLAYLISTID", playListCookie, 365);
		playedListCookie = "";
		setCookie("PLAYEDLISTID", playedListCookie, -1);		
		
}

//flash接口，
function getFlashLists() {
	//节目单联播
	var playList = getParameter('playlist');
	
	//专辑联播
	var ablumIdPlay = getParameter('albumIdPlay');
	
	//最终页面列表
	var id = getParameter('id');
	
	var task = new Array("index", 0);
	if(playList) {
		task[0] = "playlist";
		//如果是联播的，从cookie取得
		task[1] = getCookie("PLAYLISTID");
	}
	else if(ablumIdPlay) {
		task[0] = "album";
		task[1] = ablumIdPlay;
	}
	else if(id) {
		task[0] = "view";
		task[1] = id;
	}

	//return "task=" + task[0] + "&id=" + task[1];
	return task;
}


playListInit();

