function has_flagged( entry_id ) {
	var cflags = $.cookie( 'newsblog-flags' );
	if( cflags ) {
		if( cflags == entry_id ) { return true; }
		var parts = cflags = cflags.split(',');
		for( var x in parts ) {
			if( parts[x] == entry_id ) {
				return true;
			}
		}
	}
	return false;
}

function flag_as_inappropriate( entry_id ) {
	if( !has_flagged( entry_id ) ) {
		if( lina_is_logged_in() ) {
			flag_send( entry_id );
		} else {
			alert( "Ašeins innskrįšir notendur geta bent į óvišeigandi fréttatengingar.\nEf žś ert ekki skrįšur notandi į blog.is geturšu skrįš žig į www.blog.is." );
		}
	}
}


function flag_send( entry_id ) {

	if( !confirm( "Smelltu į OK ef žś vilt tilkynna um óvišeigandi tengingu viš frétt į mbl.is ķ žessari blogg fęrslu." ) ) {
		return;
	}

	var cflags = $.cookie( 'newsblog-flags' );
	if( cflags ) {
		$.cookie( 'newsblog-flags', entry_id + ',' + cflags );
	} else {
		$.cookie( 'newsblog-flags', entry_id );
	}	

	var req = new LINA({
		"comp" : "/utils/lina_methods",
		"method" : "newsblog_flag_send",
		"with_context" : 1,
		"args" : {"entry_id":entry_id}
	}, flag_send_cb );

	flag_disconnect( document.getElementById( 'newsblog-inappropriate-' + entry_id ) );
}

var res;
function flag_send_cb( resp ) {
	res = resp;
}

function init_flagging() {
	jQuery('a.newsblog-inappropriate').each( function() {
		if( has_flagged(this.id.split('-')[2]) ) {
			flag_disconnect( this );
		} else {
			jQuery(this).mbltitle();
		}
	});
}

function flag_disconnect(obj) {
	jQuery(obj).unbind();
	jQuery(obj).bind( 'click', function() { return false; } );
	jQuery(obj).css( 'opacity', '.3' );
	jQuery(obj).css( 'filter', 'alpha(opacity=30)' );
	jQuery(obj).css( 'cursor', 'default' );
	obj.title = 'Žś hefur žegar tilkynnt um óvišeigandi tengingu ķ frétt į mbl.is ķ žessari blogg fęrslu.';
	jQuery(obj).mbltitle();
}

addInitCallback( init_flagging );

