

(function($j, $tb){
	
	$j(document).ready(function(){
		
								
		//create the message box
		//we are also placing the messageBox element and matte containers in the body so that they are visible (div#calcMessages is display:none);
		co2calc.messageBox = new co2calc.util.MessageBox({
			element:$j("div.calculator-message").remove().appendTo("body"),
			matte:$j("div.calculator-message-matte").remove().appendTo("body")			
		});
		//bind the widgets to update when the sessionData is set
		co2calc.jI.bind("co2calc.sessionData", function(evt){
			co2calc.widgets.fromJSON();
		});
		//bind the loadStart event to show the message box as a loader
		co2calc.jI.bind("co2calc.loadStart", function(evt, data){
			if (co2calc.messageBox) {
				co2calc.messageBox.showTemplate($j("div.message-loader")[0], data);
			}
		});
		//bind the loadSuccess event to hide the message box loader
		co2calc.jI.bind("co2calc.loadSuccess", function(evt){
			if(co2calc.messageBox) co2calc.messageBox.hide();
		});
		//bind error event to show error in message box, alert the error if the message box is not available
		co2calc.jI.bind("co2calc.error", function(evt, data){
			if (co2calc.messageBox) {
				co2calc.messageBox.showTemplate($j("div.message-error")[0], data);
			}else{
				alert(err);
			}
		});
		//bind alert event to show alert message in message box, alert the message if the message box is not available
		co2calc.jI.bind("co2calc.alert", function(evt, data){
			if (co2calc.messageBox) {
				//co2calc.messageBox.show($j("div.message-alert")[0].outerHTML.replace("{$message}", msg));
				co2calc.messageBox.showTemplate($j("div.message-alert")[0], data);
			}else{
				alert(msg);
			}
		});
		
		
		co2calc.sessionsRepeater = new co2calc.util.SessionsRepeater({
			itemsContainer:"div.sessions-grid div.grid-content",
			itemsSelector:"div.grid-row",
			messageContainer:"div.message-sessions",
			data:[]							
		});
		
	});
	
	
})(jQuery, tbelt);

