function wopen(url, width, height) {
	if(!width) width = 50;
	if(!height) height = 50;
	window.open(url, '', "location=no,menubar=no,status=no,titlebar=no,toolbar=no,scrollbars=yes,width=" + width+",height=" + height);
}

/**
 * @author Retarded
 * @projectDescription Previews. Requires the prototype.js library
 */

var gallery = {
	
	dir: "",
	id_active: null,
	last_id: 0,
	photos: [],
	tpl: new Template('<div class="preview"><div class="previewImg" id="pid_#{id_photo}">'+
		 '<div class="innerImg"><a href="#" onclick="gallery.show_photo(#{id_photo}, this); return false;">'+
		 '<img src="#{small_url}" alt="" /></a></div>'+
		 '<a href="#" onclick="gallery.show_photo(#{id_photo}); return false;">'+
		 '<img src="/img/plus.gif" alt="" class="plus" /></a></div></div>'),
	
	init: function(dir)
	{
		gallery.dir = dir;
	},
	
	add: function(bid, small_url, regular_url, big_url)
	{
		photo = {
			id: gallery.last_id++,
			bid: bid,
			small_image: new Image(),
			regular_image: new Image(),
			big_image: new Image()
		};
		photo.small_image.src = gallery.dir + small_url;
		photo.regular_image.src = gallery.dir + regular_url;
		photo.big_image.src = gallery.dir + big_url;
		photo.html = gallery.tpl.evaluate({id_photo: photo.id, small_url: photo.small_image.src});
		
		gallery.photos[photo.id] = photo;
	},
	
	open_photo: function()
	{
		wopen('?popup=' + gallery.photos[gallery.id_active].bid);
	},
	show_photo: function(id_photo, link)
	{
		if(link) link.blur();
		if(gallery.id_active!=null){
//			$('pid_'+gallery.id_active).removeClassName('previewImgActive');
			removeClassName($('pid_'+gallery.id_active), 'previewImgActive');
		}
		$('photo_regular').src = gallery.photos[id_photo].regular_image.src;
		//$('pid_'+id_photo).addClassName('previewImgActive');
		addClassName($('pid_'+id_photo), 'previewImgActive');
		gallery.id_active = id_photo;
	},
	
	print: function()
	{
		for(i=0; i<gallery.photos.length; i++){
			photo = gallery.photos[i];
			new Insertion.Bottom($('previews'), photo.html);
		}
	}
	
}


function addClassName (_element, _className)
{
	var classReplace = '/ ' + _className + '/ig';

	if (_element.className.indexOf (_className) == -1)
	{
		_element.className += ' ' + _className;
	}
}

function removeClassName (_element, _className)
{
	var classReplace = '/ ' + _className + '/ig';

	_element.className = _element.className.replace (eval (classReplace), '');
}
