
function setCookie(c_name,value,expiredays){
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+ ((expiredays==null) ? "" : ";expires="+exdate.toUTCString());
}

function getCookie(c_name){
	if (document.cookie.length>0){
		c_start=document.cookie.indexOf(c_name + "=");
	
		if (c_start!=-1){
			c_start=c_start + c_name.length+1;
			c_end=document.cookie.indexOf(";",c_start);
			
			if (c_end==-1) c_end=document.cookie.length;
				return unescape(document.cookie.substring(c_start,c_end));
		}
	}
	return "";
}

function checkCookie(saveid){
	
	s_id=getCookie(saveid);
	
	if (s_id=="1"){
		$('tr[id="'+saveid+'"] td a').html('<img src="img/minus.gif" border="0">');

	}else{
		$('tr[id="'+saveid+'"] td a').html('<img src="img/plus.gif" border="0">');
	}
	
}


function update_rows(){
	$('tbody>tr:odd').addClass('odd');
	$('tbody>tr:even').addClass('even');
	$('tbody>tr').mouseover(function(){
		$(this).addClass('row_hover');
	})
	.mouseout(function(){
		$(this).removeClass('row_hover');
	});
	
	$('tr>td:not(:last-child)').click(function(){
		$(this).parent().each(function(){
			open_win(this.id);
		});
	});
	
	$('tr>td a[id^="save"]').each(function(){
		id = $(this).parent().parent().get(0).id;
		
		checkCookie(id);
	});
	
	
	$('a[id^="save"]').click(function(){
		
		id = $(this).parent().parent().get(0).id; 
	
		s_id=getCookie(id);
		
		if (s_id=="1"){
			setCookie(id,'0',1);
		}else{
			setCookie(id,'1',1);
		}
		checkCookie(id);
	});
	
	status();
}

function MoreInformation(MatchID) {
	if (document.getElementById("R" + MatchID).className == "hidden-result") {
		var list = document.getElementsByName("R" + MatchID);
		if (list != null) {
			for (i = 0; i < list.length; i++) {
				list[i].className = "visible-result";
			}
		}
		document.getElementById("I" + MatchID).src = "arrow-up.gif";
		document.getElementById("I" + MatchID).alt = "Hide Information";
		document.getElementById("I" + MatchID).title = document.getElementById("I" + MatchID).alt;
	} else {
		var list = document.getElementsByName("R" + MatchID);
		if (list != null) {
			for (i = 0; i < list.length; i++) {
				list[i].className = "hidden-result";
			}
		}
		document.getElementById("I" + MatchID).src = "arrow-down.gif";
		document.getElementById("I" + MatchID).title = document.getElementById("I" + MatchID).alt;
	}
}

function open_win(id){
	h =	window.open('detalii.php?id='+id,'','left=200,top=200,width=500,height=400');
	h.innerHtml = 'test';
	h.focus();
}

function open_winads(id){
	h =	window.open('http://omnibet.ro','left=200,top=200,width=500,height=400');
	h.innerHtml = 'test';
	h.focus();
}

function update_ajax() {
	$('.ui-dialog-titlebar').remove();
	if($("#update_ajax.all_matches").length){
		$.get('ajax.php', function(data){
			$('#update_ajax').html(data);
		});
	}else if($("#update_ajax.live_matches").length){
		$.get('ajax2.php', function(data){
			$('#update_ajax').html(data);
		});

		

		
		
	 }else if($("#update_ajax.my_matches").length){
$.get('ajax4.php', function(data){
$('#update_ajax').html(data);
});
}
	
}

function live_matches(){
	$('#dialog').dialog({ 
		autoOpen: true,
		width: 200,
		modal: true,
		position: [200,200]
	});
	
	$("#progressbar").progressbar({
		value: 100
	});
	
	$("#update_ajax").removeClass();
	$("#update_ajax").addClass('live_matches');
	
	if($("#update_ajax.live_matches").length){
		update_ajax();
	}
	$("#update_ajax").ajaxStop(function(){
		$('tr>td a[id^="save"]').each(function(){
			id = $(this).parent().parent().get(0).id;
			checkCookie(id);
			
			if(this.innerHTML == '<img src="img/plus.gif" border="0">'){
				$(this).parent().parent().show();
			}
			
		});
	
		$('tbody').each(function(){
			$(this).prev().show();
			$(this).show();
		});
		$("#progressbar").progressbar("destroy");
		$( "#dialog" ).dialog('close');
	});
}


function all_matches(){
	$('#dialog').dialog({ 
		autoOpen: true,
		width: 200,
		modal: true,
		position: [200,200]
	}); 
	$("#progressbar").progressbar({
		value: 100
	});
	
	$("#update_ajax").removeClass();
	$("#update_ajax").addClass('all_matches');
	
	if($("#update_ajax").length){
		update_ajax();
	}
	
	$("#update_ajax").ajaxStop(function(){
		$('tr>td a[id^="save"]').each(function(){
			id = $(this).parent().parent().get(0).id;
			checkCookie(id);
			
			if(this.innerHTML == '<img src="img/plus.gif" border="0">'){
				$(this).parent().parent().show();
			}
			
		});
	
		$('tbody').each(function(){
			$(this).prev().show();
			$(this).show();
		});
		$("#progressbar").progressbar("destroy");
		$( "#dialog" ).dialog('close');
	});
}


function my_matches(){
	$('#dialog').dialog({ 
		autoOpen: true,
		width: 200,
		modal: true,
		position: [200,200]
	}); 
	
	$("#progressbar").progressbar({
		value: 100
	});
	
	$("#update_ajax").removeClass();
	$("#update_ajax").addClass('my_matches');
	
	if($("#update_ajax").length){
		update_ajax();
	}
	
	$("#update_ajax").ajaxStop(function(){

		$('tr>td a[id^="save"]').each(function(){
			id = $(this).parent().parent().get(0).id;
			checkCookie(id);
			
			if(this.innerHTML == '<img src="img/plus.gif" border="0">'){
				$(this).parent().parent().hide();
			}
			
		});
	
		$('tbody').each(function(){
			if($(this).children('tr:not(:hidden)').length < 1){
				$(this).prev().hide();
				$(this).hide();
			}
		});
		
		$("#progressbar").progressbar("destroy");
		$( "#dialog" ).dialog('close');
	});
}

function status(){
	if($('.status').css('color')=='rgb(255, 255, 255)'){
		$('.status').css('color','#E32A2A');
		setTimeout('status();',1200);
	}else{
		setTimeout('status();',500);
	}
}

$(document).ready(function() {
	
	$("#update_ajax").ajaxStop(function(){
		update_rows();
		setTimeout('update_ajax();',30000);
	});
	
	all_matches();
	
});
