Photoshop CS - Save every layer as a jpeg

Hey need help to make a script that does this… know nothing about applescript but have done some PS scripts in the past… also want one layer to be left on as a master background… and a defined number of layers to be toggled visible and saved… if you get me.

Thanks for any help in advance.
Guy

Guy, this should get blocks out of the blocks for a starter. Hope it helps.

set inputFolder to choose folder
tell application "Finder"
	set filesList to files in inputFolder
	set MyJPEGs to make new folder at desktop with properties {name:"JPEG Images"}
end tell
repeat with aFile in filesList
	set fileIndex to 0
	tell application "Finder"
		set theFile to aFile as alias
		set theFileName to name of theFile
	end tell
	tell application "Adobe Photoshop CS"
		activate
		set display dialogs to never
		set ruler units of settings to pixel units
		open theFile
		set docRef to the current document
		set docName to name of docRef
		set docBaseName to getBaseName(docName) of me
		tell the current document
			if (mode of docRef is not RGB) then
				change mode docRef to RGB
			end if
			if (bits per channel of docRef is sixteen) then
				set bits per channel of docRef to eight
			end if
			delete (every channel whose kind is not component channel)
			delete every path item
			repeat with n from 1 to (count of layers) - 1
				set properties of every layer to {visible:false}
				set properties of background layer to {visible:true}
				set properties of layer n to {visible:true}
				set newFileName to (MyJPEGs as string) & docBaseName & "_L" & n
				save docRef in file newFileName as JPEG with options ¬
					{quality:9} appending lowercase extension with copying
			end repeat
		end tell
		close current document without saving
	end tell
end repeat

-- Returns the document name without extension (if present)
on getBaseName(fName)
	set baseName to fName
	repeat with idx from 1 to (length of fName)
		if (item idx of fName = ".") then
			set baseName to (items 1 thru (idx - 1) of fName) as string
			exit repeat
		end if
	end repeat
	return baseName
end getBaseName