'MON_IDENTIFIER determines what's used to identify each monitor, and can be one of the following:
'name: monitor name
'serial: serial number of the monitor (Windows 7 and later only)
'targetid: target id of the monitor (Windows 7 and later only)
'
'MONS has an entry for each monitor, with each entry consisting of the following (separated by comma):
'identifier of the monitor (see MON_IDENTIFIER above for more information)
'horizontal resolution
'vertical resolution
'orientation: l for landscape, p for portrait, lf for landscape flipped, pf for portrait flipped
'color depth in bit
'refresh rate, prefixed by i for interlaced modes
'
'the script switches between monitors in the order of the MONS array

MON_IDENTIFIER = "name"
MONS = Array("DELL 1707FPV,1280,1024,l,32,60", "L15C,768,1024,p,32,75", "Panasonic-TV,1920,1080,l,32,i30") 'by name
'MONS = Array("44048-16418,1280,1024,l,32,60", "28250-1875,768,1024,p,32,75", "43316-49462,1920,1080,l,32,i30") 'by serial number
'MONS = Array("1282,1280,1024,l,32,60", "1283,768,1024,p,32,75", "1281,1920,1080,l,32,i30") 'by target id

Set sys = CreateObject("UltraMon.System")
Set monP = sys.Monitors("Primary")
Select Case MON_IDENTIFIER
	Case "name"
		monPIdent = monP.Name
	Case "serial"
		monPIdent = monP.SerialNumber
	Case "targetid"
		monPIdent = monP.TargetId
End Select

'get the current monitor
indexCurMon = -1
For i = 0 To UBound(MONS)
	monData = Split(MONS(i), ",")
	If monData(0) = monPIdent Then
		indexCurMon = i
		Exit For
	End If
Next

If indexCurMon = -1 Then
	MsgBox "Invalid script configuration, no monitor with matching name found.",, WScript.ScriptName
	WScript.Quit
End If

'get the next available monitor
i = indexCurMon + 1
Do While i <> indexCurMon
	If i > UBound(MONS) Then i = 0
	monData = Split(MONS(i), ",")

	'check if that monitor is currently connected
	connected = False
	For Each mon In sys.Monitors
		If MON_IDENTIFIER = "name" And mon.Name = monData(0) Then
			connected = True
		ElseIf MON_IDENTIFIER = "serial" And mon.SerialNumber = monData(0) Then
			connected = True
		ElseIf MON_IDENTIFIER = "targetid" And mon.TargetId = monData(0) Then
			connected = True
		End If

		If connected = True Then Exit For
	Next

	If connected = True Then
		SwitchPrimary monData, sys
		Exit Do
	End If

	i = i + 1
Loop

Sub SwitchPrimary(monData, sys)
	For Each mon In sys.Monitors
		If MON_IDENTIFIER = "name" And mon.Name = monData(0) Or _
		  MON_IDENTIFIER = "serial" And mon.SerialNumber = monData(0) Or _
		  MON_IDENTIFIER = "targetid" And mon.TargetId = monData(0) Then
			mon.Enabled = True
			mon.Primary = True

			mon.Width = CLng(monData(1))
			mon.Height = CLng(monData(2))

			Select Case monData(3)
				Case "l"
					mon.Orientation = 0 'landscape
				Case "p"
					mon.Orientation = 1 'portrait
				Case "lf"
					mon.Orientation = 2 'landscape flipped
				Case "pf"
					mon.Orientation = 3 'portrait flipped
			End Select

			mon.Colordepth = CInt(monData(4))

			If Left(monData(5), 1) = "i" Then
				mon.Interlaced = True
				mon.RefreshRate = CInt(Mid(monData(5), 2))
			Else
				mon.Interlaced = False
				mon.RefreshRate = CInt(monData(5))
			End If
		Else
			mon.Enabled = False
		End If
	Next
	sys.ApplyMonitorChanges
End Sub
