// Dojo required elements

        dojo.require("dojo.parser");
		dojo.require("dojox.fx");
		dojo.require("dojox.fx.easing");
        dojo.require("dijit.layout.LayoutContainer");
        dojo.require("dojox.layout.ContentPane");
        dojo.require("dojo.data.ItemFileReadStore");
		dojo.require("dijit.form.Button");
		dojo.require("dijit.form.ComboBox");
		dojo.require("dijit.form.CheckBox");
		dojo.require("dijit.form.ValidationTextBox");
		dojo.require("dijit.Tree");
		dojo.require("dijit.Editor");
		dojo.require("dojo.io.iframe");
		dojo.require("dijit._editor.plugins.FontChoice");  // 'fontName','fontSize','formatBlock'
		dojo.require("dijit._editor.plugins.TextColor");
		dojo.require("dijit._editor.plugins.LinkDialog");

        
/// Global Variables

	var qsParm = new Array();

/// End Global Variables.

// get query string
function qs() {
	var query = window.location.search.substring(1);
	var parms = query.split('&');
	for (var i=0; i<parms.length; i++) {
		var pos = parms[i].indexOf('=');
		if (pos > 0) {
			var key = parms[i].substring(0,pos);
			var val = parms[i].substring(pos+1);
			qsParm[key] = val;
		}
	}
}


	var menuData = { };
	var menuStore = new dojo.data.ItemFileReadStore({data: menuData });
	var numImages = 0;


function changePane(page) {
	var anim = dojo.fadeOut({node: 'content', duration:500, easing: dojox.fx.easing.easeIn }).play();
	dojo.connect(anim,"onEnd",function(){
     	dijit.byId("content").setHref(page);
			dojo.fadeIn({node: 'content', duration:1000,	easing: dojox.fx.easing.easeIn }).play();
	});
}

function updateSideBar() {
	dijit.byId("sideBar").setHref('sideBar.php');
}

function showPopup(page) {
	dijit.byId("pop").setHref(page);
	dijit.byId("pop").show();
}

function showImageBox() {
	numImages++;
	document.getElementById('image'+numImages).style.display="block";
}

function callProcessor(page) {
	dojo.io.iframe.send( {
        url: page, 
		method: "post",
		handleAs: "text",
		form: dojo.byId('mainForm'),
		handle: function(data,ioArgs){
//				console.debug(data);
				changePane('result.php?message=Success');
		}
    });
}

function getMenu() {
	var getStore = dojo.xhrGet( {
		url: "loginarea/getMenu.php?cachebust=" + new Date().getTime(), 
		handleAs: "text",
		handle: function(d,ioArgs){
			 menuData = eval("(" + d + ")") ;
			 menuStore = new dojo.data.ItemFileReadStore({data: menuData });
			dijit.byId("menuDiv").setHref('menu.php');
		}
    });
 }


function init() {
	getMenu();

	// This sets up a subscription for clicks on the tree for actions
	dojo.subscribe("menuTree", null, function(message){
	if(message.event=="execute") {
		if (menuStore.getLabel(message.item)=="log off") window.location.href="loginarea/logout.php";
		if (menuStore.hasAttribute(message.item, "pane")) { 
			page = menuStore.getValue(message.item, "pane");
			if (page != '') changePane(page);
		}
		else {
			id = menuStore.getValue(message.item, "id");
			if (id != '') {
			changePane('dynamic/news.php?id='+id);
			}
		}
	}});

	updateSideBar('main');

	// if parameter on url, go to shopping list page.
	qsParm['page'] = null;
	qs();
	if (qsParm['page']) {
		changePane(qsParm['page']);
	}
}
    
    dojo.addOnLoad(init);