/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
var games = {
    showTopScore:function(gameId,option){
       var inactive = $('#active').val();
       if(inactive != ''){
          $('#'+inactive).hide();
          $('#command'+gameId+option).val('1');
          $('#showText'+gameId+option).text('เพิ่มความเห็น');
       }
       $('#showTopScore'+gameId+option).toggle();
    },
    suggestGame:function(gameId){
        var option = 'game';
        $.ajax({
                url:'modules/suggest/formSuggest.php',
                cache: false,
                data:{'refId':gameId, 'option':option, 'groupId':''},
                type:'POST',
                success:function(data){

                    if(data==-50){
                      sessionTimeout();
                    }else{
                        blockUI(data);

                    }//E-if
                }//E-succ

        });
    },
    showComment:function (refId,divide){
    //alert(refId+divide);
	//var status = $('#statusMember'+refId).val();
	//var mId = $('#memberID'+refId).val();
	var option = $('#command'+refId+divide).val();
	var comment = "comment"+refId+divide;
	var textFormat = "showText"+refId+divide;
	var keepDivs = "showComment"+refId+divide;
	var inactive = $('#active').val();//div id
	var format = $('#textFormat').val();//text div id
	var divKeep = $('#keepDIV').val();//td id
		if(option ==1){
			$('#'+inactive).hide('slow');//ซ่อน div
			$('#'+divKeep).html('');//เคลียร์ td
			$('#'+format).text('เพิ่มความเห็น');//เปลี่ยนข้อความ
            $('#showTopScore'+refId+divide).hide();
			$('#comment'+refId+divide).show("slow");
			$.ajax({
			url:'modules/comment/formJeedGameComment.php',
			cache: false,
			data:{'reqTypeCmt':'ajax','option':'game','refId':refId, 'mid':'0', 'status':'0', 'divide':divide},
			type:'POST',
			success:function(data){
					  $('#showComment'+refId+divide).empty().append(data);
					  $('#flgCommentProfile'+refId+divide).val('reply');
					  $('#showText'+refId+divide).text('ซ่อนความเห็น');
					  $('#command'+refId+divide).val('0');
				}
			});
			$('#active').val(comment);
			$('#textFormat').val(textFormat);
			$('#keepDIV').val(keepDivs);
		}else{
			$('#showComment'+refId+divide).empty();
			$('#showText'+refId+divide).text('เพิ่มความเห็น');
			$('#comment'+refId+divide).hide("slow");
			$('#command'+refId+divide).val('1');
		}
    },
    commentJGSub:function(){
       var flag = false;
       var msg = $('#messageXHTML_comment').val();

       if(msg == '' || msg == '<br />'){
            //errHTML('tdErrComment','กรุณากรอกรายละเอียดด้วยคะ');
            //$('#trErrComment').show();
            msgConfirm('กรุณากรอกรายละเอียดด้วยคะ','$.unblockUI()');
       }else{
           $('#trErrComment').hide();
           $('#btnSubmit').attr("disabled","true");
           flag = true;
       }

       if(flag == false){
           return false;
       }else{

           return true;

       }
    },
    commentJGSubSuccess:function (data){
        if(data == 1){
            var refId = $('#refId').val();
            var mId = $('#ownID').val();
            var status = $('#memState').val();
            var divide = $('#divNews').val();
            msgSuccess('บันทึกข้อมูลเรียบร้อยแล้วค่ะ');
            $.ajax({
              url:'modules/comment/formJeedGameComment.php',
              cache: true,
              data:{'reqTypeCmt':'ajax','mid':mId,'refId':refId, 'status':status, 'option':'game', 'divide':divide},
              type:'POST',
              success:function(data){
                  $('#showComment'+refId+divide).empty().append(data);
              }

          });
        }else if(data == -50){
            sessionTimeout();
        }else{
            alert(data);
        }
    },
    getFrmComment:function (){
	var RefID = $('#RefID').val();
	var CmmOption = $('#CmmOption').val();
	$.ajax({
		url:'modules/comment/formComment.php', 
		cache: true, 
		data:{'reqTypeCmt':'ajax','refId':RefID,'option':CmmOption}, 
		type:'POST', 
		success:function(data){
				if(CmmOption != 'sms'){
					$('#showAllComment').empty().append(data);
				}else{
					$('#showComment'+RefID).append(data);
				}
				getComment();
		}
	});
    },//E-func

    getComment:function(){
        var RefID = $('#RefID').val();
        var CmmOption = $('#CmmOption').val();
        $.ajax({
            url:'modules/comment/comment.php', 
            cache: true, 
            data:{'reqTypeCmt':'ajax','refId':RefID,'option':CmmOption}, 
            type:'POST', 
            success:function(data){
                    if(CmmOption != 'sms'){
                        $('#showAllComment').append(data);	
                    }else{
                        $('#showComment'+RefID).append(data);
                    }
            }
        });
    },
    addCommentJGMsg:function (refId){
        var nameEditor = $('#divNews').val();
        var msg = FCKeditorAPI.GetInstance('editorCommentJG').GetXHTML();
        $('#messageXHTML_comment').val(msg);
        $('#flgSaveProfile'+refId+nameEditor).val('save');
    },//E-func
    
    delJGComment:function(id){
         var currPage = $("#currPage").val();
         var currOffset = $("#currOffset").val();
         var totComment = $("#totComment").val();
         var limit = $("#limit").val();
         var option = $("#optComment").val();
        //alert(currPage+"\n"+currOffset);
        if(totComment==1 && currOffset>0){
           currPage = currPage-1;
           currOffset = currOffset-limit;
        }

        $.ajax({
            url:'modules/comment/deleteComment.php',
            cache: false,
            data:{'commentId':id},
            type:'POST',
            success:function(data){

                if(data == 1){
                    var divide = $('#order').val();
                    var refId = $('#refId').val();
                    var mId = $('#ownID').val();
                    var status = $('#memState').val();
                    var divide = $('#divNews').val();
                    msgSuccess('บันทึกข้อมูลเรียบร้อยแล้วค่ะ');
                    $.ajax({
                          url:'modules/comment/formJeedGameComment.php',
                          cache: true,
                          data:{'reqTypeCmt':'ajax','mid':mId,'refId':refId, 'status':status, 'option':'game', 'divide':divide},
                          type:'POST',
                          success:function(data){
                              //$('#showComment'+refId+divide).empty().append(data);
                              divPage(currPage,currOffset,'getCommentJeedGame');
                          }

                    });
                    $.unblockUI();
                }else{
                    alert(data);
                }
            }

        });
    },

    playGame:function(gameId,command){
        if(gameId != 0){
            var name = $('#game'+gameId).val();
                    $.ajax({
                        url:'modules/games/playGame.php',
                        cache: false,
                        data:{'reqType':'ajax','gameId':gameId},
                        type:'POST',
                        success:function(data){
                               $('#playgame').empty().append(data);
                               var tabpanel = Tabpanel["games_Tab"];
                               tabpanel.expandtab(tabpanel.tabs[1]);
                               //$('#whatPlay').css('display','block').text('เกม'+name);
                               //$("#nameGame").css('display','none');
                               $("li:hidden").show();
                               $('#showName').addClass('selected').text('เกม'+name);
                               $('#adsense').css('display','block');
                        }//End Success;

                    });
        }else{
           //var name = $('#game'+gameId).val();
                    $.ajax({
                        url:'modules/games/index.php',
                        cache: false,
                        data:{'reqType':'ajax'},
                        type:'POST',
                        success:function(data){
                               $('#playgame').empty().append(data);
                               var tabpanel = Tabpanel["games_Tab"];
                               tabpanel.expandtab(tabpanel.tabs[0]);
                               //$('#whatPlay').css('display','block').text('เกม'+name);
                               //$("li:hidden").show();
                               $("#nameGame").text('เกมล่าสุด');
                        }//End Success;

                    });
        }
    },
    
    playerThumbPreNext :function (currPage,currOffset){
          var gameId = $('#gameId').val();
          //var option = $('#StatMenu').val();
          //alert(gameId+"\n"+option);
          if($('#StatMenu').val()==''){
              var option = 'today';
          }else{
              var option = $('#StatMenu').val();
          }
          var imgloading = '<table width="100%" height="90%" ';
            imgloading += 'cellpadding="10" cellspacing="0" border="0">';
            imgloading += '<tr>';
            imgloading += '<td align="center" valign="middle">';
            imgloading += '<img src="images/utils/ajax-loader.gif" />';
            imgloading += '</td>';
            imgloading += '</tr>';
            imgloading += '</table>';
            $('#thumbPlayer').empty().append(imgloading); 
          $.ajax({
				url:'modules/games/playerThumb.php',
				cache: true,
				data:{'reqType':'ajax','currPage':currPage,'currOffset':currOffset,'gameId':gameId,'option':option},
				type:'POST',
				success:function(data){
                       
                       $('#thumbPlayer').focus().empty().append(data);
                                             
				}
			});
    },
    
   saveFav :function (refId){           
           //var refId = $("#refId").val();
           //var option = $("#option").val();

           $.ajax({
                url:'modules/favorite/formFav.php',
                cache: false,
                data:{'refId':refId, 'option':'game'},
                type:'POST',
                success:function(data){
                   if(data == -50){
                     sessionTimeout();
                   }else{
                        blockUI(data);
                    }//end if
                }

            });

    },

    confirmDelete :function(commentId){
        var msg = "คุณต้องการลบคอมเม้นต์นี้ใช่หรือไม่?";
        var func = "games.delJGComment("+commentId+")";
        msgConfirm(msg,func);
    },

    changeMenu :function(gameId){
        var option = $('#StatMenu').val();
        $.ajax({
                url:'modules/games/playerThumb.php',
                cache: false,
                data:{'reqType':'ajax','gameId':gameId,'option':option},
                type:'POST',
                success:function(data){
                    $('#thumbPlayer').focus().empty().append(data);
                }

       });
    },
    
    changeMenu2 :function(gameId,command){
        $('#StatMenu').val(command);
        if(command == 'today'){
            var text = 'สมาชิก JeedZone ที่เล่นเกมนี้: ';
            var textOrder = 'เรียงตามคะแนนสูงสุดในสัปดาห์นี้';
        }else{
            var text = 'เพื่อนที่เล่นเกมนี้: ';
            var textOrder = 'เรียงตามคะแนนสูงสุดที่เคยได้รับ'; 
        }
        //var option = $('#StatMenu').val();
        $.ajax({
                url:'modules/games/playerThumb.php',
                cache: false,
                data:{'reqType':'ajax','gameId':gameId,'option':command},
                type:'POST',
                success:function(data){
                    $('#thumbPlayer').focus().empty().append(data);
                    $('#textOption').html('<b>'+text+'</b>'+textOrder);
                }

       });
    }
}