var ppqu_login_status		= null;
var userAgent	= navigator.userAgent.toLowerCase();
var is_ie		= (userAgent.indexOf('msie') != -1);
var chatTop	= "60px";
function ajax()
{
	var a	= null;
	try
	{
		a	= new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			a	= new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(oc)
		{
			a	= null;
		}
	}
	if(!a && typeof XMLHttpRequest != "undefined")
	{
		a	= new XMLHttpRequest();
	}
	return a;
}
function login_ppqu()
{
	if(ppqu_login_status && ppqu_login_status.readyState!=0)
	{
		ppqu_login_status.abort()
	}
	ppqu_login_status	= ajax();
	if(ppqu_login_status)
	{
		ppqu_login_status.open("GET", "login.php?nocache=" + Math.random(), true);
		ppqu_login_status.onreadystatechange	= function()
		{
			if(ppqu_login_status.readyState==4 && ppqu_login_status.responseText)
			{
				getObject('header_login').innerHTML	= ppqu_login_status.responseText;
			}
		};
		ppqu_login_status.send(null);
	}
}

function fAddFavorite(sTitle, sURL)
{ 
	if(document.all)
		window.external.AddFavorite(sURL,sTitle); 
	else
		window.sidebar.addPanel(sTitle, sURL, "");
}
//---- COOKIE ----
function getExpTime(time, type)
{
	var expTime	= new Date();
	switch(type)
	{
		case 'year' :
			expTime.setFullYear(expTime.getFullYear() + time);
			break ;
		case 'month' :
			expTime.setMonth(expTime.getMonth() + time);
			break;
		case 'day' :
			expTime.setDate(expTime.getDate() + time);
			break;
		case 'hour' :
			expTime.setHours(expTime.getHours() + time);
			break;
	}
	return expTime.toGMTString();
}
function getCookieVal(offset)
{
	var endstr	= document.cookie.indexOf(";", offset);
	if(endstr == -1)
		endstr	= document.cookie.length;
	return decodeURIComponent(document.cookie.substring(offset, endstr));
}
function getCookie(name)
{
	var arg		= name + "=";
	var alen	= arg.length;
	var clen	= document.cookie.length;
	var i		= 0;
	while (i < clen)
	{
		var j	= i + alen;
		if (document.cookie.substring(i, j) == arg)
		{
			return getCookieVal(j);
		}
		i	= document.cookie.indexOf(" ", i) + 1;
		if (i == 0)
		{
			break;
		}
	}
	return "";
}
function setCookie(name, value, expires, path, domain, secure)
{
	document.cookie		= name + "=" + encodeURIComponent(value) +
	((expires) ? "; expires=" + expires : "") +
	((path) ? "; path=" + path : "") +
	((domain) ? "; domain=" + domain : "") +
	((secure) ? "; secure" : "");
}
function delCookie(name)
{
	var last_date	= new Date();
	expiresTime		= last_date.setTime(last_date.getTime() - 100);
	setCookie(name, '', expiresTime, '', '') ;
}
function getFavoriteNum(num)
{
	getObject("favorite_number").innerHTML	= '(' + num + ')';
}
function getHistoryNum(num)
{
	getObject("history_number").innerHTML	= '(' + num + ')';
}
function AddFav(obj)
{
	var url = obj.parentNode.getElementsByTagName("a")[0].href;
	if(url.indexOf('/show-video-') != -1)
	{
		var video_url	= url.split("/show-video-")[1];
		var video_id	= video_url.split(".htm")[0];
		if(isNaN(video_id))
		{
		}
		else
		{
			var res		= add_favorite(video_id);
			var path	= url.split("/show-video-")[0];
			if(res == "F")
			{
				del_favorite(video_id);
				obj.style.backgroundImage = 'url(' + path + '/images/AddFav.gif)';
			}
			else
			{
				obj.style.backgroundImage = 'url(' + path + '/images/DelFav.gif)';
			}
		}
	}
}
function add_favorite(vid)
{
	var expiresTime		= getExpTime(1, 'month');
	var oldValue		= getCookie('favorite_list');
	var newValue		= ''
	if(oldValue)
	{
		var matchStr	= 'v' + oldValue + 'v';
		if(new RegExp('v' + vid + 'v').test(matchStr))
		{
			return 'F';
		}
		if(oldValue.split('v').length >= 100)
		{
			newValue	= oldValue.replace(/\d*v/,'') + 'v' + vid;
		}
		else
		{
			newValue	= oldValue + 'v' + vid;
		}
		setCookie('favorite_list', newValue, expiresTime, '', '');
		favorite_number		= newValue.split('v').length;
		getFavoriteNum(favorite_number)
	}
	else
	{
		newValue	= vid;
		setCookie('favorite_list', newValue, expiresTime, '', '');
		getFavoriteNum(1);
	}
}
function add_history(vid)
{
	var expiresTime		= getExpTime(1, 'month');
	var oldValue		= getCookie('history_list');
	var newValue		= ''
	if(oldValue)
	{
		var matchStr	= 'v' + oldValue + 'v';
		if(new RegExp('v' + vid + 'v').test(matchStr))
		{
			return '';
		}
		if(oldValue.split('v').length >= 100)
		{
			newValue	= oldValue.replace(/\d*v/,'') + 'v' + vid;
		}
		else
		{
			newValue	= oldValue + 'v' + vid;
		}
		setCookie('history_list', newValue, expiresTime, '', '');
		favorite_number		= newValue.split('v').length;
		getHistoryNum(favorite_number)
	}
	else
	{
		newValue	= vid;
		setCookie('history_list', newValue, expiresTime, '', '');
		getHistoryNum(1);
	}
}
function del_favorite(vid)
{
	var expiresTime	= getExpTime(1, 'month')
	var oldValue	= getCookie('favorite_list');
	var newValue	= '';
	var matchStr	= 'v' + oldValue + 'v';
	replaceStr		= 'v' + vid + 'v';
	newValue		= matchStr.replace(replaceStr , 'v').slice(1,-1);
	setCookie('favorite_list', newValue, expiresTime, '', '');
	var num			= (newValue == '') ? 0 : newValue.split('v').length;
	getFavoriteNum(num);
}
function hidden_favorite(vid)
{
	getObject('favorite_'+vid).style.display='none';
}
function set_favorite(rand)
{
	oldValue	= getCookie('favorite_list');
	if(oldValue == "")
	{
	}
	else
	{
		getFavoriteNum(oldValue.split('v').length)
	}
}
function set_history(rand)
{
	oldValue	= getCookie('history_list');
	if(oldValue == "")
	{
	}
	else
	{
		getHistoryNum(oldValue.split('v').length)
	}
}
function StringBuilder()
{
	this._stringArray	= new Array();
}
StringBuilder.prototype.append		= function(str)
{
	this._stringArray.push(str);
}
StringBuilder.prototype.toString	= function(joinGap)
{
	return this._stringArray.join(joinGap);
}
getFlashHtml	= function(flashArguments, flashPlayerCID)
{
	var _attrs	= new StringBuilder();
	var _params	= new StringBuilder();
	if(typeof(flashPlayerCID) == 'undefined')
	{
		flashPlayerCID	= 'D27CDB6E-AE6D-11cf-96B8-444553540000';
	}
	for(var k in flashArguments)
	{
		switch(k)
		{
			case "movie":
				continue;
				break;
			case "id":
			case "name":
			case "width":
			case "height":
			case "style":
				_attrs.append(k + "='" + flashArguments[k] + "' ");
				break;
			default:
				_params.append("<param name='" + ((k=="src") ? "movie":k) + "' value='" + flashArguments[k] + "' />");
				_attrs.append(k + "='" + flashArguments[k] + "' ");
		}
	}
	if(is_ie)
	{
		return "<object classid='clsid:" + flashPlayerCID + "' " + _attrs.toString('') + ">" + _params.toString('') + "<embed " + _attrs.toString('') + " type='application/x-shockwave-flash'></embed>" + "</object>";
	}
	else
	{
		return "<embed " + _attrs.toString('') + " type='application/x-shockwave-flash'></embed>";
	}
}
function chatLoad()
{
	getObject('ChatFlashDiv').style.top		= chatTop;
	setInterval('chatMove()', 10);
}
//document.documentElement.scrollTop
function chatMove()
{
	if (navigator.userAgent.indexOf("MSIE") == -1)
	{
		chatTop	= (window.pageYOffset + document.body.clientHeight - 350) + "px";
		if(getObject('ChatFlashDiv').style.top == chatTop)
		{
			return false;
		}
		getObject('ChatFlashDiv').style.top		= "1px";
		getObject('ChatFlashDiv').style.top		= (window.pageYOffset + document.body.clientHeight - 350) + "px";
		getObject('ChatFlashDiv').style.left	= (document.body.offsetWidth - 240) + "px";
	}
	else
	{
		chatTop	= (document.body.scrollTop + document.body.offsetHeight - 350) + "px";
		if(getObject('ChatFlashDiv').style.top == chatTop)
		{
			return false;
		}
		getObject('ChatFlashDiv').style.top		= (document.body.scrollTop + document.body.offsetHeight - 350) + "px";
		getObject('ChatFlashDiv').style.left	= (document.body.offsetWidth - 240) + "px";
	}
}
function chatLogin(path)
{
	if(typeof path == "undefined")
	{
		path	= "./";
	}
	var flashvars	= "user_cookie=" + getCookie('user_cookie') + "&amp;user_email=" + getCookie('user_email');
	var swf			= getFlashHtml({"src":path + 'images/chat.swf', "width":"200", "height":"300", "allowScriptAccesss":"always", "id":"ChatFlashId", "name":"ChatFlashId", "flashvars":flashvars});
	getObject("ChatFlashDiv").innerHTML	= swf;
	/*
	if (navigator.userAgent.indexOf("MSIE") == -1)
	{
		document.embeds['ChatFlashId'].src	= path + 'images/chat.swf?user_cookie=' + getCookie('user_cookie') + '&user_email=' + getCookie('user_email') + '&nocache=' + Math.random();
	}
	else
	{
		getObject('ChatFlashId').SetVariable('user_email', getCookie('user_email'));
		getObject('ChatFlashId').SetVariable('user_cookie', getCookie('user_cookie'));
		getObject('ChatFlashId').Play();
	}
	*/
}
function chatClose()
{
	getObject('ChatFlashDiv').innerHTML	= '';
}
function copyToClipboard(txt)
{
	if(window.clipboardData)
	{
		window.clipboardData.clearData();
		window.clipboardData.setData("Text", txt);
	}
	else if(navigator.userAgent.indexOf("Opera") != -1)
	{
		window.location = txt;
	}
	else if (window.netscape)
	{
		try 
		{
			netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
		}
		catch (e)
		{
		}
		var clip = Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard);
		if (!clip)return;
		var trans = Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable);
		if (!trans)return;
		trans.addDataFlavor('text/unicode');
		var str = new Object();
		var len = new Object();
		var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
		var copytext = txt;
		str.data = copytext;
		trans.setTransferData("text/unicode",str,copytext.length*2);
		var clipid = Components.interfaces.nsIClipboard;
		if (!clip)return false;
		clip.setData(trans,null,clipid.kGlobalClipboard);
	}
}
function copy_ppqu_code(obj)
{
	getObject(obj).select();
	copyToClipboard(getObject(obj).value);
}
