var yobj = YUI({combine: true, timeout: 10000}).use('node', 'anim', function(Y) {


	/* general Variables */
	var x =0;
	var allready = false;
	var introdone = false;
	var classesAdded = false;
	var footeradjust = false;

	/* Static Info Container Variables */
	var imgList;
	var imgAnims = Array();
	var imgcount;
	var txtList = Array();
	var captList = Array();


	/* Dynamic Info Container Variables */
	var focusImg = null;
	var focusTxt = null;
	var prevImgW = 0;
	var prevImgH = 0;


	/* Design Variables */
	var rowPosY = 245;

	var distortX = 25;
	var distortY = 50;

	var imgSizeRand = 40;
	var imagenextline = 20; //35
	var imgScale = 1;

	var imgSpeed = 0.5;
	var imgSpeedinit = 0.4;
	
	var imgalpha = 1;


	/* temp variables */
	var imgPosX = 0;
	var imgPosY = 0;
	
	var modW = 0;
	var modH = 0;
	var randVe = 0;
	var randHo = 0;
	var colcount = 0;
	var nlrand = 0;

	var totalY = 0;

	var addClasses = function(){

		classesAdded = true;

		if( Y.one('#cont h1') != null){
			Y.one('#cont h1').addClass('js-firstHeader');
		}else if( Y.one('#cont h2') != null ){
			Y.one('#cont h2').addClass('js-firstHeader');
		}else if( Y.one('#cont h3') != null ){
			Y.one('#cont h3').addClass('js-firstHeader');
		}

		Y.one('#firstnav').setStyle('opacity', 0.9);
		Y.one('#secnav').setStyle('opacity', 0.9);
		Y.one('#thirdnav').setStyle('opacity', 0.9);
	}


	var adjustFooter = function() {

		//footeradjust = true;
									
		var margin = 10;
		var footer = Y.one('#footer');

		var winheight = footer.get('winHeight');
		var docheight = footer.get('docHeight');

		//console.log(footer.get('winHeight'));
		//console.log(footer.get('docHeight'));
		//console.log(totalY);
		
		if (totalY > docheight){
			footer.setStyle('top', totalY+'px');
			//console.log('set footer totalY');
		}
		
		if (typeof maxheight != 'undefined'){
			
			var getHeight = function(nodename){
				if (Y.one(nodename) != null){
					return	parseInt(Y.one(nodename).getComputedStyle('height'));
				} else {
					return 0;
				}
			}
			
			curtitleH = getHeight('#curtitle');
			tabnavH = getHeight('#tabnav');
			headerH = getHeight('div.tx_museumplus .csc-default');
			
			
			//maxheight kommt aus tabnav.js, 180 fixed part height (search/crumbpath/logo)
			var totaltabH = 180 + curtitleH + headerH + tabnavH+ maxheight;
			
			//console.log(totaltabH);
			
			
			if (totaltabH > totalY){
				footer.setStyle('top', totaltabH+'px');
				//console.log('set footer totalY');
			}
		}
		
		if (winheight >= docheight) {
			var yheight = footer.getY();
			
			if (yheight+margin  < winheight ) {
				footer.setStyle('bottom', '0');
				//console.log('set footer');
			}
		}
		footer.setStyle('visibility', 'visible');
	}
	
	var init = function(){

		//if(!footeradjust){ adjustFooter(); }
		
		if(!classesAdded){ addClasses(); }

		//var contNode = Y.get('#cont');
		imgList = Y.all('#cont img');
		imgcount = imgList.size();
		if(imgcount < 3){ rowPosY=270;  }
		//imgScale = imgcount / 25 + 1;

		imgScale = imgcount / 55 + 1;
		

		//alert(imgcount + ' bilder gefunden');
		//imgList.setStyle('position', 'absolute');

		setTimeout( function() { posAllImg();  }, 100 );
		setTimeout( function() { introAni();  }, 500 );
		setTimeout( function() { imgList.setStyle('display', 'block');  }, 520 );

	}

	var posAllImg = function(){
		x=0; //reset counter for posImg func.
		imgList.each( posImg );
		
		if(!footeradjust){ adjustFooter(); }
	}


	var posImg = function(curnode, curindex, listinstance ){

		var contextObj = { 
			name: "context",
			myid: x
		};

		var curW = parseInt( curnode.getAttribute('width') );
		var curH = parseInt( curnode.getAttribute('height') );


		//var tmprand =  1 + parseInt( Math.random() * imgSizeRand  - (imgSizeRand / 2) ) / 100;
		var tmprand =  1 - Math.random() * imgSizeRand  / 100;

		if(imgcount < 3){  var tmprand = 1 - Math.random() * 10  / 100}

		//alert('tmprand:'+ tmprand);

		modW = Math.round( curW / imgScale * tmprand );
		modH = Math.round( curH / imgScale * tmprand );

		colcount ++;
		if(imgPosX == 0){
			nlrand = 100;
		}else{
			nlrand = Math.random() * 100 - colcount * 17;
		}

		randHo = Math.round(  modW / 100 * ( parseInt( Math.random() * distortX - distortX / 1.2 ) )  );
		if(imgPosX + randHo + modW > 480 || nlrand < imagenextline ){
			imgPosX = 0;
			rowPosY += parseInt( prevH );
			colcount = 0;
		}else if(imgPosX != 0){
			imgPosX += randHo;
		}

		

		randVe = Math.round(  modH / 100 * ( parseInt( Math.random() * distortY - distortY/1.2 ) ) );
		//curnode.set('title', 'baseY:'+rowPosY+' randVe:'+randVe +' | randHo:'+ randHo + ' | nlrand:'+nlrand );

		if(imgcount < 3){ randVe =  parseInt( Math.random()* 30) }
		
		imgPosY = rowPosY + randVe;

		
		//if(imgcount == 1){ imgPosY=270 }

		imgPosYbig = imgPosY - (curH - modH ) / 2;
		
		totalY =  imgPosY + modH;
		
		//randZindex = 20 + parseInt( Math.random() * 29 );

		imgPosXbig = 0;
		if(imgPosX + curW > 480 ){
			imgPosXbig = 480 - curW;
		}else if(imgPosX - (curW - modW ) / 2  > 0){
			imgPosXbig = imgPosX - (curW - modW ) / 2
		}
		imgAnims[x] = Array();
		imgAnims[x]['big'] = new Y.Anim({
			node: curnode,
			duration: imgSpeed,
			easing: Y.Easing.easeIn,
			to: {
				width: curW,
				height: curH,
				//top: imgPosYbig,
				xy: [imgPosXbig, imgPosYbig],
				zIndex: 200 //, opacity: 1 
			}
		});
		imgAnims[x]['normal'] = new Y.Anim({
			node: curnode,
			duration: imgSpeed,
			easing: Y.Easing.easeOut,
			to: {
				width: modW,
				height: modH,
				//top: imgPosY,
				xy: [imgPosX, imgPosY],
				zIndex: 199-x //, opacity: 1 
			}
		});
		/*
		imgAnims[x]['blur'] = new Y.Anim({
			node: curnode,
			duration: imgSpeed,
			to: { opacity: imgalpha }
		});
		*/
		
		var tmpAncestor = curnode.ancestor(getRelatedTxt);
		
		
		if( tmpAncestor.getElementsByTagName('img').size() > 1 || Y.all('#cont div.singleview').size() > 0 ){
			txtList[x] = 0;
			Y.on('mouseover', onImgOver, curnode, contextObj, x );
			Y.on('mouseout', onImgOut, curnode);
		}else{
			txtList[x] = tmpAncestor;
			//var tmpTitle = tmpAncestor.query('h1, h2, h3, h4');
			Y.on('mouseover', onImgOver, tmpAncestor, contextObj, x); // tmpTitle, x );
			Y.on('mouseout', onImgOut, tmpAncestor ); //tmpTitle);
		}



		
		//captList[x] = curnode.get('parentNode').next(getDL);
		//alert('captList:'+ captList[x]);
		captList[x] = 0;
		var tmpNode =curnode.ancestor('dt')
		if( tmpNode ){
			tmpNode = tmpNode.next('dd');
			if(tmpNode){
				if( tmpNode.get('innerHTML').length > 2 ){
					captList[x] = tmpNode;
					var capY = imgPosYbig + curH -10;
					//tmpNode.setXY( Array(imgPosXbig -470, capY) );

					var CapAni = new Y.Anim({
						node: tmpNode,
						duration: imgSpeed,
						from: {opacity: 0, xy: [-470, capY] },
						to: { opacity: 1, xy: [imgPosXbig + 30, capY] }
					});

					imgAnims[x]['big'].on('end', function() { 
						//alert('big end '+ x +': '+ captList[x] + ' / '+ tmpNode );
						tmpNode.setStyle('opacity', 0);
						tmpNode.setStyle('display', 'block');
						CapAni.run(); 
					});

				
				}
			}
		}
		
		
		

		curnode.setStyle('width', modW +'px' );
		curnode.setStyle('height', modH +'px' );

		curnode.setXY( Array(imgPosX, imgPosY) );
		
		curnode.removeAttribute('title');
		curnode.removeAttribute('alt');
	

		imgAnims[x]['init'] = new Y.Anim({
			node: curnode,
			duration: imgSpeedinit + x/10,
			//startTime: x,
			//reverse: true,
			from: {
				xy: [-100, imgPosY] 	//tryout skewani: ,width: 1
			},
			to: {
				xy: [imgPosX, imgPosY] 	//tryout skewani: ,width: modW
			}
		});
		//imgAnims[x]['init'].run();		//run all at the end

		prevW = modW;
		prevH = modH;

		imgPosX += modW;
		x++;
	}



	var loadhandler = function(){
		if(allready){
			introAni();
		}else{
			allready = true;
		}
	}

	var introAni = function(){
		
	
		x=0;
		while(x<imgAnims.length){
			imgAnims[x]['init'].run();
			x++;
		}

		setTimeout( function() { introdone = true; }, 2000 );
		
	}


	var onImgClick = function(e) {
		//alert('click on '+ e.currentTarget.getAttribute('alt') +' !');
		//e.preventDefault();
	};

	var onImgOver = function(e, id) {
		//alert('over on '+ e.currentTarget.getAttribute('alt') +' !');

		if(focusImg){
			//blurCont();
		}else{
			//focusImg = e.currentTarget;
		}
		//console.log(e);
		//console.log('id:'+ id);
		
		focusCont(id);
		//e.preventDefault();
	};

	var onImgOut = function(e, id) {
		//alert('out on '+ e.currentTarget.getAttribute('alt') +' !');

		blurCont();
		//e.preventDefault();
	};

	
	

	var focusCont = function(id){
		//imgList.setStyle('opacity', 0.3 );
		//focusImg.setStyle('opacity', 1 );


		/*
		x=0;
		while(x<imgAnims.length){
			imgAnims[x]['big'].stop();
			//imgAnims[x]['blur'].run();

			if(txtList[x]){ txtList[x].setStyle('color', '#000000'); }
			x++;
		}
		*/	
	
		//imgAnims[id]['blur'].stop();

		if(introdone){
			imgAnims[id]['big'].run();
			if(txtList[id]){ txtList[id].addClass('conthover'); }
		}
		//if(captList[id]){   }


	}


	var blurCont = function(){
		//alert('blurcont');
		x=0;
		while(x<imgAnims.length){
			imgAnims[x]['big'].stop();
			//imgAnims[x]['blur'].stop();
			imgAnims[x]['normal'].run();
			
			if(txtList[x]){ txtList[x].removeClass('conthover'); }
			if(captList[x]){ captList[x].setStyle('display', 'none'); captList[x].setStyle('left', '-470px'); }
			x++;
		}
		//imgList.setStyle('opacity', 1 );
		focusImg = null;
		
	}

	var getRelatedTxt = function(node){
		if( node.hasClass('news-list-item') || node.hasClass('csc-default') || node.hasClass('listitem') ){
			return true;
		}else{
			return false;
		}
	}

	var getDL = function(node){
		if( node.hasClass('csc-textpic-caption') ){
			return true;
		}else{
			return false;
		}
	}

/*
	var bigImg = function(){
		prevImgW = parseInt( focusImg.getAttribute('width') );
		prevImgH = parseInt( focusImg.getAttribute('height') );
		//alert('curW x curH: '+ curW +'/'+ curH );

		focusImg.setStyle('width', prevImgW +'px' );
		focusImg.setStyle('height', prevImgH +'px' );
	}


	var smallImg = function(){

		focusImg.setStyle('width', prevImgW/2 +'px' );
		focusImg.setStyle('height', prevImgH/2 +'px' );
		//focusImg = null;
		
	}

*/

	
	if(Y.UA.ie != 6){
		Y.on('domready', addClasses, Y);
	}
	Y.on('load', init, window, Y);

});


