// JavaScript Document
imageServer = GLOBAL_IMAGE_PATH;
loginAccountName = '';
loginUserID = '';
/*
Trim ambas direcciones
*/
String.prototype.trim = function() {
	var str = this.replace(/^[ ]+|[ ]+$/g, '');
	str = str.replace(/^[\r\n]+|[\r\n]+$/g, '');
	str = str.replace(/^[ ]+|[ ]+$/g, '');
	return  str;
};
// trip space
String.prototype.tripSpace = function() { //Trim ambas direcciones
	return this.replace(/(\s\s+)/g, " ");
};
// repeat string
String.prototype.repeat = function() {
	var times	= String.prototype.repeat.arguments[0];
	var str		= '';
	var i;
	for (i = 0; i < times; i++) {
		str += this;
	}
	return str;
};
// filter string
String.prototype.filter = function() {
	var arrFilter	= String.prototype.filter.arguments[0];
	var strReplace	= String.prototype.filter.arguments[1];
	var pattern;
	var str	= ' ' + this + ' ';
	for (var i = 0; i < arrFilter.length; i++) {
		pattern		= new RegExp(' ' + arrFilter[i] + ' ', "ig");
		str = str.replace(pattern, ' ' + strReplace.repeat(arrFilter[i].length) + ' ');
	}
	return str;
};
// bo tieng Viet
String.prototype.stripViet = function()
{
	var replaceChr = String.prototype.stripViet.arguments[0];
	var stripped_str = this;
	var viet = [];
	i = 0;
	viet[i++] = new Array('a', "/á|à|ả|ã|ạ|ă|ắ|ằ|ẳ|ẵ|ặ|â|ấ|ầ|ẩ|ẫ|ậ/g");
	viet[i++] = new Array('o', "/ó|ò|ỏ|õ|ọ|ơ|ớ|ờ|ở|ỡ|ợ|ô|ố|ồ|ổ|ỗ|ộ/g");
	viet[i++] = new Array('e', "/é|è|ẻ|ẽ|ẹ|ê|ế|ề|ể|ễ|ệ/g");
	viet[i++] = new Array('u', "/ú|ù|ủ|ũ|ụ|ư|ứ|ừ|ử|ữ|ự/g");
	viet[i++] = new Array('i', "/í|ì|ỉ|ĩ|ị/g");
	viet[i++] = new Array('y', "/ý|ỳ|ỷ|ỹ|ỵ/g");
	viet[i++] = new Array('d', "/đ/g");
	for(var i = 0; i < viet.length; i++) {
		stripped_str = stripped_str.replace(eval(viet[i][1]), viet[i][0]);
		stripped_str = stripped_str.replace(eval(viet[i][1].toUpperCase().replace('G', 'g')), viet[i][0].toUpperCase());
	}
	if (replaceChr) {
		return stripped_str.replace(/[\W]|_/g, replaceChr).replace(/\s/g, replaceChr).replace(/^\-+|\-+$/g, replaceChr);
	} else {
		return stripped_str;
	}
};

/* kiem tra do dai tu khoa */
function CheckSearch(obj)
{
	var length = obj.q.value.trim().length;
	if(length < 2) {
		jAlert('warning', 'Từ khóa phải có chiều dài từ 2 kí tự trở lên!', 'Thông Báo');
		if (length < 1) {
			obj.q.value = '';
		}
		obj.q.focus();
		return false;
	}
	return true;
}

/*
Copy to clipboard
*/
function copy(text2copy) {
  	if (window.clipboardData) {
		window.clipboardData.setData("Text",text2copy);
	} else {
		var flashcopier = 'flashcopier';
		if(!document.getElementById(flashcopier)) {
		  var divholder = document.createElement('div');
		  divholder.id = flashcopier;
		  document.body.appendChild(divholder);
		}
		document.getElementById(flashcopier).innerHTML = '';
		var divinfo = '<embed src="' + imageServer + 'skins/zing/flash/clipboard.swf" FlashVars="clipboard='+encodeURIComponent(text2copy)+'" width="0" height="0" type="application/x-shockwave-flash"></embed>';
		document.getElementById(flashcopier).innerHTML = divinfo;
	}  	
}

/*
class get/set cookies
*/
var Os_Cookiess={$Set:function(name,value,expires,path,domain,secure)
{var today=new Date();today.setTime(today.getTime());if(expires)
expires=expires*1000*60*60;var expires_date=new Date(today.getTime()+(expires));document.cookie=name+"="+escape(value)+
((expires)?";expires="+expires_date.toGMTString():"")+
((path)?";path="+path:"")+
((domain)?";domain="+domain:"")+
((secure)?";secure":"");},$Get:function(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));},$Delete:function(name,path,domain)
{document.cookie=name+"="+
((path)?";path="+path:"")+
((domain)?";domain="+domain:"")+";expires=Thu, 01-Jan-1970 00:00:01 GMT";}};

/*
return acc if exist
*/
function getAccountName()
{	
	return Os_Cookiess.$Get('acn');
}

/*
return userID if exist
*/
function getUserID()
{	
	return Os_Cookiess.$Get('acn');
}

/*
return acc if exist
*/
function checkLogin(){	
	return Os_Cookiess.$Get('acn');
}


function checkUpdateProfile(){	
	  $.post('/includes/NewFace_CheckUpdateProfile.php', { 	
	  },  
	  function(data){  
			if(data) loadProfileForm();
	  }  
	);		
}



/*
logout
*/
function BlockLogin_Logout(){
	if(confirm('Bạn thật sự muốn thoát?')){
		//Clear Cookie Login		
		  $.post('/includes/NewFace_BlockLogin.php?logout=1', { 	
		  },  
		  function(data){  
			curUrl = window.location.href;
			isRefresh = (curUrl.indexOf('/mp3/upload') !=-1) ||
						(curUrl.indexOf('/mp3/nhac-cua-toi') !=-1);
			if (!isRefresh){
				$('#divLogin').html(data);
			}else{
				window.location.href = '/mp3';
			}
		  }  
		);		
	}
	return false;	
}

/*
reload block login
*/
function reloadBlockLogin(){
	  $.post('/includes/NewFace_BlockLogin.php', {},  
	  function(data){  
			$('#divLogin').html(data);
			tb_remove();
			//checkUpdateProfile();
	  }
	);
}

/*
login
*/
function loadLogin(query){
//	$(document).ready(function() {
			tb_show('Zing Mp3 Login','/mp3/login/index.html?no_refresh=1&TB_iframe=true&height=180&width=390&modal=false');
//	});
}
	
function loadLoginURL(url){
	$(document).ready(function() {
		tb_show('Zing Mp3 Login','/mp3/login/index.html?referer_url='+encodeURIComponent(url)+'&TB_iframe=true&height=205&width=376&modal=false');
	});
}

/*
update profile
*/
function loadProfileForm(){
	tb_show('Profile Update','/mp3/profile/thong-tin/index.html?TB_iframe=true&height=455&width=615&modal=false');
}


/*
playlist media
*/
function play()
{
	document.getElementById('wPlayer').controls.play();
}
function playlist(id, txt_search, txt_replace, player)
{
	document.getElementById(id).innerHTML = player.replace(txt_search, txt_replace);
	if(document.getElementById('wPlayer').controls) {
		window.setTimeout('play()', 1);
	}
}
/*
uppercase the first character of every word in a string
*/
function ucwords(str) {
    return (str+'').replace(/^(.)|\s(.)/g, function ( $1 ) { return $1.toUpperCase ( ); } );
}

/*
detect each email and make it on li seperate by comma (,)
*/
function ngheAlbum_DetectEmail(emailVal){
	var emailArr = emailVal.split(',');
	for(var i = 0; i < emailArr.length ; i++){		
		if(!isEmailAddr(emailArr[i])){			
			return false;
		}
	}
	return true;
}
/*
count down text limit
*/
function limitChars(textid, limit, infodiv)
{
	var text = $('#'+textid).val();	
	var textlength = text.length;
	if(textlength > limit)
	{
		$('#' + infodiv).html('kí tự tối đa '+limit+' kí tự');
//		$('#' + infodiv).html(limit);
		$('#'+textid).val(text.substr(0,limit));
		return false;
	}
	else
	{
		$('#' + infodiv).html('còn lại '+ (limit - textlength) +' kí tự.');
//		$('#' + infodiv).html(' '+ (limit - textlength) + ' kí tự');

		return true;
	}
}

/*
ktra email
*/
function isEmailAddr(email) {
	var pattern = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_.0123456789@~";
	var theStr = new String(email)
	var index = theStr.indexOf("@");

	for (var a=0; a<pattern.length; a++) {
		if (pattern.indexOf(email.charAt(a),0) == -1) return false;
	}
	if (theStr.indexOf(" ",0) != -1) return false;
	if (index > 0) {
		var pindex = theStr.indexOf(".",index);
		if ((pindex > index+1) && (theStr.length > pindex+1)) return true;
	}
	return false;
}

/*
check extension
*/
function FuncCheckExtension(optID, strExtension){
	var arr = new Array();			
	arr = optID.val().split('.');
	if (! (arr[arr.length - 1].toLowerCase() != '' && strExtension.search(arr[arr.length - 1].toLowerCase()) >= 0)) {			
		return 0;
	}
	
	return 1;
}
/*
get embed
*/
function getEmbed_forum(domain_flash, domain_site, url, extension, number, skin){
	var embed = '';
	var id = "embed_forum" + number;
	if(extension == "mp3")
		embed = '[FLASH]'+ domain_flash + 'skins/black/flash/player/mp3Player_skin'+skin+'.swf?xmlurl=' + domain_site + '/blog/?' + url+ '[/FLASH]';
	document.getElementById(id).value = embed;
	document.getElementById(id).style.display = 'block';
}

/* 
Modified to support Opera 
*/
function bookmarksite(title,url){
	if (window.sidebar) // firefox
		window.sidebar.addPanel(title, url, "");
	else if(window.opera && window.print){ // opera
		var elem = document.createElement('a');
		elem.setAttribute('href',url);
		elem.setAttribute('title',title);
		elem.setAttribute('rel','sidebar');
		elem.click();
	} 
	else if(document.all)// ie
		window.external.AddFavorite(url, title);
}

/*
active menu nhac cua toi
*/
function activeMenu(name, css, flagIndex)
{
	var arrZingMenu = document.getElementsByName(name);
	var href		= location.pathname.split("/");
	for (var i = 0; i < arrZingMenu.length; i++) {
		//if (location.pathname != '/' && location.pathname != '/mp3' && arrZingMenu[i].href.search(location.pathname) != -1) {
		if (arrZingMenu[i].href.search(href[flagIndex]) != -1) {
			arrZingMenu[i].className = css;
			break;
		}
	}
	if(arrZingMenu.length == i && arrZingMenu[0])
		arrZingMenu[0].className = css;
}
