'*******************************************************************************'
'* Variables that need Changing                                                *'
'*******************************************************************************'

Const ImageDirectory = "C:\Documents and Settings\AdamCoulthard\My Documents\My Pictures"                                 'Location of the Pictures
Const RandomWeight   = 500                                   'Weight to decide on a random Picture
                                                             'usually total number of files roughly
                                                                                          
'********************************************************************************'
'* DO NOT EDIT BELOW THIS UNLESS YOU KNOW VBSCRIPT                              *'
'********************************************************************************'
Const OutputFile = "SlideShow.setup"
RandomImageCount = 0
ImageToUse       = ""
ImageName        = ""
SearchType       = "NORMAL"
LocatedLastFile  = "NOTFOUND"

Function GetImage()
	'Get Last Picture Seen
	lastPicture = GetLastPictureSeen()
	
	'Obtain Directory
	if lastPicture = "#" then
		lastPictureDirectory = ImageDirectory
		lastPictureName      = ""
		LocatedLastFile      = "FOUND"
	Else
		lastPictureDirectory = Mid(lastPicture, 1, instr(lastPicture, "#") - 1)
		lastPictureName      = Mid(lastPicture, instr(lastPicture, "#") + 1)
	End If

	'Recuse over the directory structure to find the last picture
	findPicture lastPictureDirectory, lastPictureName, 0
	
	'Write out to the setup File
	SetLastPictureSeen()
	GetImage = ImageToUse
End Function

Function GetRandomImage()
	'Calculate a Random Number
	randomNumber = getRandomNum()	
	findPicture ImageDirectory, "", randomNumber
	
	GetRandomImage = ImageToUse
End Function

Function ChangeWallpaper(varType)
	SearchType = "WALLPAPER"
	
	If varType = "RANDOM" then
		imageValue = GetImage()
	Else 
		If varType = "ROTATE" then
			imageValue = GetRandomImage()
		Else
			return "Invalid type must use either RANDOM or ROTATE"
		End If
	End If

	HKEY_CURRENT_USER = &H80000001
	strComputer = "."
	Set objReg = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")
	strKeyPath = "Control Panel\Desktop"
	objReg.CreateKey HKEY_CURRENT_USER, strKeyPath
	ValueName = "Wallpaper"
	objReg.SetStringValue HKEY_CURRENT_USER, strKeyPath, ValueName, imageValue
	
	'Code used from WallpaperChanger
	'Bill Reid, 1/04
	Set WshShell = CreateObject("WScript.Shell")
	wshShell.Run "RUNDLL32.EXE user32.dll,UpdatePerUserSystemParameters",,True
	
	ChangeWallpaper = imageValue
	
End Function
'********************************************************************************'
'* PRIVATE METHODS USED BY THE SCRIPT                                           *'
'********************************************************************************'
Private Function GetSamurizeDirectory()
	Dim Shell
	Set Shell = CreateObject("Wscript.Shell")
	GetSamurizeDirectory = shell.RegRead("HKEY_CURRENT_USER\Software\Serious Samurize\General\DirPath")
	
	Set Shell = nothing
End Function

Private Function GetLastPictureSeen()
	'Create File System Object
	Set fs = CreateObject("Scripting.FileSystemObject")
	
	samurize_directory = GetSamurizeDirectory()
	
	if(fs.FileExists(samurize_directory & OutputFile) = False) then
		'Must be at the start so return blank
		GetLastPictureSeen = "#"
	Else
		'Read in the .ini file from the Skins Directory
		Set SetupFile = fs.OpenTextFile(samurize_directory & OutputFile, 1, True)
		
		COUNT = 1
		
		Do while SetupFile.AtEndOfStream <> True
			SetupInfo = SetupFile.readline()
			
			'First Line should contain
			'lastPictureSeen=<Picture Name> (directory#picturename)
			GetLastPictureSeen = SetupInfo
			exit do
		loop
	End If
End Function

Private Function SetLastPictureSeen()
	'Create File System Object
	Set fs = CreateObject("Scripting.FileSystemObject")
	
	samurize_directory = GetSamurizeDirectory()
	
	Set outFile = fs.CreateTextFile(samurize_directory & OutputFile, True)
	
	if(ImageToUse = "" OR ImageName = "") then
		'We must not of found an image so reset the search values
		FileDirectory = ""
		ImageName     = ""
	Else
		'Separate the Image Name and Path
		FileDirectory = Mid(ImageToUse, 1, instr(ImageToUse, ImageName) - 1)
	End If

	outFile.write FileDirectory & "#" & ImageName
End Function

Private Function findPicture(varDirectory, varName, varWeight)
	'if varWeight = 0 then not random
	Set oShell = CreateObject("Shell.Application")
	Set oSHFolder = oShell.Namespace(varDirectory)

	Set oSHFItems = oSHFolder.Items

	On Error Resume Next
	For Each oSHFItem in oSHFItems
		if RandomImageCount = -1 OR LocatedLastFile = "EXIT" then
			'Found the Image that we want so exit
			exit for
		End If
		'Check to see if the folder is a directory
		if oSHFItem.IsFolder then
			'Its a folder so search in here as well
			findPicture oSHFItem.Path, varName, varWeight
		Else
			'Need to add a check to ensure that its an image we return
			if (instr(1, oSHFItem.Name, "jpg", VBTEXTCOMPARE) > 0 OR instr(1, oSHFItem.Name, "bmp", VBTEXTCOMPARE) > 0 OR instr(1, oSHFItem.Name, "gif", VBTEXTCOMPARE) > 0 OR instr(1, oSHFItem.Name, "ico", VBTEXTCOMPARE) > 0) then
				if SearchType = "WALLPAPER" AND (instr(1, oSHFItem.Name, "jpg", VBTEXTCOMPARE) > 0 OR instr(1, oSHFItem.Name, "gif", VBTEXTCOMPARE) > 0 OR instr(1, oSHFItem.Name, "ico", VBTEXTCOMPARE) > 0) then
					'Not a valid wallpaper type so dont do anything
				Else
					ImageToUse = oSHFItem.Path
					ImageName  = oSHFItem.Name
					if varWeight = 0 then
						'The File was located on the previous iterate so drop out
						if LocatedLastFile = "FOUND" then
							ImageToUse = oSHFItem.Path
							ImageName  = oSHFItem.Name
							LocatedLastFile = "EXIT"
							exit for
						End If
						'Must locate the last File that we used
						if((instr(1, oSHFItem.Path, varDirectory, VBTEXTCOMPARE) > 0 AND oSHFItem.Name = varName)) OR varName = "" then
							'Located the last File signal to next run tho
							LocatedLastFile = "FOUND"
						End If
					else
						'This section is used for the random part
						if RandomImageCount = varWeight then
							ImageToUse = oSHFItem.Path
							RandomImageCount = -1
							exit for
						End If

						RandomImageCount = RandomImageCount + 1
					End If
				End If
				
				if varWeight <> 0 AND SearchType <> "WALLPAPER" then
					'Just in case the weight over runs
					ImageToUse = oSHFItem.Path
					ImageName  = oSHFItem.Name
				End If
			End If
		End If
	Next
	On Error GoTo 0

End Function

Private Function getRandomNum
	Randomize Timer
	getRandomNum = Int(Rnd * RandomWeight)
End Function