(function() {

    var Dom = YAHOO.util.Dom,
        Event = YAHOO.util.Event,
        cal1 = null,
        dateHolder = [],
        myTooltip = null,


       //infrafrontier event date map
      myDates = {
	      '10/26/2010':'Conference: Mouse Development, Genetics and Genomics, NY, USA',
		  '10/27/2010':'Conference: Mouse Development, Genetics and Genomics, NY, USA',
		  '10/28/2010':'Conference: Mouse Development, Genetics and Genomics, NY, USA',
		  '10/29/2010':'Conference: Mouse Development, Genetics and Genomics, NY, USA',
		  '10/30/2010':'Conference: Mouse Development, Genetics and Genomics, NY, USA',
		  '9/10/2010':'9th Annual Workshop on the Pathology of Mouse Models for Human Disease',
		  '9/11/2010':'9th Annual Workshop on the Pathology of Mouse Models for Human Disease',
		  '9/12/2010':'9th Annual Workshop on the Pathology of Mouse Models for Human Disease',
		  '9/13/2010':'9th Annual Workshop on the Pathology of Mouse Models for Human Disease',
		  '9/14/2010':'9th Annual Workshop on the Pathology of Mouse Models for Human Disease',
		  '9/15/2010':'9th Annual Workshop on the Pathology of Mouse Models for Human Disease',
		  '9/16/2010':'9th Annual Workshop on the Pathology of Mouse Models for Human Disease',
		  '4/23/2010':'2010 Spring Meeting - The Genetics Society',
		  '4/14/2010':'4th ESF Conference on Functional Genomics and Disease, Dresden, Germany',
		  '4/15/2010':'4th ESF Conference on Functional Genomics and Disease, Dresden, Germany',
		  '4/16/2010':'4th ESF Conference on Functional Genomics and Disease, Dresden, Germany',
		  '4/17/2010':'4th ESF Conference on Functional Genomics and Disease, Dresden, Germany',
		  '3/23/2010':'ECRI 2010, Barcelona, Spain',
		  '3/24/2010':'ECRI 2010, Barcelona, Spain',
		  '3/18/2010':'WP5 Workshop, Barcelona, Spain',
		  '3/17/2010':'WP5 Workshop, Barcelona, Spain',
		  '2/22/2010':'Theoretical Course, Trieste, Italy',
		  '2/23/2010':'Theoretical Course, Trieste, Italy',
		  '2/24/2010':'Theoretical Course, Trieste, Italy',
		  '2/25/2010':'Theoretical Course, Trieste, Italy',
		  '1/25/2010':'EMMA Training Course, Orleans, France',
		  '1/26/2010':'EMMA Training Course, Orleans, France',
		  '1/27/2010':'EMMA Training Course, Orleans, France',
		  '11/20/2009':'WP5 Site Visit MRC Harwell and Sanger institute',
		  '11/21/2009':'WP5 Site Visit MRC Harwell and Sanger institute',
  		  '11/22/2009':'WP5 Site Visit MRC Harwell and Sanger institute',
  		  '11/23/2009':'WP5 Site Visit MRC Harwell and Sanger institute',
		  '10/22/2009':'2nd EMMA Service Meeting, Monterotondo, Italy',
		  '10/23/2009':'2nd EMMA Service Meeting, Monterotondo, Italy',
		  '10/8/2009':'MUGEN Conference, Athens',
		  '10/9/2009':'MUGEN Conference, Athens',
		  '10/10/2009':'MUGEN Conference, Athens',
	      '9/30/2009':'WP5 Site Visit JAX, Bar Harbor, Maine',
	      '10/1/2009':'WP5 Site Visit JAX, Bar Harbor, Maine',
	      '9/16/2009':'Infrafrontier Scientific Strategy Meeting, Munich',
		  '9/8/2009':'WP5 Site Visit ICS, Strasbourg',
		  '9/2/2009':'Mouse Genetics & Genomics, Hinxton',
		  '9/3/2009':'Mouse Genetics & Genomics, Hinxton',
		  '9/4/2009':'Mouse Genetics & Genomics, Hinxton',
		  '9/5/2009':'Mouse Genetics & Genomics, Hinxton',
		  '9/6/2009':'Mouse Genetics & Genomics, Hinxton'},

	   //map with dates as key aund http links as values
  myLinks = {
      '10/26/2010':"http://www.infrafrontier.eu/events.php#event201010_2630",
	  '10/27/2010':"http://www.infrafrontier.eu/events.php#event201010_2630",
	  '10/28/2010':"http://www.infrafrontier.eu/events.php#event201010_2630",
	  '10/29/2010':"http://www.infrafrontier.eu/events.php#event201010_2630",
	  '10/30/2010':"http://www.infrafrontier.eu/events.php#event201010_2630",
	  '9/10/2010':"http://www.infrafrontier.eu/events.php#event201009_1016",
	  '9/11/2010':"http://www.infrafrontier.eu/events.php#event201009_1016",
	  '9/12/2010':"http://www.infrafrontier.eu/events.php#event201009_1016",
	  '9/13/2010':"http://www.infrafrontier.eu/events.php#event201009_1016",
	  '9/14/2010':"http://www.infrafrontier.eu/events.php#event201009_1016",
	  '9/15/2010':"http://www.infrafrontier.eu/events.php#event201009_1016",
	  '9/16/2010':"http://www.infrafrontier.eu/events.php#event201009_1016",
	  '4/23/2010':"http://www.infrafrontier.eu/events.php#event201004_23",
	  '4/14/2010':"http://www.infrafrontier.eu/events.php#event201004_1417",
	  '4/15/2010':"http://www.infrafrontier.eu/events.php#event201004_1417",
	  '4/16/2010':"http://www.infrafrontier.eu/events.php#event201004_1417",
	  '4/17/2010':"http://www.infrafrontier.eu/events.php#event201004_1417",
	  '3/23/2010':"http://www.infrafrontier.eu/events.php#event201003_2324",
	  '3/24/2010':"http://www.infrafrontier.eu/events.php#event201003_2324",
	  '3/18/2010':"http://www.infrafrontier.eu/events.php#event201003_1718",
	  '3/17/2010':"http://www.infrafrontier.eu/events.php#event201003_1718",
      '2/22/2010':"http://www.infrafrontier.eu/events.php#event201002_2225", 
	  '2/23/2010':"http://www.infrafrontier.eu/events.php#event201002_2225", 
	  '2/24/2010':"http://www.infrafrontier.eu/events.php#event201002_2225",
	  '2/25/2010':"http://www.infrafrontier.eu/events.php#event201002_2225",
	  '1/25/2010':"http://www.infrafrontier.eu/events.php#event201001_2527", 
	  '1/26/2010':"http://www.infrafrontier.eu/events.php#event201001_2527",
	  '1/27/2010':"http://www.infrafrontier.eu/events.php#event201001_2527",
 	  '11/20/2009':"http://www.infrafrontier.eu/events.php#event200911_2023",
	  '11/21/2009':"http://www.infrafrontier.eu/events.php#event200911_2023",
	  '11/22/2009':"http://www.infrafrontier.eu/events.php#event200911_2023",
	  '11/23/2009':"http://www.infrafrontier.eu/events.php#event200911_2023",
	  '10/22/2009':"http://www.infrafrontier.eu/events.php#event200910_2223",
	  '10/23/2009':"http://www.infrafrontier.eu/events.php#event200910_2223",
	  '10/8/2009':"http://www.infrafrontier.eu/events.php#event200910_0810",
	  '10/9/2009':"http://www.infrafrontier.eu/events.php#event200910_0810",
	  '10/10/2009':"http://www.infrafrontier.eu/events.php#event200910_0810",
	  '10/1/2009':"http://www.infrafrontier.eu/events.php#event20090930_0110",
	  '9/30/2009':"http://www.infrafrontier.eu/events.php#event20090930_0110",
	  '9/16/2009':"http://www.infrafrontier.eu/events.php#event200908_0206",
  	  '9/8/2009':"http://www.infrafrontier.eu/events.php#event200908_0206",
  	  '9/2/2009':"http://www.infrafrontier.eu/events.php#event200909_0206",
 	  '9/3/2009':"http://www.infrafrontier.eu/events.php#event200909_0206", 
 	  '9/4/2009':"http://www.infrafrontier.eu/events.php#event200909_0206", 
 	  '9/5/2009':"http://www.infrafrontier.eu/events.php#event200909_0206", 
 	  '9/6/2009':"http://www.infrafrontier.eu/events.php#event200909_0206"};


                              
function init1(date) {
	        strDates = '';
        for (var tmpDate in myDates) {
	            strDates = strDates + ',' + tmpDate;
	        }
	        cal1 = new YAHOO.widget.Calendar('cal1', 'cal1Container', {
	            pagedate: date,
	            selected: strDates,
	            MULTI_SELECT: true
	        }
	        );

	        cal1.selectEvent.subscribe(mySelectHandler, cal1, true);
	        cal1.deselectEvent.subscribe(myDeselectHandler, cal1, true);
	        cal1.renderEvent.subscribe(addListeners);
	        cal1.cfg.setProperty("start_weekday","1",false);
	        cal1.render();


        Event.addListener('addTip', 'click', addDynamicTip);
}


function init() {
	        strDates = '';
        for (var tmpDate in myDates) {
	            strDates = strDates + ',' + tmpDate;
	        }
	        cal1 = new YAHOO.widget.Calendar('cal1', 'cal1Container', {
	            pagedate: new Date(),
	            selected: strDates,
	            MULTI_SELECT: true
	        }
	        );

	        cal1.selectEvent.subscribe(mySelectHandler, cal1, true);
	        cal1.deselectEvent.subscribe(myDeselectHandler, cal1, true);
	        cal1.renderEvent.subscribe(addListeners);
	        cal1.cfg.setProperty("start_weekday","1",false);
	        cal1.render();


        Event.addListener('addTip', 'click', addDynamicTip);
}











	    function mySelectHandler(type,args,obj)
		{

		  var datedata = args[0][0];
		  var year = datedata[0];
		  var month = datedata[1];
		  var day = datedata[2];

		  var selectedDate = month+'/'+day+'/'+year;

		  var myURL=myLinks[selectedDate];

		  if(myURL!=null) {
		  	window.location.href=myURL;
		   }
		   else {
		   	init1(new Date(selectedDate));
		   }
		   


		}


		var myDeselectHandler = function(type,args,obj) {

			var datedata = args[0];

		    var year = datedata[0];
		   	var month = datedata[1];
     		var day = datedata[2];

		    var selectedDate = month+'/'+day+'/'+year;

			var myURL=myLinks[selectedDate];

			if(myURL!=null) {
		   		window.location.href=myURL;
		   		init1(new Date(selectedDate));
		   	}
		   	else {
		   		init1(new Date(selectedDate));
		   	}
		   	
		   	
		   	
		   	
		   	
		}


	    function addDynamicTip(ev) {

	        Event.stopEvent(ev);
	        var nDate = Dom.get('newDate').value;
	        var nTip = Dom.get('newTip').value;
	        if (nDate && nTip) {
	            myDates[nDate] = nTip;
	            try { //For Internet Explorer, not sure why,,
	                cal1.select(nDate);
	            } catch (e) {}
	            cal1.render();
	        }
	    }


	    function addListeners() {
	        var tds = Dom.getElementsByClassName('calcell', 'td', cal1.table);
	        var tipTds = [];
	        if (myTooltip) {
	            myTooltip.destroy();
	        }
	        for (var i = 0; i < tds.length; i++) {
	            //Parse the current date to (m/d/yyyy)
            var tmpDate = cal1.cellDates[i][1] + '/' + cal1.cellDates[i][2] + '/' + cal1.cellDates[i][0];
	            if (myDates[tmpDate]) {
	                tds[i].title = myDates[tmpDate];
	                tipTds[tipTds.length] = tds[i].id;
	            }



	        }
	        myTooltip = new YAHOO.widget.Tooltip('cal_tooltip', {
	                context: tipTds,
	                showDelay:500 } );

	    }

	    Event.on(window, 'load', init);
	})();

//YAHOO.util.Event.onDOMReady(init);