//for epgimages
#ifdef HAVE_IMAGEMAGICK
#include "bitmap.h"
bool epgimages;
int Pid, updateimage;
int epgimagecolors = 0;
const char *buttons[] = {NULL, NULL, NULL, NULL};
const char *title = NULL;
#else
#warning WARNING: YOU WILL NOT BE ABLE TO USE EPG-IMAGES
#warning UNCOMMENT "HAVE_IMAGEMAGICK = 1" IN "MAKEFILE" IF YOU WANT EPG-IMAGES
#endif

#include "DisplayMenu.h"
#include "i18n.h"
#include "vdrstatus.h"

#ifdef VIDEOSTATUS
#include "videostatus.h"
extern int fd_video;
#endif
extern bool vdrosd;

bool changed;
int UpdatePid;
cCharScroll CurrentItemScroll;
cCharScroll TitleScroll;

#define INITXY(X1, Y1, X2, Y2) ( {					\
    x0 = X1 & ~0x07;							\
    int lhh = lh / 2;							\
    x1 = x0 + lhh;							\
    x8 = (x0 + X2) & ~0x07;						\
    x6 = (x8 - lh + lhh) & ~0x07;					\
    x2 = x0 + x8 - x6; x5 = x6 - lhh;					\
    x7 = x8 - lhh; roundness = x2 - x0; y0 = Y1;			\
    y1 = y0 + roundness;						\
    y2 = y0 + lh;							\
    y3 = y2 + lh;							\
    y8 = y0 + Y2;							\
    y7 = y8 - roundness;						\
    y6 = y8 - lh;							\
    y5 = y6 - lh;							\
    } )

#define INITY(Y2) ( {							\
    y8 = y0 + Y2;							\
    y7 = y8 - roundness;						\
    y6 = y8 - lh;							\
    y5 = y6 - lh;							\
    } )

#define EPGAREAS tArea							\
    Areas[] = { { x0, y0, x8 - 1, y2 - 1, 2 },				\
		{ x0, y2, x8 - 1, y3 - 1, 2 },				\
		{ x0, y3, x4 - 1, y4 - 1, 2 },				\
		{ x4, y3, x6 - 1, y4 - 1, 4 },				\
		{ x0, y4, x6 - 1, y5 - 1, 2 },				\
		{ x6, y3, x8 - 1, y5 - 1, 2 },				\
		{ x0, y5, x8 - 1, y8 - 1, 4 }, }

#define MENUAREAS tArea							\
    Areas[] = { { x0, y0, x8 - 1, y2 - 1, 2 },				\
		{ x0, y2, x6 - 1, y3 - 1, 2 },				\
		{ x0, y3, x6 - 1, y5 - 1, 2 },				\
		{ x6, y2, x8 - 1, y5 - 1, 2 },				\
		{ x0, y5, x8 - 1, y8 - 1, 4 }, }

#define REFILLAREA {							\
    if (area == 2) {							\
	osd->GetBitmap(2)->Reset();					\
	osd->DrawRectangle (x0, y2, x6 - 1, y3 - 1, bg);		\
	osd->DrawRectangle (x0, y3, x6 - 1, y5 - 1, bg);		\
	}								\
    }

#define BGMENU {							\
    tColor tr = Theme.Color(Transparent);				\
    tColor tb = Theme.Color(clrMenuTitleBg);				\
    osd->DrawRectangle(x0, y0, x2 - 1, y1 - 1, tr);			\
    osd->DrawEllipse(x0, y0,x2 - 1, y1 - 1, tb, 2);			\
    osd->DrawRectangle(x0, y1, x2 - 1, y2 - 1, tb);			\
    osd->DrawRectangle(x6, y0, x8 - 1, y1 - 1, tr);			\
    osd->DrawEllipse(x6, y0, x8 - 1, y1  - 1, tb, 1);			\
    osd->DrawRectangle(x6, y1, x8 - 1, y2 - 1, tb);			\
    tColor bg = Theme.Color(clrBackground);				\
    osd->DrawRectangle(x0, y2, x6 - 1, y3 - 1, bg);			\
    osd->DrawRectangle(x6, y2, x8 - 1, y3 - 1, bg);			\
    osd->DrawRectangle(x0, y3, x4 - 1, y4 - 1, bg);			\
    osd->DrawRectangle(x4, y3, x6 - 1, y4 - 1, bg);			\
    osd->DrawRectangle(x0, y4, x6 - 1, y5 - 1, bg);			\
    osd->DrawRectangle(x6, y3, x8 - 1, y4 - 1, bg);			\
    osd->DrawRectangle(x6, y4, x8 - 1, y5 - 1, bg);			\
    osd->DrawRectangle(x0, y5, x8 - 1, y6 - 1, bg);			\
    }

#define SETCOLORAREA(X) {						\
    osd->GetBitmap(X)->SetColor(0, Theme.Color(clrBackground));		\
    osd->GetBitmap(X)->SetColor(1, Theme.Color(clrMenuItemCurrentBg));	\
    osd->GetBitmap(X)->SetColor(2, Theme.Color(clrMenuItemCurrentFg));	\
    osd->GetBitmap(X)->SetColor(3, Theme.Color(clrMenuItemSelectable));	\
    }
//clrMenuItemNonSelectable is not available because 2-bit-area contains only 4 colors

cSkinElchiDisplayMenu::cSkinElchiDisplayMenu(void)
{
    noflush = true;
#ifdef DEBUG
    isyslog("skinelchi: DisplayMenu::cSkinElchiDisplayMenu()");
#endif
#ifdef VIDEOSTATUS
    VideoStatus.Update();
#endif
    message = false;
    showVolume = false;
    volumechange = VdrStatus.VolumeChange;
    timer = SkinElchiSetup.timer;
#if VDRVERSNUM >= 10330 || defined(PATCH_CUSTOMSERVICES)
    if (timer & 2)
	if (!cPluginManager::CallFirstService("CheckTimerConflict-v1.0"))
	    timer &= ~2;
#endif
#ifdef HAVE_IMAGEMAGICK
    epgimages = false;
    item1text = NULL;//for direct switching in epgsearch 0.9.3
    item2text = NULL;
    setitem = 0	    ;//END for direct switching in epgsearch 0.9.3
#endif
    asprintf(&olddate, " ");
    asprintf(&olditem, " ");
    font = cFont::GetFont(fontOsd);
    lh = font->Height();
#ifdef VIDEOSTATUS
    if (SkinElchiSetup.videostatus)
	vdrosd = (VideoStatus.NTSC() == (SkinElchiSetup.videosystem == vsNTSC));
    else
#endif
	vdrosd = (cDevice::PrimaryDevice()->GetVideoSystem() == vsNTSC) == (SkinElchiSetup.videosystem == vsNTSC);
    if (vdrosd)
	INITXY(Setup.OSDLeft, Setup.OSDTop, Setup.OSDWidth, Setup.OSDHeight);
    else
	INITXY(SkinElchiSetup.osdleft, SkinElchiSetup.osdtop, SkinElchiSetup.osdwidth, SkinElchiSetup.osdheight);
    Show();
    TitleScroll.DestWidth = 0;
    CurrentItemScroll.DestWidth = 0;
    TitleScroll.dir = 0;
    CurrentItemScroll.dir = 0;
    update = new cUpdate;
    SkinElchiSetup.changing = false;
    SkinElchiSetup.display = 2;
}

#ifdef HAVE_IMAGEMAGICK
void cSkinElchiDisplayMenu::ShowEpgImage(void)
{
#ifdef DEBUG
    isyslog("skinelchi: DisplayMenu::ShowEpgImage()");
#endif
    changed = true;
    osd = cOsdProvider::NewOsd(0, 0);
    tArea Areas[] = { { x0, y0, x8 - 1, y8 - 1, 8} };
    if ((osd->CanHandleAreas(Areas, sizeof(Areas) / sizeof(tArea)) == oeOk) && (SkinElchiSetup.singlearea)) {
	osd->SetAreas(Areas, sizeof(Areas) / sizeof(tArea));
	osd->GetBitmap(0)->Reset();
	for (int i = 0; i < MAX_ELCHI_THEME_COLORS; i++)
    	    osd->GetBitmap(0)->SetColor(i, SkinElchiSetup.ThemeColors[i]);
	epgimagecolors = 256 - MAX_ELCHI_THEME_COLORS;
	area = 0;
	}
    else {
	EPGAREAS;
    	if (osd->CanHandleAreas(Areas, sizeof(Areas) / sizeof(tArea)) == oeOk) {
	    osd->SetAreas(Areas, sizeof(Areas) / sizeof(tArea));
	    epgimagecolors = 16;
	    area = 4;
	    }
	else {
	    INITY((vdrosd ? Setup.OSDHeight : SkinElchiSetup.osdheight) - (((x6 - x4) * (y4 - y3) * 3) / (vdrosd ? Setup.OSDWidth : SkinElchiSetup.osdwidth)));
	    EPGAREAS;
    	    if (osd->CanHandleAreas(Areas, sizeof(Areas) / sizeof(tArea)) == oeOk) {
		osd->SetAreas(Areas, sizeof(Areas) / sizeof(tArea));
		epgimagecolors = 16;
		area = 4;
		}
	    else {
#ifdef VIDEOSTATUS
		if (SkinElchiSetup.videostatus)
		    vdrosd = (VideoStatus.NTSC() == (SkinElchiSetup.videosystem == vsNTSC));
		else
#endif
		    vdrosd = (cDevice::PrimaryDevice()->GetVideoSystem() == vsNTSC) == (SkinElchiSetup.videosystem == vsNTSC);
		INITY(vdrosd ? Setup.OSDHeight : SkinElchiSetup.osdheight);
		tArea Areas[] = { { x0, y0, x8 - 1, y2 - 1, 2 },
				{ x0, y2, x6 - 1, y3 - 1, 2 },
			    	{ x0, y3, x6 - 1, y5 - 1, 2 },
		    		{ x6, y2, x8 - 1, y5 - 1, 2 },
            	    		{ x0, y5, x8 - 1, y8 - 1, 4 },
            	    		};
		osd->SetAreas(Areas, sizeof(Areas) / sizeof(tArea));
		area = 2;
		epgimages = false;
	        }
	    }
	}	
#ifdef DEBUG
    isyslog("skinelchi: call DisplayMenu::SetTitle()");
#endif
    SetTitle(title);
#ifdef DEBUG
    isyslog("skinelchi: call DisplayMenu::SetButtons()");
#endif
    SetButtons(buttons[0], buttons[1], buttons[2], buttons[3]);
    BGMENU;
    TitleScroll.xOff = x2;
    TitleScroll.yOff = y0;
    TitleScroll.delay = 5;
    TitleScroll.intervall = 20;
    noflush = false;
}
#endif

void cSkinElchiDisplayMenu::Show(void)
{
#ifdef DEBUG
    isyslog("skinelchi: DisplayMenu::Show()");
#endif
    changed = true;
    lh = font->Height();
#ifdef HAVE_IMAGEMAGICK
    bit = (SkinElchiSetup.singlearea == 2) ? true : false;
    switch (SkinElchiSetup.epgimages) {
    	case 1: x4 = x6 - 120; y4 = y3 + 90; break;
	case 3: x4 = x6 - 120; y4 = y3 + 120; break;
	default: x4 = x6 - 180; y4 = y3 + 144;
	}
#else
    x4 = x6 - 720 / 4;
    y4 = y3 + 576 / 4;
#endif
    osd = cOsdProvider::NewOsd(0, 0);
    tArea Areas[] = { { x0, y0, x8 - 1, y8 - 1, 4
#ifdef HAVE_IMAGEMAGICK
+ bit * 4
#endif
}};
    if ((osd->CanHandleAreas(Areas, sizeof(Areas) / sizeof(tArea)) == oeOk) && (SkinElchiSetup.singlearea)) {
	osd->SetAreas(Areas, sizeof(Areas) / sizeof(tArea));
#ifdef HAVE_IMAGEMAGICK
	if (bit) {
	    osd->GetBitmap(0)->Reset();
	    for (int i = 0; i < MAX_ELCHI_THEME_COLORS; i++)
		osd->GetBitmap(0)->SetColor(i,SkinElchiSetup.ThemeColors[i]);
	    epgimagecolors = 256 - MAX_ELCHI_THEME_COLORS;
	    }
#endif
	area = 0;
	}
    else {
#ifdef HAVE_IMAGEMAGICK
	bit = false;
	epgimagecolors = 16;
	tArea Areas[] = { { x0, y0, x8 - 1, y8 - 1, 4} };
	if ((osd->CanHandleAreas(Areas, sizeof(Areas) / sizeof(tArea)) == oeOk) && (SkinElchiSetup.singlearea) && (bit)) {
	    osd->SetAreas(Areas, sizeof(Areas) / sizeof(tArea));
	    area = 0;
	    }
	else
#endif
	    {
	    MENUAREAS;
    	    if (osd->CanHandleAreas(Areas, sizeof(Areas) / sizeof(tArea)) == oeOk)
		osd->SetAreas(Areas, sizeof(Areas) / sizeof(tArea));
	    else {
		if (vdrosd)
		    INITXY(Setup.OSDLeft, Setup.OSDTop, MINOSDWIDTH, MINOSDHEIGHT);
		else
		    INITXY(SkinElchiSetup.osdleft, SkinElchiSetup.osdtop, MINOSDWIDTH, MINOSDHEIGHT);
		MENUAREAS;
		osd->SetAreas(Areas, sizeof(Areas) / sizeof(tArea));
		}
	    area = 2;
	    SETCOLORAREA(2);
	    }
	}
    menuHeight = y5 - y3;
    menuTop = y3;
    BGMENU;
    TitleScroll.xOff = x2;
    TitleScroll.yOff = y0;
    TitleScroll.delay = 5;
    TitleScroll.intervall = 20;
    noflush = false;
}

cSkinElchiDisplayMenu::~cSkinElchiDisplayMenu()
{
#ifdef DEBUG
    isyslog("skinelchi: DisplayMenu::~cSkinElchiDisplayMenu()");
#endif
    SkinElchiSetup.display = 0;
    noflush = true;
    UpdatePid = 0;
#ifdef VIDEOSTATUS
    if (fd_video != -1 && !SkinElchiSetup.videostatus) {
	close(fd_video);
	fd_video = -1;
	}
#endif
#ifdef HAVE_IMAGEMAGICK
    epgimages = false;
    if (item1text)	//for direct switching in epgsearch 0.9.3
	free(item1text);
    if (item2text)
	free(item2text);//END for direct switching in epgsearch 0.9.3
#endif
    if (olddate)
	free(olddate);
    if (olditem)
	free(olditem);
    if (osd) {
	delete osd;
	osd = NULL;
	}
}

void cSkinElchiDisplayMenu::SetScrollbar(void)
{
    if (textScroller.CanScroll()) {
#ifdef DEBUG
	isyslog("skinelchi: DisplayMenu::SetScrollbar()");
#endif
	changed = true;
	int yt = textScroller.Top();
	int yb = yt + textScroller.Height();
	if (area == 2) {
	    osd->GetBitmap(3)->Reset();
	    osd->DrawRectangle (x6, y2, x8 - 1, yt - 1, Theme.Color(clrBackground));
	    osd->DrawRectangle (x6, yb, x8 - 1, y5 - 1, Theme.Color(clrBackground));
	    }
        int st = yt;
        int sb = yb;
        int tt = st + (sb - st) * textScroller.Offset() / textScroller.Total();
        int tb = tt + (sb - st) * textScroller.Shown() / textScroller.Total();
        int xl = x6 + 2;
        osd->DrawRectangle(xl, st, x8 - 1, sb, Theme.Color(clrMenuScrollbarTotal));
        osd->DrawRectangle(xl, tt, x8 - 1, tb, Theme.Color(clrMenuScrollbarShown));
	}
#ifdef DEBUG
    else
	isyslog("skinelchi: skip DisplayMenu::SetScrollbar()");
#endif
}

void cSkinElchiDisplayMenu::Scroll(bool Up, bool Page)
{
#ifdef DEBUG
    isyslog("skinelchi: DisplayMenu::Scroll(%s%s)", Up ? "'Up'" : "'Down'", Page ? "'Page'" : "'Line'");
#endif
    cSkinDisplayMenu::Scroll(Up, Page);
    SetScrollbar();
}

int cSkinElchiDisplayMenu::MaxItems(void)
{
#ifdef DEBUG
    isyslog("skinelchi: DisplayMenu::MaxItems()");
#endif
    lh = font->Height();
    return (menuHeight / lh);
}

void cSkinElchiDisplayMenu::Clear(void)
{
#ifdef DEBUG
    isyslog("skinelchi: DisplayMenu::Clear()");
#endif
    changed = true;
    if (olddate)
	free(olddate);
    asprintf(&olddate, " ");
    if (olditem)
	free(olditem);
    asprintf(&olditem, " ");
    textScroller.Reset();
    CurrentItemScroll.dir = 0;
    tColor bg = Theme.Color(clrBackground);
    if (area == 2) {
	osd->GetBitmap(2)->Reset();
	SETCOLORAREA(2);
	osd->GetBitmap(3)->Reset();
	}
    osd->DrawRectangle(x0, y3, x4 - 1, y4 - 1, bg);
    osd->DrawRectangle(x4, y3, x6 - 1, y4 - 1, bg);
    osd->DrawRectangle(x6, y3, x8 - 1, y4 - 1, bg);
    osd->DrawRectangle(x0, y4, x6 - 1, y5 - 1, bg);
    osd->DrawRectangle(x6, y4, x8 - 1, y5 - 1, bg);

    if (timer) {
	int x = x6;
	cTimer *tim = Timers.GetNextActiveTimer();
	if (tim) {
	    font = cFont::GetFont(fontOsd);
#if VDRVERSNUM >= 10330 || defined(PATCH_CUSTOMSERVICES)
	    bool conflict = false;	
	    if ((timer & 2) && cPluginManager::CallFirstService("CheckTimerConflict-v1.0", &conflict)) {
		if (conflict) {
		    x = x6 - font->Width(" Timerkonflikte! ");
		    osd->DrawText(x, y2, " Timerkonflikte! ", Theme.Color(clrMessageWarningFg), Theme.Color(clrMessageWarningBg), font);
		    }
		}
#endif
	    if (timer & 1) {
		char *s;
#if VDRVERSNUM < 10323
		asprintf(&s, "%s: %s %d %02d:%02d %s - %s", tr("Next recording"), *WeekDayName(tim->StartTime()), tim->Day(), tim->Start() / 100, tim->Start() % 100, tim->Channel()->Name(), tim->File());
#else
		asprintf(&s, "%s: %s %d %02d:%02d %s - %s", tr("Next recording"), *WeekDayName(tim->StartTime()), cTimer::GetMDay(tim->Day()), tim->Start() / 100, tim->Start() % 100, tim->Channel()->Name(), tim->File());
#endif
		osd->DrawText(x2, y2, s, Theme.Color(clrMenuItemNonSelectable), bg, font, (x6 == x) ? (x6 - x2) : (x6 - x2 - (x6 - x)));
		free(s);
		}
    	    }
	else
	    osd->DrawRectangle(x2, y2, x - 1, y3 - 1, bg);
	}
    else
        osd->DrawRectangle(x2, y2, x6 - 1, y3 - 1, bg);
    osd->DrawRectangle(x6, y2, x8 - 1, y3 - 1, bg);
}

void cSkinElchiDisplayMenu::SetTitle(const char *Title)
{
#ifdef DEBUG
    isyslog("skinelchi: DisplayMenu::SetTitle(%s)", Title);
#endif
    changed = true;
    smallfont = cFont::GetFont(fontSml);
    font = cFont::GetFont(fontOsd);
//for epgimages
#ifdef HAVE_IMAGEMAGICK
    if (!bit)
	title = Title;
#endif
//
    lh = font->Height();
    int xx = x6 - font->Width(*DayDateTime());
    int x = xx - font->Width("  ");
    TitleScroll.DestWidth = x - x2;
    TitleScroll.Fg = Theme.Color(clrMenuTitleFg);
    TitleScroll.Bg = Theme.Color(clrMenuTitleBg);
    TitleScroll.Font = smallfont;
    if (TitleScroll.SetText(Title)) {
        TitleScroll.pos = 0;
        TitleScroll.dir = -1;
	TitleScroll.Time.Set();
	}
    else
        TitleScroll.dir = 0;
    osd->DrawText(TitleScroll.xOff, TitleScroll.yOff, TitleScroll.Text, TitleScroll.Fg, TitleScroll.Bg, TitleScroll.Font, TitleScroll.DestWidth);
    int y = y2 - (font->Height() - smallfont->Height());
    osd->DrawRectangle(x, TitleScroll.yOff, xx - 1, y - 1, TitleScroll.Bg);
    osd->DrawRectangle(TitleScroll.xOff, y, x6 - 1, y2 - 1, TitleScroll.Bg);
}

void cSkinElchiDisplayMenu::SetButtons(const char *Red, const char *Green, const char *Yellow, const char *Blue)
{
#ifdef DEBUG
    isyslog("skinelchi: DisplayMenu::SetButtons(%s,%s,%s,%s)", Red, Green, Yellow, Blue);
#endif
    changed = true;
    smallfont = cFont::GetFont(fontSml);
    font = cFont::GetFont(fontOsd);
    lh = font->Height();
    int w = x8 - x0;
    int t0 = x0;
    int t1 = x2;
    int t2 = t0 + w / 4;
    int t3 = t0 + w / 2;
    int t4 = x8 - w / 4;
    int t5 = x6;
    int t6 = x8;
//for epgimages
#ifdef HAVE_IMAGEMAGICK
    if (!bit) {
	buttons[0] = Red;
        buttons[1] = Green;
	buttons[2] = Yellow;
        buttons[3] = Blue;
	}
#endif
//
    tColor redbg = Theme.Color(clrButtonRedBg);
    tColor bluebg = Theme.Color(clrButtonBlueBg);
    osd->DrawRectangle(t0, y6, t1 - 1, y7 - 1, Red ? redbg : Theme.Color(clrBackground));
    osd->DrawRectangle(t0, y7, t1 - 1, y8 - 1, Theme.Color(Transparent));
    osd->DrawEllipse(t0, y7, t1 - 1, y8 - 1, Red ? redbg : Theme.Color(clrBackground), 3);
    osd->DrawText(t1, y6, Red,    Theme.Color(clrButtonRedFg), Red ? redbg : Theme.Color(clrBackground), smallfont, t2 - t1, 0, taCenter);
    osd->DrawRectangle(t1, y8 - (font->Height() - smallfont->Height()), t2 - 1, y8 - 1, Red ? redbg : Theme.Color(clrBackground));
    osd->DrawText(t2, y6, Green,  Theme.Color(clrButtonGreenFg), Green ? Theme.Color(clrButtonGreenBg) : Theme.Color(clrBackground), smallfont, t3 - t2, 0, taCenter);
    osd->DrawRectangle(t2, y8 - (font->Height() - smallfont->Height()), t3 - 1, y8 - 1, Green ? Theme.Color(clrButtonGreenBg) : Theme.Color(clrBackground));
    osd->DrawText(t3, y6, Yellow, Theme.Color(clrButtonYellowFg), Yellow ? Theme.Color(clrButtonYellowBg) : Theme.Color(clrBackground), smallfont, t4 - t3, 0, taCenter);
    osd->DrawRectangle(t3, y8 - (font->Height() - smallfont->Height()), t4 - 1, y8 - 1, Yellow ? Theme.Color(clrButtonYellowBg) : Theme.Color(clrBackground));
    osd->DrawText(t4, y6, Blue,   Theme.Color(clrButtonBlueFg), Blue ? bluebg : Theme.Color(clrBackground), smallfont, t5 - t4, 0, taCenter);
    osd->DrawRectangle(t4, y8 - (font->Height() - smallfont->Height()), t6 - 1, y8 - 1, Blue ? bluebg : Theme.Color(clrBackground));
    osd->DrawRectangle(t5, y6, t6 - 1, y7 - 1, Blue ? bluebg : Theme.Color(clrBackground));
    osd->DrawRectangle(t5, y7, t6 - 1, y8 - 1, Theme.Color(Transparent));
    osd->DrawEllipse(t5, y7, t6 - 1, y8 - 1, Blue ? bluebg : Theme.Color(clrBackground), 4);
}

void cSkinElchiDisplayMenu::SetMessage(eMessageType Type, const char *Text)
{
    if (Text) {
#ifdef DEBUG
	isyslog("skinelchi: DisplayMenu::SetMessage(%d,%s)", int(Type), Text);
#endif
	changed = true;
	message = true;
        font = cFont::GetFont(fontOsd);
	lh = font->Height();
        int lhh = lh / 2;
	tColor bg = Theme.Color(clrBackground);
        osd->DrawRectangle(x0, y5, x2 - 1, y6 - 1, bg);
	tColor msb = Theme.Color(clrMessageStatusBg + 2 * Type);
	osd->DrawEllipse(x0, y5, x0 + lhh - 1, y6 - 1, msb, 7);
        osd->DrawText(x0 + lhh, y5, Text, Theme.Color(clrMessageStatusFg + 2 * Type), msb, font, x6 - x0 - lhh, 0, taCenter);
	osd->DrawRectangle(x6, y5, x8 - lhh - 1, y6 - 1, msb);
        osd->DrawRectangle(x8 - lhh, y5, x8 - 1, y6 - 1, bg);
	osd->DrawEllipse(x8 - lhh, y5, x8 - 1, y6 - 1, msb, 5);
	}
    else {
	if (message) {
#ifdef DEBUG
	    isyslog("skinelchi: DisplayMenu::SetMessage(%d,%s)", int(Type), Text);
#endif
	    changed = true;
	    message = false;
	    tColor bg = Theme.Color(clrBackground);
	    osd->DrawRectangle(x0, y5, x6 - 1, y6 - 1, bg);
	    osd->DrawRectangle(x6, y5, x8 - 1, y6 - 1, bg);
	    }
#ifdef DEBUG
	else
	    isyslog("skinelchi: skip DisplayMenu::SetMessage(%d,%s)", int(Type), Text);
#endif
	}
}

void cSkinElchiDisplayMenu::SetItem(const char *Text, int Index, bool Current, bool Selectable)
{
#ifdef HAVE_IMAGEMAGICK
  if (epgimages) {
    if (!bit) {				//for direct switching in epgsearch 0.9.3
	if (setitem < 2) {
#ifdef DEBUG
  isyslog("skinelchi: skip DisplayMenu::SetItem(%s,%d,%s,%s)", Text, Index, Current ? "'Current'" : "'nonCurrent'", Selectable ? "'Selectable'" : "'nonSelectable'");
#endif
	    setitem++;
	    if (setitem == 1) {
		if (item1text)
		    free(item1text);
		asprintf(&item1text, "%s", Text);
	        item1index = Index;
		item1current = Current;
		item1selectable = Selectable;
		}
	    else {
		if (item2text)
		    free(item2text);
		asprintf(&item2text, "%s", Text);
		item2index = Index;
		item2current = Current;
		item2selectable = Selectable;
		}
	    if (Current)
		currentIndex = Index;
	    return;
	    }
	}				//for direct switching in epgsearch 0.9.3
    epgimages = false;
    loading = 0;
    if (!bit) {
	noflush = true;
	delete osd;
	osd = NULL;
#ifdef VIDEOSTATUS
	if (SkinElchiSetup.videostatus)
	    vdrosd = (VideoStatus.NTSC() == (SkinElchiSetup.videosystem == vsNTSC));
	else
#endif
	    vdrosd = (cDevice::PrimaryDevice()->GetVideoSystem() == vsNTSC) == (SkinElchiSetup.videosystem == vsNTSC);
	if (y8 != (y0 + (vdrosd ? Setup.OSDHeight : SkinElchiSetup.osdheight)))
	    INITY(vdrosd ? Setup.OSDHeight : SkinElchiSetup.osdheight);
	Show();
#ifdef DEBUG
	isyslog("skinelchi: DisplayMenu::SetItem - call DisplayMenu::SetTitle()");
#endif
	SetTitle(title);
#ifdef DEBUG
	isyslog("skinelchi: DisplayMenu::SetItem - call DisplayMenu::SetButtons()");
#endif
	SetButtons(buttons[0], buttons[1], buttons[2], buttons[3]);
	}
    }
#endif
#ifdef DEBUG
  isyslog("skinelchi: DisplayMenu::SetItem(%s,%d,%s,%s)", Text, Index, Current ? "'Current'" : "'nonCurrent'", Selectable ? "'Selectable'" : "'nonSelectable'");
#endif
  changed = true;
  tColor ColorFg, ColorBg;
  if (Current) {
    ColorFg = Theme.Color(clrMenuItemCurrentFg);
    ColorBg = Theme.Color(clrMenuItemCurrentBg);
    currentIndex = Index;
    }
  else {
    ColorFg = Theme.Color(Selectable ? clrMenuItemSelectable : clrMenuItemNonSelectable);
    ColorBg = Theme.Color(clrBackground);
    }
  font = cFont::GetFont(fontOsd);
  lh = font->Height();
  int y = menuTop + Index * lh;
  int x;
  int xx;
  if (area) {
    x = x5;
    xx = x6;
    }
  else {
    x = x7;
    xx = x8;
    }	
  if (currentIndex == Index) {
    osd->DrawEllipse(x0, y, x1 - 1, y + lh - 1, ColorBg, 7);
    osd->DrawRectangle(x1, y, x2, y + lh - 1, ColorBg);
    osd->DrawEllipse(x, y, xx - 1, y + lh - 1, ColorBg, 5);
  }
  const char *s;
  for (int i = 0; i < MaxTabs; i++) {
    s = GetTabbedText(Text, i);
    if (s) {
        CurrentItemScroll.xOff = x1 + Tab(i);
	osd->DrawText(CurrentItemScroll.xOff, y, s, ColorFg, ColorBg, font, x - CurrentItemScroll.xOff);
        }
    if (!Tab(i + 1))
        break;
    }
  if (Current) {
    if (s) {
	if (strcmp(olditem, s)) {//for scrolling
	    if (olditem)
		free(olditem);
	    asprintf(&olditem, "%s", s);
	    CurrentItemScroll.DestWidth = x - CurrentItemScroll.xOff;
	    CurrentItemScroll.Fg = ColorFg;
	    CurrentItemScroll.Bg = ColorBg;
	    CurrentItemScroll.Font = font;
	    if (CurrentItemScroll.SetText(s)) {
    		CurrentItemScroll.pos = 0;
    		CurrentItemScroll.dir = -1;
		CurrentItemScroll.yOff = y;
	        CurrentItemScroll.delay = 10;
		CurrentItemScroll.intervall = 15;
	        CurrentItemScroll.Time.Set();
		}
	    else
    		CurrentItemScroll.dir = 0;
	    }
	else {
	    if (CurrentItemScroll.yOff != y)
	        CurrentItemScroll.yOff = y;
	    }
	}
    else
    	CurrentItemScroll.dir = 0;
    }
  SetEditableWidth(x - x1 - Tab(1));
}

void cSkinElchiDisplayMenu::SetEvent(const cEvent *Event)
{
  if (!Event)
#ifdef DEBUG
    {
    isyslog("skinelchi: skip DisplayMenu::SetEvent()");
#endif
     return;
#ifdef DEBUG
    }
#endif

#ifdef DEBUG
  isyslog("skinelchi: DisplayMenu::SetEvent(%d)", Event->EventID());
#endif
  changed = true;
  tColor bg = Theme.Color(clrBackground);
  tColor me = Theme.Color(clrMenuEventTitle);
//for epgimages
  int x;
#ifdef HAVE_IMAGEMAGICK
  if (SkinElchiSetup.epgimages) {
    x = x4 - x2 - (x2 - x0);
    eventID = Event->EventID();
    char *filename = NULL;
    asprintf(&filename, "%s/%d.png", SkinElchiSetup.epgimagedir, eventID);
    FILE *file = fopen(filename, "r");
    free(filename);
    if (file) {
	loading = eventID;
	fclose(file);
	if (!epgimages) {
	    epgimages = true;
	    if (!bit) {
		noflush = true;
		delete osd;
		osd = NULL;
		}
	    Pid = -1;
	    epgimage = new cEpgImage();
	    switch (SkinElchiSetup.epgimages) {
		case 1: x4 = x6 - 120; y4 = y3 + 90; break;
		case 3: x4 = x6 - 120; y4 = y3 + 120; break;
		default: x4 = x6 - 180; y4 = y3 + 144;
		}
	    if (!bit)
		ShowEpgImage();
	    }
	epgimage->loading = eventID;
	char *s = NULL;
	asprintf(&s, tr("Picture"));
	int x64 = x6 - x4;
	int y4432 = y4 - (y4 - y3) / 2;
	osd->DrawText(x4, y4432 - lh, s, me, bg, smallfont, x64, 0, taCenter);
	asprintf(&s, tr("loading"));
	osd->DrawText(x4, y4432, s, me, bg, smallfont, x64, 0, taCenter);
	free(s);
	}
    else {
	REFILLAREA;
	loading = 0;
	char *s;
	asprintf(&s, tr("No picture"));
	int x64 = x6 - x4;
	osd->DrawText(x4, y3, s, me, bg, smallfont, x64, 0, taCenter);
	asprintf(&s, tr("available"));
	osd->DrawText(x4, y3 + lh, s, me, bg, smallfont, x64, 0, taCenter);
	free(s);
	}
    setitem = 0;//for direct switching in epgsearch 0.9.3
    }
  else {
#endif
    x = x6 - x2;
    REFILLAREA;
#ifdef HAVE_IMAGEMAGICK
    }
#endif
//
  font = cFont::GetFont(fontOsd);
  smallfont = cFont::GetFont(fontSml);
  tColor tr = Theme.Color(Transparent);

  int y = y3;
  cTextScroller ts;
  char t[32];
  snprintf(t, sizeof(t), "%s  %s - %s", *Event->GetDateString(), *Event->GetTimeString(), *Event->GetEndTimeString());
  ts.Set(osd, x2, y, x, y5 - y, t, font, Theme.Color(clrMenuEventTime), tr);
  lh = font->Height();
  y += ts.Height();
  if (Event->Vps() && Event->Vps() != Event->StartTime()) {
    char *s = NULL;
    asprintf(&s, " VPS: %s", *Event->GetVpsString());
    osd->DrawText(x2, y, s, Theme.Color(clrMenuEventVpsFg), Theme.Color(clrMenuEventVpsBg), smallfont);
    free(s);
    }
  y += lh;
  ts.Set(osd, x2, y, x, y5 - y, Event->Title(), font, me, tr);
  y += ts.Height();
#ifdef HAVE_IMAGEMAGICK
  int slh = smallfont->Height();
#endif
  if (!isempty(Event->ShortText())) {
    if (isempty(Event->Title()) || strcmp(Event->Title(), Event->ShortText())) {
	y += lh;
#ifdef HAVE_IMAGEMAGICK
	if ((epgimages && (((y4 + slh) <= y) || (!area && !loading))) || (!epgimages && !loading))
	    x = x6 - x2;
#endif
	char *s = NULL;
        asprintf(&s, Event->ShortText());
	if (isempty(Event->Description())) {
	    textScroller.Set(osd, x2, y, x, y5 - y, compactspace(s), smallfont, Theme.Color(clrMenuEventShortText), bg);
	    SetScrollbar();
	    }
	else {
	    ts.Set(osd, x2, y, x, y5 - y, compactspace(s), smallfont, Theme.Color(clrMenuEventShortText), tr);
	    y += ts.Height();
	    }
	if (s)
	    free(s);
	}
    }
  if (!isempty(Event->Description())) {
    y += lh;
#ifdef HAVE_IMAGEMAGICK
    if (epgimages && (y4 + slh) > y)
        y = y4 + slh;
#endif
    char *s = NULL;
    asprintf(&s, Event->Description());
    textScroller.Set(osd, x2, y, x6 - x2, y5 - y, compactspace(s), smallfont, Theme.Color(clrMenuEventDescription), bg);
    if (s)
	free(s);
    SetScrollbar();
    }
}

void cSkinElchiDisplayMenu::SetRecording(const cRecording *Recording)
{
#ifdef DEBUG
    isyslog("skinelchi: DisplayMenu::SetRecording(...)");
#endif
    changed = true;
#if VDRVERSNUM >= 10325
    if (!Recording)
	return;
    const cRecordingInfo *Info = Recording->Info();
    font = cFont::GetFont(fontOsd);
    tColor tr = Theme.Color(Transparent);
    tColor bg = Theme.Color(clrBackground);
    int y = y3;
    int x = x6 - x2;
    REFILLAREA;
    lh = font->Height();
    cTextScroller ts;
    char t[32];
    snprintf(t, sizeof(t), "%s  %s", *DateString(Recording->start), *TimeString(Recording->start));
    ts.Set(osd, x2, y, x, y5 - y, t, font, Theme.Color(clrMenuEventTime), tr);
    y += ts.Height();
    y += lh;
    const char *Title = Info->Title();
    if (isempty(Title))
	Title = Recording->Name();
    ts.Set(osd, x2, y, x, y5 - y, Title, font, Theme.Color(clrMenuEventTitle), tr);
    y += ts.Height();
    if (!isempty(Info->ShortText())) {
	smallfont = cFont::GetFont(fontSml);
	ts.Set(osd, x2, y, x, y5 - y, Info->ShortText(), smallfont, Theme.Color(clrMenuEventShortText), tr);
	y += ts.Height();
	}
    y += lh;
    if (!isempty(Info->Description())) {
	textScroller.Set(osd, x2, y, x6 - x2, y5 - y, Info->Description(), font, Theme.Color(clrMenuEventDescription), bg);
	SetScrollbar();
    }
#else
    SetText(Recording->Summary(), false); //TODO
#endif
}

void cSkinElchiDisplayMenu::SetText(const char *Text, bool FixedFont)
{
#ifdef DEBUG
    isyslog("skinelchi: DisplayMenu::SetText(%s,%s)", Text, FixedFont ? "'FixedFont'" : "'nonFixedFont'");
#endif
    changed = true;
#if VDRVERSNUM >= 10342
    textScroller.Set(osd, x2, menuTop, GetTextAreaWidth(), menuHeight, Text, GetTextAreaFont(FixedFont), Theme.Color(clrMenuText), Theme.Color(clrBackground));
#else
    font = cFont::GetFont((FixedFont && !SkinElchiSetup.usenofixedfont) ? fontFix : fontOsd);
    textScroller.Set(osd, x2, menuTop, x6 - x2, menuHeight, Text, font, Theme.Color(clrMenuText), Theme.Color(clrBackground));
#endif
    SetScrollbar();
}

#if VDRVERSNUM >= 10342
int cSkinElchiDisplayMenu::GetTextAreaWidth(void) const {
    return x6 - x2;
}

const cFont *cSkinElchiDisplayMenu::GetTextAreaFont(bool FixedFont) const {
    return cFont::GetFont((FixedFont && !SkinElchiSetup.usenofixedfont) ? fontFix : fontOsd);
}
#endif

void cSkinElchiDisplayMenu::Flush(void)
{
if (!noflush) {
  noflush = true;
  update->noflush = true;
#ifdef HAVE_IMAGEMAGICK
    if (setitem) {		//for direct switching in epgsearch 0.9.3
	changed = true;
    	if (epgimages) {
	    epgimages = false;
	    loading = 0;
	    noflush = true;
	    delete osd;
	    osd = NULL;
#ifdef VIDEOSTATUS
	    if (SkinElchiSetup.videostatus)
		vdrosd = (VideoStatus.NTSC() == (SkinElchiSetup.videosystem == vsNTSC));
	    else
#endif
		vdrosd = (cDevice::PrimaryDevice()->GetVideoSystem() == vsNTSC) == (SkinElchiSetup.videosystem == vsNTSC);
	    if (y8 != (y0 + (vdrosd ? Setup.OSDHeight : SkinElchiSetup.osdheight)))
		INITY(vdrosd ? Setup.OSDHeight : SkinElchiSetup.osdheight);
	    Show();
#ifdef DEBUG
	    isyslog("skinelchi: call DisplayMenu::SetTitle()");
#endif
	    SetTitle(title);
#ifdef DEBUG
	    isyslog("skinelchi: call DisplayMenu::SetButtons()");
#endif
	    SetButtons(buttons[0], buttons[1], buttons[2], buttons[3]);
	    }
	if (setitem == 2)
	    SetItem(item2text, item2index, item2current, item2selectable);
	SetItem(item1text, item1index, item1current, item1selectable);
	setitem = 0;
	}		//END for direct switching in epgsearch 0.9.3
#endif
    cString date = DayDateTime();
    font = cFont::GetFont(fontOsd);
    if (strcmp(olddate, date)) {
#ifdef DEBUG
	isyslog("skinelchi: show DayDateTime in DisplayMenu::Flush()");
#endif
	changed = true;
	if (olddate)
	    free (olddate);
	asprintf(&olddate, date);
	osd->DrawText(x6 - font->Width(date), y0, date, Theme.Color(clrMenuDate), Theme.Color(clrMenuTitleBg), font);
	}
#ifdef DEBUG
     else
	isyslog("skinelchi: skip show DayDateTime in DisplayMenu::Flush()");
#endif
    lh = font->Height();
#ifdef HAVE_IMAGEMAGICK
    if (epgimages) {
	smallfont = cFont::GetFont(fontSml);
        if (epgimage->imageready == eventID && (epgimage->ShowTime.TimedOut() || epgimage->first)) {
	    int w = epgimage->bmpEpgImage->Width();
	    int h = epgimage->bmpEpgImage->Height();
    	    tColor bg = Theme.Color(clrBackground);
	    if (area == 4)
	        osd->GetBitmap(3)->Reset();
	    else {
	        osd->GetBitmap(0)->Reset();
	        for (int i = 0; i < MAX_ELCHI_THEME_COLORS; i++)
	            osd->GetBitmap(0)->SetColor(i, SkinElchiSetup.ThemeColors[i]);
	        }
	    osd->DrawRectangle(x4, y3, x6 - w - 1, y4 - 1, bg);
	    osd->DrawRectangle(x6 - w - 1, y3, x6 - 1, y4 - h - 1, bg);
#ifdef DEBUG
	    isyslog("skinelchi: show epgimage -> EventID = %d", eventID);
#endif
	    osd->DrawBitmap( x6 - w, y4 - h, *(epgimage->bmpEpgImage));
	    epgimage->imageready++;
	    updateimage = 0;
	    epgimage->ShowTime.Set(epgimage->epgshowtime);
	    changed = true;
	    }
	}
#endif
  if (CurrentItemScroll.dir) {//for scrolling
    if (CurrentItemScroll.DrawNeeded()) {
	for (int i = 0; i < CurrentItemScroll.DestWidth; i++) {
	    for (int j = 0; j < CurrentItemScroll.Height(); j++)
		osd->DrawPixel(CurrentItemScroll.xOff + i, CurrentItemScroll.yOff + j, CurrentItemScroll.bmp->Color(*(CurrentItemScroll.bmp->Data(i + CurrentItemScroll.pos, j))));
	    }
	changed = true;
	}
    }
  if (TitleScroll.dir) {//for scrolling
    if (TitleScroll.DrawNeeded()) {
        for (int i = 0; i < TitleScroll.DestWidth; i++) {
	    for (int j = 0; j < TitleScroll.Height(); j++)
	        osd->DrawPixel(TitleScroll.xOff + i, TitleScroll.yOff + j, TitleScroll.bmp->Color(*(TitleScroll.bmp->Data(i + TitleScroll.pos, j))));
	    }
	changed = true;
	}
    }

  if (!message) {
    if (VdrStatus.VolumeChange != volumechange) {
	volumechange = VdrStatus.VolumeChange;
	oldvolume = VdrStatus.Volume;
	int w = x6;
	int y = y5 + 4;
	int h = y6 - 5;
	int p = ((w - x2) * oldvolume) / 255;
	int xp = x2 + p;
	osd->DrawRectangle(x2, y, xp - 1, h, Theme.Color(clrVolumeBarUpper));
        osd->DrawRectangle(xp, y, x6 - 1, h, Theme.Color(clrVolumeBarLower));
	showVolume = true;
	volume.Set(1000);
	changed = true;
	}
    else {
	if (showVolume && volume.TimedOut()) {
	    tColor bg = Theme.Color(clrBackground);
	    osd->DrawRectangle(x2, y5 + 4, x6 - 1, y6 - 5, bg);
	    showVolume = false;
	    changed = true;
	    }
	}
    }

/*  else
    if (SkinElchiSetup.helptext) SetHelpText(); */ //TODO

  if (SkinElchiSetup.changing) {
#ifdef DEBUG
    isyslog("skinelchi: colorchanging - DisplayMenu::Flush()");
#endif
    tColor searchcolor = Theme.Color(clrBackground);
    int index = osd->GetBitmap(area)->Index(searchcolor);
    osd->GetBitmap(area)->SetColor(index, Theme.Color(changing));
    osd->Flush();
    osd->GetBitmap(area)->SetColor(index, searchcolor);
    }
  else {
    if (changed) {
#ifdef DEBUG
	isyslog("skinelchi: DisplayMenu::Flush()");
#endif
	osd->Flush();
	changed = false;
	}
#ifdef DEBUG
    else
	isyslog("skinelchi: skip DisplayMenu::Flush()");
#endif
    }
  update->noflush = false;
  noflush = false;
  }
#ifdef DEBUG
else
  isyslog("skinelchi: skip cSkinElchiDisplayMenu::Flush() because cSkinElchiDisplayMenu::Flush() is active");
#endif
}

void cSkinElchiDisplayMenu::Update(void) {
    update->lock();
    update->UpdateOsd = osd;
    update->unlock();
    update->Update();
    }

//for epgimages
#ifdef HAVE_IMAGEMAGICK
cEpgImage::cEpgImage(void) {
#ifdef DEBUG
    isyslog("skinelchi: DisplayMenu - cEpgImage::cEpgImage()");
#endif
    bmpEpgImage = new cBitmap(8,8,1);
    direventid = filename = NULL;
    file = NULL;
    eventid = loading = 0;
    imageready = 1;
    first = true;
    moreimages = false;
    imagenumber = -1;
    epgshowtime = SkinElchiSetup.epgshowtime * 1000;
    filetype = "png";
    switch (SkinElchiSetup.epgimages) {
	case 1: x = 120; y = 90; break;
	case 3: x = 120; y = 120; break;
	default: x = 180; y = 144;
	}
    Start();
}

cEpgImage::~cEpgImage() {
#ifdef DEBUG
    isyslog("skinelchi: DisplayMenu - cEpgImage::~cEpgImage()");
#endif
    if (direventid) {
	free (direventid);
	direventid = NULL;
	}
    if (filename) {
	free (filename);
	filename = NULL;
	}
    if (bmpEpgImage) {
	delete bmpEpgImage;
	bmpEpgImage = NULL;
	}
    if (file)
	fclose(file);
}

void cEpgImage::Action(void) {
    pid = Pid = (int)getpid();
#ifdef DEBUG
    isyslog("skinelchi: DisplayMenu - cEpgImage::Action() - pid=%d", pid);
#endif
    cOSDImageBitmap image;
    while(epgimages && Pid == pid) {
	if (loading || (imageready != eventid && moreimages)) {
	    if (loading) {
		eventid = loading;
		loading = 0;
		updateimage = 0;
		imageready = eventid + 1;
		first = true;
		if (direventid)
		    free (direventid);
		asprintf(&direventid, "%s/%d", SkinElchiSetup.epgimagedir, eventid);
		if (filename)
		    free (filename);
	        asprintf(&filename, "%s.%s", direventid, filetype);
		imagenumber = 0;
		moreimages = true;
		}
	    else {
		first = false;
		imagenumber++;
		if (filename)
		    free (filename);
		if (!imagenumber) {
		    if (direventid)
			free (direventid);
		    asprintf(&direventid, "%s/%d", SkinElchiSetup.epgimagedir, eventid);
	    	    asprintf(&filename, "%s.%s", direventid, filetype);
		    }
		else
	    	    asprintf(&filename, "%s_%d.%s", direventid, imagenumber, filetype);
		}
	    file = fopen(filename, "r");
	    if (file) {
		fclose(file);
#ifdef DEBUG
		isyslog("skinelchi: load from epgimages: '%s'", filename);
#endif
		if (!epgimages || Pid != pid)
		    break;
		if (!loading)
#ifdef DEBUG
		if (!
#endif
		image.LoadZoomed(filename, 0, 0, 0, 0)
#ifdef DEBUG
)
		    isyslog("skinelchi: LoadZoomed failed");
#else
;
#endif
		if (!epgimages || Pid != pid)
		    break;
		if (!loading)
    		    image.Render(*bmpEpgImage, x, y, epgimagecolors, 0);
		if (!epgimages || Pid != pid)
		    break;
		if (!loading) {
		    imageready = eventid;
		    updateimage = pid;
		    }
		}
	    else {
#ifdef DEBUG
		isyslog("skinelchi: File '%s' not found", filename);
#endif
		if (imagenumber < 2)
		    moreimages = false;
		else
		    imagenumber = -1;
		}
	    }
	else {
	    cCondWait::SleepMs(9);
	    if (!epgimages || Pid != pid)
		break;
	    }
	}
    if (direventid) {
	free (direventid);
	direventid = NULL;
	}
    if (filename) {
	free (filename);
	filename = NULL;
	}
    if (bmpEpgImage) {
	delete bmpEpgImage;
	bmpEpgImage = NULL;
	}
#ifdef DEBUG
    isyslog("skinelchi: DisplayMenu - cEpgImage-thread end (pid=%d)", pid);
#endif
}
#endif

cUpdate::cUpdate(void) {
#ifdef DEBUG
    isyslog("skinelchi: DisplayMenu - cUpdate::cUpdate()");
#endif
    UpdateOsd = NULL;
    Start();
}

cUpdate::~cUpdate() {
#ifdef DEBUG
    isyslog("skinelchi: DisplayMenu - cUpdate::~cUpdate()");
#endif
    UpdatePid = 0;
}

void cUpdate::Update() {
#ifdef DEBUG
    isyslog("skinelchi: cUpdate::Update() is called");
#endif
    Wait.Signal();
    }

void cUpdate::lock() {
    Lock();
}

void cUpdate::unlock() {
    Unlock();
}

void cUpdate::Action(void) {
    pid = (int)getpid();
#ifdef DEBUG
    isyslog("skinelchi: DisplayMenu - cUpdate::Action() - pid=%d", pid);
#endif
    UpdatePid = pid;
    while (UpdatePid == pid) {
	Wait.Wait(9);
#ifdef HAVE_IMAGEMAGICK
	if ((/*(
#ifdef HAVE_IMAGEMAGICK
!epgimages && 
#endif
((CurrentItemScroll.dir && 
((int)CurrentItemScroll.Time.Elapsed() / CurrentItemScroll.intervall))
|| (TitleScroll.dir && 
((int)TitleScroll.Time.Elapsed() / TitleScroll.intervall))))
#ifdef HAVE_IMAGEMAGICK
||*/(epgimages && (updateimage == Pid))
//#endif
)
	    && (!changed && !noflush)) {
#ifdef DEBUG
		isyslog("skinelchi: DisplayMenu - cUpdate call Skins.Flush()");
#endif
    		if (Skins.IsOpen())
		    Skins.Flush();
		}
#endif
    	}
#ifdef DEBUG
    isyslog("skinelchi: DisplayMenu - cUpdate-thread end (pid=%d)", pid);
#endif
}

//
