
var live_save_empty_content = "";

var live_scores = {};

var live_limit_view = 3;

var live_counts = {
			'soccer': 0,
			'tennis': 0,
			'hockey': 0,
			'basketball': 0
		  };

var live_shows = {
			'soccer': 0,
			'tennis': 0,
			'hockey': 0,
			'basketball': 0
		};
		
var live_shows_cookie = get_cookie('live_shows');
if (live_shows_cookie) {
	live_shows_cookie = deserialize(live_shows_cookie);
	for(var key in live_shows_cookie) {
		live_shows[key] = parseInt(live_shows_cookie[key]);
	}
}

function live_more_open(type)
{
	live_shows[type] = 1;
	
	set_cookie('live_shows',serialize(live_shows));

	$('#live_' + type + '').children('.live_more').slideDown('fast');
	$('#live_' + type + '').children('.live_more').children('.live_more').slideDown('fast');
	$('#live_' + type + '').children('.live_more').children('.live_more').children('.live_more').slideDown('fast');
	$('#live_' + type + '_up').show();
	$('#live_' + type + '_up').children('.livehidebut').show()
	$('#live_' + type + '_down').hide();
}

function live_show_goal(type, id)
{
	$('#live_' + type + '_' + id).addClass('betlisthover');

	setTimeout('live_hide_goal("' + type + '", ' + id + ');', 300);
}

function live_hide_goal(type, id)
{
	$('#live_' + type + '_' + id).removeClass('betlisthover');
}

function live_more_hide(type)
{
	live_shows[type] = 0;
	
	set_cookie('live_shows',serialize(live_shows));

	$('#live_' + type + '').children('.live_more').slideUp('fast');
	$('#live_' + type + '_up').hide();
	$('#live_' + type + '_down').show();
	$('#live_' + type + '_down').children('.livehidebut').show()
}

function parse_list(list)
{
	var $live_events_list = $('#live_events_list_inside');

	$live_events_list.empty();

	live_counts['soccer'] = 0;
	live_counts['tennis'] = 0;
	live_counts['hockey'] = 0;
	live_counts['basketball'] = 0;

	$('#live_soccer').html('');
	$('#live_tennis').html('');
	$('#live_hockey').html('');
	$('#live_basketball').html('');

	$('#livelisthead').activity({align: 'right', segments: 12, steps: 3, width:2, space: 1, length: 3, color: '#030303', speed: 1.5});

	if(!list.length)
	{
		var $live_div_no_live_events = $('<div></div>');
		$live_div_no_live_events.hide();
		$live_events_list.append(planetofbets_data['pb_user_translates']['Empty live events']);
		$live_div_no_live_events.show();

		var $live_div_end = $('<div class="livelisthead alc no-padding botrad5">&nbsp;</div>');
		$live_events_list.append($live_div_end);
	}
	else
	{
		for(var i = 0; i <= list.length; i++)
		{
			if(list[i])
			{
				var $a_down = $('<a class="button" id="live_' + list[i].sport + '_down" onclick="live_more_open(\'' + list[i].sport + '\');" style="display: none;"><div class="livelisthead alc no-padding livehidebut" style="display: none; background: #FFA500;">&#9660;</div></a>');
				var $a_up = $('<a class="button" id="live_' + list[i].sport + '_up" onclick="live_more_hide(\'' + list[i].sport + '\');" style="display: none;"><div class="livelisthead alc no-padding livehidebut" style="display: none; background: #FFA500;">&#9650;</div></a>');
				var $div_sport_main = $('<div id="live_' + list[i].sport + '_main" style="display: none;"></div>');
				var $div_sport = $('<div id="live_' + list[i].sport + '"></div>');
				var $div_sport_title = $('<div class="livelistsport" id="live_' + list[i].sport + '_title_sport">' + list[i].name + '&nbsp;<span id="live_' + list[i].sport + '_count"></span></div>');

				$div_sport_main.append($div_sport_title);
				$div_sport_main.append($div_sport);
				$div_sport_main.append($a_down);
				$div_sport_main.append($a_up);
				$live_events_list.append($div_sport_main);

				for(var k = 0; k < list[i].events.length; k++)
				{
					if(list[i].events[k])
					{
						if(list[i].events[k].name.length)
						{
							var scores = 0;

							$('#live_' + list[i].sport + '_title_sport').show();

							if(list[i].events[k].score != undefined && list[i].events[k].score.length > 1)
								scores = list[i].events[k].score;

							var $live_div_parent_parent = $('<div></div>');
							$live_div_parent_parent.css('overflow', 'hidden');
							$live_div_parent_parent.css('height', '40px');
							$live_div_parent_parent.css('display', 'none');

							var $live_a_parent = $('<a href="/live/' + list[i].sport + '/' + list[i].events[k].id + '" style="text-decoration: none;"></a>');
							var $live_div_parent_item = $('<div class="live_a_par"></div>');

							$live_a_parent.append($live_div_parent_item);

							$live_div_parent_parent.append($live_a_parent);

							$live_div_parent_item.hide();
							$live_div_parent_item.attr('id', 'live_' + list[i].sport + '_' + list[i].events[k].id);

							$live_a_parent.mouseover(function()
							{
								$(this).find('.live_a_par').addClass('betlisthover');
							});

							$live_a_parent.mouseout(function()
							{
								$(this).find('.live_a_par').removeClass('betlisthover');
							});

							$live_div_parent_item.addClass('livelistitem');
							$live_div_parent_item.addClass('fs-11');

							var $live_div_str_0_item = $('<div><div class="f-left floatfix" style="width: 84%; text-decoration: none;">' + list[i].events[k].name[0] + ' - ' + list[i].events[k].name[1] + '</div><div class="yellow" style="text-align: right; text-decoration: none;">' + (scores ? scores.join(' : ') : '&nbsp;') + '</div></div>');
							$live_div_str_0_item.hide();
							$live_div_str_0_item.addClass('livelistteam');

							var status = "";
							if(!list[i].events[k].status)
								status = list[i].events[k].start_time;
							else
							{
								status = list[i].events[k].text;

								if(list[i].events[k].minute)
									status += '&nbsp;&nbsp;' + list[i].events[k].minute;
							}

							var $live_div_str_1_item = $('<div><div class="f-left">&nbsp;</div><div class="f-right">' + status + '</div></div>');
							$live_div_str_1_item.hide();
							$live_div_str_1_item.addClass('livelisttime');

							$live_div_str_0_item.show();
							$live_div_str_1_item.show();

							$live_div_parent_item.append($live_div_str_0_item);
							$live_div_parent_item.append($live_div_str_1_item);

							live_counts[list[i].sport]++;

							$('#live_' + list[i].sport).append($live_div_parent_parent);
							$('#live_' + list[i].sport + '_main').show();

							if(live_counts[list[i].sport] <= live_limit_view)
							{
								$live_a_parent.show();
								$live_div_parent_item.show();
								$live_div_parent_parent.show();
							}
							else
							{
								$live_div_parent_item.addClass('live_more');
								$live_div_parent_parent.addClass('live_more');
								$live_a_parent.addClass('live_more');

								if(live_shows[list[i].sport])
								{
									$live_a_parent.show();
									$live_div_parent_item.show();
									$live_div_parent_parent.show();
								}
							}

							if(live_counts[list[i].sport] > live_limit_view && !live_shows[list[i].sport])
							{
								$('#live_' + list[i].sport + '_down').show();
								$('#live_' + list[i].sport + '_down').children('.livehidebut').show();
							}
							else if(live_counts[list[i].sport] > live_limit_view && live_shows[list[i].sport])
							{
								$('#live_' + list[i].sport + '_up').show();
								$('#live_' + list[i].sport + '_up').children('.livehidebut').show();
							}

							var new_scores = scores ? scores.join(':') : '';

							if(new_scores != live_scores['' + list[i].events[k].id + ''] && live_scores['' + list[i].events[k].id + ''] != undefined && $('#live_' + list[i].sport + '_' + list[i].events[k].id).is(':visible'))
							{
								if(list[i].events[k].status == 'inplay')
								{
									live_show_goal(list[i].sport, list[i].events[k].id, 0);

									setTimeout('live_hide_goal("' + list[i].sport + '", ' + list[i].events[k].id + ');', 400);
								}
							}

							live_scores['' + list[i].events[k].id + ''] = new_scores;
						}
					}
				}
			}
		}

		var $live_div_end = $('<div id="live_list_end" class="livelisthead alc no-padding botrad5">&nbsp;</div>');
		$live_events_list.append($live_div_end);

		$('#live_soccer_count').text('(' + live_counts['soccer'] + ')');
		$('#live_tennis_count').text('(' + live_counts['tennis'] + ')');
		$('#live_hockey_count').text('(' + live_counts['hockey'] + ')');
		$('#live_basketball_count').text('(' + live_counts['basketball'] + ')');
	}

	$('#livelisthead').activity(false);
}

function update_list(url,id) {
	$('#livelisthead').activity({align: 'right', segments: 12, steps: 3, width:2, space: 1, length: 3, color: '#030303', speed: 1.5});

	var script = document.getElementById(id);
	if (script)
		script.parentNode.removeChild(script);
	var head = document.getElementsByTagName("head")[0];
	script = document.createElement('script');
	script.id = id;
	script.type = 'text/javascript';
	script.src = url + 'r=' + Math.random().toString().replace(/\./g,'');
	head.appendChild(script);
}

function serialize(array) {
	var string = '';
	for(var key in array) {
		string += key + ':' + array[key] + '|';
	}
	return string;
}

function deserialize(string) {
	var array = {};
	var tmp = string.split('|');
	for(var i = 0; i < tmp.length; i++) {
		var item = tmp[i].split(':');
		if (item.length == 2)
			array[item[0]] = item[1];
	}
	return array;
}

function get_cookie(name) {
	var pairs = document.cookie.split(';');
	for (var i = 0; i < pairs.length; i++) {
		var item = pairs[i].split('=');
		if (item.length == 2 && trim(item[0]) == name)
			return trim(item[1]);
	}
	return null;
}

function set_cookie(name,value) {
	document.cookie = name + '=' + value + '; path=/;';
}

function trim(str) {
	return str.replace(/^\s+|\s+$/, '');
}

