/*
 * Common function for Yume1.0
 * Type:	file
 * Name: 	common.js
 * Date:	2008/05/20 
 * @version:1.0
 * @copyright 2008 Timnhanh.com
 * ---------------------------- /

/*
 * Ham thuc hien chuc nang "Chinh sua" box Profile
 * Type:	onload function
 * Name:	
 * Date:	2008/05/20
 */
jQuery(document).ready(function(){		
	//TNLibControlEdit();
	setThemeDefault();
	autoReFlash();
})
function TNLibControlEdit()
{
	$("#ym_BProfile .edit_control a").click(function(){			
		$("#edit_nickname").slideDown('fast');
		return false;
	})	
	$("#edit_nickname p").click(function(){			
		$("#edit_nickname").slideUp('fast');
	})
};

/*
 * Ham thuc hien thay doi icon bo go
 * Type:	onload function
 * Name:	
 * Date:	2008/05/20
 * @author:	Hien Nguyen <hiennh@von-inc.com>
 */
var typing_icon_status = 0;
function typing_change_icon(){	
	$('.typing_icon').css('background-position', function(){
		typing_icon_status++;
		if (typing_icon_status % 2 == 0) { setMethod(0); return 'top'; }
		else {setMethod(-1); return 'bottom';  }		
	});
}//end function


/*
 * Ham thuc hien chuc nang lay gia tri cua window va document
 * Type:	function
 * Name:	TNCom_getDocumentSize
 * Date:	2008/05/21
 * @param int val cac gia tri:	0: pageWidth, 1: pageHeight, 2: windowWidth, 3: windowHeight
 * @return int gia tri tra ve khi thuc hien goi ham		
 */
function TNCom_getDocumentSize(val)
{
	var xScroll,yScroll,value;
	var hasInner = typeof(window.innerWidth) == 'number';
	var hasElement = document.documentElement && document.documentElement.clientWidth;
	var numShiftY = hasInner?pageYOffset:hasElement?document.documentElement.scrollTop:document.body.scrollTop;
	if(window.innerHeight&&window.scrollMaxY)
	{
		xScroll=window.innerWidth+window.scrollMaxX;
		yScroll=window.innerHeight+window.scrollMaxY;
	}
	else if(document.body.scrollHeight>document.body.offsetHeight)
	{
		xScroll=document.body.scrollWidth;
		yScroll=document.body.scrollHeight;
	}
	else{
		xScroll=document.body.offsetWidth;
		yScroll=document.body.offsetHeight;
	}
	var windowWidth,windowHeight;
	
	if(self.innerHeight)
	{
		if(document.documentElement.clientWidth){
			windowWidth=document.documentElement.clientWidth;
		}
		else{
			windowWidth=self.innerWidth;
		}
		windowHeight=self.innerHeight;
	}
	else if(document.documentElement&&document.documentElement.clientHeight)
	{
		windowWidth=document.documentElement.clientWidth;
		windowHeight=document.documentElement.clientHeight;
	}
	else if(document.body)
	{
		windowWidth=document.body.clientWidth;
		windowHeight=document.body.clientHeight;
	}
	if(yScroll<windowHeight){
		pageHeight=windowHeight;
	}
	else{
		pageHeight=yScroll
	}
	
	if(xScroll<windowWidth){
		pageWidth=xScroll
	}
	else{
		pageWidth=windowWidth
	}
	arrayPageSize=new Array(pageWidth,pageHeight,windowWidth,windowHeight,numShiftY)	
	return arrayPageSize[val]
}// end function
/*
 * Ham thuc hien chuc nang lay gia tri cua window va document
 * Type:	function
 * Name:	TNCom_getDocumentSize
 * Date:	2008/05/21
 * @param int val cac gia tri:	0: pageWidth, 1: pageHeight, 2: windowWidth, 3: windowHeight
 * @return int gia tri tra ve khi thuc hien goi ham		
 */
function ym2_getDocumentSize(val)
{
	var xScroll,yScroll,value;
	var hasInner = typeof(window.innerWidth) == 'number';
	var hasElement = document.documentElement && document.documentElement.clientWidth;
	var numShiftY = hasInner?pageYOffset:hasElement?document.documentElement.scrollTop:document.body.scrollTop;
	if(window.innerHeight&&window.scrollMaxY)
	{
		xScroll=window.innerWidth+window.scrollMaxX;
		yScroll=window.innerHeight+window.scrollMaxY;
	}
	else if(document.body.scrollHeight>document.body.offsetHeight)
	{
		xScroll=document.body.scrollWidth;
		yScroll=document.body.scrollHeight;
	}
	else{
		xScroll=document.body.offsetWidth;
		yScroll=document.body.offsetHeight;
	}
	
	var windowWidth,windowHeight,pageWidth,pageHeight;
	
	if(self.innerHeight)
	{
		if(document.documentElement.clientWidth){
			windowWidth=document.documentElement.clientWidth;
		}
		else{
			windowWidth=self.innerWidth;
		}
		windowHeight=self.innerHeight;
	}
	else if(document.documentElement&&document.documentElement.clientHeight)
	{
		windowWidth=document.documentElement.clientWidth;
		windowHeight=document.documentElement.clientHeight;
	}
	else if(document.body)
	{
		windowWidth=document.body.clientWidth;
		windowHeight=document.body.clientHeight;
	}

	if(yScroll<windowHeight){
		pageHeight=windowHeight;
	}
	else{
		pageHeight=yScroll
	}

	if(xScroll<windowWidth){
		pageWidth=xScroll
	}
	else{
		pageWidth=windowWidth
	}

	arrayPageSize=new Array(pageWidth,pageHeight,windowWidth,windowHeight,numShiftY)	
	return arrayPageSize[val]
}// end function

/*
 * Ham thuc hien chuc nang tab
 * Type:	function
 * Name:	TNProfile_activeTabFriend
 * Date:	2008/05/21
 * @param int obj la tab duoc active
 */
function TNProfile_activeTabFriend(obj){
	for (var i=1; i<4; i++){
		var content = "cont_0"+i;
		var content_header = "tab"+i;
		document.getElementById(content).style.display='none';
		document.getElementById(content_header).className='tab_title ym_border_module ym_link'
		if (content_header==obj){ 
			document.getElementById(content).style.display='block';
			document.getElementById(content_header).className='tab_title ym_border_module ym_text'
		}
	}
}
/*
 * Ham thuc hien chuc nang tab
 * Type:	function
 * Name:	TNProfile_activeTabActFriend
 * Date:	2008/07/29
 * @author:	Thanh Bui <thanhbt@von-inc.com>
 * @param int obj la tab duoc active
 */
function TNProfile_activeTabActFriend(obj){
	for (var i=1; i<6; i++){
		var content = "cont_0"+i;
		var content_header = "tab"+i;
		document.getElementById(content).style.display='none';
		document.getElementById(content_header).className='tab_title ym_border_module ym_link'
		if (content_header==obj){ 
			document.getElementById(content).style.display='block';
			document.getElementById(content_header).className='tab_title ym_border_module ym_text'
		}
	}
}


/*
 * Ham thuc hien chuc nang tab box BanBe trang Profile
 * Type:	function
 * Name:	TNPro_actTabFriend
 * Date:	2008/05/21
 * @param obj la ID cua tab
 */
function TNPro_actTabFriend(obj){
	for (var i=1; i<4; i++){
		var content = "ym_banbe_con"+i;
		var content_header = "ym_banbe_tab"+i;
		$('#'+content).hide();
		$('#'+content_header).removeClass('active_icon');
		if (content_header==obj){ 
			$('#'+content).fadeIn('normal');
			$('#'+content_header).addClass('active_icon');
		}
	}
}

/*
 * Ham thuc hien chuc nang them link box Blogroll
 * Type:	function
 * Name:	TNPro_blogroll
 * Date:	2008/06/02
 * @param obj la ID cua tab
 */
function TNPro_blogroll(obj){
	$('#'+obj).fadeIn('slow');
	if (obj=="ym_blogrollB") selfDiv = obj.replace('B','A');
	else selfDiv = obj.replace('A','B');
	$('#'+selfDiv).hide();
}

/*
 * Ham thuc hien chuc nang copylink cua cac box playlist
 * Type:	function
 * Name:	TNPro_HighlightAll
 * Date:	2008/06/02
 * @param obj la ID cua tab
 */
function HighlightAll(theField) {
	var tempval=eval("document."+theField).select()
}

/*
 * Ham thuc hien chuc nang back to top
 * Type:	function
 * Name:	GoToTop
 * Date:	2008/06/025
 * @param 
 */
jQuery(function( $ ){
	$('#gototop').click(function(){
		$.scrollTo( 0, 700);					
		return false;
	});
	$('#gotocm').click(function(){
	$.scrollTo('#ym_writebl_inner', 700);					
		return false;
	});
})

function IsNumeric(ch){
	if(ch=="0"||ch=="1"||ch=="2"||ch=="3"||ch=="4"||ch=="5"||ch=="6"||ch=="7"||ch=="8"||ch=="9"||ch=="A"||ch=="B"||ch=="C"||ch=="D"||ch=="E"||ch=="F"||ch=="a"||ch=="b"||ch=="c"||ch=="d"||ch=="e"||ch=="f")
		return 1
	else
		return 0
}

function isHex(str){
	var i,ch
	if(str.length>6)
		return false
		
	for(i=0;i<str.length;i++){
		ch=str.substring(i,i+1)
		if(!IsNumeric(ch))
			return false
	}
	return true
}

/*
 * Collapse/Expand description info
 * Type:	onload function
 * Name:	
 * Date:	2008/07/09
 */
var desInfo, desInfo_s;
var link_viewAll_f = "... <a href='#' class='ym_link' onclick='desInfo_control";
jQuery(document).ready(function(){	
	desInfo = $('#desInfo').html();
	if (desInfo){
		var desInfo_len = eval(desInfo.length);
		if (desInfo_len > 100){		
			desInfo_s = desInfo.substring(0,100);
			var link_viewAll = link_viewAll_f + "(\"all\"); return false;'>Xem thêm</a>";
			$('#desInfo').html(desInfo_s + link_viewAll);		
		}	
	}
});
function desInfo_control(opt){
	if (opt=="all"){
		$('#desInfo').html(desInfo);
		var link_viewAll = link_viewAll_f + "(); return false;'>Thu gọn</a>";
		$('#desInfo').html(desInfo + link_viewAll);
	}
	else{
		$('#desInfo').html(desInfo_s);
		var link_viewAll = link_viewAll_f + "(\"all\"); return false;'>Xem thêm</a>";
		$('#desInfo').html(desInfo_s + link_viewAll);
	}
}

/*
 * Collapse/Expand lyric for music flash player
 * Type:	function
 * Name:	
 * Date:	2008/07/10
 */
function open_lyric(){
	if (document.getElementById("songfeature")) document.getElementById("songfeature").style.height="230px";
}
function close_lyric(){
	if (document.getElementById("songfeature")) document.getElementById("songfeature").style.height="89px";
}


/*
 * Align image center box
 * Type:	function
 * Name:	
 * Date:	2008/08/29
 */
function align_center_image(boxID, h){
	var imgs = $(boxID + " img");
	imgs.each( function(){								
			if(this.height < h){
				$(this).css('marginTop', function(){
					return (h-this.height)/2 +"px";
				})
			}
	});
}
align_center_image("#ym_tinnhanh_inner",45);

/*
 * progress bar
 * Type:	function
 * Name:	
 * Date:	2008/12/22
 */
var progessbar={
	status:1,
	start:function(){
			progessbar.status=1;
			progessbar.play();
		},
	play:function(){
		$('.wormbar').css('left','-106px')
		$('.wormbar').animate({
			left:'480px'
		  }, 2500,function(){
			if(progessbar.status) progessbar.play();
		});
	},
	stophere:function(){
		progessbar.status=0;
	}
}
/*
 * setThemeDefault
 * Type:	function
 * Name:	
 * Date:	2009/01/08
 */
function setThemeDefault(){
	var arrLink;
	var strLink=null;
	var str;
	var itemindex;
	var templink=null;			   	
	arrLink=$('head > link');
	if(arrLink.length>0){
		for(i=0;i<arrLink.length;i++){
			str=$(arrLink[i]).attr('href');
			if(str.match('2cols')) strLink='css/common/ym_2cols_default.css';
			if(str.match('3cols')) strLink='css/common/ym_3cols_default.css';
			if(str.match('ym_demo')||str.match('ym-demo')) 
				{
					templink= $(arrLink[i]).attr('href');
					itemindex=i;
				}
		}	
	}
	if(strLink!=null && templink!=null){
		$('.btndefault .fortheme').toggle(			
			function(){
				$('head link').eq(itemindex).attr('href',strLink);
				$(this).text('Xem có Theme');
				return false;
			},
			function(){
				$('head link').eq(itemindex).attr('href',templink);
				$(this).text('Xem không Theme');
				return false;
			}
		);
	}
	return false;
}
/*
 * view check box import blog
 * Type:	function
 * Name:	
 * Date:	24/03/2009
 */
/*function closebox(boxId){
	$('#'+boxId).hide();
}
$('.qcheck').click(function(){
	$('#boxqcheck').css('top',$(this).offset().top+12);
	$('#boxqcheck').css('left',$(this).offset().left);
	$('#boxqcheck').show();
	return false;
});

$('.closecont .close').click(function(){
	closebox('boxqcheck');				  
})
$('.chktype').click(function(){
	closebox('boxqcheck');
	$('#boxqcheck input').attr('checked',true);
	$('#boxcheck .active').removeClass('active')
	$(this).parent().addClass('active');									   
})
*/
$('.chktype').click(function(){
	$('#boxqcheck input').attr('checked',true);									   
})

/*
* Ham thuc hien chuc nang resize kich thuoc app
* Type:	onload function
* Name:	
* Date:	2009/04/01
*/
function autoReFlash(boxId)
{
var f;
if(boxId)
{
	var objthis=$('#'+boxId);
		 if($.browser.msie)
		 {
			f=($(objthis).find('object').height())/($(objthis).find('object').width());
			$(objthis).find('object').width($(objthis).width()-20);
			$(objthis).find('object').height(($(objthis).width()-20)*f);
		}
		else
		{
			f=($(objthis).find('embed').height())/($(objthis).find('embed').width());
			$(objthis).find('embed').width($(objthis).width()-20);
			$(objthis).find('embed').height(($(objthis).width()-20)*f);
		}
	}
else
{
	$('.flashapp').each(function(){
			 if($.browser.msie)
			 {
				f=($(this).find('object').height())/($(this).find('object').width());
				$(this).find('object').width($(this).width()-20);
				$(this).find('object').height(($(this).width()-20)*f);
			}
			else
			{
				f=($(this).find('embed').height())/($(this).find('embed').width());
				$(this).find('embed').width($(this).width()-20);
				$(this).find('embed').height(($(this).width()-20)*f);
			}
		});	
}
}

/*
 * Dropdown menu settings
 * Type:	function
 * Name:	
 * Date:	2008/08/29
 */
var objmnu;
var tmnu;
$('.shortcutmenu').hover(		 
function(){
	objmnu=$(this).parents('.edit_control').find('.dropmnu');
	if($(objmnu).css('display')!='none'){
		clearTimeout(tmnu);
	}
	else{
		$('.dropmnu').hide();
		clearTimeout(tmnu);
	}
	$(objmnu).show();
},
function(){
	tmnu=setTimeout(function(){
		$(objmnu).hide();
	},1000);
	
});	
$('.dropmnu').hover(
function(){
	clearTimeout(tmnu);
},
function(){
	tmnu=setTimeout(function(){
		$(objmnu).hide();
	},1000);
	
});	
/*
 * chon user gui fungif
 * Type:	function
 * Name:	
 * Date:	12/04/2009
 *	tridn@von-inc.com
 */
var defaultNum;
function fun_selpeople(obj,defaultNum){
	defaultNum=defaultNum?defaultNum:3;
	var strlink=$(obj).parent().children('.nickname').attr('href');
	var strlinktext=$(obj).parent().children('.nickname').text();
	var strid=$(obj).attr('id');
	var strval=$(obj).attr('value');
	var strlang=$(obj).attr('lang');
	var strsrc=$(obj).attr('src');
	var strran=$(obj).attr('randomnum');
	if($('#addpeople .node').length<defaultNum){
		if($(obj).attr('checked')==true)
		{
			$(obj).attr('disabled','disabled');
			fun_addpeople(strid,strlink,strlinktext,strval,strlang,strsrc,strran)
		}
	}
	else{
		$(obj).attr('checked',false);
		fun_messerr();
		return false;	
	}
}
function fun_addpeople(strid,strlink,strlinktext,strval,strlang,strsrc,strran){
	var str='<div class="node" id="_'+strid+'" val="'+strval+'" lang="'+strlang+'" src="'+strsrc+'"  randomnum="'+strran+'" ><div class="cor_r"><a href="'+strlink+'" class="username">'+strlinktext+'</a><div class="iconclose" onclick="fun_removepeople(\'_'+strid+'\')"></div></div></div>'
	var nodeObj=$(str);
	$(nodeObj).hide();
	$('#addpeople').append($(nodeObj));
	$(nodeObj).fadeIn(500);
}
function fun_removepeopleall(){
	$('#addpeople .node').each(function(i){
		fun_removepeople($(this).attr('id'));
	})
}
function fun_removepeople(strid){
	$('#'+strid).fadeOut(500,function(){
	$('#'+strid).remove();        
	});
	var listId=strid.substr(1,strid.length);
	$('#'+listId).attr('disabled','');
	$('#'+listId).attr('checked',false);
 }
function fun_messerr(){
	$('#errselpeople').stop(true,true);
	$('#errselpeople').fadeIn(10,function(){
		$(this).fadeOut(5000);								  
	})
}
function fun_setchecked(boxlistid){
	var listId,strid;
	boxlistid=boxlistid?boxlistid:'listpeople';
	$('#'+boxlistid+' li input').attr('disabled','');
	$('#'+boxlistid+' li input').attr('checked',false);
	$('#addpeople .node').each(function(i){
		strid=$(this).attr('id');							
		listId=strid.substr(1,strid.length);
		$('#'+listId).attr('disabled','disabled');
		$('#'+listId).attr('checked',true);
	});
}
function fun_setcheckedtagblog(boxlistid){
	setTimeout(function(){
		if(g_arrObjSel)
		{
			var listId,strid;
			boxlistid=boxlistid?boxlistid:'listpeople';
			$('#'+boxlistid+' li input').attr('disabled','');
			$('#'+boxlistid+' li input').attr('checked',false);
			$(g_arrObjSel).each(function(i){
				strid=$(this).attr('id');							
				listId=strid.substr(1,strid.length);
				$('#'+listId).attr('disabled','disabled');
				$('#'+listId).attr('checked',true);
			});
			$('#addpeople').html($(g_arrObjSel));
		}
	},500)
}
var g_arrObjSel;
function addselnickname(toID){
	$('#'+toID).html('');
	$('#txtYumerID').val('');
	$('#txtYumerUsername').val('');
	g_arrObjSel=$('#addpeople .node');
	$('#addpeople .node').each(function(i){
		var obj=$(this).find('.username').clone();
		$('#'+toID).append($(obj));
		if(i==$('#addpeople .username').length-1)
		{
			$('#'+toID).append('<span></span>');
			$('#txtYumerID').val($('#txtYumerID').val()+$(this).attr('randomnum'));
			$('#txtYumerUsername').val($('#txtYumerUsername').val()+$(this).attr('lang'));
		}
		else
		{
			$('#'+toID).append('<span> , </span>');
			$('#txtYumerID').val($('#txtYumerID').val()+$(this).attr('randomnum')+',');
			$('#txtYumerUsername').val($('#txtYumerUsername').val()+$(this).attr('lang')+',');
		}
		
	});
	caobox.close_popup();
}
function delayCheck(inttime){
			setTimeout(function(){
								fun_setchecked('listpeople');
						},500);
}

function uni_submitSearch()
{
	text=jQuery.trim($("input#uni_timnhanh").val());
	if(!text){
		alert("Xin vui lòng nhập từ khóa");
		return false;
	}
	unitybarSubmit();
	return false;
}


function unitybarSubmit()
{
	var text=jQuery.trim($("input#uni_timnhanh").val());
	if (!text){
		$("#uni_timnhanh").focus();
	}
	else{
		var type = $("#uni_idtype").val();
		var action = document.uni_frmSearch.action;
		window.location.href=action+type+"/"+text;
	}
}

/*try{
	ajax.callback.global.onComplete = function(){
		fun_setchecked('listpeople');
		};
}
catch(err)
{
}
*/
/*----------------tridn22/10/2009*/
var tmnu2;
$('#hasmore').hover(		 
function(){
	var myol=$(this).offset().left;
	var myot=$(this).offset().top+20;
	$('#subtop').css("left",myol);
	$('#subtop').css("top",myot);
	clearTimeout(tmnu2);		
	$('#subtop').show();
},
function(){
	tmnu2=setTimeout(function(){
		$('#subtop').hide();
	},100);
	
});	
$('#subtop').hover(
function(){
	clearTimeout(tmnu2);
},
function(){
	tmnu2=setTimeout(function(){
		$('#subtop').hide();
	},100);
	
});	
/*------end----------tridn22/10/2009*/