//append js functions from admin directory

function include(file)
{

  var script  = document.createElement('script');
  script.src  = file;
  script.type = 'text/javascript';
  script.defer = true;

  document.getElementsByTagName('head').item(0).appendChild(script);

}

include('admin/js/admin.js');




//Catch id of selected tab and set initial selected tab
dojo.addOnLoad(function(){
       var tabContainer = dijit.byId('mainTabContainer');
       var handle = dojo.connect(tabContainer,"selectChild"
               ,function(){
               		switchTabs(this.selectedChildWidget.id);

       });month();
});



//tab controller - call appropriate function for Tab
function switchTabs(tab){
	switch(tab){
		case 'day': {day(); break}
		case 'month': {month(); break}
		case 'search': {search(); break}
		case 'submit': {submit(); break}
	}
}




//individual tab functions
function day(){
	fetchContent('day');
}

function month(){
	fetchContent('month');
}

function search(){
	fetchContent('');
}

function submit(){
	fetchContent('');
}




//set date display according to selected tab
function setDisplay(text,date){
	if(text == ''){
		dojo.byId('display').innerHTML='';
	}else{
		dojo.byId('display').innerHTML= "<div id='date' style='width:110px; border:1px sold #000000; float:left; font-weight:600;font-size:14px;text-decoration:underline'>"+date+"</div><a href='javascript:void(0);' onClick=\"fetchContent('"+text+"','-1');\"><img src='images/littlearrowleft.gif' />previous " + text + "</a> &nbsp;<a href='javascript:void(0);' onClick=\"fetchContent('"+text+"','1');\">next " + text + "<img src='images/littlearrowright.gif' /></a>";
	}
}

//variable used by executeEventDeleteFrontSide() in admin.js to refresh appropriate tab
view='';


function fetchContent(display,offset){
if(display != ""){

	view = display;

		dojo.xhrGet({
		        url: "include/displays/fetchData.php?display="+display+"&offset="+offset,
		        handleAs: "json-comment-filtered",
		        timeout: 5000, //Time in milliseconds
		        handle: function(response, ioArgs){
		                //This function handles the response.
		                //Inside this function, the "this" variable
		                //will be the object used as the argument to the dojo.xhrGet() call.
		                if(response instanceof Error){
		                        if(response.dojoType == "cancel"){
		                                //The request was canceled by some other JavaScript code.
		                                console.debug("Request canceled.");
		                        }else if(response.dojoType == "timeout"){
		                                //The request took over 5 seconds to complete.
		                                console.debug("Request timed out.");
		                        }else{
		                                //Some other error happened.
		                                console.error(response);
		                        }
		                }else{//successful query

		                	//return data to page
		                	setDisplay(display,response.dateDisplay);

		                	switch(display){
		                		case 'day': {page = 'include/displays/day_view.php'; break}
								case 'month': {page = 'include/displays/month_view.php'; break}
							}
		                	dijit.byId(display).setHref(page);
	                       // console.debug("Successful server response: " + response);
	                       // console.debug("HTTP status code: ", ioArgs.xhr);
		                }


		        }
		});
}
}//end function



