var Gallery = {
	getTransport: function(){
		if(window.XMLHttpRequest){ // Mozilla, Safari,...
			return new XMLHttpRequest();
		}else if(window.ActiveXObject){ // IE
			try{
				return new ActiveXObject("Msxml2.XMLHTTP");
			}catch(e){
				try{
					return new ActiveXObject("Microsoft.XMLHTTP");
				}catch(e){}
			}
		}else{
			return false;
		}
	},
	xmlFileList: 'xml/gallery.ajax.xml.php',
	method: 'POST',
	fotoDir: 'fotos/',
	__name: new Array(),
	__uri:  new Array(),
	__pathfoto:  new Array(),
	picNum: 8,
	//picPathNum:0,
	picWidth: 80,
	picTotal: 0,
	pathPicWidth: 84,
	containerWidth: 0,
	clipWidth:0,
	pathWidth:0,
	setContainerWidth: function(){ 
		this.containerWidth = (this.picNum*this.picWidth);
	},
	setClipWidth: function(){
		this.clipWidth = (this.picNum*this.picWidth);
	},
	setPathWidth: function(val){
		if(val != 0){
			this.pathWidth = this.__name.length*this.pathPicWidth;
		}else{
			this.pathWidth = 0;
		}
	},
	/*runTest: function () {
		hCarousel = new UI.Carousel("horizontal_carousel");
		//vCarousel = new UI.Carousel("vertical_carousel", {direction: "vertical"});
		
	},*/
	changeThumbnail: function(id,opt,nopic){
		document.getElementById("thumbnail["+id+"]").style.backgroundPosition = "center 28px";
		document.getElementById("thumbnail["+id+"]").style.backgroundImage = "url(\'gfx/ajax-loader.gif\')";
		
		var setNopic = '';
		var setOption = '';
		if(nopic == 1){
			setNopic = '&imgurl=nopic.jpg';
		}
		
		if(opt == 'sw'){
			 setOption = '&option=sw';
		}
		
		var url = "thumbnail.php?id="+id+setOption+setNopic;
		
		Preloading = new Image();
		Preloading.onload=function(){
			document.getElementById("thumbnail["+id+"]").style.backgroundPosition = "center center";
			document.getElementById("thumbnail["+id+"]").style.backgroundImage = "url("+url+")";
			
			//Preloading.onload=function(){};
		}
		Preloading.src = url;
		
		
		
		//document.getElementById("thumbnail["+id+"]").style.backgroundImage = "url("+url+")";
		
	},
	construct: function(parentid){
		var transport = this.getTransport();
		transport.open(this.method, this.xmlFileList, true);
		transport.onreadystatechange = function(){
			if(transport.readyState == 4){
				//$('#items').hide();
				if(transport.status == 200){
					var request = transport.responseXML;	
					//"alert"+ request.getElementsByTagName('ajax')[0].childNodes[0].firstChild.nodeValue
					var len = request.getElementsByTagName('ajax')[0].childNodes.length;
					
					Gallery.picTotal = len;
					
					if(parentid == 0 || parentid == ''){
						$('#home').fadeOut('normal');	
					}else{
						$('#home').fadeIn('normal');	
					}
					
					var c=0;
					var content = '<table border="0" cellpadding="0" cellspacing="0"><tr><td><ul id="rsfgallery" class="jcarousel-skin-rsf">';
					var nopic;
					for(var i=0; i<len; i++){
						Preloading = new Image();
						id = request.getElementsByTagName('ajax')[0].childNodes[i].getAttribute('id');
						element = request.getElementsByTagName('ajax')[0].childNodes[i].getAttribute('element');
						if(request.getElementsByTagName('ajax')[0].childNodes[i].getAttribute('nopic') == 1) {
							foto = 'thumbnail.php?id='+id+'&option=sw';
							Preloading.src = 'thumbnail.php?id='+id;
							nopic = 0;
							pathFoto = 'thumbnail.php?id='+id;
						}else{
							foto = 'thumbnail.php?option=sw&imgurl=nopic.jpg';
							Preloading.src = 'thumbnail.php?imgurl=nopic.jpg';
							nopic = 1;
							pathFoto = 'thumbnail.php?imgurl=nopic.jpg';
						}
						
	
						content += '<li>';
						
						/*if(c != i){
							content += '<td>&nbsp;&nbsp;&nbsp;</td>';	
						}*/
						
						//'+foto+'
						content += '<table border="0" valign="middle" cellpadding="0" cellspacing="0"><tr><td height="92" style="background-repeat:no-repeat" id="thumbnail['+id+']" valign="top"><a onfocus="this.blur()" href="javascript:';
						if(element == 'dir'){
							content += "Gallery.construct("+id+");Gallery.path('" + request.getElementsByTagName('ajax')[0].childNodes[i].firstChild.nodeValue  + "', '" + id + "', '','"+pathFoto+"');Gallery.setPicNum('-',1);";
						}else if(element == "foto"){
							content += 'Main.foto('+id+');Main.setBG();"';
						}
						content += '" onmouseover="Gallery.changeThumbnail('+id+',\'\','+nopic+');" onmouseout="Gallery.changeThumbnail('+id+',\'sw\','+nopic+');"><img width="67" src="gfx/placeholder.gif" border="0" /></a>';
						if(element == 'dir'){
							content += '<table width="67" height="25" border="0" valign="middle" cellpadding="0" cellspacing="0"><tr><td class="thumbnailTitle" align="center">'+ request.getElementsByTagName('ajax')[0].childNodes[i].firstChild.nodeValue +'</td><tr></table>';
						}
						content += '</td></tr></table></li>';
						
										
					}
					
					content += '</ul></td></tr></table>';
					//$("#rsfgallery").hide('normal');
					$("#items").html(content);
									
									
					for(var i=0; i<len; i++){
						id = request.getElementsByTagName('ajax')[0].childNodes[i].getAttribute('id');
						Gallery.changeThumbnail(id,'sw',nopic);	
					}
					//$("#rsfgallery").show('normal');
					
					//$("#debug").html("debug"+Gallery.containerWidth);					
	
					//$("#debug").html($(".jcarousel-skin-rsf .jcarousel-container-horizontal").css("width")+' '+ Gallery.picNum);	
					Gallery.setContainerWidth();
					Gallery.setClipWidth();
					Gallery.Carousel(Gallery.PicNum);
					/*
					*/
					fixPNG.loadPngs();
				}
			}
		}
		
		transport.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		transport.send('parentid='+parentid);
		
	 	//new UI.Carousel("horizontal_carousel");
		//Event.observe(window, "load", this.runTest);
	},
	
	show: function(){
		
		document.write('<table width="90%" height="92" border="0" cellpadding="0" cellspacing="0" align="center">'+
					   '<tr><td valign="top" width="90" style="color:#ffcc00;font-family:Verdana, Arial, Helvetica, sans-serif; font-weight:bold;font-size:18px;">'+
					   'Portfolio<div id="home"><a href="javascript:Gallery.setPicNum(\'reset\',8);Gallery.construct(0);Gallery.emptyPath();" style="color:#cccccc; font-size:11px; font-weight:normal;">&laquo;&nbsp;zur&nbsp;Übersicht</a></div>'+
					   '</td><td id="pathContent"><div id="path"></div></td><td id="items" align="left">'+
					   
					   '</td></tr>'+
					   '</table>');
		this.construct(0);
		
		
	},
	
	myInitCallback: function(carousel){
		carousel.options.scroll = parseInt(Gallery.picNum);//jQuery.jcarousel.intval(Gallery.picNum);
		carousel.options.size = parseInt(Gallery.picTotal);//jQuery.jcarousel.intval(Gallery.picTotal);
		$(".jcarousel-skin-rsf .jcarousel-clip-horizontal").css("width", parseInt(Gallery.clipWidth)+"px");
		$(".jcarousel-skin-rsf .jcarousel-container-horizontal").css("width", parseInt(Gallery.containerWidth)+"px");
		
			//$('#debug').text('clipWidth: '+Gallery.clipWidth+'; containerWidth: '+Gallery.containerWidth+'; picNum: '+Gallery.picNum+' picTotal:'+Gallery.picTotal+'; size: '+carousel.options.size);
		//alert(this.picNum)
		//document.getElementById('debug').innerHTML = Gallery.pathWidth;
	},
	myinitLoadCallback: function(carousel){
	
	
	
	},
	Carousel: function(picnum){
		

		jQuery('#rsfgallery').jcarousel({
			item:{
				size: 8
			},
			initCallback: Gallery.myInitCallback,
			
			itemLoadCallback: Gallery.myinitLoadCallback
			//size: Galery.picTotal,
	
		});
	},
	path: function(name, uri, pos, foto){
		
		this.__name.push(name);
		this.__uri.push(uri);
		this.__pathfoto.push(foto);
		
		if(pos != ''){
			var j = this.__name.length - pos;
			this.__name.splice(pos,j);
			this.__uri.splice(pos,j);
			this.__pathfoto.splice(pos,j);
		}
		var m = '';
		/*for(i=0;i<this.__name.length;i++){
			m += '<a href="javascript:__file(\'xml/file.xml.php\',\'POST\',\'getObjects\','+ this.__uri[i] +');path(\'' +this.__name[i]+ '\', \''+this.__uri[i]+'\', '+ (i+1) +');">' + this.__name[i] + '</a>';// &raquo; 
		}*/
		
		m += '<table height="92" border="0" valign="middle" cellpadding="0" cellspacing="0"><tr>';
		m += '<td>&nbsp;&nbsp;&nbsp;</td>';
		l = this.__name.length-1;
		for(i=0;i<this.__name.length;i++){
			
		if(i> 0){
			
			m += '<td valign="top"><div style="margin-right:8px;margin-left:8px;background-color:#828282;width:1px;height:65px;"></div></td>';
			
		}
		
		m += '<td  style="background-image:url('+this.__pathfoto[i]+');background-repeat :no-repeat;" valign="top"><a onfocus="this.blur()" href="javascript:Gallery.construct('+this.__uri[i]+');Gallery.path(\'' +this.__name[i]+ '\', \''+this.__uri[i]+'\', '+ (i+1) +',\''+this.__pathfoto[i]+'\');Gallery.setPicNum(\'+\','+l+');"><img width="67" src="gfx/placeholder.gif" border="0" /></a>';
						
							m += '<table width="67" height="22" border="0" valign="middle" cellpadding="0" cellspacing="0"><tr><td class="thumbnailTitle" align="center">'+ this.__name[i] +'</td><tr></table>';
						
	m += '</td>';
			l--;
		}
	
	m += '<td>&nbsp;&nbsp;&nbsp;</td>';
	m += '</tr></table>';
	
		
	
		//$('#path').hide('normal');
		
		
		Gallery.setPathWidth(1);
		
		//$('#path').hide();
		/**/
	
		
		$('#pathContent').animate({
			width: Gallery.pathWidth+'px'
		
		},'normal');
		$('#path').html(m);	
		//$('#path').fadeIn('slow');
		//alert(Gallery.pathWidth+'px '+Gallery.__name.length);
		//$('#path').show('normal');
		//alert(__uri);
		//$('#debug').html(Gallery.pathWidth);
	},
	emptyPath:function(){
		this.__name = new Array();
		this.__uri = new Array();
		this.__pathfoto = new Array();
		
		//$('#path').fadeOut('normal');
		
		$('#pathContent').queue(function () {
			Gallery.setPathWidth(0);
			$(this).animate({width: Gallery.pathWidth+'px'},'normal');
			$('#path').html('');
			$(this).dequeue();
		});
	
	
		
		
		//$('#debug').html(Gallery.pathWidth+'px');
	},
	setPicNum:function(char,value){
			
		if(char == "-"){
			this.picNum = this.picNum-value;
			/*$('#pathContainer').animate({
			left: char+Gallery.pathWidth+'px'
		
		},'normal');*/
		}else if(char == "+"){
				/*$('#pathContainer').animate({
			left: char+Gallery.pathWidth+'px'
		
		},'normal');*/
			this.picNum = this.picNum+value;
		}else if(char == "reset"){
			this.picNum = value;
		}//alert(value);
			//alert(this.picNum);
	}
}
