$(document).ready(function(){
	$("img.imgGame").lazyload({ 
		threshold : 0
		, effect : 'fadeIn'
		, effectspeed : 'slow'
		, placeholder : '/lib/img/blank.gif'
	});
	
});

//----------------------------------------------------------


function changePage($id,$page,$url){
	$('#'+$id).empty();
	$("#"+$id).load($url+"&"+$page).hide().fadeIn('slow'); 
}

$(document).ready(function(){
	$('.ads350').click(function(){
		adsClick('google_ads_350_250','.ads350');
	})	
	$('.ads728').click(function(){
		adsClick('google_ads_728_90','.ads728');
	})	
})
function adsClick($type,$class){
	$.ajax({
		type: 'POST',
		url: '/ajax/ads/',
		data: { 'type': $type },
		dataType: 'json',
		success: function(msg)
		{
			if( msg.count > 3){
				$($class).empty();
			}
		}
	});
	return true;		
}

function voteGame($id,$type){
	$.ajax({
		type: 'POST',
		url: '/ajax/vote/',
		data: { 'id': $id, 'control': $type },
		dataType: 'json',
		success: function(msg)
		{
			if( msg.error=='' )
			{
				alert(msg.mess); return;
			}
			else
			{
				alert(msg.error); return;
			}
		}
	});
	

	$(".voteGame").load('/ajax/vote/?control=vote-load&iData='+$id);
	return true;
}

function addGame($id){
	$(".boxGameLike").load('/ajax/game/?control=add-game&id='+$id); 	
}

function removeGame($page_id,$control,$id){
	$('#'+$page_id+' .line-'+$id).fadeOut('slow',function() {
		$(this).remove();
		if( parseInt($('#'+$page_id+' .item-line').length) <= 0 ){
			$("#"+$page_id).load('/ajax/game/?control='+$control+'&id='+$id); 	
		}
	});
	$.ajax({
		type: 'GET',
		url: '/ajax/game/',
		data: { 'control' : $control, 'id': $id }
	});
	return true;	
}

// Show flash
	function ShowFlash_swf(FlashIDName, FlashFileName, FlashWidth, FlashHeight, Title, DNSSetting, WMODESetting, QSetting, FlashAlign){
		document.write('<OBJECT CLASSID="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"');
		document.write('CODEBASE="http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab#version=8,0,0,0" ');
		document.write(' ID="'+FlashIDName+'" WIDTH="' + FlashWidth + '" HEIGHT="' + FlashHeight + '" TITLE="' + Title + '" ALIGN="'+FlashAlign+'">');
		document.write('<PARAM NAME="movie" VALUE="'+ FlashFileName +'">');
		document.write('<PARAM NAME="quality" VALUE="'+QSetting+'">');
		/*document.write('<PARAM NAME="bgcolor" VALUE="'+FlashBGColor+'">');  BGCOLOR="'+FlashBGColor+'"*/
		document.write('<PARAM NAME="wmode" VALUE="'+WMODESetting+'">');
		document.write('<PARAM NAME="allowScriptAccess" VALUE="'+DNSSetting+'">');
		document.write('<EMBED SRC="'+ FlashFileName +'"  NAME="'+FlashIDName+'"');
		document.write(' WIDTH="' + FlashWidth + '" HEIGHT="' + FlashHeight + '" TITLE="' + Title + '" QUALITY="'+QSetting+'" ');
		document.write(' ALLOWSCRIPTACCESS="'+DNSSetting+'" ALIGN="'+FlashAlign+'" WMODE="'+WMODESetting+'" TYPE="application/x-shockwave-flash" ');
		document.write(' PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer" >');
		document.write('</EMBED>');
		document.write('</OBJECT>');
	}
// ENd Show flash

//
<!--
function ShowFlash_dcr(dcr, width, height, bgcolor, sw1, sw2, sw3, sw4, sw5, sw6, sw7, sw8, sw9, swlist, version, menu) {
	if (!(sw1)) { sw1 = ""; }
	if (!(sw2)) { sw2 = ""; }
	if (!(sw3)) { sw3 = ""; }
	if (!(sw4)) { sw4 = ""; }
	if (!(sw5)) { sw5 = ""; }
	if (!(sw6)) { sw6 = ""; }
	if (!(sw7)) { sw7 = ""; }
	if (!(sw8)) { sw8 = ""; }
	if (!(sw9)) { sw9 = ""; }
	if (!(swlist)) { swlist = ""; }

	document.write('   <OBJECT CLASSID="clsid:166B1BCA-3F9C-11CF-8075-444553540000" CODEBASE="http://download.macromedia.com/pub/shockwave/cabs/director/sw.cab#version=' + version + '" WIDTH="' + width + '" HEIGHT="' + height + '">	');
	document.write('   <PARAM NAME="SRC" VALUE="' + dcr + '">	');
	document.write('   <PARAM NAME="BGCOLOR" VALUE="' + bgcolor + '">	');
	document.write('   <PARAM NAME="SW1" VALUE="' + sw1 + '">	');
	document.write('   <PARAM NAME="SW2" VALUE="' + sw2 + '">	');
	document.write('   <PARAM NAME="SW3" VALUE="' + sw3 + '">	');
	document.write('   <PARAM NAME="SW4" VALUE="' + sw4 + '">	');
	document.write('   <PARAM NAME="SW5" VALUE="' + sw5 + '">	');
	document.write('   <PARAM NAME="SW6" VALUE="' + sw6 + '">	');
	document.write('   <PARAM NAME="SW7" VALUE="' + sw7 + '">	');
	document.write('   <PARAM NAME="SW8" VALUE="' + sw8 + '">	');
	document.write('   <PARAM NAME="SW9" VALUE="' + sw9 + '">	');
	document.write('   <PARAM NAME="SWLIST" VALUE="' + swlist + '">	');
	document.write('   <PARAM NAME="logo" VALUE="true">	');
	document.write('   <PARAM NAME="progress" VALUE="true">	');
	document.write('	 <PARAM NAME="PlayerVersion" VALUE="11">');

	if (menu == 0) {
		document.write('   <PARAM NAME="SWREMOTE" VALUE="swSaveEnabled=\'false\' swVolume=\'false\' swRestart=\'false\' swPausePlay=\'false\' swFastForward=\'false\' swContextMenu=\'false\'">	');
		document.write('   <EMBED SRC="' + dcr + '" WIDTH="' + width + '" HEIGHT="' + height + '"  BGCOLOR="' + bgcolor + '"  logo="true' + '"  progress="true' + '" SW1="' + sw1 + '" SW2="' + sw2 + '" SW3="' + sw3 + '" SW4="' + sw4 + '" SW5="' + sw5 + '" SW6="' + sw6 + '" SW7="' + sw7 + '" SW8="' + sw8 + '" SW9="' + sw9 + '" SWLIST="' + swlist + '" SWREMOTE="swSaveEnabled=\'false\' swVolume=\'false\' swRestart=\'false\' swPausePlay=\'false\' swFastForward=\'false\' swContextMenu=\'false\'" PlayerVersion=11 PLUGINSPAGE="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveDirector" PALETTE="background"></EMBED>	');
	} else {
		document.write('   <PARAM NAME="SWREMOTE" VALUE="swSaveEnabled=\'false\' swVolume=\'false\' swRestart=\'false\' swPausePlay=\'false\' swFastForward=\'false\' swContextMenu=\'true\'">	');
		document.write('   <EMBED SRC="' + dcr + '" WIDTH="' + width + '" HEIGHT="' + height + '"  BGCOLOR="' + bgcolor + '"  logo="true' + '"  progress="true' + '" SW1="' + sw1 + '" SW2="' + sw2 + '" SW3="' + sw3 + '" SW4="' + sw4 + '" SW5="' + sw5 + '" SW6="' + sw6 + '" SW7="' + sw7 + '" SW8="' + sw8 + '" SW9="' + sw9 + '" SWLIST="' + swlist + '" SWREMOTE="swSaveEnabled=\'false\' swVolume=\'false\' swRestart=\'false\' swPausePlay=\'false\' swFastForward=\'false\' swContextMenu=\'true\'" PlayerVersion=11 PLUGINSPAGE="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveDirector" PALETTE="background"></EMBED>	');
	}
	document.write('   </OBJECT>	');
}
// -->
//

/* Menu top */
	showtime = 0;
	function changtab(id,count,clear){
		for(i=0;i<=count;i++){
			try{
				document.getElementById('tab_'+i).className='tab_normal';
				document.getElementById('tabconten_'+i).style.display='none';
				$("#tabconten_"+id).hide();
			}
			catch(e){}			
		}
		document.getElementById('tab_'+id).className='tab_select';
		//document.getElementById().style.display='block';
		$("#tabconten_"+id).show();
	}
/* End */

/* Click to change tab */
	function tabdiv(id,count){
		for(i=1;i<=count;i++){
			try{
				document.getElementById('tabt_'+i).className='n';
				document.getElementById('tabc_'+i).style.display='none';
			}
			catch(e){}			
		}
		document.getElementById('tabt_'+id).className='s';
		document.getElementById('tabc_'+id).style.display='block';
	}
	function tabdiv1(id,count){
		$(document).ready(function(){
			for(i=1;i<=count;i++){
				try{
					$('.tabg_'+i).removeClass('s');
					$('.tabg_'+i).addClass('n');
					$('#tabh_'+i).css('display','none');
				}
				catch(e){}			
			}
			$('.tabg_'+id).removeClass('n');
			$('.tabg_'+id).addClass('s');
			$('#tabh_'+id).css('display','block');
		})
	}
	function tabdiv2(id,count){
		for(i=1;i<=count;i++){
			try{
				document.getElementById('taba_'+i).className='n';
				document.getElementById('tabb_'+i).style.display='none';
			}
			catch(e){}			
		}
		document.getElementById('taba_'+id).className='s';
		document.getElementById('tabb_'+id).style.display='block';
	}
	
	function tab_ajax(tabclick,tabdisplay,tabid,count,url){
		for($i=1;$i<=count;$i++){
			$('#'+tabclick+$i).removeClass('s');
			$('#'+tabclick+$i).addClass('n');
			$('#'+tabdisplay+$i).empty();
		}
		$('#'+tabclick+tabid).removeClass('n');
		$('#'+tabclick+tabid).addClass('s');
		$('#'+tabdisplay+tabid).load(url);
	}
	
	
/* End */


$(document).ready(function()
{
    //Proccessing bar
    $("#loadingBar").ajaxSend(function() {
        $(this).show();
    }).ajaxComplete(function() {
        $(this).fadeOut("fast");
    });
    //End
    
	//Publish
    $('.publish, .featured, .frontpage, .active, .approve, .show, .hot , .new , .popular, .daily, .km, .home').click(function()
    {
        var $object = $(this);
        var $id     = $object.attr('id');
        var $task   = $object.attr('class');
        var $path   = $object.attr('path');
        var $state  = $object.attr('name');
        var $control = $('input.controller').val();
        if( !$control ){
            $control = 'controllers';
        }
        
        $.ajax({
            type: 'POST',
            url: $control,
            data: { 'task' : $task, 'id': $id, 'state' : $state, 'control': $task },
            dataType: 'json',
            success: function(msg)
            {   
                if( msg.error=='' )
                {
                    //alert(msg.mess);
                    if($state==1){
                        $object.attr('src', $path+'active_0.png').attr('name', '0');
                    }else{
                        $object.attr('src', $path+'active_1.png').attr('name', '1');   
                    }
                }
                else
                {
                    alert(msg.error); return;
                }
            }
        });
    })
    //End
	
	//Remove
    $('.remove').click(function()
    {
        if( !confirm('Are you sure to delete?') ){
            return false;    
        }
        
        var $object  = $(this);
        var $id      = $object.attr('id');
		var $task    = $object.attr('class');
        var $control = $('input.controller').val();
        if( !$control ){
            $control = 'controllers';
        }
        
        $.ajax({
            type: 'POST',
            url: $control,
            data: { 'task' : 'remove', 'id': $id, 'control': $task },
            dataType: 'json',
            success: function(msg)
            {
                if( msg.error=='' )
                {
					
                    $('tr.row-'+$id).fadeOut(600,function() {
                        $(this).remove();
                        if( parseInt($('.item-row').length) <= 0 ){
                            window.location.reload();
                        }
                    });
					
					//alert(msg.mess); return;
                }
                else
                {
                    alert(msg.error); return;
                }
            }
        });
        return true;
    })
    //End
	
	
	//Publish
    $('.game-save').click(function()
    {
        var $object = $(this);
        var $id     = $object.attr('id');
		var $path   = $object.attr('path');
		var $state  = $object.attr('name');

        $.ajax({
            type: 'POST',
            url: '/ajax/game-save/',
            data: { 'control': 'game-save', 'id': $id  },
            dataType: 'json',
            success: function(msg)
            {   
                if( msg.error=='' )
                { 
                    if($state=='cong.gif'){
                        $object.attr('src', $path+'tru.png').attr('name', 'cong.gif');
                    }
					alert(msg.mess); return;
                }
                else
                {
                    alert(msg.error); return;
                }
            }
        });
    })
    //End
});
