var initNGV = function() {};

var exhibitionformatDate = function(dateString, type){
    try{
        dateString = dateString.split("/");
        date= new Date(dateString[2], dateString[1], dateString[0]);
        return Format(date, type);
    } catch (e) {
        if(window.console && console.error){
             console.error("Date parsing fail");
        }
    }
}

$(function(){

	/*Initalise Modules
	---------------------------------------------*/
	//Program Calendar
	$("#program-calendar").each(function(){
		var calendarAsset = $(this);
		
		//Category by Calendar Date List
		if(calendarAsset.parents(".programResultsCategory").length > 0){
			var links = calendarAsset.parents(".chooseView");
			calendarAsset.ngvCalendar(location.protocol+"//"+location.hostname + location.pathname, $(".previousMonth",links), $(".nextMonth",links),[new dateQueryObj("queries_calendar_program_query","yyyymmdd"),new dateQueryObj("queries_calendar_progam_tour_query","yyyymmdd")]);
			
		}//Programs by Calendar Header Calendar
		else if(calendarAsset.parents(".programsHeader").length > 0){
			calendarAsset.ngvCalendar("", null, null, [new dateQueryObj("queries_calendar_program_query","yyyymmdd"),new dateQueryObj("queries_calendar_progam_tour_query","yyyymmdd"), new dateQueryObj("queries_exhibtion_start_query_tovalue[d]","d"), new dateQueryObj("queries_exhibtion_start_query_tovalue[m]","m"), new dateQueryObj("queries_exhibtion_start_query_tovalue[y]","yyyy"), new dateQueryObj("queries_exhibiton_end_query_fromvalue[d]","d"), new dateQueryObj("queries_exhibiton_end_query_fromvalue[m]","m"), new dateQueryObj("queries_exhibiton_end_query_fromvalue[y]","yyyy")]);
		}//Solo Calendar
		else{
			calendarAsset.ngvCalendar();
		}	
	});	
	
	//Exhibtion Details
        /* No longer required as the date formats output by Matrix have been changed for these fields */
	/*if($(".exhibitionDetailModule").length > 0){
		//format Date
		$(".exhibitionDetailModule .displayDate span.openDate").text(exhibitionformatDate($(".exhibitionDetailModule .displayDate span.openDate").text(),"dd XXX yyyy"));
		$(".exhibitionDetailModule .displayDate span.closeDate").text(exhibitionformatDate($(".exhibitionDetailModule .displayDate span.closeDate").text(),"dd XXX yyyy"));
	}*/
	
	//Programs by Exhibtion 
	if($(".programsHeaderExhibition").length > 0){
		//format Date
		$(".programsHeaderExhibition .displayDate span.openDate").text(exhibitionformatDate($(".programsHeaderExhibition .displayDate span.openDate").text(),"dd XXX yy"));
		$(".programsHeaderExhibition .displayDate span.closeDate").text(exhibitionformatDate($(".programsHeaderExhibition .displayDate span.closeDate").text(),"dd XXX yy"));
	}
    
	//Begin NGV Script
	initNGV();
});
