'Script Copyright  2012 Ryan Marshall (Skarn)
'All Rights Reserved

Dim strRootName
Dim intSecFrames, intMinFrames, intHourFrames
Dim tickSeconds, tickMinutes, tickHours
Dim objHourHand, objMinuteHand, objSecondHand
Dim hasHourHand, hasMinuteHand, hasSecondHand
Dim showSeconds, showMinutes, showHours
Dim strSecondsImagesPath,strMinutesImagesPath,strHoursImagesPath
Dim strPublicDocumentsPath

Dim status
Dim xmlClock, xmlFile

Sub Object_OnScriptEnter()
	On Error Resume Next
	Call dxInit()
End Sub

Sub dxInit()	
	dragging = False
	strRootName = Object.Name
	Set objShell = CreateObject("Shell.Application")
	strPublicDocumentsPath = objShell.Namespace(&h2e).Self.Path
	
	Set dxs = DesktopX.ScriptObject("dxScriptor")
	Set dxEdit = DesktopX.ScriptObject("dxEdit_script")
	Set xmlFile = dxEdit.dxEditXML
	Set xmlClock = xmlFile.selectSingleNode("//controls/control[@name = '" & strRootName & "']")
	If xmlClock Is Nothing Then
		MsgBox("Clock: " & strRootName & " has not been able to load settings and is disabled.")
		status = False
		Exit Sub
	End If
	
	If DesktopX.IsObject(strRootName & "_hourHand") Then
		Set objHourHand = DesktopX.Object(strRootName & "_hourHand")
		Call dxEdit.dxPipeEvents(strRootName & "_hourHand",strRootName)
		strHoursImagesPath = strPublicDocumentsPath & "\Stardock\desktopx\graphics\" & strRootName & "_hourHand" & "\"
		hasHourHand = True
	End If
	
	If DesktopX.IsObject(strRootName & "_minuteHand") Then
		Set objMinuteHand = DesktopX.Object(strRootName & "_minuteHand")
		Call dxEdit.dxPipeEvents(strRootName & "_minuteHand",strRootName)
		strMinutesImagesPath = strPublicDocumentsPath & "\Stardock\desktopx\graphics\" & strRootName & "_minuteHand" & "\"
		hasMinuteHand = True
	End If
	
	If DesktopX.IsObject(strRootName & "_secondHand") Then
		Set objSecondHand = DesktopX.Object(strRootName & "_secondHand")
		Call dxEdit.dxPipeEvents(strRootName & "_secondHand",strRootName)
		strSecondsImagesPath = strPublicDocumentsPath & "\Stardock\desktopx\graphics\" & strRootName & "_secondHand" & "\"
		hasSecondHand = True
	End If
	
	intSecFrames = xmlClock.selectSingleNode("secFrames").text
	intMinFrames = xmlClock.selectSingleNode("minFrames").text
	intHourFrames = xmlClock.selectSingleNode("hourFrames").text
	
	tickSeconds = CBool(xmlClock.selectSingleNode("tickSeconds").text)
	tickMinutes = CBool(xmlClock.selectSingleNode("tickMinutes").text)
	tickHours = CBool(xmlClock.selectSingleNode("tickHours").text)
	
	showSeconds = CBool(xmlClock.selectSingleNode("showSeconds").text)
	showMinutes = CBool(xmlClock.selectSingleNode("showMinutes").text)
	showHours = CBool(xmlClock.selectSingleNode("showHours").text)
	
	If hasSecondHand And showSeconds Then
		objSecondHand.Visible = True
	ElseIf hasSecondHand = True And showSeconds = False Then
		objSecondHand.Visible = False
	End If
	
	If hasMinuteHand And showMinutes Then
		objMinuteHand.Visible = True
	ElseIf hasMinuteHand = True And showMinutes = False Then
		objMinuteHand.Visible = False
	End If
	
	If hasHourHand And showHours Then
		objHourHand.Visible = True
	ElseIf hasHourHand = True And showHours = False Then
		objHourHand.Visible = False
	End If
	
	Object.SetTimer 1,10			'ticking timer
	Object.SetTimer 2,5000			'garbage timer
End Sub

Function Object_OnRButtonUpEx(obj,x,y,dragged)
	Select Case obj.Name
		Case strRootName, strRootName & "_hourHand", strRootName & "_minuteHand", strRootName & "_secondHand"
			Object_OnRButtonUpEx = True
			Dim menu
			Set menu = DesktopX.CreatePopupMenu
			If tickSeconds = True Then
				menuOpt = &H00000008
			Else
				menuOpt = 0
			End If
			menu.AppendMenu menuOpt, 1, "tick seconds"
	
			If tickMinutes = True Then
				menuOpt = &H00000008
			Else
				menuOpt = 0
			End If
			menu.AppendMenu menuOpt, 2, "tick minutes"
			
			If tickHours = True Then
				menuOpt = &H00000008
			Else
				menuOpt = 0
			End If
			menu.AppendMenu menuOpt, 3, "tick hours"
			
			If showSeconds = True Then
				menuOpt = &H00000008
			Else
				menuOpt = 0
			End If
			menu.AppendMenu menuOpt, 4, "show seconds"
			
			If showMinutes = True Then
				menuOpt = &H00000008
			Else
				menuOpt = 0
			End If
			menu.AppendMenu menuOpt, 5, "show minutes"
			
			If showHours = True Then
				menuOpt = &H00000008
			Else
				menuOpt = 0
			End If
			menu.AppendMenu menuOpt, 6, "show hours"
			
			result = menu.TrackPopupMenu(0, System.CursorX, System.CursorY)
			Select Case result
				Case 1
					tickSeconds = Not tickSeconds
					If tickSeconds = True then
						xmlClock.selectSingleNode("tickSeconds").text = "1"
					Else
						xmlClock.selectSingleNode("tickSeconds").text = "0"
					End If
					xmlFile.Save(Object.Directory & "dxEdit.xml")
					
				Case 2
					tickMinutes = Not tickMinutes
					If tickMinutes = True then
						xmlClock.selectSingleNode("tickMinutes").text = "1"
					Else
						xmlClock.selectSingleNode("tickMinutes").text = "0"
					End If		
					xmlFile.Save(Object.Directory & "dxEdit.xml")
					
				Case 3
					tickHours = Not tickHours
					If tickHours = True then
						xmlClock.selectSingleNode("tickHours").text = "1"
					Else
						xmlClock.selectSingleNode("tickHours").text = "0"
					End If
					xmlFile.Save(Object.Directory & "dxEdit.xml")
				
				Case 6
					showHours = Not showHours
					If showHours = True then
						xmlClock.selectSingleNode("showHours").text = "1"
					Else
						xmlClock.selectSingleNode("showHours").text = "0"
					End If
					If hasHourHand Then
						objHourHand.Visible = showHours
					End If
					xmlFile.Save(Object.Directory & "dxEdit.xml")
					
				Case 5
					showMinutes = Not showMinutes
					If showMinutes = True then
						xmlClock.selectSingleNode("showMinutes").text = "1"
					Else
						xmlClock.selectSingleNode("showMinutes").text = "0"
					End If
					If hasMinuteHand Then
						objMinuteHand.Visible = showMinutes
					End If
					xmlFile.Save(Object.Directory & "dxEdit.xml")
					
				Case 4
					showSeconds = Not showSeconds
					If showSeconds = True then
						xmlClock.selectSingleNode("showSeconds").text = "1"
					Else
						xmlClock.selectSingleNode("showSeconds").text = "0"
					End If
					If hasSecondHand Then
						objSecondHand.Visible = showSeconds
					End If
					xmlFile.Save(Object.Directory & "dxEdit.xml")
					
			End Select
			
	End Select
	
End Function

Sub Object_OnTimer1
	On Error Resume Next
	nowTimer = Timer
	intHours = Fix(nowTimer/3600)
	intMinutes = Fix((nowTimer-intHours*3600)/60)
	intSeconds = Round(nowTimer-intHours*3600-intMinutes*60,2)
	
	If intHours > 12 Then intHours = intHours - 12
	
	If Not tickHours Then
		intHours = Round(intHours + intMinutes/60,2)
	End If
	
	If Not tickMinutes Then
		intMinutes = Round(intMinutes + intSeconds/60,2)
	End If
	
	If tickSeconds Then
		intSeconds = Fix(intSeconds)
	End If
	
	If hasSecondHand And showSeconds Then
		secondFrame = CInt(intSeconds * intSecFrames/60)
		If secondFrame >= intSecFrames-1 Then
			secondFrame = 0
		End If
		objSecondHand.Picture = strSecondsImagesPath & objSecondHand.Name & "_" & Right("0000" & secondFrame,4) & ".png"
	End If
	
	If hasMinuteHand And showMinutes Then
		minuteFrame = CInt(intMinutes * intMinFrames/60)
		If minuteFrame >= intMinFrames Then
			minuteFrame = 0
		End If
		objMinuteHand.Picture = strMinutesImagesPath & objMinuteHand.Name & "_" & Right("0000" & minuteFrame,4) & ".png"
	End If
	
	If hasHourHand And showHours Then
		hourFrame = CInt(intHours * intHourFrames/12)
		If hourFrame >= intHourFrames Then
			hourFrame = 0
		End If
		objHourHand.Picture = strHoursImagesPath & objHourHand.Name & "_" & Right("0000" & hourFrame,4) & ".png"
	End If
End Sub

Sub Object_OnTimer2
	DesktopX.GarbageCollection
End Sub



