jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

function removeDoubles( arr )
{
	arr.sort();
	var i = 1;
	var size = arr.length;
	while( i < size )
	{
		if( arr[i-1] == arr[i] )
		{
			arr.splice(i-1,1);
			size--;
		}
		else
		{
			i++;
		}
	}
	return arr;
}

function getSchedule()
{
	var arr;
	if( $.cookie("schedule") )
		arr = $.cookie("schedule").split(",");
	else
		return new Array();
	arr = removeDoubles(arr);
	return arr;
}

function addToSchedule( id )
{
	//$.log($("#lnk_"+id));

	if( $.cookie("schedule") )
		$.cookie("schedule", $.cookie("schedule") + "," + id), { expires: 100, path: '/', domain: 'riff.is' };
	else
		$.cookie("schedule", id, { expires: 100, path: '/', domain: 'riff.is' });
	// Remove doubles and store the schedule again
	setSchedule(getSchedule());
	//$.log(getSchedule());
	
	$("#lnk_"+id).html("<img src='/media/movies/IconRemove.jpg'>");
	$("#lnk_"+id).attr( "href", "javascript:removeFromSchedule(" + id + ");");
}

function setSchedule( arr )
{
	var c = null;
	if( arr.length > 0 ) {
		var c = arr[0];
		if( arr.length > 1 ) {
			for( var i = 1; i < arr.length; i++ )
				c += "," + arr[i];
		}
	}
	$.cookie("schedule", null);	
	$.cookie("schedule", c, { expires: 100, path: '/', domain: 'riff.is' });
}

function removeFromSchedule( id )
{
	//$.log($("#lnk_"+id));

	var arr = getSchedule();
	for(var i = 0; i < arr.length; i++)
		if(arr[i] == id)
			arr.splice(i,1);
	setSchedule( arr );
	//$.log( arr );
	
	$("#lnk_"+id).html("<img src='/media/movies/IconAdd.jpg'>");
	$("#lnk_"+id).attr( "href", "javascript:addToSchedule(" + id + ");");
}

function toggleRows( table )
{
	$(table).siblings("tr").each( function(i, item) { $(item).toggleClass("hidden"); } );
}