function matBox(contents){
	var that = this;
	this.isShown = false;
	this.current = null;
	this.articles = [];
	if(typeof(iDatalinkURL)=="string"){
		this.baseURL=iDatalinkURL;
	} else {
		this.baseURL = '';
	}
	$('body').append('<div class="matbox"></div>');
	try{
		if(this.baseURL.slice(-1) == '/'){
			sep = '';
		} else { sep = '/';}
	var uri = this.baseURL+sep+'common/scripts/matbox2/templates/matbox.tpl';
	
	$('div.matbox').load(uri,function(){$('img.closeMat').attr('src',that.baseURL+sep+'common/images/matbox/buttons/close.'+_Lan.toLowerCase()+'.gif')
		.click(that.close);});
	//$('div.matbox > div.matbox_nav').dblclick(function(){that.close();});
	} catch (e){throw 'no template';}
	
	this.addContent = function(title,content,handler){
		obj = {'title':title,'content':content};
		var current = that.articles.push(obj) - 1;
		that.goToArticle(current,handler);
	};
	this.print = function(){
		var body = $('body').html();
		var htm = $('.matbox').get(0);
		$('body').empty().append(htm);
		//window.print();
		$('body').html(body);
	};
	this.goToArticle = function(index,handler){
		if(typeof(that.articles[index]!="undefined")){
			var article = that.articles[index];
		} else {
			throw "Invalid article called";
		}
		
		that.current = index;
		var fadeOutHandler = function(handler){
			$('div.matbox div.center span').html(article['title']);
			$('div.matbox div.matbox_contents').html(article['content']);
			that.show();
			if(typeof(handler)=='function') handler();
		};
		$('div.matbox div.center, div.matbox div.matbox_contents').fadeOut('slow',
				function(){
					fadeOutHandler(handler);
					fadeOutHandler = function(){};
				}
		);
	};
	this.close = function(){
		$('div.matbox').slideUp('slow');
		$('body>table').animate({ 
	        marginBottom: "0px"
	      }, 1500 );
		that.isShown = false;
	};
	this.next = function(){
		var new_index = that.current + 1;
		if(typeof(that.articles[new_index])!="undefined"){
			that.goToArticle(new_index);
		} else {
			that.goToArticle(0);
		}
	};
	this.previous = function(){
		if(that.current == 0){ var new_index = that.articles.length - 1;} else {
			var new_index = that.current - 1;}
		if(typeof(that.articles[new_index])!="undefined"){
			that.goToArticle(new_index);
		} else {
			that.goToArticle(0);
		}
	};
	this.show = function(){
		var fadeInHandler = function(){$('div.matbox div.center, div.matbox div.matbox_contents').fadeIn('slow')};
		if(!that.isShown){
			
			var height = $('div.matbox').css('height');
			$('body>table').animate({ 
		        marginBottom: height
		      }, 1500 );
			that.isShown = true;
			$('div.matbox div.center, div.matbox div.matbox_contents').hide();
			$('div.matbox').slideDown('slow', fadeInHandler);
		} else {fadeInHandler();}
		
	};
}

var MB_bottom;
$(function(){
	try{
	MB_bottom = new matBox();
	} catch(e){}
});
