function getProgram(channelID, day, nr, onlyNow)
{
	//alert('channelID: '+channelID+', channelKey: '+channelKey+', day: '+day+', nr: '+nr); return false;
	$.get("/Programs/getProgramHTML/"+channelID+'/'+nr+'/'+day+'/'+onlyNow, function(data){
		  $('#column_'+nr).html(data);
		  
		  
		  c1 = getCookie('pr1');
		  c2 = getCookie('pr2');
		  c3 = getCookie('pr3');
		  c4 = getCookie('pr4');

		  selectedChannels[0] = c1;
		  selectedChannels[1] = c2;
		  selectedChannels[2] = c3;
		  selectedChannels[3] = c4;
		  
		  disableSelects();
		  
		  selectChannels();
		  
		});
}

function mark(val)
{
	if(val)
		$('.ch').each(function(i){this.checked=true});
	else
		$('.ch').each(function(i){this.checked=false});
}

function resized()
{
	if(checkResolution() && $('#contentBeforeLeft').html()!='')
        $('#contentBeforeLeft').show();
    else
        $('#contentBeforeLeft').hide();
}

function checkResolution()
{
	if(document.body.clientWidth>1194)
		return true;
	else
		return false;
}

function disableSelects()
{
	$('.channelSelect').each(function(i){this.disabled=false});
    
	for(i=0;i<4;i++)
	{
		if(selectedChannels[0]!='-' && isset(document.getElementById('sel_'+i+'_'+selectedChannels[0])))
			document.getElementById('sel_'+i+'_'+selectedChannels[0]).disabled = true;

		if(selectedChannels[1]!='-' && isset(document.getElementById('sel_'+i+'_'+selectedChannels[1])))
			document.getElementById('sel_'+i+'_'+selectedChannels[1]).disabled = true;

		if(selectedChannels[2]!='-' && isset(document.getElementById('sel_'+i+'_'+selectedChannels[2])))
			document.getElementById('sel_'+i+'_'+selectedChannels[2]).disabled = true;

		if(selectedChannels[3]!='-' && isset(document.getElementById('sel_'+i+'_'+selectedChannels[3])))
			document.getElementById('sel_'+i+'_'+selectedChannels[3]).disabled = true;
	}
	
	$('.channelSelect:selected').each(function(i){this.disabled=false});
}

function selectChannels()
{
	mark(false);
	
	if(selectedChannels[0]!='-' && isset(document.getElementById('ch_'+selectedChannels[0])))
        document.getElementById('ch_'+selectedChannels[0]).checked = true;
        
    if(selectedChannels[1]!='-' && isset(document.getElementById('ch_'+selectedChannels[1])))
    	document.getElementById('ch_'+selectedChannels[1]).checked = true;

    if(selectedChannels[2]!='-' && isset(document.getElementById('ch_'+selectedChannels[2])))
    	document.getElementById('ch_'+selectedChannels[2]).checked = true;

    if(selectedChannels[3]!='-' && isset(document.getElementById('ch_'+selectedChannels[3])))
    	document.getElementById('ch_'+selectedChannels[3]).checked = true;
}

var selectedChannels = new Array();

c1 = getCookie('pr1');
c2 = getCookie('pr2');
c3 = getCookie('pr3');
c4 = getCookie('pr4');

selectedChannels[0] = c1;
selectedChannels[1] = c2;
selectedChannels[2] = c3;
selectedChannels[3] = c4;


function selectChannel(channel, checkbox)
{
	
	if(checkbox.checked==false)
	{
		c = 1;
		
		for(i=0;i<4;i++)
			if(selectedChannels[i]=='-')
				c++;

		if(c>3)
		{
			checkbox.checked = true;
			return;
		}
		
		if(count(selectedChannels)<1)
		{
			checkbox.checked = true;
			return;
		}
			
		index = array_search(channel, selectedChannels);
		
		tmp = new Array();
		
		for(i=0;i<4;i++)
			if(index!=i)
				array_push(tmp, selectedChannels[i]);
			
		for(i=0;i<4;i++)
			if(empty(tmp[i]))
				tmp[i] = '-';
		
		selectedChannels = tmp;
		
		for(i=0;i<4;i++)
			setcookie('pr'+(i+1), selectedChannels[i], time()+1080000, '/');
		
		document.location.reload();
		
		return;
	}
	
	c = 0;
	
	for(i=0;i<4;i++)
		if(selectedChannels[i]=='-')
			c++;

	if(c<1)
		for(i=0;i<4;i++)
			if(selectedChannels[i]!='-')
			{
				array_shift(selectedChannels);
				break;
			}
	
	array_push(selectedChannels, channel);
	//alert(selectedChannels);
	$('.ch').each(function(i){
		if((this.id!='ch_'+selectedChannels[0]) && (this.id!='ch_'+selectedChannels[1]) && (this.id!='ch_'+selectedChannels[2]) && (this.id!='ch_'+selectedChannels[3])) this.checked=false;
	});
	
	tmp = new Array();
	for(i=0;i<5;i++)
		if(isset(selectedChannels[i]) && selectedChannels[i]!='-')
			array_push(tmp, selectedChannels[i]);
	
	for(i=0;i<4;i++)
		if(empty(tmp[i]))
			tmp[i] = '-';
	
	
		
	selectedChannels = tmp;
	
	for(i=0;i<4;i++)
		setcookie('pr'+(i+1), selectedChannels[i], time()+1080000, '/');
	
	document.location.reload();
	
	//alert(selectedChannels)
}

function getCookie(check_name) 
{
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f

	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );


		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}
