// Devay - normal and shade mode scripts

#include "../../../lib/std.mi"
#include "../../../lib/config.mi"
#include "AutoRepeatButton.m"

Function setTempText(String txt);
Function ShadesetTempText(String txt);
Function emptyTempText();
Function StartupPlayStatus();

Function initSeek();
Function updateSeeker(int x, int y);
Function setSeekerPosition(int p);

Function SongtickerMenu();
Function MenuResult(int ticking);

Class GuiObject HintObject;
Global HintObject Play, Pause;
Global int playStatus; 

Global Text Songticker;
Global Timer Songtickertimer;
Global AutoRepeatButton VolumeUp, VolumeDown;
Global float CurrentVolume; 
Global Slider hidvol; 

Class ToggleButton HintToggleButton;
Class ConfigAttribute ToggleConfigAttribute;
Global HintToggleButton ToggleXFade, ToggleShuffle, ToggleRepeat;
Global ToggleConfigAttribute attr_repeat, attr_shuffle, attr_crossfade;
Global HintObject ShadePlay, ShadePause, Play, Stop, Previous, Next, Pause, Thinger, Open, close, layoutswitch, minimize;

Global Slider ShadeVolume, Seeker;
Global int Seeking;
Global Text ShadeSongTicker;
Global Timer ShadeTextTimer;
Global button SongInfoEditor;

Global HintToggleButton ToggleXbass;
Global int eqStatus, plStatus;
Global button ShadeEq, ShadePl;
Global button Eq, Pl;

Global AnimatedLayer Minidisc;
Global int isPaused;

Global button thingertoggle;
Global group displaygroup, thingergroup;
Global int thStatus;
Global AnimatedLayer Seek;
Global Map seekMap;
Global Timer SongPlayingTimer;
Global Slider HiddenSeek;
Global int SeekChanging, SeekFrames, LastSeek, TickerExist;

Global slider eqPreamp, eq1, eq2, eq3, eq4, eq5, eq6, eq7, eq8, eq9, eq10;
Global int FullStatus, PopStatus, RockStatus, SoftStatus, xbassStatus;
Global HintToggleButton Fullpreset, Poppreset, Rockpreset, Softpreset;
Global HintObject FullpresetDummy, PoppresetDummy, RockpresetDummy, SoftpresetDummy, ToggleXbassDummy;

Global Popupmenu tickerMenu; 
Global Text PlshadeSongticker;

System.onScriptLoaded() {
  AutoRepeat_Load();  
  Layout mainnormal = getContainer("main").getLayout("normal");
  Pause = mainnormal.getObject("Pause");
  Play = mainnormal.getObject("Play");
  VolumeUp = mainnormal.getObject("VolumeUp");
  VolumeDown = mainnormal.getObject("VolumeDown");
  hidvol = mainnormal.getObject("hidvol");

  Pause = mainnormal.getObject("Pause");
  Play = mainnormal.getObject("Play");
  Stop = mainnormal.getObject("Stop");
  Next = mainnormal.getObject("Next");
  Previous = mainnormal.getObject("Prev");
  Open = mainnormal.getObject("Eject");
  ToggleXFade = mainnormal.getObject("Crossfade");
  ToggleShuffle = mainnormal.getObject("Shuffle");
  ToggleRepeat = mainnormal.getObject("Repeat");
  ToggleXbass = mainnormal.getObject("Xbass");
  Eq = mainnormal.getObject("Eq");
  Pl = mainnormal.getObject("Pl");
  close = mainnormal.getObject("Close");
  layoutswitch = mainnormal.getObject("winshade");
  minimize = mainnormal.getObject("Minimize");

  Group displaygroup = getContainer("main").getLayout("normal").getObject("display-group");
  SongTicker = displaygroup.getObject("Songticker");
  SongInfoEditor = displaygroup.getObject("SongInfoEditor");
  CurrentVolume = System.GetVolume();
  SongTickerTimer = new Timer;
  SongTickerTimer.setDelay(750);

  Group thingergroup = getContainer("main").getLayout("normal").getObject("thinger-group");
  thingertoggle = mainnormal.getObject("TH");
  thingergroup.hide();

  Seek = displaygroup.getObject("SeekAnim");
  HiddenSeek = displaygroup.findObject("hideseek");
  SongPlayingTimer = new Timer;
  SongPlayingTimer.setDelay(750);

  seekMap = new Map;
  seekMap.loadMap("player.seek-map");

  LastSeek = 0;
  SeekChanging = 0;
  SeekFrames = Seek.getLength();
  SeekFrames = SeekFrames - 1;

  Layout mainslim = getContainer("main").getLayout("slim");
  ShadePause = mainslim.getObject("Pause");
  ShadePlay = mainslim.getObject("Play");
  ShadeSongTicker = mainslim.getObject("Songticker");
  ShadeEq = mainslim.getObject("Eq");
  ShadePl = mainslim.getObject("Pl");
  ShadeVolume = mainslim.getObject("Volume");
  Seeker = mainslim.getObject("Seeker");
  ShadeTextTimer = new Timer;
  ShadeTextTimer.setDelay(750);

  Layout eqnorm = System.getContainer("eq").getLayout("normal");
  eqPreamp = eqnorm.getObject("preamp");
  eq1 = eqnorm.getObject("eq1");
  eq2 = eqnorm.getObject("eq2");
  eq3 = eqnorm.getObject("eq3");
  eq4 = eqnorm.getObject("eq4");
  eq5 = eqnorm.getObject("eq5");
  eq6 = eqnorm.getObject("eq6");
  eq7 = eqnorm.getObject("eq7");
  eq8 = eqnorm.getObject("eq8");
  eq9 = eqnorm.getObject("eq9");
  eq10 = eqnorm.getObject("eq10");
  Fullpreset = displaygroup.getObject("Full");
  Poppreset = displaygroup.getObject("Pop");
  Rockpreset = displaygroup.getObject("Rock");
  Softpreset = displaygroup.getObject("Soft");
  
  // these are just for temp text
  ToggleXbassDummy = mainnormal.getObject("Xbass");
  FullpresetDummy = displaygroup.getObject("Full");
  PoppresetDummy = displaygroup.getObject("Pop");
  RockpresetDummy = displaygroup.getObject("Rock");
  SoftpresetDummy = displaygroup.getObject("Soft");

  int FullStatus = getPrivateInt("Devay", "Full", 0);
  Fullpreset.setActivated(FullStatus);

  int PopStatus = getPrivateInt("Devay", "Pop", 0);
  Poppreset.setActivated(PopStatus);

  int RockStatus = getPrivateInt("Devay", "Rock", 0);
  Rockpreset.setActivated(RockStatus);

  int SoftStatus = getPrivateInt("Devay", "Soft", 0);
  Softpreset.setActivated(SoftStatus);

  int xbassStatus = getPrivateInt("Devay", "Xbass", 0);
  ToggleXbass.setActivated(xbassStatus);

  playStatus = System.getStatus(); 
  StartupPlayStatus();

  Minidisc = displaygroup.getObject("AnimCD");

  if (getLeftVUMeter() != 0) {
     Minidisc.setStartFrame(1);
     Minidisc.setEndFrame(8);
     Minidisc.play();
  }   

  Layout plshade = getContainer("Pledit").getLayout("shade");
  PlshadeSongticker = plshade.getObject("Songticker");
 
  int tickerstatus=getPrivateInt("Devay", "scrolling", 1);
	if (tickerstatus==1) {
		SongTicker.setXMLParam("ticker", "1");
		ShadeSongTicker.setXMLParam("ticker", "1");
		PlshadeSongticker.setXMLParam("ticker", "1");
	} else {
		SongTicker.setXMLParam("ticker", "0");
		ShadeSongTicker.setXMLParam("ticker", "0");
		PlshadeSongticker.setXMLParam("ticker", "0");
	}

	SongtickerMenu();
	MenuResult( getPrivateInt("Devay", "scrolling", 1) );
}    

System.onScriptUnLoading() {
  AutoRepeat_UnLoad();
  delete Songtickertimer;
  delete SongPlayingTimer;
  delete seekMap;
}

SongTickerTimer.onTimer() {
  SongTicker.setText("");
  stop();
}  

setTempText(String txt) {
  Songtickertimer.stop();
  Songticker.setText(txt);
  Songtickertimer.start();
}

emptyTempText() {
  Songticker.setText("");
  Songtickertimer.stop();
}
  
//  Play/Pause and spinning Minidisc

System.onPlay()
{
  Play.hide();
  Pause.show();
  ShadePlay.hide();
  ShadePause.show();

  Minidisc.setStartFrame(1);
  Minidisc.setEndFrame(8);
  Minidisc.play();
}


System.onPause()
{
  Play.show();
  Pause.hide();
  ShadePlay.show();
  ShadePause.hide();

  Minidisc.pause();
}

System.onStop()
{
  Play.show();
  Pause.hide();
  ShadePlay.show();
  ShadePause.hide();

  Minidisc.stop();
  Minidisc.gotoFrame(0);
}


System.onresume()
{
  Play.hide();
  Pause.show();
  ShadePlay.hide();
  ShadePause.show();

  Minidisc.setStartFrame(1);
  Minidisc.setEndFrame(8);
  Minidisc.play();
}
 
StartupPlayStatus() {
  if (playStatus != 1) {
    Play.show();
    Pause.hide();
    ShadePlay.show();
    ShadePause.hide();
  }
  else {
    Pause.show();
    Play.hide();
    ShadePause.show();
    ShadePlay.hide();
  } 
}

// Thinger toggle
thingertoggle.onActivate(int thStatus) {
 Group displaygroup = getContainer("main").getLayout("normal").getObject("display-group");
 Group thingergroup = getContainer("main").getLayout("normal").getObject("thinger-group");
  if (thStatus == 1) {
 displaygroup.hide();
 thingergroup.show();
  } else {
 thingergroup.hide();
 displaygroup.show();
  }
}

// Animated Seek
System.onPlay() {
	SongPlayingTimer.start();
}

SongPlayingTimer.onTimer() {
	if (System.getPlayItemLength() > 0) {
		float p = System.getPosition();
		p = p / System.getPlayItemLength();
		p = p * 255;
		setSeekerPosition(p);
	}
}

System.onStop() {
	SongPlayingTimer.stop();
	Seek.gotoFrame(0);
}


Seek.onLeftButtonDown(int x, int y) {
	if (getPlayItemLength() > 0) {
		SongPlayingTimer.stop();
		SeekChanging = 1;
		updateSeeker(x, y);
	}
}

Seek.onLeftButtonUp(int x, int y) {
	Float l;
	if (SeekChanging) {
		l = seekMap.getValue(x - Seek.getLeft(), y - Seek.getTop());
		if (l > 0) {
			if (System.getPlayItemLength() >= 0) {
				l = (l / 255) * System.getPlayItemLength();
				System.seekTo(l);
			}
		}
		SongPlayingTimer.start();
		SeekChanging = 0;
	}
}

Seek.onMouseMove(int x, int y) {
	if (SeekChanging) {
		updateSeeker(x, y);
	}
}

updateSeeker(int x, int y) {
	int v = seekMap.getValue(x - Seek.getLeft(), y - Seek.getTop());
	if (v > 0) {
		setSeekerPosition(v);
		if (System.getPlayItemLength() >= 0) {
			float len = System.getPlayItemLength();
			int p = (v * 100) / 255;
			int s = (v * len) / 255;
			Songtickertimer.stop();
			Songtickertimer.start();
			Songticker.setText("Seek " + System.integerToTime(s));
		}
	}
}

setSeekerPosition(int p) {
	int f = (p * SeekFrames) / 255;
    if (p > 0) {
		Seek.gotoFrame(f + 1);
	}
	if (p == 0) {
		Seek.gotoFrame(0);
	}
}

HiddenSeek.onPostedPosition(int p) {
	if (p != LastSeek && !SeekChanging) {
		setSeekerPosition(p);
		LastSeek = p;
	}
}

// Autorepeat volume .... thanks Will :)  
VolumeUp.onLeftClick() { 
  if (AutoRepeat_ClickType != 0) {
    CurrentVolume = CurrentVolume + 2.55; 
    System.SetVolume(system.integer(CurrentVolume));
	SongTickerTimer.stop();
	SongTickerTimer.start();
	SongTicker.SetText("Volume: " + System.IntegertoString(CurrentVolume/2.55) + "'\.");
  }
}

VolumeDown.onLeftClick() { 
  if (AutoRepeat_ClickType != 0) { 
    CurrentVolume = CurrentVolume - 2.55;
    System.SetVolume(system.integer(CurrentVolume)); 
 	SongTickerTimer.stop();
	SongTickerTimer.start();
    SongTicker.SetText("Volume: " + System.IntegertoString(CurrentVolume/2.55) + "'\.");
  }
}  

hidvol.onPostedPosition(int p) {
  Float f;
  f = p;
  f = f / 255 * 100;
  setTempText("Volume: " + System.integerToString(f) + "'\.");
}

System.onVolumeChanged(int newvol) {
  if (System.Integer(CurrentVolume) != newvol) {
    CurrentVolume = Newvol;
  }
}

//  Regular toggles temp text
HintToggleButton.onLeftButtonDown(int x, int y) {
	if (HintToggleButton == ToggleXFade) setTempText("Crossfade");
	else if (HintToggleButton == ToggleRepeat) setTempText("Repeat");
	else if (HintToggleButton == ToggleShuffle) setTempText("Shuffle");
}

HintToggleButton.onToggle(int onoff) {
	String cmd, txt;
	txt="none";
	if (HintToggleButton == ToggleXFade) {
		txt = "Cfd";
	} else if (HintToggleButton == ToggleRepeat) {
		txt = "Rep";
	} else if (HintToggleButton == ToggleShuffle) {
		txt = "Shf";
	}
	if (txt != "none") {
		String s;
		if (onoff) s = "on"; else s = "off";
		setTempText(txt + " now " + s);
	}
}

ToggleConfigAttribute.onDataChanged() {
  ToggleButton t = NULL;
  if (ToggleConfigAttribute == attr_crossfade) t = ToggleXFade;
  else if (ToggleConfigAttribute == attr_shuffle) t = ToggleShuffle;
  else if (ToggleConfigAttribute == attr_repeat) t = ToggleRepeat;
  if (t != NULL)
    t.setActivated(StringToInteger(getData()));
}

HintObject.onLeftButtonDown(int x, int y) {
  if (HintObject == Play) setTempText("Play");
  else if (HintObject == Stop) setTempText("Stop");
  else if (HintObject == Pause) setTempText("Pause");
  else if (HintObject == Next) setTempText("Next");
  else if (HintObject == Previous) setTempText("Previous");
  else if (HintObject == Open) setTempText("Open");
  else if (HintObject == close) setTempText("Exit Winamp");
  else if (HintObject == layoutswitch) setTempText("Shade Mode");
  else if (HintObject == minimize) setTempText("Minimize");
  else if (HintObject == FullpresetDummy) setTempText("Full Bass Treb.");
  else if (HintObject == PoppresetDummy) setTempText("Pop Music");
  else if (HintObject == RockpresetDummy) setTempText("Rock Music");
  else if (HintObject == SoftpresetDummy) setTempText("Soft Music");
  else if (HintObject == ToggleXbassDummy) setTempText("X-Bass");
}

//  Slim-mode Temp text
ShadeTextTimer.onTimer() {
  ShadeSongTicker.setText("");
  stop();
}

ShadeVolume.onSetPosition(int p) {
  Float f;
  f = p;
  f = f / 255 * 100;
  ShadesetTempText("Volume: " + System.integerToString(f) + "%");
}

ShadeVolume.onPostedPosition(int p) {
  Float f;
  f = p;
  f = f / 255 * 100;
  ShadesetTempText("Volume: " + System.integerToString(f) + "%");
}

Seeker.onSetPosition(int p) {
  if (seeking) {
    Float f;
    f = p;
    f = f / 255 * 100;
    Float len = getPlayItemLength();
    if (len != 0) {
      int np = len * f / 100;
      ShadesetTempText("Seek: " + integerToTime(np) + " / " + integerToTime(len));
    }
  }
}

Seeker.onPostedPosition(int p) {
  if (seeking) {
    Float f;
    f = p;
    f = f / 255 * 100;
    Float len = getPlayItemLength();
    if (len != 0) {
      int np = len * f / 100;
      ShadesetTempText("Seek: " + integerToTime(np) + " / " + integerToTime(len));
    }
  }
}

Seeker.onLeftButtonDown(int x, int y) {
  seeking = 1;
}

Seeker.onLeftButtonUp(int x, int y) {
  seeking = 0;
}

ShadesetTempText(String txt) {
  ShadeTextTimer.stop();
  ShadeSongTicker.setText(txt);
  ShadeTextTimer.start();
}

emptyTempText() {
  ShadeSongTicker.setText("");
  ShadeTextTimer.stop();
}

//  Eq/Pl norm/shade temp text
Eq.onActivate(int eqStatus) {
  if (eqStatus == 1) {
    setTempText("Open Eq");
  } else {
    setTempText("Close Eq");
  }
}  

Pl.onActivate(int plStatus) {
  if (plStatus == 1) {
    setTempText("Open Pl");
  } else {
    setTempText("Close Pl");
  }
} 

ShadeEq.onActivate(int eqStatus) {
  if (eqStatus == 1) {
    ShadesetTempText("Open Equalizer");
  } else {
    ShadesetTempText("Close Equalizer");
  }
}  

ShadePl.onActivate(int plStatus) {
  if (plStatus == 1) {
    ShadesetTempText("Open Playlist");
  } else {
    ShadesetTempText("Close Playlist");
  }
}  

// Toggle media info editor
Songticker.onLeftButtonDblClk(Int intX, Int intY) {
	SongInfoEditor.leftClick();
}
ShadeSongticker.onLeftButtonDblClk(Int intX, Int intY) {
	SongInfoEditor.leftClick();
}

// X-bass button
ToggleXbass.onLeftButtonUp(int x, int y) {
  int xbassStatus = getPrivateInt("Devay", "Xbass", 0);
  if (xbassStatus == 0) {
    eqPreamp.setPosition((eqPreamp.getPosition() + 10));
    eq1.setPosition((eq1.getPosition() + 41));
    eq2.setPosition((eq2.getPosition() + 35));
    eq3.setPosition((eq3.getPosition() + 26));
    setPrivateInt("Devay", "Xbass", 1);
       } else {
    eqPreamp.setPosition((eqPreamp.getPosition() - 10));
    eq1.setPosition((eq1.getPosition() - 41));
    eq2.setPosition((eq2.getPosition() - 35));
    eq3.setPosition((eq3.getPosition() - 26));
    setPrivateInt("Devay", "Xbass", 0);
  }
}

// Full Bass & Trebble button
Fullpreset.onActivate(int FullStatus) {
    setPrivateInt("Devay", "Full", FullStatus);
  if (FullStatus == 1) {
    Poppreset.setActivated(0);
    setPrivateInt("Devay", "Pop", 0);
    Rockpreset.setActivated(0);
    setPrivateInt("Devay", "Rock", 0);
    Softpreset.setActivated(0);
    setPrivateInt("Devay", "Soft", 0);
    ToggleXbass.setActivated(0);
    setPrivateInt("Devay", "Xbass", 0);
    eqPreamp.setPosition(127);
    eq1.setPosition(170);
    eq2.setPosition(162);
    eq3.setPosition(127);
    eq4.setPosition(76);
    eq5.setPosition(92);
    eq6.setPosition(134);
    eq7.setPosition(178);
    eq8.setPosition(194);
    eq9.setPosition(202);
    eq10.setPosition(202);
    setPrivateInt("Devay", "Full", 1);
       } else {
    setPrivateInt("Devay", "Full", 0);
  }
}

// Pop button
Poppreset.onActivate(int PopStatus) {
    setPrivateInt("Devay", "Pop", PopStatus);
  if (PopStatus == 1) {
    Fullpreset.setActivated(0);
    setPrivateInt("Devay", "Full", 0);
    Rockpreset.setActivated(0);
    setPrivateInt("Devay", "Rock", 0);
    Softpreset.setActivated(0);
    setPrivateInt("Devay", "Soft", 0);
    ToggleXbass.setActivated(0);
    setPrivateInt("Devay", "Xbass", 0);
    eqPreamp.setPosition(127);
    eq1.setPosition(112);
    eq2.setPosition(154);
    eq3.setPosition(170);
    eq4.setPosition(174);
    eq5.setPosition(158);
    eq6.setPosition(116);
    eq7.setPosition(108);
    eq8.setPosition(108);
    eq9.setPosition(112);
    eq10.setPosition(112);
    setPrivateInt("Devay", "Pop", 1);
       } else {
    setPrivateInt("Devay", "Pop", 0);
  }
}

// Rock button
Rockpreset.onActivate(int RockStatus) {
    setPrivateInt("Devay", "Rock", RockStatus);
  if (RockStatus == 1) {
    Fullpreset.setActivated(0);
    setPrivateInt("Devay", "Full", 0);
    Poppreset.setActivated(0);
    setPrivateInt("Devay", "Pop", 0);
    Softpreset.setActivated(0);
    setPrivateInt("Devay", "Soft", 0);
    ToggleXbass.setActivated(0);
    setPrivateInt("Devay", "Xbass", 0);
    eqPreamp.setPosition(127);
    eq1.setPosition(174);
    eq2.setPosition(154);
    eq3.setPosition(88);
    eq4.setPosition(72);
    eq5.setPosition(100);
    eq6.setPosition(150);
    eq7.setPosition(182);
    eq8.setPosition(194);
    eq9.setPosition(194);
    eq10.setPosition(194);
    setPrivateInt("Devay", "Rock", 1);
       } else {
    setPrivateInt("Devay", "Rock", 0);
  }
}

// Soft button
Softpreset.onActivate(int SoftStatus) {
    setPrivateInt("Devay", "Soft", SoftStatus);
  if (SoftStatus == 1) {
    Fullpreset.setActivated(0);
    setPrivateInt("Devay", "Full", 0);
    Poppreset.setActivated(0);
    setPrivateInt("Devay", "Pop", 0);
    Rockpreset.setActivated(0);
    setPrivateInt("Devay", "Rock", 0);
    ToggleXbass.setActivated(0);
    setPrivateInt("Devay", "Xbass", 0);
    eqPreamp.setPosition(127);
    eq1.setPosition(154);
    eq2.setPosition(134);
    eq3.setPosition(116);
    eq4.setPosition(108);
    eq5.setPosition(116);
    eq6.setPosition(150);
    eq7.setPosition(178);
    eq8.setPosition(186);
    eq9.setPosition(194);
    eq10.setPosition(202);
    setPrivateInt("Devay", "Soft", 1);
       } else {
    setPrivateInt("Devay", "Soft", 0);
  }
}

// Songticker Menu
SongtickerMenu() {
	tickerMenu = new PopupMenu;
	tickerMenu.addCommand("Songticker Scrolling (on)", 1, 0, 0);
	tickerMenu.addCommand("Songticker Scrolling (off)", 2, 0, 0);
}

Songticker.onRightButtonUp(Int intX, Int intY) {
	MenuResult(tickerMenu.popAtMouse());
	complete;
}

ShadeSongticker.onRightButtonUp(Int intX, Int intY) {
	MenuResult(tickerMenu.popAtMouse());
	complete;
}

MenuResult(int ticking) {
	if(ticking > 0) {
	 if(ticking == 1) {
		Songticker.setXMLParam("ticker", "1");
		ShadeSongTicker.setXMLParam("ticker", "1");
		PlshadeSongticker.setXMLParam("ticker", "1");
		tickerMenu.checkCommand(1, 1);
		tickerMenu.checkCommand(2, 0);
		setPrivateInt("Devay", "scrolling", 1);
	}

	if(ticking == 2) {
		Songticker.setXMLParam("ticker", "0");
		ShadeSongTicker.setXMLParam("ticker", "0");
		PlshadeSongticker.setXMLParam("ticker", "0");
		tickerMenu.checkCommand(1, 0);
		tickerMenu.checkCommand(2, 1);
		setPrivateInt("Devay", "scrolling", 0);
	}
  }
}
