var onloads = new Array();
function bodyOnLoad() {
     for ( var i = 0 ; i < onloads.length ; i++ )
        onloads[i]();
}

var gozzi={
	opened:null,
	init:function() {
		
	},
	showMenu:function(id) {
		var context=this;
		
		if (this.opened) {
			if (this.opened==id) {
				Effect.toggle(id,'appear',{afterFinish:function() { context.opened=null; } });	
			} else {
				Effect.toggle(context.opened,'appear',{afterFinish:function() { 
					Effect.toggle(id,'appear',{afterFinish:function() { context.opened=id; } });
				} });
			}
			
		
		} else {
			Effect.toggle(id,'appear',{afterFinish:function() { context.opened=id; } });	
		}
	},
	setGallery:function(l,vl,path) {
		this.gallery_length=l;
		this.gallery_viewport=vl;
		this.gallery_path=path;
		this.moving=false;
	},
	gallery2l:function() {
		//console.log("2l:"+$('pics').positionedOffset().left);
		if ((!this.moving)&&($('pics').positionedOffset().left<0)) {
			this.moving=true;
			new Effect.Move('pics',{
						x:240,
						y:0,
						mode: 'relative',
						afterFinish:function() {
							//console.log($('pics').positionedOffset().left);
							this.moving=false;
						}.bind(this)
			});
		}
	},
	gallery2r:function() {
		//console.log("2r:"+$('pics').positionedOffset().left);
		if ((!this.moving)&&($('pics').positionedOffset().left>(-this.gallery_length+this.gallery_viewport))) {
			this.moving=true;
			new Effect.Move('pics',{
						x:-240,
						y:0,
						mode: 'relative',
						afterFinish:function() {
							//console.log($('pics').positionedOffset().left);
							this.moving=false;
						}.bind(this)
			});
		}
	},
	galleryOpenImg:function(b_id,pic) {
		if (!this.moving) {
			//this.moving=true;
			$('img_overlay').show();
			var url="flcjax.php";
			new Ajax.Updater(
				{success:$('my_pic')},
				url,
				{
					method: 'get',
					parameters: {
						b:b_id,
						p:pic,
						nocache:new Date().getTime()
					},
					onFailure:function(request) {
						alert("Error!!!!!");
					},
					onComplete: function(originalRequest) {
						var img=$('my_pic').down("img");
						//alert(img);
						if (img.complete) {
							//alert("complete");
							//$('img_overlay').hide();
							//this.moving=false;
							new Effect.Fade('img_overlay',{
								duration:0.5,
								afterFinish:function(){
									this.moving=false;
								}.bind(this)
							});
						} else {
							img.onload = function() {
								//alert("loaded");
								//$('img_overlay').hide();
								//this.moving=false;
								new Effect.Fade('img_overlay',{
									duration:0.5,
									afterFinish:function(){
										this.moving=false;
									}.bind(this)
								});
							}.bind(this);
						}
					}.bind(this)
				}
			);
		}
	},
	galleryOpenImg2:function(b_id,pic) {
		if (!this.moving) {
			//$('img_overlay').show();
			//this.moving=true;
			$("picture").down("img").remove();
			//var img=document.createElement("img");
			//$my_pic['b_id']."/images/".$my_pic['numImg']."_icon.jpg";
			var src=b_id+"/images/"+pic+"_normal.jpg";
			
			//$("picture").appendChild(img);
			//img.src=this.gallery_path+src;
			//alert(img.src);
			$("picture").innerHTML="<img id='my_img' src='"+(this.gallery_path+src)+"' border='0' alt=''/>";
			//alert(this.gallery_path+src);
			//var img=$("picture").down("img");
			var img=$('my_img');
			/*
			if (img.complete) {
				//alert("complete");
				new Effect.Fade('img_overlay',{
					duration:0.5,
					afterFinish:function(){
						this.moving=false;
					}.bind(this)
				});
			} else {
				img.onload = function() {
					//alert("loaded");
					new Effect.Fade('img_overlay',{
						duration:0.5,
						afterFinish:function(){
							this.moving=false;
						}.bind(this)
					});
				}.bind(this);
			}
			*/
		}
	}
}
function parseUri(sourceUri){
    var uriPartNames = ["source","protocol","authority","domain","port","path","directoryPath","fileName","query","anchor"];
    var uriParts = new RegExp("^(?:([^:/?#.]+):)?(?://)?(([^:/?#]*)(?::(\\d*))?)?((/(?:[^?#](?![^?#/]*\\.[^?#/.]+(?:[\\?#]|$)))*/?)?([^?#/]*))?(?:\\?([^#]*))?(?:#(.*))?").exec(sourceUri);
    var uri = {};
    
    for(var i = 0; i < 10; i++){
        uri[uriPartNames[i]] = (uriParts[i] ? uriParts[i] : "");
    }
    
    // Always end directoryPath with a trailing backslash if a path was present in the source URI
    // Note that a trailing backslash is NOT automatically inserted within or appended to the "path" key
    if(uri.directoryPath.length > 0){
        uri.directoryPath = uri.directoryPath.replace(/\/?$/, "/");
    }
    
    return uri;
}

onloads.push(function(){
	gozzi.init();
});

//Event.observe(window,'load',bodyOnLoad);
