/* 
-------------

A collection of scripts used throughout the MCC club website
Copied and adapted from the good people on the interweb,
assumedly sourced from helloWorld.js at one point or another ;>

-------------

*/


var xmlhttp;

function loadXMLDoc(url,cfunc)
{
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=cfunc;
xmlhttp.open("GET",url,true);
xmlhttp.send();
}


/* ------
This loads the top level navigation into the page
at the div 'topNav'
-------- */

function loadTopNav()
{
loadXMLDoc('mcc_topNav.html',function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("topNav").innerHTML=xmlhttp.responseText;
    }
  });
}



/* ------
This checks to see if there is a breaking news file (mcc_breakingNews.html) 
and if so, prints the contents in the 'breakingNews' div. Used primarily
on the front page (index.html)
-------- */


function getBreakingNews()
{ 
loadXMLDoc('mcc_home_breakingNews.html',function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200) {
    document.getElementById("breakingNews").innerHTML=xmlhttp.responseText;
    }
  });
}



/* ------
This will display our calendar of events on the home page
Resources used:
http://www.hunlock.com/blogs/Mastering_JSON_%28_JavaScript_Object_Notation_%29

UPDATE NOTES
 a) be sure to include 4 digit year when specifying the event date in the data set below
 b) be sure to escape quotes within the values (such as if you want to include a link)
   i.e. <a href="www.yahoo.com" target="_blank"> should be <a href=\"www.yahoo.com\" target=\"_blank\">
 c) be sure that the events are in chronological order (particularly with regard to year)
 
   
-------- */

var mccEventsJSON = { "events" : [   
  { "day" : "Thursday", "date" : "9/8/2011",  "title1" : "Welcome Back and information session", "title2" : "", "time" : "7.30pm"},
  { "day" : "Thursday", "date" : "9/22/2011",  "title1" : "Enjoying Street Photography", "title2" : "by <a href=\"http://www.agphotoworks.com\" target=\"_blank\">Arik Gorban</a>", "time" : "7.30pm"},
  { "day" : "Saturday", "date" : "9/24/2011",  "title1" : "MCC field trip: <a href=\"http://www.visitnj.org/princeton\" target=\"_blank\">Princeton</a>", "title2" : "", "time" : "10.00am, parking lot #10"},
  { "day" : "Thursday", "date" : "10/13/2011",  "title1" : "Merit Judging (Open)", "title2" : "Judge: Lila Pollinger", "time" : "7.30pm"},
  { "day" : "Saturday",  "date" : "10/15/2011",  "title1" : "Field Trip: <a href=\"http://www.capemaycountyzoo.org/capemaycountyzoo.org/Home_Page.html\" target=\"_blank\">Cape May Zoo</a>", "title2" : "", "time" : ""},
  { "day" : "Thursday", "date" : "10/27/2011",  "title1" : "On-location Portable Studio Lighting with Flashes", "title2" : "by <a href=\"http://www.anthonybphotos.com\" target=\"_blank\">Anthony Bianciella</a>", "time" : "7.30pm"},
  { "day" : "Thursday",  "date" : "11/11/2011",  "title1" : "Merit Judging (PRINT only)", "title2" : "Judge: David Mills", "time" : "7.30pm"},
  { "day" : "Saturday", "date" : "11/19/2011",  "title1" : "MCC field trip: <a href=\"http://www.theredmill.org/\" target=\"_blank\">Red Mill, Clinton, NJ</a>", "title2" : "", "time" : ""},
  { "day" : "Thursday",  "date" : "12/1/2011",  "title1" : "Photography in \"Black and White\"", "title2" : "by <a href=\"http://palmieri.photoshelter.com\" taqrget=\"_blank\">Nick Palmieri</a>", "time" : "7.30pm"},
  { "day" : "Saturday",  "date" : "12/17/2011",  "title1" : "MCC field trip: <a href=\"http://www.kipscastle.org \" target=\"_blank\">Kip's Castle</a>", "title2" : "", "time" : ""},
  { "day" : "Thursday",  "date" : "12/22/2011",  "title1" : "Merit Judging (DIGITAL only)", "title2" : "Judge: Howard Knauer", "time" : "7.30pm"},
  { "day" : "Thursday",  "date" : "01/12/2012",  "title1" : "Nature", "title2" : "by Bill Kirns", "time" : "7.30pm"},
  { "day" : "Saturday",  "date" : "01/21/2012",  "title1" : "MCC Field Trip: <a href=\"http://www.ellisisland.org/genealogy/ellis_island_visiting.asp\" target=\"_blank\">Ellis Island</a>", "title2" : "<b>postponed to Feb. 18</b>", "time" : ""},
  { "day" : "Thursday",  "date" : "01/26/2012",  "title1" : "Merit Judging - Open", "title2" : "Judge: <a href=\"http://www.natureimages321.com\" target=\"_blank\">Brien Szabo</a>", "time" : "7.30pm"},
  { "day" : "Thursday",  "date" : "02/09/2012",  "title1" : "Self-Assignments", "title2" : "<a href=\"http://www.natureimages321.com\" target=\"_blank\">Brien Szabo</a>", "time" : "7.30pm"},
  { "day" : "Saturday",  "date" : "02/18/2012",  "title1" : "MCC Field Trip: <a href=\"http://www.ellisisland.org/genealogy/ellis_island_visiting.asp\" target=\"_blank\">Ellis Island</a>", "title2" : "", "time" : ""},
  { "day" : "Thursday",  "date" : "02/23/2012",  "title1" : "Merit Judging (Low light photography)", "title2" : "Judge: <a href=\"http://www.agphotoworks.com\" target=\"_blank\">Arik Gorban</a>", "time" : "7.30pm"},
  { "day" : "Thursday",  "date" : "03/08/2012",  "title1" : "<a href=\"/mcc_event_expoNight.asp\">Expo Night</a>", "title2" : "An assortment of vendors and information of special interest to members and other photographers.", "time" : "7.30pm"},
  { "day" : "Thursday",  "date" : "03/22/2012",  "title1" : "Merit Judging (landscapes/seascapes)", "title2" : "Judge: Phil Echo", "time" : ""},
  { "day" : "Saturday",  "date" : "03/24/2012",  "title1" : "MCC Field Trip: <a href=\"http://www.batstovillage.org/village.htm\" target=\"_blank\">Batsto Village</a>", "title2" : "", "time" : ""},
  { "day" : "Thursday",  "date" : "04/12/2012",  "title1" : "Essay Night", "title2" : "", "time" : "7.30pm"},
  { "day" : "Saturday",  "date" : "04/21/2012",  "title1" : "MCC Field Trip: Cherry Blossoms", "title2" : "<a href=\"http://www.branchbrookpark.org/\" target=\"_blank\">Branch Brook Park</a>", "time" : ""},
  { "day" : "Thursday",  "date" : "04/26/2012",  "title1" : "Merit Judging - Open", "title2" : "Judge: Tom Icklan", "time" : "7.30pm"},
  { "day" : "Thursday",  "date" : "05/03/2012",  "title1" : "Exposure & Understanding Your Histogram", "title2" : "by Phil Echo", "time" : "7.30pm"},
  { "day" : "Friday",  "date" : "05/11/2012",  "title1" : "Annual Club Dinner", "title2" : "<a href=\"http://www.saltcreekgrille.com \" target=\"_blank\">Salt Creek Grill</a>", "time" : ""},
  { "day" : "Saturday",  "date" : "05/19/2012",  "title1" : "MCC Field Trip: <a href=\"  http://www.bbg.org/\" target=\"_blank\"Brooklyn Botanical Gardens</a>", "title2" : "", "time" : ""},
  { "day" : "Thursday",  "date" : "05/24/2012",  "title1" : "Merit Judging - Open", "title2" : "Judge: <a href=\"http://www.desrochersphotography.com\" target=\"_blank\">David DesRochers</a>", "time" : "7.30pm"},
  { "day" : "Thursday",  "date" : "06/14/2012",  "title1" : "Creating Award Winning Images", "title2" : "by Bill Rudock", "time" : "7.30pm"},
  { "day" : "Saturday",  "date" : "06/16/2012",  "title1" : "MCC Field Trip: Walk on the Wild Side", "title2" : "<a href=\"http://www.dukefarms.org/Tours/Walk-on-the-Wild-Side/\" target=\"_blank\">Duke Gardens</a>", "time" : ""},
  { "day" : "Thursday",  "date" : "06/28/2012",  "title1" : "Annual Club Contest", "title2" : "", "time" : "7.30pm"},
  { "day" : "Thursday",  "date" : "07/12/2012",  "title1" : "General Planning Meeting", "title2" : "", "time" : "7.30pm"}
]
} // End mccEventsJSON


function printCalendar(count)
{

var loopCount;
var printImage;
if(count) { loopCount = count; } else {loopCount=50; printImage=true;}

var today = new Date();
var todayDay = today.getDate();
var todayMonth = today.getMonth();
var todayYear = today.getFullYear();

var dateElements;

var eventsWritten = 0;
var eventsElement = 0;

var eventMonth;
var eventDay;
var eventYear;
if(printImage) { 
  document.writeln("<img src=\"mcc_site_images/bridgePerspective_1_m.jpg\" style=\"float:right; padding-left:10px; padding-right:200px;\" width=\"200\" height=\"300\" alt=\"Bridge Perspective #1, Andy Beldowicz\" title=\"Bridge Perspective #1, Andy Beldowicz\">");
  document.writeln("<table border=\"0\" cellpadding=\"1\" width=\"50%\" class=\"content\">"); 
}
else {document.writeln("<table border=\"0\" cellpadding=\"1\" width=\"100%\" class=\"content\">");}


while (eventsWritten < loopCount) {
  if(mccEventsJSON.events[eventsElement] == null) {break;}	// this breaks the loop if we run out of elements in our JSON object and our eventsWritten 
															//  never reaches out requested number to print (loopCount)
  dateElements = mccEventsJSON.events[eventsElement].date.split("/");


  eventMonth = dateElements[0];
  eventDay = dateElements[1];
  eventYear = dateElements[2];
  
  eventDayOfWeek = mccEventsJSON.events[eventsElement].day;
  eventDisplayDate = eventDayOfWeek + " " + eventMonth + "/" + eventDay;
  eventTitle1 = mccEventsJSON.events[eventsElement].title1;
  eventTitle2 = mccEventsJSON.events[eventsElement].title2;
  eventTime = mccEventsJSON.events[eventsElement].time;
  
  if(eventTitle1 != "") { eventTitle1 = eventTitle1 + "<br>"; }
  if(eventTitle2 != "") { eventTitle2 = eventTitle2 + "<br>"; }
  if(eventTime != "") { eventTime = eventTime + "<br>"; }

  function printRow() {
    document.writeln("<tr><td width=\"3%\" valign=\"top\">" + eventDisplayDate + "</td><td width=\"1%\">&nbsp;</td><td width=\"96%\" valign=\"top\">" + eventTitle1 + eventTitle2 + eventTime + "</td></tr>");
	document.writeln("<tr><td colspan=\"3\" height=\"5px\"></td></tr>");
  }	

// use this to print all (even past) events, based on count set to 50 above
if(loopCount==50) { printRow(); eventsWritten++; eventsElement++; continue; }

if(eventYear > todayYear) { 

	printRow();
	eventsWritten++; eventsElement++; continue; 
    }
    
    if(eventYear == todayYear) { 
      if(eventMonth > todayMonth+1) { 
	printRow();
 	eventsWritten++; eventsElement++; continue;  
      }
      if(eventMonth == todayMonth+1) { 
	if(eventDay >= todayDay) { 
	  printRow();
	  eventsWritten++; eventsElement++; continue;  
	}
      }
    }
eventsElement++;

} // close while

document.writeln("</table>");


} // close printCalendar



