// Timer.jsx
// Copyright (c) 2012 Motion Boutique

function run()
{
	var comp = app.project.activeItem;
	if (!(comp instanceof CompItem))
	{
		alert("Please select a text layer.");
		return;
	}
	var layer = comp.selectedLayers[0];
	if (!(layer instanceof TextLayer))
	{
		alert("Please select a text layer.");
		return;
	}
	
	app.beginUndoGroup("Timer.jsx");
	
	var hSld = layer.Effects.addProperty("ADBE Slider Control");
	hSld.name = "H";
	hSld.property(1).setValue(16);
	
	var mSld = layer.Effects.addProperty("ADBE Slider Control");
	mSld.name = "M";
	mSld.property(1).setValue(30);
	
	var sSld = layer.Effects.addProperty("ADBE Slider Control");
	sSld.name = "S";
	sSld.property(1).setValue(35);

	var speedSld = layer.Effects.addProperty("ADBE Slider Control");
	speedSld.name = "Speed";
	speedSld.property(1).setValue(500);
	
	layer.sourceText.expression = 
		"function Format(x) \r" +
		"{ \r" +
		"   return (x < 10) ? \"0\" + x : \"\" + x; \r" +
		"} \r" +
		" \r" +
		"H_init = effect(\"H\")(1); \r" +
		"M_init = effect(\"M\")(1); \r" +
		"S_init = effect(\"S\")(1); \r" +
		"speed = effect(\"Speed\")(1); \r" +
		"t = time*speed; \r" +
		" \r" +
		"S_temp = S_init + Math.floor(t); \r" +
		"M_temp = M_init + Math.floor(S_temp/60); \r" +
		"H_temp = H_init + Math.floor(M_temp/60); \r" +
		" \r" +
		"S = Format(S_temp%60); \r" +
		"M = Format(M_temp%60); \r" +
		"H = Format(H_temp%24); \r" +
		" \r" +
		"H + \":\" + M + \":\" + S";
		
	app.endUndoGroup();	
}

run();