/*function prizeType(id)
{
	if(jQuery('#'+id).attr("class") != 'active')
	{
		var scss = jQuery("#"+id).attr("class")+" tabActive";
		jQuery("#first").attr("class","tab firstpriceTab");
		jQuery("#second").attr("class","tab secondpriceTab");
		jQuery("#third").attr("class","tab thirdpriceTab");
		jQuery("#other").attr("class","tab otherpriceTab");
		jQuery("#"+id).attr("class",scss);
		jQuery("#first_content").css("display","none");
		jQuery("#second_content").css("display","none");
		jQuery("#third_content").css("display","none");
		jQuery("#other_content").css("display","none");
		jQuery("#"+id+"_content").css("display","block");
		return false;
	}
}*/

var isFullscreen = false;

function closeAdv() {
	clearTimeout(myInterval);
	jQuery('#advPregame, #advPregameClose').css('display','none');

	jQuery('#flashgameWrapper').css('visibility','visible');
	
	if (!isFullscreen){
		jQuery('#flashgameBox').css('height', parseInt(height)+110);
		jQuery('#flashgameWrapper').css('height', parseInt(height)+93);
		jQuery("#advUndergame").css({position: "absolute", top: parseInt(height)+17,marginLeft: 46-parseInt(xpos),width: "auto"});
	}
}

function setFrameGame() {
	xpos = 5 + parseInt(xpos);
	var x = xpos + 'px';
	var y = ypos + 'px';
	jQuery('#flashgameWrapper').css({
		width: 560-xpos,
		height: 422,
		marginTop: '-15px',
		marginLeft:x
	});
	
	if (bannerPregame){
		jQuery('#flashgameWrapper').css('visibility','hidden');
		jQuery(document).ready(function (){
			myInterval = setTimeout("closeAdv()", 8000);
		});
	} else{
		closeAdv();
	}

	jQuery('#flashgameWrapper').css('height', parseInt(height)+93);
	jQuery("#advUndergame").css({position: "absolute", top: parseInt(height)+17,marginLeft: 46-parseInt(xpos),width: "auto"});
}

function fullscreen(){
	isFullscreen = true;
	
	var wrapperObject = jQuery("#siteContent").offset();
	var objTop = 0-wrapperObject.top;
	var objLeft = 0-wrapperObject.left;
	
	jQuery('.advbox .admicro_banner').css({'visibility':'hidden'});

	jQuery('#flashgameWrapper').css({
		top: objTop,
		left: objLeft,
		marginLeft: '0px',
		width: jQuery(document).width(),
		height: jQuery(document).height()
	});
	
	jQuery('#flashgameOverlay').css({
		background: '#000000',
		display: 'block',
		opacity: 0.7,
		zIndex: 999,
		width: jQuery(document).width(),
		height: jQuery(document).height()
	});
	
	//var flw = Math.floor((jQuery(window).width()-$('#flashgameEmbed').width())/2);
	var flw = Math.floor((jQuery(window).width()-parseInt(or_width))/2);
	var scrollY = window.pageYOffset || document.documentElement && document.documentElement.scrollTop || document.body.scrollTop || 0;
	//var flh = scrollY + Math.floor((jQuery(window).height()-$('#flashgameEmbed').height())/2);
	var flh = scrollY + Math.floor((jQuery(window).height()-parseInt(or_height))/2);
	var fh = flh + 'px';
	var fw = flw + 'px';
	var w = or_width + 'px';
	var h = or_height + 'px';
	jQuery('#flashgameEmbed').css({
		top: fh,
		left: fw,
		width: w,
		height: h,
		zIndex: 1000
	});
	
	jQuery('#advUndergame').css({
		position: 'absolute',
		top: flh+parseInt(or_height)+17,
		zIndex: 2002,
		width: jQuery(window).width()+'px',
		marginLeft: '0px'
	});
	
	jQuery('#flashgameCloseFullscreen').css({
		position: 'absolute',
		display: 'block',
		top: fh,
		left: (parseInt(flw)+parseInt(or_width))+'px',
		zIndex: 1000
	});
}

function exitFullscreen(){
	isFullscreen = false;
	
	//var new_height =  Math.floor(height*570/width);
	var w = width + 'px';
	var h = height + 'px';
	var x = xpos + 'px';
	var y = ypos + 'px';
	
	jQuery('.advbox .admicro_banner').css({'visibility':'visible'});
	
	jQuery('#flashgameWrapper').css({
		top: '',
		left: '',
		width: 560-xpos,
		height: parseInt(height)+94,
		marginTop: '-15px',
		marginLeft:x,
		marginBottom:'10px'
	});
	
	jQuery('#flashgameOverlay').css({
		display: 'none'
	});

	jQuery('#flashgameEmbed').css({
		top: '',
		left: '',
		margin: '0px auto',
		width: w,
		height: h
	});
	
	jQuery('#advUndergame').css({
		position: 'absolute',
		top: parseInt(height)+17,
		marginLeft: 46-parseInt(xpos),
		width: 'auto'
	});
	
	jQuery('#flashgameCloseFullscreen').css({
		display: 'none'
	});
}

/* Edit by Mr-Kem on 08-12-2008 */
function limitText(limitField, limitCount, limitNum) {
	if (limitField.value.length > limitNum) {
		limitField.value = limitField.value.substring(0, limitNum);
	} else {
		limitCount.value = limitNum - limitField.value.length;
	}
}

function textFieldClick(textField) {
	var textNote = "Bạn có thể nhập tối đa 150 ký tự cho mỗi lần bình luận! Mọi bình luận không phù hợp với thuần phong mỹ tục sẽ bị chặn vì bị coi là không hợp lệ!";
	if (textField.value == textNote) {
		textField.value = "";	
	}
}
/* End edit */

function callExternalInterface() {
    thisMovie("VoteMovie").goHome(vote_time,vote_point, voteRate);
}
function thisMovie(movieName) {
    if (navigator.appName.indexOf("Microsoft") != -1) {
        return window[movieName]
    }
    else {
        return document[movieName];
    }
}


function div2Type(id)
{
	if(jQuery('#'+id).attr("class") != 'active')
	{
		if( (id == 'pointlist') && (jQuery("#pointlist_content").html()=="") )
		{
			jQuery.get(WEB_ROOT+"ajax.php?act=game&code="+id+"&game_id="+game_id, function(data)
			{
				jQuery("#"+id+"_content").html(data);
			});

		}
		
		if( id == 'price' && jQuery("#price_content").html()=="" )
		{
			jQuery.get(WEB_ROOT+"ajax.php?act=game&code="+id+"&game_id="+game_id, function(data)
			{
				jQuery("#"+id+"_content").html(data);
			});
		}
		if (id == 'download') {
			if (userId == 0) {
				alert('Bạn phải đăng nhập để tải game!');
			} else {
				jQuery.get(WEB_ROOT+"ajax.php?act=game&code="+id,{game_id:game_id}, function(data) {
					if (data == '') {
						alert('Hiện tại chức năng này đang bị lỗi, bạn sử dụng lại sau nhé!');
					} else {
						window.location.href = data;
					}
				});
			}
		}else {
			jQuery("#gameinfo").attr("class","tab gameinfoTab");
			jQuery("#sharegame").attr("class","tab sharegameTab");
			jQuery("#pointlist").attr("class","tab pointlistTab");
			jQuery("#price").attr("class","tab priceTab");
			jQuery("#"+id).attr("class",jQuery("#"+id).attr("class")+" tabActive");
			jQuery("#gameinfo_content").css("display","none");
			jQuery("#sharegame_content").css("display","none");
			jQuery("#pointlist_content").css("display","none");
			jQuery("#price_content").css("display","none");
			jQuery("#"+id+"_content").css("display","block");
		}
	}
}

function resetWidth() {
	jQuery("#gameFlashEmbed").css({
		width: "100%"
	});
	
	jQuery("#border_div").css({
		margin: 0,
		width: jQuery(document).width(),
		height: jQuery(document).height(),
		background: "#000000",
		opacity: 0.8
	});
	
	var flw = Math.floor((jQuery(window).width()-parseInt(width))/2);
	var flh = Math.floor((jQuery(window).height()-parseInt(height))/2);
	
	jQuery("#out_div").css({
		position:"absolute",
		margin: 0,
		top: flh,
		left: flw,
		width: parseInt(width),
		height: parseInt(height)
	});
	
	var rsTop = parseInt(flh)+parseInt(height);
	
	jQuery("#reset_size").css({
		position:"absolute",
		margin: "0px auto",
		top: rsTop,
		textAlign: "center",
		display: "block",
		width: "100%"
	});
}
function closediv()
{
	jQuery("#reset_size").css({display:"none"});
	
	jQuery("#gameFlashEmbed").css({
		width: 968
	});
	
	if (width>580 || height>436)
		var marginLeft = 30;
	else
		var marginLeft =  Math.floor((580-parseInt(width))/2)+30;
				
	if (type==3){
		var margin = "440px 0px 0px "+marginLeft+"px";
		jQuery("#border_div").css({
			margin: margin,
			width: 580,
			height: 436,
			background: "none",
			opacity: 1
		});
	} else{
		var margin = "310px 0px 0px "+marginLeft+"px";
		jQuery("#border_div").css({
			margin: margin,
			width: 580,
			height: 436,
			background: "none",
			opacity: 1
		});
	}
	if(width > 580 || height > 436)
	{
		jQuery("#out_div").css({
			position:"absolute",
			margin: 0,
			width: 580,
			height: 436,
			top: 0,
			left: 0
		});
	}
	else
	{
		jQuery("#out_div").css({
			position:"absolute",
			margin: 0,
			width: width,
			height: height,
			top: 0,
			left: 0
		});
	}
	
	jQuery("#reset_size").css({
		display: "none"
	});
}

function add_cart()
{
	if(userId == 0)
	{
		alert("Bạn phải đăng nhập mới được sử dụng chức năng này.");
	}
	
	jQuery.get(WEB_ROOT+"ajax.php?act=game&code=add_cart&game_id="+game_id, function(data){
		if(data == 'success')
		{
			jQuery("#text_add_game").html('<a onclick="remove_cart();" href="#" id="add_game_to_cat" class="button removefromcartButton"</a>');
		}
	});
}

function remove_cart()
{
	if(userId == 0)
	{
		alert("Bạn phải đăng nhập mới được sử dụng chức năng này.");
	}
	jQuery.get(WEB_ROOT+"ajax.php?act=game&code=add_cart&game_id="+game_id, function(data){
		if(data == 'success')
		{
			jQuery("#text_add_game").html('<a onclick="add_cart();" href="#" id="add_game_to_cat" class="button addtocartButton"></a>');
		}
	});
}

/* Add by Mr-Kem on 13-12-2008 */
//Ham thong bao Link game hong//
function submitReport () {
	if(userId == 0) {
		alert ("Bạn hãy đăng nhập để sử dụng chức năng này!");
	} else {
		if (!report) {
			jQuery.get(WEB_ROOT+"ajax.php?act=game&code=reportlink&game_id="+game_id, function(data){
				if(data == 'success') {
					report = true;
					alert ("Thông báo link game hỏng của bạn đã được gửi đi, xin cảm ơn!");
				}else {
					alert ("Chức năng này hiện tại đang bị lỗi, bạn hãy sử dụng sau nhé!");
				}
			});		
		}else {
			return;
		}
	}
}

/* Add by Mr-Kem on 14-12-2008 */
//Ham Vote Game//
function submitVote (point) {
	if(!vote) {
		jQuery.get(WEB_ROOT+"ajax.php?act=game&code=voteGame&game_id="+game_id+"&point="+point, function(data){
			vote = true;
			data = data.split(",");
			vote_time = data[0];
			vote_point = data[1];
			voteRate = data[2];
			callExternalInterface();	
		});		
	}else {
		alert ("Bạn chỉ được phép vote 1 lần cho 1 lượt chơi!");
	}
}

/* DzungDH added 27/11/2008 */
function changeAvatar(){
	jQuery('#changeAva').css("display","none");
	jQuery('#avatarImg').animate({opacity: 0.2}, function (){
		jQuery.get(WEB_ROOT+"ajax.php", {act: "game", code: "new_avatar"}, function (data){
			if (data.length > 0){
				var img = document.createElement('img');
				img.onload = function(){
					avatar = data;
					jQuery('#avatarImg').attr("src", WEB_ROOT+"images/avatars/"+data).animate({opacity: 1});
					jQuery('#changeAva').css("display","block");
					img.onload = null;
				}
				img.src =  WEB_ROOT + "images/avatars/" + data;
			} else{
				jQuery('#avatarImg').animate({opacity: 1});
			}
		});												 
	});
}

function createAvaList() {
	jQuery.get(WEB_ROOT+"ajax.php", {act: "game", code: "get_ava_list"}, function (data){
		if(data.length > 0) {
			jQuery('#FrameAva').html(data);
		}
	});
}

function openAvaList() {
	if(!opened) {
		opened = true;
		jQuery('#FrameAva').css("visibility","visible");
		jQuery('#FrameAva').animate({opacity: 1}, function (){
			jQuery('body').bind('click', function (){
				jQuery('#FrameAva').animate({opacity: 0.2}, function (){
					jQuery(this).css("visibility","hidden");
					opened = false;
					jQuery('body').unbind('click');
				});
			});												
		});
	}
}

function selectAvatar(avaLink) {
	avatar = avaLink;
	opened = false;
	jQuery('#avatarImg').animate({opacity: 0.2}, function (){		
		jQuery('#avatarImg').attr("src", WEB_ROOT+"images/avatars/"+avaLink).animate({opacity: 1});
		//jQuery('#changeAva').css("display","block");									 
	});
	jQuery('#FrameAva').animate({opacity: 0.2}, function (){
		jQuery(this).css("visibility","hidden");
		//jQuery(window).unbind('click');
	});	
}

function getComment(page){
	jQuery('#commentList').animate({opacity: 0.3}, function (){
		jQuery.get(WEB_ROOT+"ajax.php", {act: "game", code: "comment", page: page, game_id: game_id, iplay_time: iplay_time}, function (data){
			if (data.length>0)
				jQuery('#commentList').html(data);																		
			
			jQuery('#commentList').css('height', 'auto').animate({opacity: 1});
			var paging_width 	= jQuery('#pageLeft').width() + jQuery('#pageRight').width() + jQuery('#pageMid').width();
			jQuery('#pageDib').css('width',paging_width);
			commentListHeight	= jQuery('#commentList').height();			
		});														 
	});
}

/* Add by Mr-Kem on 12-01-2009 */
function getAwardList(page) {
	if((game_id >= 1066) && (game_id <= 1110)) {
		jQuery('#AwardList').animate({opacity: 0.3}, function (){
			jQuery.get(WEB_ROOT+"ajax.php", {act: "game", code: "awardList", page: page, game_id: game_id}, function (data){
				if (data.length>0) {
					jQuery('#AwardList').html(data);																		
				}
				jQuery('#AwardList').css('height', 'auto').animate({opacity: 1});
				//commentListHeight = jQuery('#AwardList').height();			
			});														 
		});
	}
}
/* End add */

/* Add by Mr-Kem on 08-12-2008 */
function removeLineBreak (str) {
	var counter = 0;
	var textNote = str.split("");
	var i = 0;
	while (i < textNote.length-1) {
		if (str.charCodeAt(i) == 10) {
			textNote[i] = "";
		}
		i++;
	}
	return textNote.join("");
}
/* End add code */

function submitComment(){
	if (loading) return;
	
	if (avatar != "") {
		var content = jQuery('#comment_content').val();
		var username = jQuery('#comment_nickname').val();
		
		if ((content.length==0) || (content=='Bạn có thể nhập tối đa 150 ký tự cho mỗi lần bình luận! Mọi bình luận không phù hợp với thuần phong mỹ tục sẽ bị chặn vì bị coi là không hợp lệ!')) {
			alert('Bạn hãy nhập vào nội dung bình luận nhé!');
			jQuery('#comment_content').focus();
			return;
		}
		
		if (username.length==0){
			alert('Bạn hãy nhập vào nickname của mình nhé!');
			jQuery('#comment_nickname').focus();
			return;
		}
		
		loading = true;
		
		content = removeLineBreak(content);
		
		jQuery.get(WEB_ROOT+"ajax.php?act=game&code=act_comment&game_id="+game_id, {username:username, content: content, avatar: avatar}, function (data){
			var json = eval('(' + data + ')');
			if (json.status){
				jQuery('#commentList').html(json.message);
				var paging_width 	= jQuery('#pageLeft').width() + jQuery('#pageRight').width() + jQuery('#pageMid').width();
				jQuery('#pageDib').css('width',paging_width);
				jQuery('#comment_content').val("");
				if (json.commentStatus==0){
					alert('Bình luận của bạn sẽ qua quá trình kiểm duyệt trước khi được phép hiển thị!');
				}
			} else alert(json.message);
			loading = false;
		});
	}else {
		alert('Chờ tí nhé...hệ thống đang tải dữ liệu! Bạn hãy gửi lại sau nhé!');
	}
}

function delComment(cm_id) {
	jQuery.get(WEB_ROOT+"ajax.php?act=game&code=del_comment&game_id="+game_id, {comment_id:cm_id}, function (data){
		if(data.length > 0) {
			jQuery('#commentList').html(data);																		
			jQuery('#commentList').css('height', 'auto').animate({opacity: 1});
			var paging_width 	= jQuery('#pageLeft').width() + jQuery('#pageRight').width() + jQuery('#pageMid').width();
			jQuery('#pageDib').css('width',paging_width);
		} else {
			alert('Hệ thống bị lỗi khi xử lý!');	
		}
	});
}


function showCommentList(){
	jQuery('#commentList').animate({height: commentListHeight}, 500);
	jQuery('#bbBinhluanykien .bbHeader').html('[ - <a href="javascript:hideCommentList();" class="lightblueText">thu nhỏ box</a> ]');
}

function hideCommentList(){
	jQuery('#commentList').animate({height: 0}, 500);
	jQuery('#bbBinhluanykien .bbHeader').html('[ + <a href="javascript:showCommentList();" class="lightblueText">mở rộng box</a> ]');
}

//jQuery(window).load(function (){
jQuery(document).ready(function() {
	//advPregame();
	changeAvatar();
	getComment(0);
	getAwardList(0);
	createAvaList();
});
/* DzungDH added 27/11/2008 */
