var cc=0;
var dataText = new Array();
var rs;
var result;
var dataTextMsg;
function showHide(id,i) {
	if (cc==0) {
		cc=1
		document.getElementById(id).style.display="block";
		setResult(i);
	} else {
		cc=0
		document.getElementById(id).style.display="none";
	}
}
function setResult(id)
{
	$.ajax({
		url:'control.php?c=tips_result&id='+id+'&random=' + Math.random(),
		type:'POST',
		dataType:'json',
		data:{},
		success:function(data){
			if( data.status == 'true' )
			{
				if(data.row != 'false')
				{
					dataTextMsg = "";
					dataTextMsg += "\t\t\t\t\t\t<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" class=\"resulttbl\">\n";
					dataTextMsg += "\t\t\t\t\t\t<col width=\"150\" />\n";
					dataTextMsg += "\t\t\t\t\t\t<tr>\n";
					dataTextMsg += "\t\t\t\t\t\t	<td class=\"leftcol\">Date :</td>\n";
					dataTextMsg += "\t\t\t\t\t\t	<td class=\"rightcol\">"+data.row.tips_date+"</td>\n";
					dataTextMsg += "\t\t\t\t\t\t</tr>\n";
					dataTextMsg += "\t\t\t\t\t\t<tr>\n";
					dataTextMsg += "\t\t\t\t\t\t	<td class=\"leftcol\">Match Time :</td>\n";
					dataTextMsg += "\t\t\t\t\t\t	<td class=\"rightcol\">"+data.row.tips_match_time+"</td>\n";
					dataTextMsg += "\t\t\t\t\t\t</tr>\n";
					dataTextMsg += "\t\t\t\t\t\t<tr>\n";
					dataTextMsg += "\t\t\t\t\t\t	<td class=\"leftcol\">Post Time :</td>\n";
					dataTextMsg += "\t\t\t\t\t\t	<td class=\"rightcol\">"+data.row.tips_post_time+"</td>\n";
					dataTextMsg += "\t\t\t\t\t\t</tr>\n";
					dataTextMsg += "\t\t\t\t\t\t<tr>\n";
					dataTextMsg += "\t\t\t\t\t\t	<td class=\"leftcol\">Sport :</td>\n";
					dataTextMsg += "\t\t\t\t\t\t	<td class=\"rightcol\">"+data.row.tips_league_sports+"</td>\n";
					dataTextMsg += "\t\t\t\t\t\t</tr>\n";
					dataTextMsg += "\t\t\t\t\t\t<tr>\n";
					dataTextMsg += "\t\t\t\t\t\t	<td class=\"leftcol\">Match :</td>\n";
					dataTextMsg += "\t\t\t\t\t\t	<td class=\"rightcol\">"+data.row.tips_sports_all+"</td>\n";
					dataTextMsg += "\t\t\t\t\t\t</tr>\n";
					dataTextMsg += "\t\t\t\t\t\t<tr>\n";
					dataTextMsg += "\t\t\t\t\t\t	<td class=\"leftcol\">Tip :</td>\n";
					dataTextMsg += "\t\t\t\t\t\t	<td class=\"rightcol\">"+data.row.tips_detail+"</td>\n";
					dataTextMsg += "\t\t\t\t\t\t</tr>\n";
					dataTextMsg += "\t\t\t\t\t\t<tr>\n";
					dataTextMsg += "\t\t\t\t\t\t	<td class=\"leftcol\">Odds :</td>\n";
					dataTextMsg += "\t\t\t\t\t\t	<td class=\"rightcol\">"+data.row.tips_odds+"</td>\n";
					dataTextMsg += "\t\t\t\t\t\t</tr>\n";
					dataTextMsg += "\t\t\t\t\t\t<tr>\n";
					dataTextMsg += "\t\t\t\t\t\t	<td class=\"leftcol\">Result :</td>\n";

					if(data.row.tips_result == "0") result = "";
					if(data.row.tips_result == "1") result =  "<img src=\"images/accept.png\"/>";
					if(data.row.tips_result == "2") result =  "<img src=\"images/icondraw.png\"/>";
					if(data.row.tips_result == "3") result =  "<img src=\"images/cross.png\"/>";
					
					dataTextMsg += "\t\t\t\t\t\t	<td class=\"rightcol\"><div style=\"position:relative;color:#c6bfa5;\">"+data.row.tips_result_name+result+"</div></td>\n";
					dataTextMsg += "\t\t\t\t\t\t</tr>\n";
					dataTextMsg += "\t\t\t\t\t\t</table>\n";
				}else{
					dataTextMsg = '<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><center><h1><font color="#FF00FF">No Permission!</font></h1><center><br/><br/>';
				}
				$("#setResult").html(dataTextMsg);
			}
		},
		error:function(data){
			dataText[type] += "\t\t\t\t\t\t</table>\n";
			$("#setResult").html("");
		}
	});
}

function Table_Tips(tb, type , time)
{
	if( !time ) time = '';
	dataText[type] = "";
	dataText[type] += "\t\t\t\t\t\t<table cellpadding=\"0\" cellspacing=\"0\" class=\"basictbl\">\n";
	dataText[type] += "\t\t\t\t\t\t	<col width=\"80\" />\n";
	dataText[type] += "\t\t\t\t\t\t	<col width=\"61\" />\n";
	dataText[type] += "\t\t\t\t\t\t	<col width=\"61\" />\n";
	dataText[type] += "\t\t\t\t\t\t	<col width=\"148\" />\n";
	dataText[type] += "\t\t\t\t\t\t	<col width=\"\" />\n";
	dataText[type] += "\t\t\t\t\t\t	<col width=\"48\" />\n";
	dataText[type] += "\t\t\t\t\t\t	<col width=\"200\" />\n";
	dataText[type] += "\t\t\t\t\t\t	<col width=\"67\" />\n";
	dataText[type] += "\t\t\t\t\t\t	<tr class=\"headrow\">\n";
	dataText[type] += "\t\t\t\t\t\t		<td class=\"date\" >DATE</td>\n";
	dataText[type] += "\t\t\t\t\t\t		<td class=\"mtime\" >MATCH<br />TIME</td>\n";
	dataText[type] += "\t\t\t\t\t\t		<td class=\"ptime\" >POST<br />TIME</td>\n";
	if(type=='soccer')	dataText[type] += "\t\t\t\t\t\t		<td class=\"league\" >LEAGUE</td>\n";
	if(type=='sports')	dataText[type] += "\t\t\t\t\t\t		<td class=\"league\" >SPORTS</td>\n";
	dataText[type] += "\t\t\t\t\t\t		<td class=\"match\" >MATCH</td>\n";
	dataText[type] += "\t\t\t\t\t\t		<td class=\"view\" >VIEW</td>\n";
	dataText[type] += "\t\t\t\t\t\t		<td class=\"tips\" >TIPS</td>\n";
	dataText[type] += "\t\t\t\t\t\t		<td class=\"result lastchild\" >RESULT</td>\n";
	dataText[type] += "\t\t\t\t\t\t	</tr>\n";
	$.ajax({
		url:'control.php?c=tips&type='+type+'&time='+time+'&random=' + Math.random(),
		type:'POST',
		dataType:'json',
		data:{},
		success:function(data){
			$('#'+type+'_prev').attr('rel',data.next); 
			$('#'+type+'_next').attr('rel',data.prev); 
			if( data.status != 'true' )
			{
				dataText[type] += "\t\t\t\t\t\t<tr class=\"row2\" height=\"29px\">\n";
				dataText[type] += "\t\t\t\t\t\t	<td colspan=\"9\">No Information.</td>\n";
				dataText[type] += "\t\t\t\t\t\t</tr>\n";
				dataText[type] += "\t\t\t\t\t\t</table>\n";
				$("#"+tb).html(dataText[type])
			}
			rs = data.row;
			for(var prop in rs) 
			{
				dataText[type] += "\t\t\t\t\t\t<tr class=\"row2\" height=\"29px\">\n";
				dataText[type] += "\t\t\t\t\t\t	<td class=\"date\">"+rs[prop].tips_date+"</td>\n";
				dataText[type] += "\t\t\t\t\t\t	<td class=\"mtime\">"+rs[prop].tips_match_time+"</td>\n";
				dataText[type] += "\t\t\t\t\t\t	<td class=\"ptime\">"+rs[prop].tips_post_time+"</td>\n";
				dataText[type] += "\t\t\t\t\t\t	<td class=\"league\">"+rs[prop].tips_league_sports+"</td>\n";
				dataText[type] += "\t\t\t\t\t\t	<td class=\"match\">\n";
				if(type=='soccer') 
				{
					dataText[type] += "\t\t\t\t\t\t		<div class=\"matchdiv\">\n";
					dataText[type] += "\t\t\t\t\t\t			<span class=\"fname1\"><a href=\"#\" onclick=\"showHide('resultwindow','"+rs[prop].tips_id+"');return false;\">"+rs[prop].tips_home_team+"</a></span>\n";
					dataText[type] += "\t\t\t\t\t\t			<span class=\"dash\">-</span>\n";
					dataText[type] += "\t\t\t\t\t\t			<span class=\"fname2\"><a href=\"#\" onclick=\"showHide('resultwindow','"+rs[prop].tips_id+"');return false;\">"+rs[prop].tips_away_team+"</a></span>\n";
					dataText[type] += "\t\t\t\t\t\t		</div>\n";
				}else{
					dataText[type] += "\t\t\t\t\t\t		"+rs[prop].tips_sports_all+"\n";
				}
				dataText[type] += "\t\t\t\t\t\t	</td>\n";
				dataText[type] += "\t\t\t\t\t\t	<td class=\"view\">"+rs[prop].tips_view+"</td>\n";
				dataText[type] += "\t\t\t\t\t\t	<td class=\"tips\"><a href=\"#\" onclick=\"showHide('resultwindow','"+rs[prop].tips_id+"');return false;\">MEMBER</a></td>\n";
				
				if(rs[prop].tips_result == "0") result = "-";
				if(rs[prop].tips_result == "1") result =  "<img src=\"images/accept.png\" />";
				if(rs[prop].tips_result == "2") result =  "<img src=\"images/icondraw.png\" />";
				if(rs[prop].tips_result == "3") result =  "<img src=\"images/cross.png\" />";
				
				dataText[type] += "\t\t\t\t\t\t	<td class=\"result lastchild\">"+result+"</td>\n";
				dataText[type] += "\t\t\t\t\t\t</tr>\n";
			}
			dataText[type] += "\t\t\t\t\t\t</table>\n";
			$("#"+tb).html(dataText[type])
		},
		error:function(data){
			dataText[type] += "\t\t\t\t\t\t</table>\n";
			$("#"+tb).html(dataText[type])
		}
	});

}
IsEmail=function(str){
	var RegExp = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;

	if(RegExp.test(str)){
		return true;
	}else{
		return false;
	}
}
	function toggleObj(btn,obj,num,pos){	
		var statFace = 'off';
		var p = (pos=='left')?-1:-1;
		var s = (pos=='left')?'left':'right';
		
		for(i in btn){
			var ele = btn[i];
			$(ele).css('cursor','pointer');
			$(ele).click(function(){
				if(statFace == 'off'){
					statFace = 'on';
					if(obj!='#facebookPanela'){
						$('.btnLeftToggle').animate({'left':'-122'},1000);
					}
					if(s=='right'){
						$(obj).animate({'right':'0px'},1000);
					}else{
						$(obj).animate({'left':'0px'},1000);
					}
				}else{
					statFace = 'off';
					if(obj!='#facebookPanela'){
						$('.btnLeftToggle').animate({'left':'-1px'},1000);
					}
					if(s=='right'){
						$(obj).animate({'right':(num*p)+'px'},1000);
					}else{
						$(obj).animate({'left':(num*p)+'px'},1000);
					}
				}
			});
			
		}
		
		
	}
	
function clsnewsletter(){$('.newslettertxt').val("Your email.");}
$(function(){
	$(".loadtips").click(function(){
		var dt = $(this).attr('rel');
		if( dt == 0 ) return alert("Sorry!\nNo Information.");
		if( $(this).attr('id') == 'sports_prev' || $(this).attr('id') == 'sports_next' )
		{
			Table_Tips('sports_table', 'sports', dt);
		}else{
			Table_Tips('soccer_table', 'soccer', dt);
		}
	});
	Table_Tips('soccer_table', 'soccer');
	Table_Tips('sports_table', 'sports');
	$('.newslettertxt').focus(function(){if($(this).val()=='Your email.')$(this).val("");});
	$('.newslettertxt').blur(function(){if($(this).val()=='')$(this).val("Your email.");});
	$('.email').change(function(){ if(!IsEmail($(this).val())){$(this).val('');}});
	$('.btnfollow').click(function(){if($('.newslettertxt').val()!='Your email.' && $('.newslettertxt').val() != ''){$('#newsletterSubmit').submit();}else{ alert('รูปแบบอีเมล์ไม่ถูกต้อง');}});
	

	var tObj1 = new toggleObj(['#arrowFacebook','#facebookPanel .btnCloseRank'],'#facebookPanel',292,'left');
	var tObj3 = new toggleObj(['#btnVdoT','#vdoBox .btnCloseRank'],'#vdoBox',229,'left');
});


