/*
 * Diamond Way Buddhism Event Widget
 * Version: 0.1
 * Install:
 * 2. Input the aktuelle_events.js in your HTML File (like "<script src="aktuelle_events.js...")
 * 3. Browse the html file (it should work)
 * 4. Please read the section below about xml data (url)
 * TODO: preparse the xml struture to json string - for better performance
 * If you have any questions about the script - don't hesitate to contact me.
 */
var anzahl = 3;
var event_id = 1;
function createEventItem(text){
	if(event_id <= anzahl) {
		if(event_id == 1 ) {
			$("#events").html(text);
			$("#events").hide();	
		} else {
			$("#events").append(text);
		} 
		event_id++;	
	} 
	if(event_id == anzahl) {
		$("#events").slideDown(1500);
	}	
}

function event_calendar_init(){

    // PLEASE READ THIS!
    // About the url of XML Data:
    
    // The XML structure, used in this script is hosted at www.diamondway-service.org/events/
    // For performance optimization it is better to hold a copy 
    // of the data local on the your server
    // like you see in the folder /data/my_events.xml
    // But it will outdate soon, as information changes daily.
    
    // Please write a cronjob to download the original file from
    // "http://www.diamondway-service.org/events/event_xml.asp" and replace the local file at /data/center_all.xml
    
    // If you have any question don't hesitate to ask me for help :-)
    // Karmapa Chenno, marcel w.
    
    var hostingPath = location.href.substr(0, location.href.lastIndexOf("/"));
    var url = hostingPath + "/js/my_events.xml";
    //var url = 'http://www.diamondway-service.org/events/events_xml.asp?region=Nord&von=01.01.2010&bis=31.12.2063';
	
    $.get(url, function(xml){
        var json = $.xml2json(xml);
        $.each(json.event, function(i, event){

            var infoBox = "<div class=\"event\">";
			
			var wo = "";
			var wann = "";
			var description = "";
			var subtitle = "";
			
			if(event.fldcatName != "") {
				wo = event.fldcatName;
			}
			
            if (event.fldprgDatumBis != "") {
                    wann += event.fldprgDatumBis;
		            if (event.fldprgDatumUhrZusText != "") {
						wann += ", " + event.fldprgDatumUhrZusText; 					
					}
            }

			infoBox += "<p class=\"event date\">" + wann + " | "+ wo +"</p>";
		                    
		    if (event.fldprgTitelText != "") {
                infoBox += "<p class=\"event title\">" + event.fldprgTitelText + "</p>";
            }
			
			if(event.fldprgText != "") {
				description = event.fldprgText;
			}
			
			if(event.fldprgTitelLehrer != "") {
				subtitle = event.fldprgTitelLehrer;
			}
			
			if(description != "" || subtitle != "") {
				infoBox += "<p class=\"event description\">" + subtitle + " " + description + "</p>";
			}			

            infoBox += "</div>"
            createEventItem(infoBox);
        }); // each
    }); // get
}

