//
//  JS for WordADay class
//
//  Clio Soleil (clio@PrettyGetter.TV)
//  PrettyGetter Productions (http://PrettyGetter.TV) 
//  

WAD_url  = "/_pgtv/_php/class/WordADay/WordADay.php";     // where installed
WAD_calendar = null;                                      // the calendar used for date picking

//--------------------------------------------------------------------------------------------------------------------------------------------
// GUI function which shows/hides an element and sets the state in the session
function WAD_toggleDisplay(db, ssid, container)
//--------------------------------------------------------------------------------------------------------------------------------------------
{
  // show/hide the containing div (client side) and save state in session (server-side)
  toggleDisplayByID(container, "", "none");
  requestAJAX(WAD_url + "?ssid="+ssid+
                        "&db="+db+
                        "&ajax=toggleDisplay"+
                        "&name="+container);
}

//--------------------------------------------------------------------------------------------------------------------------------------------
// GUI function which shows/hides an element and sets the state in the session
function WAD_toggleNewWhen(db, ssid, selector, container)
//--------------------------------------------------------------------------------------------------------------------------------------------
{
  // get the named div's DOM object and toggle the classname
  s = document.getElementById(selector);
  c = document.getElementById(container);
  
  c.style.display = (s.selectedIndex == 0) ? "none" : "";
      
  requestAJAX(WAD_url + "?ssid="+ssid+
                        "&db="+db+
                        "&ajax=setSession"+
                        "&name="+selector+
                        "&value="+s.selectedIndex);
}

//--------------------------------------------------------------------------------------------------------------------------------------------
// GUI function which shows/hides an element and sets the state in the session
function WAD_addWord(db, ssid, uid)
//--------------------------------------------------------------------------------------------------------------------------------------------
{
  // show/hide the containing div (client side) and save state in session (server-side)
  requestAJAX(WAD_url + "?ssid="+ssid+
                        "&db="+db+
                        "&uid="+uid+
                        "&ajax=addWord"+
                        "&word="+document.getElementById("WAD_NewWord").value+
                        "&date="+document.getElementById("WAD_NewDate").value+
                        "&sort="+document.getElementById("WAD_SortWordsBy").value+
                        "&order="+document.getElementById("WAD_SortWordsDir").value,
              "getDataCB",
              "WAD_Index");
}

//--------------------------------------------------------------------------------------------------------------------------------------------
// GUI function which shows/hides an element and sets the state in the session
function WAD_addTextEntry(db, ssid, wid, uid)
//--------------------------------------------------------------------------------------------------------------------------------------------
{
  //var $json = JSON.stringify(Array(db, ssid, wid, uid));
  
  // show/hide the containing div (client side) and save state in session (server-side)
  requestAJAX(WAD_url + "?ssid="+ssid+
                        "&db="+db+
                        "&ajax=addEntry"+
                        "&wid="+wid+
                        "&type=text"+
                        "&uid="+uid+
                        "&title="+document.getElementById("WAD_NewTextTitle").value+
                        "&content="+(document.getElementById("WAD_NewTextText").value).replace(/\n/gi, "<br>"));
  WAD_TextEntryForm.hide();
  alert("Thank you for your contribution; your experience has been added to the dictionary!");
/*
              "WAD_addTextEntry_CB",
              $json);
*/
}

//--------------------------------------------------------------------------------------------------------------------------------------------
// 
function WAD_addPhotoEntry(db, ssid, wid, uid, path)
//--------------------------------------------------------------------------------------------------------------------------------------------
{
  // show/hide the containing div (client side) and save state in session (server-side)
  requestAJAX(WAD_url + "?ssid="+ssid+
                        "&db="+db+
                        "&ajax=addEntry"+
                        "&wid="+wid+
                        "&type=photo"+
                        "&uid="+uid+
                        "&path="+path+
                        "&title="+document.getElementById("WAD_NewPhotoTitle").value+
                        "&content="+document.getElementById("FTP_status_WAD_photo").innerHTML);
  WAD_PhotoEntryForm.hide();
  alert("Thank you for your contribution; your experience has been added to the dictionary!");
}

//--------------------------------------------------------------------------------------------------------------------------------------------
// 
function WAD_addAudioEntry(db, ssid, wid, uid, path)
//--------------------------------------------------------------------------------------------------------------------------------------------
{
  // show/hide the containing div (client side) and save state in session (server-side)
  requestAJAX(WAD_url + "?ssid="+ssid+
                        "&db="+db+
                        "&ajax=addEntry"+
                        "&wid="+wid+
                        "&type=audio"+
                        "&uid="+uid+
                        "&path="+path+
                        "&title="+document.getElementById("WAD_NewAudioTitle").value+
                        "&content="+document.getElementById("FTP_status_WAD_audio").innerHTML);
  WAD_AudioEntryForm.hide();
  alert("Thank you for your contribution; your experience has been added to the dictionary!");
}

//--------------------------------------------------------------------------------------------------------------------------------------------
// 
function WAD_addVideoEntry(db, ssid, wid, uid, path)
//--------------------------------------------------------------------------------------------------------------------------------------------
{
  // show/hide the containing div (client side) and save state in session (server-side)
  requestAJAX(WAD_url + "?ssid="+ssid+
                        "&db="+db+
                        "&ajax=addEntry"+
                        "&wid="+wid+
                        "&type=video"+
                        "&uid="+uid+
                        "&path="+path+
                        "&title="+document.getElementById("WAD_NewVideoTitle").value+
                        "&content="+document.getElementById("FTP_status_WAD_video").innerHTML);
  WAD_VideoEntryForm.hide();
  alert("Thank you for your contribution; your experience has been added to the dictionary!");
}

//--------------------------------------------------------------------------------------------------------------------------------------------
// 
function WAD_getWAD(db, ssid, uid)
//--------------------------------------------------------------------------------------------------------------------------------------------
{
  WAD_getAllWords(db, ssid, uid); 
  WAD_getEntries(db, ssid, uid, "");
}

//--------------------------------------------------------------------------------------------------------------------------------------------
// 
function WAD_getRandomWord(db, ssid, uid)
//--------------------------------------------------------------------------------------------------------------------------------------------
{
  WAD_getAllWords(db, ssid, uid); 

  // show/hide the containing div (client side) and save state in session (server-side)
  requestAJAX(WAD_url + "?ssid="+ssid+
                        "&db="+db+
                        "&uid="+uid+
                        "&ajax=getRandomWord",
              "getDataCB",
              "WAD_Content");
}

//--------------------------------------------------------------------------------------------------------------------------------------------
// 
function WAD_getAllWords(db, ssid, uid)
//--------------------------------------------------------------------------------------------------------------------------------------------
{
  document.getElementById("WAD_Keywords").value = ""; 
  WAD_getWords(db, ssid, uid); 
}

//--------------------------------------------------------------------------------------------------------------------------------------------
// GUI function which shows/hides an element and sets the state in the session
function WAD_getWords(db, ssid, uid)
//--------------------------------------------------------------------------------------------------------------------------------------------
{
  WAD_getWordsByLetter(db, ssid, uid, "");
}

//--------------------------------------------------------------------------------------------------------------------------------------------
// GUI function which shows/hides an element and sets the state in the session
function WAD_getWordsByLetter(db, ssid, uid, letter)
//--------------------------------------------------------------------------------------------------------------------------------------------
{
  var search = document.getElementById("WAD_Keywords").value;
  if (search == "lookup word") search = "";
  
  // show/hide the containing div (client side) and save state in session (server-side)
  requestAJAX(WAD_url + "?ssid="+ssid+
                        "&db="+db+
                        "&uid="+uid+
                        "&ajax=getWords"+
                        "&letter="+letter+
                        "&search="+search+
                        "&sort="+document.getElementById("WAD_SortWordsBy").value+
                        "&order="+document.getElementById("WAD_SortWordsDir").value,
              "getDataCB",
              "WAD_Index");
}

//--------------------------------------------------------------------------------------------------------------------------------------------
// GUI function which shows/hides an element and sets the state in the session
function WAD_getEntries(db, ssid, uid, wid)
//--------------------------------------------------------------------------------------------------------------------------------------------
{
  // show/hide the containing div (client side) and save state in session (server-side)
  requestAJAX(WAD_url + "?ssid="+ssid+
                        "&db="+db+
                        "&uid="+uid+
                        "&ajax=getEntries"+
                        "&wid="+wid,
              "getDataCB",
              "WAD_Content");
}

//--------------------------------------------------------------------------------------------------------------------------------------------
// 
function WAD_sortWords(db, ssid, uid)
//--------------------------------------------------------------------------------------------------------------------------------------------
{
  requestAJAX(WAD_url + "?ssid="+ssid+
                        "&db="+db+
                        "&ajax=setSession"+
                        "&name=WAD_SortWordsDir"+
                        "&value="+document.getElementById("WAD_SortWordsDir").selectedIndex);
                        
  requestAJAX(WAD_url + "?ssid="+ssid+
                        "&db="+db+
                        "&ajax=setSession"+
                        "&name=WAD_SortWordsBy"+
                        "&value="+document.getElementById("WAD_SortWordsBy").selectedIndex);
                        
  WAD_getWordsByLetter(db, ssid, uid, "");
}

//--------------------------------------------------------------------------------------------------------------------------------------------
// 
function WAD_expandTextEntry(eid)
//--------------------------------------------------------------------------------------------------------------------------------------------
{
  s = document.getElementById("WAD_Entry_"+eid+"_Summary").style;
  if (s.maxHeight == "none")
  {
    //s.height = "auto";
    s.maxHeight = "64px";
    s.overflow = "hidden";
  }
  else
  {
    //s.height = "auto";
    s.maxHeight = "none";
    s.overflow = "visible";
  }
}

//--------------------------------------------------------------------------------------------------------------------------------------------
// 
function WAD_expandPhotoEntry(eid, url)
//--------------------------------------------------------------------------------------------------------------------------------------------
{
  s = document.getElementById("WAD_Entry_"+eid+"_Summary").style;
  p = document.getElementById("WAD_Entry_"+eid+"_Photo").style;
  if (s.maxHeight == "none")
  {
    //s.height = "auto";
    s.maxHeight = "64px";
    s.overflow = "hidden";
    p.display = "none";
  }
  else
  {
    //s.height = "auto";
    s.maxHeight = "none";
    s.overflow = "visible";
    p.display = "";
  }
}

//--------------------------------------------------------------------------------------------------------------------------------------------
// 
function WAD_expandVideoEntry(url)
//--------------------------------------------------------------------------------------------------------------------------------------------
{
  FLV_playWin("FLVWindow", url, true);
}

//--------------------------------------------------------------------------------------------------------------------------------------------
// 
function WAD_expandAudioEntry(url)
//--------------------------------------------------------------------------------------------------------------------------------------------
{
  MP3_playWin("MP3Window", url, true);
}

/////////////////
// FTP ROUTINES
/////////////////

//--------------------------------------------------------------------------------------------------------------------------------------------
// 
function FTP_status_WAD_photo_completed(filename) 
//--------------------------------------------------------------------------------------------------------------------------------------------
{ 
  //alert(filename+" was uploaded and is ready for processing."); 
  document.getElementById("WAD_NewPhotoSubmit").style.display = "";
}

//--------------------------------------------------------------------------------------------------------------------------------------------
// 
function FTP_status_WAD_audio_completed(filename) 
//--------------------------------------------------------------------------------------------------------------------------------------------
{ 
  document.getElementById("WAD_NewAudioSubmit").style.display = "";
}

//--------------------------------------------------------------------------------------------------------------------------------------------
// 
function FTP_status_WAD_video_completed(filename) 
//--------------------------------------------------------------------------------------------------------------------------------------------
{ 
  document.getElementById("WAD_NewVideoSubmit").style.display = "";
}

////////////////////////
// CALENDAR ROUTINES
////////////////////////

//--------------------------------------------------------------------------------------------------------------------------------------------
// show calendar
function WAD_showCalendar(ssid, id, result, reload, editable, gameLocation) 
//--------------------------------------------------------------------------------------------------------------------------------------------
{
  //alert("WAD_showCalendar("+ssid+", "+id+", "+result+", "+reload+", "+editable+", "+gameLocation+")");

  if (WAD_calendar) return;
 
  var anchor = document.getElementById(id);

  // create and configure the calendar
	WAD_calendar = new PGTVCalendar();
	WAD_calendar.start_week_day = 7;
	WAD_calendar.show_time = false;
	WAD_calendar.user_onchange_handler = WAD_calendarOnChange;
	WAD_calendar.user_onclose_handler = WAD_calendarOnClose;
	WAD_calendar.user_onautoclose_handler = WAD_calendarOnClose;
  
  WAD_calendar.WAD_format = '%Y-%m-%j';   // our date format
  WAD_calendar.result = result;
  WAD_calendar.reload = reload;
  WAD_calendar.editable = editable;
  WAD_calendar.gameLocation = gameLocation;
  WAD_calendar.ssid = ssid;
 
  // set the date from the input box and show the calendar
	//calendar.parse_date(anchor.value, WAD_format);
	WAD_calendar.show_at_element(anchor, 'adj_left-bottom');
}
 
//--------------------------------------------------------------------------------------------------------------------------------------------
// user defined onChange handler
function WAD_calendarOnChange(cal, what) 
//--------------------------------------------------------------------------------------------------------------------------------------------
{
	if (what == 'day') 
  {
		document.getElementById(cal.result).value = cal.get_formatted_date(cal.WAD_format);
		cal.hide();
		WAD_calendar = null;
    
//    if (cal.reload) 
//      WAD_getTournaments(cal.ssid, cal.get_formatted_date(cal.WAD_format)+" 00:00:00", cal.editable, cal.gameLocation);
	}
}
 
//--------------------------------------------------------------------------------------------------------------------------------------------
// user defined onClose handler (used in pop-up mode - when auto_close is true)
function WAD_calendarOnClose(cal) 
//--------------------------------------------------------------------------------------------------------------------------------------------
{
	WAD_calendar.hide();
	WAD_calendar = null;
}
