String.prototype.trim = function() { return this.replace(/^\s+|\s+$/, ''); };
//Auto resize image:
function resizeImage(img,nwsize,nhsize)
{
	//var img = document.getElementById(id);		
	var nWidth=img.width;
	var	nHeight=img.height;	
	nScale = nWidth/nHeight;		
	
	var imgWidth=img.width;
	var imgHeight=img.height;
	
	wScale = imgWidth/nwsize;
	hScale = imgHeight/nhsize;
	
	if(wScale >= 1 || hScale >= 1){
		if(wScale > hScale){
			nWidth = nwsize;
			nHeight = imgHeight/wScale;
			nMargin = (nhsize - nHeight)/2;
			
			img.width = nWidth;
			img.height = nHeight;
			img.style.margin = nMargin + 'px 0px';
		}
		else{
			nHeight = nhsize;
			nWidth = imgWidth/hScale;
			nMargin = (nwsize - nWidth)/2;
			
			img.width = nWidth;
			img.height = nHeight;
			img.style.margin = '0px '+ nMargin + 'px';
		}
	}
	else{
		wMargin = (nwsize-imgWidth)/2;
		hMargin = (nhsize-imgHeight)/2;
		
		img.style.margin = hMargin+'px '+ wMargin + 'px';
	}
	
}


function rgbToHex(rgb) {
	var rgbvals = /rgb\((.+),(.+),(.+)\)/i.exec(rgb);
	var rval = parseInt(rgbvals[1]);
	var gval = parseInt(rgbvals[2]);
	var bval = parseInt(rgbvals[3]);

	return '#' + (
		rval.toString(16) +
		gval.toString(16) +
		bval.toString(16)
	).toUpperCase();
}

function check_search_content() {
	var content = $("#searchContent").val();
	if (content == 'Nhập từ khóa cần tìm...')
		$("#searchContent").val("");
		
	if (rgbToHex($("#searchContent").css('color')) == '#CCCCCC')
		$("#searchContent").css('color', '#000000');
}

function changeSearhType(type, title){
	if(type=='all'){
		$("#header_search_form").attr('action', 'http://search.tamtay.vn' ).attr('method', 'get');
	}
	else if(type=='video'){
		$("#header_search_form").attr('action', search_url ).attr('method', 'post');
	}
	else if(type=='photo'){
		$("#header_search_form").attr('action', 'http://search.tamtay.vn/?f=photo' ).attr('method', 'get');
	}
	else if(type=='music'){
		$("#header_search_form").attr('action', 'http://search.tamtay.vn/?f=music' ).attr('method', 'get');
	}
	else if(type=='blog'){
		$("#header_search_form").attr('action', 'http://search.tamtay.vn/?f=blog' ).attr('method', 'get');
	}
	$("#selected_cat").html(title);
	$("#search_type").val(type);
}

function checkVideoData()
{
    if ($('#video_title').val()=='')
    {
        alert('Bạn phải nhập tên video');
        return false;
    }
    
    if ($('#clip_cat_id').val()=='0')
    {
        alert('Bạn phải chọn chuyên mục của video');
        return false;
    }
    
    if ($('#tags').val()=='')
    {
        alert('Bạn phải nhập từ khóa cho video');
        return false;
    }
    return true;
}

var video_common=
{
	authorList: "",
	shareURL: "",		
	commentPage: 0,
	posting: false,
	addedMyTT: false,
	promotedClip: new Array(),
	animId: 0,
	currAnim: 0,
	alertLogin: function(){
		alert("Bạn cần đăng nhập để thực hiện được chức năng này");
		return false;
	},
	toggleDesc: function(){
		if ($("#clip_full_desc").css('display')=='none')
		{
			$("#clip_full_desc").show();
			$("#clip_short_desc").hide();
			$("#clip_toggle_desc").html('thu gọn');
		}
		else
		{
			$("#clip_full_desc").hide();
			$("#clip_short_desc").show();
			$("#clip_toggle_desc").html('xem hết');
		}
		return false;
	},
	makeFriend: function(uid, uname){
		if (confirm("Bạn chắc chắn muốn kết bạn với " + uname))
		{
			$.getJSON("http://www.tamtay.vn/myfriend/makeFriend/" + uid + "?jsoncallback=?", function(data){alert("Yêu cầu kết bạn đã được gửi đến " + uname);});
		}
	},
	alertContent: function(nid)
	{
		var width = 500;
		var height = 220;
		var left = parseInt((screen.availWidth/2) - (width/2));
		var top = parseInt((screen.availHeight/2) - (height/2));
		var windowFeatures = "width=" + width + ", height=" + height + ", left=" + left + ", top=" + top + ", screenX=" + left + ",screenY=" + top + ", resizable=no, status=no, menubar=no, scrollbars=no";
		window.open("/alert/" + nid, "subWind", windowFeatures);
		return false;
	},
	getAuthor: function()
	{
		if ($("#c_cungtacgia").html().trim()!="")
		{
			return;
		}
		$.ajax({
		   url: this.authorList,
		   beforeSend: function(req){
				$("#ctg_loading_author").show();
			},
		   error: function(req, status, info){
		   	   alert("Có lỗi khi lấy dữ liệu. Hãy thử lại lần khác");
		   },
		   success: function(data, status){

		   	   $("#c_cungtacgia").html(data);
		   	   $("#c_cungtacgia").show();
		   	   $("#t_cungtacgia").removeClass("title_none");
		   	   $("#t_cungtacgia").addClass("title_click");
		   },
		   complete: function(res, s){
				$("#ctg_loading_author").hide();
			}
		});
	},
	getCategory: function()
	{
		if ($("#c_cungloai").html().trim()!="")
		{
			return;
		}
		$.ajax({
		   url: this.categoryList,
		   beforeSend: function(req){
				$("#ctl_loading_category").show();
		   },
		   error: function(req, status, info){
		   	   alert("Có lỗi khi lấy dữ liệu. Hãy thử lại lần khác");
		   },
		   success: function(data, status){
		   	   $("#c_cungloai").html(data);
		   	   $("#c_cungloai").show();
		   	   $("#t_cungloai").removeClass("title_none");
		   	   $("#t_cungloai").addClass("title_click");
		   },
		   complete: function(res, s){
				$("#ctl_loading_category").hide();
			}
		});
	},
	getRelate: function()
	{
		if ($("#c_related").html().trim()!="")
		{
			return;
		}
		$.ajax({
		   url: this.relateList,
		   beforeSend: function(req){
				$("#ctl_loading_related").show();
		   },
		   error: function(req, status, info){
		   	   //alert("Có lỗi khi lấy dữ liệu. Hãy thử lại lần khác");
		   },
		   success: function(data, status){
		   	   $("#c_related").html(data);
		   	   $("#c_related").show();
		   	   $("#t_related").removeClass("title_none");
		   	   $("#t_related").addClass("title_click");
		   },
		   complete: function(res, s){
				$("#ctl_loading_related").hide();
			}
		});
	},
	shareVideo: function()
	{
		var open=function(hash)
		{
			$('#flashvideo').hide();
			hash.w.show();
		}
		var close=function(hash)
		{ 
			hash.w.hide(); 
			hash.o.remove();
			$('#flashvideo').show();
		};
		$('#share_dialog').jqm({onShow: open, onHide: close}).jqmShow();
	},
	sendMail: function(){
		if ($('#mail_address').val()=="")
		{
			alert('Bạn cần nhập địa chỉ email người nhận');
			return;
		}
		$('#share_dialog').jqmHide();
		$.post(this.shareURL, $("#mail_address").serialize() + '&' + $('#mail_subject').serialize() + '&' + $('#mail_content').serialize(), function(data){});
	},
	addMyTT: function(url)
	{
		if (this.addedMyTT)
		{
			alert("Video này đã có trong nhà bạn");
			return;
		}
		if (!confirm("Bạn chắc chắn muốn thêm video này vào nhà mình?"))
		{
			return;
		}
		$.ajax({
		   url: url,
		   error: function(req, status, info){
		   	   alert("Có lỗi khi gửi dữ liệu. Hãy thử lại lần khác");
		   },
		   success: function(data, status){
		   	   alert("Video đã được thêm vào nhà bạn");
		   	   this.addedMyTT=true;
		   }
		});
	},
	switchView: function(t, url){
		$.cookie("vt", t, {path: '/', expires: 30});
		window.location.href=url;
	},
	switchPlayer: function(pv, url){
		$.cookie("pv", pv, {path: '/', expires: 30});
		window.location.href=url;
	},
	filterUrl: function(type){
		$('#filterBoxContent a.filter').attr('href', function(index){

			if(order=='c'+(index+1)){
				$(this).addClass('filterOn');
			}
			return '/'+type+'/'+cid+'/'+filter+'/c'+(index+1);
		});
	},
	insertSymbol: function(symbol){
		var obj=$('#comment_content').get(0);
		if (document.selection)
		{
			obj.focus();
			sel = document.selection.createRange();
			sel.text = symbol;
		}
		else if (obj.selectionStart || obj.selectionStart == 0)
		{
			var startPos = obj.selectionStart;
			var endPos = obj.selectionEnd;
			obj.value = obj.value.substring(0, startPos)
						+ symbol
						+ obj.value.substring(endPos, obj.value.length);
		} 
		else
		{
			obj.value += symbol;
		}
	},
	startAnim: function(){
		if($('#top_video').html() != ''){
			this.promotedClip[0] = $('#top_video').html();
			this.promotedClip[1] = $('#top_video_1').html();
			this.promotedClip[2] = $('#top_video_2').html();
			this.animId = setInterval("video_common.switchPromoted()", 5000);
		}
	},
	stopAnim: function(){
		if(this.animId > 0){
			clearInterval(this.animId);
			this.animId = 0;
		}
	},
	switchPromoted: function(){
		if(video_common.animId > 0){
			$('#top_video').fadeOut(1000, function(){
				var next_id=video_common.currAnim+1;
				if (next_id>2)
				{
					next_id=0;
				}
				$('#top_video').html(video_common.promotedClip[next_id]);
				$('#top_video').fadeIn(1000);
			});
			video_common.currAnim += 1;
			if (video_common.currAnim>2){
				video_common.currAnim=0;
			}
		}
	}
}


$(document).ready(function() {
	$("a#login_button").fancybox({ 
		'hideOnContentClick':false,
		'frameWidth':370,
		'frameHeight':230,
		'overlayOpacity':0.7
	});
	
	//get_user_info();
	
	//Define second typing (use "slashing" cursor at the end):
	//new TypingText(document.getElementById("statisticDiv"), 120, function(i){ var ar = new Array("\\", "|", "/", "-"); return " " + ar[i.length % ar.length]; });
	
	//TypingText.runAll();	
});
