// JavaScript functions for the press gallery

function Gallery(name)
{
	this.name = name;
	this.items = new Array();
	this.add = Gallery_add;
	this.write = Gallery_write;
}

function Gallery_add()
{
	this.items[this.items.length] = new Gallery_Item(Gallery_add.arguments);
}

function Gallery_write()
{
	var content = "";
	var cols = 3;
	
	content += '<table border="0" cellpadding="0" cellspacing="0" class="gallery">';
	
	var i = 0, n = this.items.length;
	var rows = Math.ceil(n / cols);
	var row = 0;
	
	while(i < n) {
		
		content += "<tr>";
		
		col = 0;
		while(col < cols) {
			var style;
			if(row == 0 && rows > 1) {
				if(col == 0) style = "ul";
				else if(col == (cols - 1)) style = "ur";
				else style = "uc";
			}
			else if(row == (rows - 1) && rows > 1) {
				if(col == 0) style = "bl";
				else if(col == (cols - 1)) style = "br";
				else style = "bc";
			}
			else {
				if(col == 0) style = "l";
				else if(col == (cols - 1)) style = "r";
				else style = "c";
			}
			
			content += '<td class="' + style + '">';
			
			if(i < n) {
			var it = this.items[i];
			
			if(it.image != "") { // there is an image
				var has_link = 0, url;
				
				if(it.url != "") {
					has_link = 1;
					url = it.url;
				}
				else if(it.full != "") {
					has_link = 1;
					url = it.full;
				}
			
				if(has_link) {
					content += '<a href="" onclick="javascript:window.open(\'' + url + '\',\'press\')">';
				}
	
				img_id = this.name + '_' + parseInt(i);
				content += '<img id="' + img_id + '" src="' + it.image + '"';
				if(it.rollover != "") { // there is a rollover
					var onmouseover = "MM_swapImage('" + img_id + "','','" + it.rollover + "',1)";
				
					content += ' onmouseover="' + onmouseover + '"';
					content += ' onmouseout="MM_swapImgRestore();"';
				}
				content += '>';
				
				if(has_link) { // there is a hyperlink
					content += '</a>';
				}
			}
			
			if(it.text.length > 0) { // there is text
				for(line in it.text) {
					content += "<p>" + it.text[line] + "</p>";
				}
			}
			}
			
			content += '</td>';
			
			i++;
			col++;
		}
		
		content += "</tr>\n";
		
		row ++;
	}
	
	content += '</table>';
	
	document.writeln(content);
}

function Gallery_Item(args)
{
	this.image = "";
	this.rollover = "";
	this.full = "";
	this.url = "";
	this.text = new Array();
	
	var i = 0;
	while(i < args.length) {
		var key = args[i];
		i++;
		var value = args[i];
		i++;
		
		if(key == "image") {
			this.image = value;
		}
		else if(key == "rollover") {
			this.rollover = value;
		}
		else if(key == "full") {
			this.full = value;
		}
		else if(key == "url") {
			this.url = value;
		}
		else if(key == "text") {
			this.text[this.text.length] = value;
		}
	}
}
