function focus_comment(){	
	document.location="#comment_section";
	$("#comment_text").focus();
}
var timers={};

var posting_comment=false;

var faqQuestionLimit = 500;

function post_comment(){
	if(posting_comment)return;
	clearTimeout(timers.comments);
	var t = $("#comment_text").val();
	//var t = $("#comment_text").val().trim();

	if(!IS_LOGGED){
		checkLoginpop("You need to login to post a comment!!");
		return;
	}
	if(t==""){
		$("#comment_messages").html("<div class='msg'>Please enter comment</div>").css({"display":"block"});
		timers.comments=setTimeout(function(){$("#comment_messages").css({"display":"none"})},3000);
		return;
	}

	var comment = BASE+'comment/post/k/'+POGO.id;
	posting_comment=true;
	var j=$(this);
	//j.addClass(" posting");
	jQuery('#comment_loader').show();
	jQuery('#postacomment').hide();
	$.ajax({
  type: "POST",
  url: comment,
	data : 'comment='+window.encodeURIComponent(t),
  dataType: "html",
	  success:function(xhr){
	  //j.removeClass(" posting");
	    jQuery('#comment_loader').hide();
		jQuery('#postacomment').show();
		posting_comment=false;
	  $("#comment_text").val("");
	  $("#text_counter").val("500");
	  var msg= '';
	  if(xhr=="notconfirmed")msg='Your parent has not allowed you to post any comments';
	   if(xhr=="success"){
		   msg='Comment has been posted successfully and queued for moderation';
			

	   }
		if(msg!=''){
			$("#comment_messages").html("<div class='msg'>"+msg+"</div>").css({"display":"block"});
			timers.comments=setTimeout(function(){$("#comment_messages").css({"display":"none"});},10000);
		}

  }
});

}



var comment_processing=false;

function get_comments(what, layout){
	if (typeof layout == "undefined") {
    optional = "default";
  }

	if(comment_processing)return;
	comment_processing=true;
	$.ajax({type: "GET",url:BASE+'comment/get/k/'+POGO.id+'?page='+what+'&layout='+layout,dataType: "html",success:function(xhr){comment_processing=false;$("#comment_content").html(xhr);}});
}

var jokes_processing=false;

function get_jokes(what){
	if(jokes_processing)return;
	jokes_processing=true;
        
        $("#loader").ajaxStart(function(){
            $(this).show();
        });

        $("#loader").ajaxStop(function(){
            $(this).hide();
        });


	$.ajax({type: "GET",url:BASE+'jokes/getjokeslist/?page='+what,dataType: "html",success:function(xhr){jokes_processing=false;$("#jokes_content").html(xhr);}});
	//$.ajax({type: "GET",url:BASE+'jokes/getjokepagination/?page='+what,dataType: "html",success:function(xhr){jokes_processing=false;$("#jokes_pagination").html(xhr);}});

}


function attach_jokes_scroller(func){
var el = $("#jokes_content").offset();

$(window).bind("scroll",function(){
	var winh=document.documentElement.clientHeight;
	var s = document.documentElement.scrollTop;

	if((winh + s) >=el.top){

		$(window).unbind("scroll");
		func();
	}
});
}

function attach_scroller(func){
var el = $("#comment_content").offset();

$(window).bind("scroll",function(){	
	var winh=document.documentElement.clientHeight;
	var s = document.documentElement.scrollTop;
	
	if((winh + s) >=el.top){

		$(window).unbind("scroll");		
		func();
	}
});
}

var video_processing=false;
function video_goto(where,t,show,me){
	if(video_processing)return;
	video_processing=true;
	$.ajax({type: "GET",url:BASE+'videos/paginate?page='+where+'&t='+t+'&show='+show,dataType: "html",success:function(xhr){video_processing=false;$("#all_videos").html(xhr);}});
}

var news_processing=false;
function news_goto(where,t,show,me){
	if(news_processing)return;
	news_processing=true;
	$.ajax({type: "GET",url:BASE+'news/paginate?page='+where+'&t='+t+'&show='+show,dataType: "html",success:function(xhr){news_processing=false;$("#all_news").html(xhr);}});
}

var wallpaper_processing=false;
function wallpaper_goto(where,t,show,me,item){
	if(wallpaper_processing)return;
	wallpaper_processing=true;
	$.ajax({type: "GET",url:BASE+'downloads/paginate?page='+where+'&t='+t+'&show='+show+'&item='+item,dataType: "html",success:function(xhr){wallpaper_processing=false;$("#all_items").html(xhr);}});
}


function game_cat_select(){
	if(this.value=="")return;
	if(this.value=="all"){document.location.href=BASE+"games";return;}
	var c=this.options[this.selectedIndex].getAttribute("nu");
	/*$.ajax({type: "GET",url:BASE+'games/paginate?page=1&t='+c+'&show='+this.value,dataType: "html",success:function(xhr){video_processing=false;$("#all_games").html(xhr);}});*/

	document.location.href=BASE+'games/index/total/'+c+'/show/'+this.value+'/type/0/page/1';
}

function game_goto(where,t,show,type,tme){
	if(video_processing)return;
	video_processing=true;
	$.ajax({type: "GET",url:BASE+'games/paginate?page='+where+'&t='+t+'&show='+show+'&type='+type,dataType: "html",success:function(xhr){video_processing=false;$("#all_games").html(xhr);}});
}

function game_type_goto(p,t,s,ty,me){
	$("#game_tabs li").each(function(){this.className="";});
	me.parentNode.className="current";
	game_goto(p,t,s,ty,me);
}

function video_cat_select(){
	if(this.value=="")return;
	if(this.value=="all"){document.location.href=BASE+"webtv";return;}
	var c=this.options[this.selectedIndex].getAttribute("nu");
	/*
	$.ajax({type: "GET",url:BASE+'videos/paginate?page=1&t='+c+'&show='+this.value,dataType: "html",success:function(xhr){video_processing=false;$("#all_videos").html(xhr);}});
	*/
	document.location.href=BASE+'webtv/index/total/'+c+'/show/'+this.value+'/type/0/page/1';
}

function pop_create_popup(id){
	if(!IS_LOGGED){
		checkLoginpop("You need to login to create a topic!!");
		return;
	}
	$("#topic_title").val("");
	$("#topic_errors").css({"display":"none"});
	$("#topic_text").html("");
	var t=$("#topic_popup");
	$("#topic_forum").val(id);
	var doc=$(document);
	var c=$('<div id="popup_later"> </div>').css({"position":"absolute","top":0,"left":0,"width":doc.width(),"height":doc.height(),"background-color":"#198EA8","z-index":102});
	$(document.body).append(c);
	t.css({"visibility":"hidden","display":"block","position":"absolute"});
	var w=t.width();var h=t.height();
	var dh=document.documentElement.clientHeight;var dw=doc.width();
	var off_left=(dw-w)/2;var off_top=(dh-h)/2;
	t.css({"top":off_top+document.documentElement.scrollTop,"left":off_left,"visibility":"visible","z-index":105});
}

function open_post_popup(id,title,body){
	if(!IS_LOGGED){
		checkLoginpop("You need to login to post a comment on a topic!!");
		return;
	}
	$("#post_errors").html("");
	$("#replytext").html("");
	$("#post_messages").css({"display":"none"});
	$("#comment_title").html(title);
	$("#comment_body").html(body);
	var t=$("#post_popup");
	$("#topicid").val(id);
	var doc=$(document);
	var c=$('<div id="popup_later"> </div>').css({"position":"absolute","top":0,"left":0,"width":doc.width(),"height":doc.height(),"background-color":"#198EA8","z-index":102});
	$(document.body).append(c);
	t.css({"visibility":"hidden","display":"block","position":"absolute"});
	var w=t.width();var h=t.height();
	var dh=document.documentElement.clientHeight;var dw=doc.width();
	var off_left=(dw-w)/2;var off_top=(dh-h)/2;
	t.css({"top":off_top+document.documentElement.scrollTop,"left":off_left,"visibility":"visible","z-index":105});
}

function close_postpop(){
	$("#popup_later").remove();
	$("#post_popup").css({"display":"none"});
}

function open_loginpop(id){
	var t=$("#login_popup");
	$("#topic_forum").val(id);
	var doc=$(document);
	var c=$('<div id="popup_later"> </div>').css({"position":"absolute","top":0,"left":0,"width":doc.width(),"height":doc.height(),"background-color":"#198EA8","z-index":102});
	$(document.body).append(c);
	t.css({"visibility":"hidden","display":"block","position":"absolute"});
	var w=t.width();var h=t.height();
	var dh=document.documentElement.clientHeight;var dw=doc.width();
	var off_left=(dw-w)/2;var off_top=(dh-h)/2;
	t.css({"top":off_top+document.documentElement.scrollTop,"left":off_left,"visibility":"visible","z-index":105});
}




var posting_topic=false;
function do_create_topic(){
	if(posting_topic)return;

	var title=$("#topic_title");
	var forum=$("#topic_forum");
	var content=$('#topic_text').html();
	var error='';
	if(title.val()==""){
		error='<div class="msg">Please enter topic title</div>';
	}
	if(content==""){
		error+='<div class="msg">Please enter topic text</div>';
	}
	if(error!=''){
		$("#topic_errors").html(error).css({"display":"block"});
		return;
	}
	if(forum.val()==''){
		return;
	}
	$("#topic_errors").html(error).css({"display":"none"});
	posting_topic=true;	
	$.ajax({type: "POST",url:BASE+'messageboard/posttopic',data:'forumid='+forum.val()+'&topic_title='+window.encodeURIComponent(title.val())+'&topic_text='+window.encodeURIComponent(content),dataType: "html",success:function(xhr){posting_topic=false;
	if(xhr=="topicadded"){
		$("#page_messages").html('<div class="msg">Topic is created and queued for moderation.</div>').css({"display":"block"});
		clear_topic_popup('topic_popup');
		window.setTimeout(function(){$("#page_messages").css({"display":"none"})},6000);
	}
	}});
}

var mb_is_posting = false;

function do_post(){
	if(mb_is_posting)return;	
	var topicid=$("#topicid");
	var comment=$('#replytext').html();
	var error='';

	if(comment==""){
		error+='<div class="errors">Please enter post</div>';
	}
	if(error!=''){
		$("#post_messages").html('<div class="msg">'+error+'</div>').css({"display":"block"});
		//window.setTimeout(function(){$("#post_messages").css({"display":"none"})},4000);
		return;
	}
	if(topicid.val()==''){
		return;
	}
	$("#post_messages").css({"display":"none"});
	mb_is_posting=true;	
	$.ajax({type: "POST",url:BASE+'messageboard/commentpost',data:'topicid='+topicid.val()+'&body='+window.encodeURIComponent(comment),dataType: "html",success:function(xhr){mb_is_posting=false;
	if(xhr=="postadded"){
		close_postpop();
		$("#page_messages").html('<div class="msg">Your reply to this topic has been posted and queued for moderation</div>').css({"display":"block"});
		window.setTimeout(function(){$("#page_messages").css({"display":"none"})},6000);
		$("#post_errors").html("Your post is queued for moderation.");
	}
	}});
}

var loginpop=false;
function checkLoginpop(message){
	if(IS_LOGGED)return;
	if(loginpop)return;
	loginpop=true;
	if(typeof message=="string"){
		$("#login_messages").html('<div class="msg">'+message+'</div>').css({"display":"block"});
	}else{
		$("#login_messages").css({"display":"none"});
	}
	var content_id="";
	var content_data="";
	//$("#game_play_area").css({"display":"none"});	
	var t=$("#login_popup");
	document.body.appendChild(t[0]);
	var doc=$(document);
	
	$("#playing").css({"visibility":"hidden"});
	$("#game_play_area").css({"visibility":"hidden"});

	var c=$('<div id="popup_later"> </div>').css({"position":"absolute","top":0,"left":0,"width":doc.width(),"height":doc.height(),"background-color":"#198EA8","z-index":102});

	$(document.body).append(c);
	t.css({"visibility":"hidden","display":"block","position":"absolute"});
	var w=t.width();var h=t.height();
	var dh=document.documentElement.clientHeight;var dw=doc.width();
	var off_left=(dw-w)/2;var off_top=(dh-h)/2;
	t.css({"top":off_top+document.documentElement.scrollTop,"left":off_left,"visibility":"visible","z-index":105});
	$("#popusername").focus();
	return false;
}

function clear_topic_popup(){
	$("#popup_later").remove();
	$("#topic_popup").css({"display":"none"});
}
function closelogin(handler){
	loginpop=false;
	$("#popup_later").remove();
	$("#login_popup").css({"display":"none"});
	if(typeof handler=="function")handler();
}
function opengameplay(){			
	$("#playing").css({"visibility":"visible"});
	$("#game_play_area").css({"visibility":"visible"});
}


function openDownloadPop(divId) {

    jQuery('#file_form_'+divId).next().next().remove();
    jQuery.blockUI({ 
        message: jQuery('#download_div_'+divId),
        css: {
        padding:        0,
        margin:         0,
        width:          '25%',
        top:            '25%',
        left:           '25%',
        textAlign:      'left',
        color:          '#000',
        border:         '',
        //backgroundColor:'#fff',
        cursor:         ''
    },
    overlayCSS:  {
        backgroundColor: '#000',
        opacity:0.6,
        cursor:	''
    }
});
}

function openTopDownloadPop(divId) {
    jQuery.blockUI({ 
        message: jQuery('#top_download_div_'+divId),
        css: {
        padding:        0,
        margin:         0,
        width:          '25%',
        top:            '25%',
        left:           '25%',
        textAlign:      'left',
        color:          '#000',
        border:         '',
        //backgroundColor:'#fff',
        cursor:         ''
    },
    overlayCSS:  {
        backgroundColor: '#000',
        opacity:0.6,
        cursor:	''
    }
});
}

function closeDownloadPop() {
   jQuery.unblockUI();
}

function loadWallpaper(form) {
   //console.log(form);
   var url = jQuery("#"+form+" :input[name='selected_file']:checked").val();
   //console.log(url);
    var id = jQuery("#"+form+" :input[name='selected_file_id']").val();
   if(url) {
       var link = BASE+"downloads/getfile?f="+url+"&id="+id;
       jQuery.unblockUI();
       window.open(link,"Download");
       jQuery(".file_form").each(function () {
           var form = this;
           jQuery(':input', form).each(function() {
               this.checked = false;
           });

       });
   }
   else {
       jQuery("#"+form).next().after('<div class="errormsg"><div class="msg">Please select a resolution</div></div><div class="clear"></div>');
       //alert('Please select a resolution');
   }
   
}

function loadScreensaver(form) {
   //console.log(form);
   var url = jQuery("#"+form+" :input[name='selected_file']").val();
   //console.log(url);
   var id = jQuery("#"+form+" :input[name='selected_file_id']").val();
   var link = BASE+"downloads/getfile?f="+url+"&id="+id;

   //console.log(link);
   jQuery.unblockUI();
   window.open(link,"Download");
   jQuery(".file_form").each(function () {
       var form = this;
       jQuery(':input', form).each(function() {
           this.checked = false;
       });

   });
}

function checkField(actualText, Id,defaultText) {
    if(jQuery('#'+Id).val() == '' || jQuery('#'+Id).val() == defaultText) {
        jQuery('#'+Id).val(defaultText);
    }
    else {
        jQuery('#'+Id).val(actualText);
    }
}

function clearField(actualText, Id,defaultText) {
     if(jQuery('#'+Id).val() == defaultText) {
        jQuery('#'+Id).val('');
    }
}

function get_home_page_counts(){
	var topgames=$(".jgetcontent");var t=[];var i=0;var st=new Array();topgames.each(function(){i=this.id.replace(/^\d/,'');st.push('"'+i+'"');});
	st = "["+st.join(",")+"]";
	$.ajax({type: "POST",url:BASE+'views/get',data:'ids='+st, dataType: "html",success:function(xhr){
		  var obj=eval("("+xhr+")");
		  var text={"game":" plays","video":" views","ss":" downloads","wall":" downloads"};
		  for(var o in obj){
			  var m = o.match(/^(\w*?)_\d+/);
				if(m && obj[o][0]>0){
					var col=$("."+o);
					col.html(obj[o][0]+text[m[1]]);
					var stars=$('<div id="ratings">'+obj[o][1]+'</div>');
					col.parent().append(stars);	
				}
			}
		}
	});
}


function get_mad_page_counts(){
	var topgames=$(".jgetcontent");var t=[];var i=0;var st=new Array();topgames.each(function(){i=this.id.replace(/^\d/,'');st.push('"'+i+'"');});
	st = "["+st.join(",")+"]";	
	$.ajax({type: "POST",url:BASE+'views/get',data:'star=big&ids='+st,dataType: "html",success:function(xhr){
	  var obj=eval("("+xhr+")");
	  var text={"game":" plays","video":" views","ss":" downloads","wall":" downloads"};
	  for(var o in obj){
		  var m = o.match(/^(\w*?)_\d+/);
		if(m && obj[o][0]>0){
			var col=$("."+o);		
			//col.html(obj[o][0]+text[m[1]]);
			var stars=$('<div class="mad_ratings">'+obj[o][1]+'</div>');
			col.html(stars);
			//col.parent().append(stars);	
		}}}});
}

function openForgotPassPop() {
    jQuery('#forgot_pass_identity').val('');
    jQuery('#forgot_pass_response').html('');
    jQuery('#forgot_pass_response').hide();
    jQuery('#forgot_pass_err').next().html('');
    jQuery('#forgot_pass_err').hide();


    jQuery.blockUI({
        message: jQuery('#forgot_pass_div'),
        css: {
        padding:        0,
        margin:         0,
        width:          '25%',
        top:            '40%',
        left:           '25%',
        textAlign:      'left',
        color:          '#000',
        border:         '',
        //backgroundColor:'#fff',
        cursor:         ''
    },
    overlayCSS:  {
        backgroundColor: '#000',
        opacity:0.6,
        cursor:	''
    }
});
}

function closeForgotPassPop() {
   jQuery.unblockUI();
}

function sendForgotPassEmail() {
    jQuery('#forgot_pass_response').next().html('');
    jQuery('#forgot_pass_response').hide();
    jQuery('#forgot_pass_loader').show();
    jQuery('#forgot_pass_btn').hide();

    var identity = jQuery('#forgot_pass_identity').val();
    if(identity == "") {
        jQuery('#forgot_pass_response').html('<div class="msg">Please enter your username or email</div>');
        jQuery('#forgot_pass_response').show();
        jQuery('#forgot_pass_loader').hide();
        jQuery('#forgot_pass_btn').show();
        return false;
    }
    jQuery.ajax({
        type : 'POST',
        data : 'identity='+identity,
        url  : BASE+'auth/retrievepassword',
        success : function (response) {
            var chunks = response.split('|');
            //if(chunks[1] == 'TRUE') {
                jQuery('#forgot_pass_response').html('<div class="msg">'+chunks[2]+'</div>');
                jQuery('#forgot_pass_response').show();
//            }
//            else {
//                jQuery('#forgot_pass_err').next().html(chunks[2]);
//                jQuery('#forgot_pass_err').show();
//            }
            jQuery('#forgot_pass_loader').hide();
            
            
        }
    });

    jQuery('#forgot_pass_btn').show();
    return false;

}

function openReactivationPop() {
    jQuery('#reactivation_identity').val('');
    jQuery('#reactivation_response').html('');
    jQuery('#reactivation_response').hide();
    jQuery('#reactivation_err').next().html('');
    jQuery('#reactivation_err').hide();


    jQuery.blockUI({
        message: jQuery('#reactivation_div'),
        css: {
        padding:        0,
        margin:         0,
        width:          '25%',
        top:            '40%',
        left:           '25%',
        textAlign:      'left',
        color:          '#000',
        border:         '',
        //backgroundColor:'#fff',
        cursor:         ''
    },
    overlayCSS:  {
        backgroundColor: '#000',
        opacity:0.6,
        cursor:	''
    }
});
}

function sendReactivationEmail() {
    jQuery('#reactivation_response').next().html('');
    jQuery('#reactivation_response').show();
    jQuery('#reactivation_loader').show();
    jQuery('#reactivation_btn').hide();

    var identity = jQuery('#reactivation_identity').val();
    if(identity == "") {
        jQuery('#reactivation_response').html('<div class="msg">Please enter your username or email</div>');
        jQuery('#reactivation_response').show();
        jQuery('#reactivation_loader').hide();
        jQuery('#reactivation_btn').show();
        return false;
    }
    jQuery.ajax({
        type : 'POST',
        data : 'identity='+identity,
        url  : BASE+'auth/reactivation',
        success : function (response)
		{
			alert(response);
            var chunks = response.split('|');
            jQuery('#reactivation_response').html('<div class="msg">'+chunks[2]+'</div>');
			jQuery('#reactivation_response').show();
			jQuery('#reactivation_loader').hide();
		}
    });

    jQuery('#reactivation_btn').show();
    return false;

}

$(document).ready(function(){
var objects = document.getElementsByTagName("object");
for (var i = 0; i < objects.length; i++)
{
   // objects[i].outerHTML = objects[i].outerHTML;
}
});


function checkFaqField() {
        if(checkLoginpop()) {
            jQuery('#login_popup').css('top','450px');
            //clearField(jQuery('#faq_question').val(),'faq_question','Type your question here');
        }
        clearField(jQuery('#faq_question').val(),'faq_question','Type your question here');
    }
    function validateFaqForm() {

        var q = jQuery('#faq_question').val();

	if (q == '' || q == 'Type your question here')
	{
                jQuery('.msg').html('Please enter your question');
                jQuery('.faq_errormsg').show();
	    	jQuery('#faq_question').focus();
		return false;
	}
	return true;
    }


    function calcLimit() {
        
        var tex = jQuery('#faq_question').val();
        var len = tex.length;
        if(len > faqQuestionLimit){
            tex = tex.substring(0,faqQuestionLimit);
            jQuery('#faq_question').val(tex);
            return false;
        }
    }

    function getFaqPages(pageNo,module) {
    jQuery.ajax({
        type : 'POST',
        url  : BASE+'faq/'+module+'/page/'+pageNo,
        data :'page='+pageNo,
        success : function (response) {
            jQuery('#listing').html(response);
        }
    });
}
