function addEvent(elm, evType, fn, useCapture){if(elm.addEventListener){elm.addEventListener(evType, fn, useCapture);return true;}else if (elm.attachEvent){var r = elm.attachEvent('on' + evType, fn);return r;}else{elm['on' + evType] = fn;}}

function doPopUp(e)
{
	showPicture(this.id.substring(3));
	
	if (window.event){
		window.event.cancelBubble = true;
		window.event.returnValue = false;
	}else if (e){
		e.stopPropagation();
		e.preventDefault();
	}
}

function findPopUps()
{
	var popups = document.getElementsByTagName("a");
	for (var i=0;i<popups.length;i++){
		if (popups[i].rel.indexOf("pic_pop")!=-1){
			popups[i].onclick = popups[i].onkeypress = doPopUp;
		}
	}
}

function showPicture(id,orig){
	orig = orig || false;
	
	if($('picture_id_'+id)){
		$('picture_id_'+id).style.zIndex = zindex+1;
		return;
	}
	
	var dims = getBrowserDims();
	var scrl = getScrollXY();

	new Effect.Fade('container',{to: 0.25, duration: 0.3, transition: Effect.Transitions.linear});
	new Effect.Move('wait_dlg',{x: dims[0]/2-64, y: scrl[1]+dims[1]/2-75, mode: 'absolute', duration: 0.0 });
	new Effect.Appear('wait_dlg', {duration: 0.0, to: 1.0, mode: 'absolute' });

	
	var newElement=document.createElement('div');
	newElement.setAttribute('id','picture_id_'+id);
	newElement.setAttribute('onclick','setTopLayer(this);');
	newElement.style.zIndex = zindex+1;
	$('photos').insertBefore(newElement, $('photos').firstChild);
	
	
	newElement.style.display = 'none';
	newElement.style.position = 'absolute';	
	
	var url = '/gallery/'+id;
	var params = 'no_body=1&scroll_y='+scrl[1]+'&b_w='+dims[0]+'&b_h='+dims[1];
	
	if(orig){
		params += '&orig=1';
	}
	
	var ajax = new Ajax.Updater(
		'picture_id_'+id,
		url,
		{method: 'get', parameters: params, evalScripts: true, 
		onComplete: function(){
			new Effect.Fade('wait_dlg',{duration: 0.2});
			new Effect.Appear('container',{to: 1.0, duration: 0.1});
		}
	});
}

function setTopLayer(layer){
	layer.style.zIndex = ++zindex;
}

function displayDesc(id,obj){
	new Effect.Move('picture_desc_id_'+id,{ x: $('picture_id_'+id).offsetWidth/2-120, y: $('picture_id_'+id).offsetHeight/2, mode: 'absolute', duration: 0.0 });
	Element.show('picture_desc_id_'+id);
}

function closeDesc(id){
	Element.hide('picture_desc_id_'+id);
}

function closePicture(id){
	Element.remove('picture_id_'+id);
	draggables['pic_'+id].destroy()
	draggables['desc_'+id].destroy()
}

function showPictureMenu(id,obj){
	var posx = findPosX(obj);
	var posy = findPosY(obj);
	new Effect.Move('picture_menu_id_'+id,{ x: posx+120, y: posy+45, mode: 'absolute', duration: 0.0 });
	Element.show('picture_menu_id_'+id);
}

function closePictureMenu(id){
	Element.hide('picture_menu_id_'+id);
}

function getRandomThumbs(){
	var ajax = new Ajax.Updater(
		'randThumbs',
		'/js/ajax.gallery.php',
		{method: 'get', evalScripts: true
	});	
	
	return false;
}

addEvent(window, 'load', findPopUps, false);

window.onload = function() {
	if($('more_rands')){
		$('more_rands').onclick = getRandomThumbs;
	}
}