if(gallery == null) gallery = new Object;

gallery.images = Array();
gallery.loadedImages = Array();
gallery.content = null;
gallery.current = null;

gallery.show = function(el) {
	this.current = el;
	this.loadImage(el.href);
	return false;
}

gallery.showNext = function() {
	if( this.current == null )
		this.current = this.getCurrentTmb();
	if( $('a.current').parent('li').next('li').length )
		this.show(this.current.parentNode.nextSibling.firstChild);
	return false;
}

gallery.showPrev = function() {
	if( this.current == null )
		this.current = this.getCurrentTmb();
	if( $('a.current').parent('li').prev('li').length )
		this.show(getPreviousSibling(this.current.parentNode).childNodes[0]);
	return false;
}

gallery.getCurrentTmb = function() {
	i=1;
	e=document.getElementById('tmb'+i);
	while(e!='undefined' && e!=null && e.className != 'current') {
		e=document.getElementById('tmb'+i);
		i++;
	}
	return e;
}

gallery.showImage = function(url) {
	i=1;
	e=document.getElementById('tmb'+i);
	while(e!='undefined' && e!=null) {
		e.className="";
		i++;
		e=document.getElementById('tmb'+i);
	}
	this.current.className="current";
	var ul = $(".slides ul");
	if( ul.css('width').replace('px','') - ul.css('left').replace('px','') )
	
	this.goSlideTo();
	
	$('#galleryLoading').hide();

	hlink = document.getElementById('highslideLink');
	hlink.href = url.replace('m.','uploaded.');
	hlink.style.width = this.content.width+'px';
	hlink.style.height = this.content.height+'px';
	
	img = document.getElementById('galleryPhoto');
	img.src = url;
	img.style.width = this.content.width+'px';
	img.style.height = this.content.height+'px';
	
	this.showTitle();

	if( $('a.current').parent('li').next('li').length ) $('#nextPhoto').show();
	else $('#nextPhoto').hide();
	if( $('a.current').parent('li').prev('li').length ) $('#prevPhoto').show();
	else $('#prevPhoto').hide();
}

gallery.loadImage = function(url) {
	this.showLoading();
	var img = document.createElement('img');
    this.content = img;
	img.onload = function () {
		gallery.showImage(url);
	};
	img.src = url;
}

gallery.imageLoad = function(url) {
	var img = document.createElement('img');
    this.content = img;
    img.onload = function () {
		gallery.showImage(url);
	};
	
}

gallery.showTitle = function() {
	var p = document.getElementById('galleryPhotoTitle');
	var container = document.getElementById('galleryPhotoContainer');
	if( this.current.title.length > 0 ) {
		if( p == null || p == 'undefined') {
			var p = document.createElement('p');
			p.id = 'galleryPhotoTitle';
			container.appendChild(p);
		}
		p.style.width = (this.content.width-12)+'px';
		p.style.left = (526-this.content.width)/2+'px';
		p.innerHTML = this.current.title;
	} else {
		if( p != null && p != 'undefined') {
			container.removeChild(p);
		}
	}
}

gallery.showLoading = function() {
	container = document.getElementById('galleryPhotoContainer');
	loading = document.getElementById('galleryLoading');
	loading.style.display = 'block';
	loading.style.left = (container.offsetWidth-loading.offsetWidth)/2+'px';
	loading.style.top = (container.offsetHeight-loading.offsetHeight)/2+'px';
}

gallery.goSlideTo = function() {
	var ulposition = $(".slides ul").position();
	var liposition = $(".slides a.current").position();
	if( liposition.left < -1*ulposition.left+60 ) {
		var i = this.current.id.replace('tmb','');
		/*$('#slideCurr').val(i);*/
		$('#slideCurr').val(i-1);
	} else if( liposition.left > -1*ulposition.left+478-60 ) {
		var i = this.current.id.replace('tmb','');
		/*$('#slideCurr').val(i-7);*/
		$('#slideCurr').val(i-6);
	}
}

function getPreviousSibling(n){
	var x=n.previousSibling;
	if( x != null ) {
		while (x.nodeType!=1) {
			x=x.previousSibling;
		}
	}
	return x;
}
