/*
======================================================================
	iTunes Direct Podcast Opener by Mitchum Owen
   	http://www.milopublishing.com
======================================================================
*/


	
	
/*
======================================================================
	Directions
	
	* To directly open a specific podcast...

		1) Determine whether your podcasts use the same Track title or Track description
		      for all podcasts in a particular subscription
		2) Based on #1, set only one of the variables below: myDesc or myName
		4) Simply run the javascript file to directly open your podcast.
		
	* To schedule your podcast alarm...
	
		1) Create the direct opener described above
		2) Open Start > Programs > Accessories > Systems Tools > Scheduled Tasks
		3) Double-Click "Add Scheduled Task" and follow the prompts
		4) When promted, click Browse to locate your javascript file
		5) Set the appropriate schedule details
		6) You will probably also want to adjust your iTunes prefs to download the podcasts
		   automatically every hour, so you have the newest podcast available the schedule is activated
		7) Note - the alarm will not function unless you have your computer on, if your speakers
		   are on, and your volume is not muted and adjusted to an appropriate level
		

======================================================================
*/



	/* ******  Whichever variable is used MUST BE EXACTLY CORRECT !!! ****** */



	var myDesc = ""; // filter play by description
	
	var myAlbum = "Wall Street Journal This Morning"; // filter by album
		
	var myName = ""; // filter by track name






/*
======================================================================
	Do NOT change anything below this line !!
======================================================================
*/

var ITPlaylistKindUser = 2;
var iTunesApp = WScript.CreateObject("iTunes.Application");
var mainLibrary = iTunesApp.LibrarySource;
var playLists = mainLibrary.Playlists;

var numPlayLists = playLists.Count;
var currPlayList = playLists.Item(numPlayLists);
var currTrack;
while (numPlayLists != 0)
{
	var currPlayList = playLists.Item(numPlayLists);
	var tracks = currPlayList.Tracks;
	var numTracks = tracks.Count;
	
	// is this a user playlist?
	if (currPlayList.Kind == ITPlaylistKindUser)
	{
		if(currPlayList.Name == "Podcasts"){
					
			for (t = 1; t <= numTracks; t++)
			{
				currTrack = tracks.Item(t);
				if(currTrack.Description == myDesc || currTrack.Name == myName || currTrack.Album == myAlbum){
					currPlayList.PlayFirstTrack();
					var tmp = 1;
					while(tmp < t){
						iTunesApp.NextTrack();
						tmp++;
					}
					
					// testing to see if there really is that podcast available and playing
					var playTrack = iTunesApp.CurrentTrack;					
					if(playTrack != null)						
						WScript.Quit (1); // only quit if track acutally exists and is playing					
				}
			}			
		}
	}
	
	numPlayLists--;
}

// if podcast not found ... will play a random song from the music library
numPlayLists = playLists.Count;
currPlayList = playLists.Item(numPlayLists);
tracks = currPlayList.Tracks;
numTracks = tracks.Count;
while (numPlayLists != 0)
{
	var currPlayList = playLists.Item(numPlayLists);

	if(currPlayList.Name == "Library"){
		currPlayList.Shuffle = true;
		currPlayList.PlayFirstTrack();
		iTunesApp.NextTrack();
		WScript.Quit (1);
	}
	
	numPlayLists--;
}