#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_icon=Images\Icon.ico
#AutoIt3Wrapper_outfile=Yt-Converter.exe
#AutoIt3Wrapper_Compression=4
#AutoIt3Wrapper_Res_Description=YouTube Converter
#AutoIt3Wrapper_Res_Fileversion=1.0
#AutoIt3Wrapper_Res_LegalCopyright=DP-Ware.de
#AutoIt3Wrapper_Res_SaveSource=y
#AutoIt3Wrapper_Res_Field=Homepage|http://www.dp-ware.de/
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <ProgressConstants.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <INet.au3>

;Test-URL: http://www.youtube.com/watch?v=FZSUPCoIUsY

If (Not FileExists(@ScriptDir & "\Videos\")) Then DirCreate(@ScriptDir & "\Videos\")

Global $hDll32 = DllOpen("user32.dll")
Global $bSlidedIn = False

FileInstall("Images\YT.jpg", @TempDir & "\YT.jpg", 1)
FileInstall("Images\Icon.ico", @TempDir & "\Icon.ico", 1)

Opt("TrayMenuMode", 1)
TraySetIcon(@TempDir & "\Icon.ico")

TrayCreateItem("YT-Converter")
TrayItemSetState(-1, $GUI_DISABLE)
TrayCreateItem("Copyright " & @YEAR & " - " & (@YEAR + 3) & " DP-Ware.de")
TrayItemSetState(-1, $GUI_DISABLE)
TrayCreateItem("")
$hExitTray = TrayCreateItem("Beenden")

TraySetState()

$hWin1 = GUICreate("YT-Converter", 164, 185, -1, -1, BitOR($WS_POPUP, $WS_CLIPSIBLINGS, $WS_EX_TOOLWINDOW, $WS_DLGFRAME, $DS_MODALFRAME, $WS_EX_LAYERED), 0x80)
; BitOR($WS_POPUP, $WS_CLIPSIBLINGS, $WS_EX_TOOLWINDOW, $WS_DLGFRAME, $DS_MODALFRAME, $WS_EX_LAYERED))
GUISetBkColor(0xFFFFFF)
GUICtrlCreatePic(@TempDir & "\YT.jpg", 8, 8, 148, 65, BitOR($SS_NOTIFY, $WS_GROUP, $WS_CLIPSIBLINGS))
GUICtrlCreateLabel("YouTube Video URL:", 8, 80, 148, 17, $SS_CENTER)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
$hInp1 = GUICtrlCreateInput("", 8, 104, 148, 21, BitOR($ES_CENTER, $ES_AUTOHSCROLL)) ;Video URL
$hPrg1 = GUICtrlCreateProgress(8, 130, 148, 17) ;Fortschritt's-Anzeige
$hBtn1 = GUICtrlCreateButton("Download!", 40, 152, 80, 25, $WS_GROUP) ;Download Button

_SlideWin($hWin1, "out", "right") ;Rechts an Bildschirm positionieren

GUISetState(@SW_SHOW)

While 1 * Sleep(10)

	Switch TrayGetMsg()
		Case $hExitTray
			For $iStep = 255 To 000 Step -3
				WinSetTrans($hWin1, '', $iStep)
				Sleep(10)
			Next
			FileDelete(@TempDir & "\YT.jpg")
			DllClose($hDll32)
			Exit
	EndSwitch

	$nMsg = GUIGetMsg(1)
	Select
		Case $nMsg[0] = $hBtn1
			$sYtURL = GUICtrlRead($hInp1)
			If $sYtURL = "" Then ContinueLoop

			GUICtrlSetState($hInp1, $GUI_DISABLE)
			GUICtrlSetState($hBtn1, $GUI_DISABLE)

			For $iStep = 255 To 100 Step -3
				WinSetTrans($hWin1, '', $iStep)
				Sleep(10)
			Next

			$aPos = WinGetPos($hWin1)

			WinMove($hWin1, '', @DesktopWidth - 164, @DesktopHeight - 185, 164, 185, 10) ; Breite = 164, Höhe = 185
			WinSetOnTop($hWin1, '', 1)

			_Download($sYtURL)

			For $iStep = 100 To 255 Step 3
				WinSetTrans($hWin1, '', $iStep)
				Sleep(10)
			Next

			GUICtrlSetState($hInp1, $GUI_ENABLE)
			GUICtrlSetState($hBtn1, $GUI_ENABLE)

			WinSetOnTop($hWin1, '', 0)
			GUICtrlSetData($hInp1, '')

			WinMove($hWin1, '', $aPos[0], $aPos[1], 164, 185, 10) ; Breite = 164, Höhe = 185

		Case _WinIsHovered($hWin1, $hDll32)
			If Not $bSlidedIn Then
				_SlideWin($hWin1, "in", "right")
				$bSlidedIn = True
			EndIf

		Case Else
			Select
				Case $bSlidedIn
					_SlideWin($hWin1, "out", "right")
					$bSlidedIn = False
			EndSelect
	EndSelect
WEnd

Func _SlideWin($hWnd, $sMode, $sSide)
	;$hWnd = Window to slide.
	;$sMode = Slide-In or -out (in|out)
	;$sSide = Side where you want the GUI to slide. (left|right|top|bottom)
	Local $aWPos = WinGetPos($hWnd), $m = 0 - ($sMode = "in") + ($sMode = "out")
	Local $h = 0 - ($sSide = "left") + ($sSide = "right"), $v = 0 - ($sSide = "top") + ($sSide = "bottom")

	WinMove($hWnd, "", _ ; Move GUI according to $sMode and $sSide.
			0 - (($h = -1) * ($m = -1) * ($aWPos[2] - 10)) _
			 + (($h = 1) * (($h = 1) * @DesktopWidth - (10 * ($m = -1)) - ($m <> -1) * $aWPos[2])) _
			 + (($h = 0) * (($h = 0) * (@DesktopWidth / 2) - ($aWPos[2] / 2))) _
			, _
			0 - (($v = -1) * ($m = -1) * ($aWPos[3] - 10)) _
			 + (($v = 1) * (($v = 1) * @DesktopHeight - (10 * ($m = -1)) - ($m <> -1) * $aWPos[3])) _
			 + (($v = 0) * (($v = 0) * (@DesktopHeight / 2) - ($aWPos[3] / 2))) _
			)

	$aWPos = WinGetPos($hWnd)
	; Slide GUI.
	Switch $sSide
		Case "left", "right"
			Local $STEP = (((($h = 1) And ($m = 1)) Or (($h = -1) And ($m = -1))) - ((($h = -1) And ($m = 1)) Or (($h = 1) And ($m = -1)))) * 10
			Local $FROM = $aWPos[0], $TO = $aWPos[0] _
					 + (((($h = 1) And ($m = 1)) Or (($h = -1) And ($m = -1))) * ($aWPos[2])) _
					 - (((($h = 1) And ($m = -1)) Or (($h = -1) And ($m = 1))) * ($aWPos[2])) - $STEP
			For $i = $aWPos[0] To $TO Step $STEP
				WinMove($hWnd, "", $i, $aWPos[1])
				Sleep(10)
			Next
		Case "top", "bottom"
			Local $STEP = (((($v = 1) And ($m = 1)) Or (($v = -1) And ($m = -1))) - ((($v = -1) And ($m = 1)) Or (($v = 1) And ($m = -1)))) * 10
			Local $FROM = $aWPos[1], $TO = $aWPos[1] _
					 + (((($v = 1) And ($m = 1)) Or (($v = -1) And ($m = -1))) * ($aWPos[3])) _
					 - (((($v = 1) And ($m = -1)) Or (($v = -1) And ($m = 1))) * ($aWPos[3])) - $STEP
			For $i = $aWPos[1] To $TO Step $STEP
				WinMove($hWnd, "", $aWPos[0], $i)
				Sleep(10)
			Next
	EndSwitch
EndFunc   ;==>_SlideWin

Func _WinIsHovered($hWnd, $vDLL = "User32.dll")
	Local $aResult, $aWPos = WinGetPos($hWnd), $aMPos = MouseGetPos()
	Local $tRect = DllStructCreate("int Left;int Top;int Right;int Bottom")
	Local $iLeft = $aWPos[0], $iTop = $aWPos[1], $iWidth = $aWPos[2], $iHeight = $aWPos[3]
	Local $iX = $aMPos[0], $iY = $aMPos[1]
	DllStructSetData($tRect, "Left", $iLeft)
	DllStructSetData($tRect, "Top", $iTop)
	DllStructSetData($tRect, "Right", $iLeft + $iWidth)
	DllStructSetData($tRect, "Bottom", $iTop + $iHeight)

	$aResult = DllCall($vDLL, "int", "PtInRect", "ptr", DllStructGetPtr($tRect), "int", $iX, "int", $iY)
	If @error Then Return SetError(@error, 0, False)

	Return $aResult[0] <> 0
EndFunc   ;==>_WinIsHovered

Func _Download($sURL, $bHD = False)
	Local $sSource, $sTitle, $sID, $sDownloadLink, $sSWFArgs, $sSWFID, $iSize, $iCurrent, $iProzent
	$sSource = _INetGetSource($sURL)
	$sSource = StringStripCR($sSource)
	$sSource = StringReplace($sSource, @LF, '')

	$sTitle = StringRegExp($sSource, "'VIDEO_TITLE': '(.*?)'", 1, 1) ;Titel Auslesen
	If IsArray($sTitle) Then $sTitle = $sTitle[0] ;Anpassen
	GUICtrlSetData($hPrg1, 25)
	Sleep(500)

	$sID = StringRegExp($sSource, "'VIDEO_ID': '(.*?)'", 1, 1) ;ID Auslesen
	If IsArray($sID) Then $sID = $sID[0] ;Anpassen
	GUICtrlSetData($hPrg1, 50)
	Sleep(500)

	;SWF ARG - Bereich auslesen
	$sSWFArgs = StringRegExp($sSource, 'SWF_ARGS(.*?)}', 1, 1)
	If IsArray($sSWFArgs) Then $sSource = $sSWFArgs[0]
	GUICtrlSetData($hPrg1, 75)
	Sleep(500)

	;Code ID aus dem SWF ARG Bereich auslesen
	$sSWFID = StringRegExp($sSource, '"t": "(.*?)",', 1, 1)
	If IsArray($sSWFID) Then $sSWFID = $sSWFID[0]
	GUICtrlSetData($hPrg1, 100)
	Sleep(500)
	GUICtrlSetData($hPrg1, 0)

	$sDownloadLink = 'http://www.youtube.com/get_video?fmt=18&video_id=' & $sID & '&t=' & $sSWFID
	$iDlSize = InetGetSize($sDownloadLink)
	$hDL = InetGet($sDownloadLink, @ScriptDir & "\Videos\" & StringRegExpReplace($sTitle, "((?<!.)([a-zA-Z]\:\\)(?!\s)(([\w-+.]*" & _
			"((?<!\s)(\s{1})(?!\\)|(?<!\\)(\\{1})(?!\s))*)*)(\\?)(?!.))", "") & ".mp4", 1, 1)
	Do
		$iSize2 = InetGetInfo($hDL, 0)
		$PROZENT = (100 * $iSize2) / $iDlSize
		GUICtrlSetData($hPrg1, $PROZENT)
		Sleep(200)
	Until $iSize2 = $iDlSize
	Return 1
EndFunc   ;==>_Download