var animation = function(){
					var news = $('update-news');
					news.getFirst().fade('hide');
					//news.getFirst().setStyle('opacity','0');
					news.getFirst().inject(news);
					news.getLast().fade('in', {duration: 'long'});
					//news.getLast().setStyle('opacity','100');
				};
				
var slideshow = function(){
	this.getFirst('.slidebox').fade('hide');
	//news.getFirst().setStyle('opacity','0');
	this.getFirst('.slidebox').inject(this);
	this.getLast().fade('in', {duration: 'long'});
	//news.getLast().setStyle('opacity','100');
}

function initmnu(){
	//var bigBG = new Fx.Morph('bigbg', {duration:2000, link:'chain', unit:'%'});
	var bigBG = $('bigbg').set('morph', {fps:120, duration:4000, link:'chain', unit:'%'});
	
	var divWrap = $('wrap');
	var mnuHome = $('mnuhome').set('morph', {fps:120, duration:4000, unit:'%', transition:Fx.Transitions.Quad.easeOut});
	var mnuAbou = $('mnuabou');
	var mnuCont = $('mnucont');
	var mnuWork = $('mnuwork');
	var mnuBlog = $('mnublog');

	var homeM = new Fx.Morph('mnuhome', {fps:120, duration:4000, link:'chain', unit:'px'});
	//var homeC = new Fx.Morph('mnuhome', {fps:120, duration:4000, link:'chain', unit:'%'});
	var logoanim = new Fx.Tween('logo', {fps:120, duration:4000, link:'chain', unit:'px'});
	var bgMethod = new Fx.Tween(document.body, {fps:120, duration:4000, link:'chain', unit:'px', transition:Fx.Transitions.Quad.easeOut});
	var cornerfx = new Fx.Morph('corner', {fps:120, duration:4500, link:'chain', unit:'px', transition:Fx.Transitions.Quad.easeOut});
	
	var wrapFx = new Fx.Morph(divWrap, {fps:120, duration:4000, link:'chain', unit:'%', transition:Fx.Transitions.Quad.easeOut});
	
	var docHeight = document.getSize().y;
	var docWidth = document.getSize().x;
	
	
	var bgTop = bigBG.getStyle('top');
	var bgLeft = bigBG.getStyle('left');
	if(bgTop.contains('%')){
	}else{
		topPr = (bgTop.toInt() / docHeight) * 100;
		bigBG.setStyle('top', topPr+'%');
		leftPr = (bgLeft.toInt() / docWidth) * 100;
		bigBG.setStyle('left', leftPr+'%');
	}
	
	var wrapTop = divWrap.getStyle('top');
	//alert(wrapTop);
	var wrapLeft = divWrap.getStyle('left');
	if(wrapTop.contains('%')){
	}else{
		dtopPr = (wrapTop.toInt() / docHeight) * 100;
		//alert(divWrap.getStyle('top').toInt() + '////' +docHeight);
		divWrap.setStyle('top', dtopPr+'%');
		dleftPr = (wrapLeft.toInt() / docWidth) * 100;
		divWrap.setStyle('left', dleftPr+'%');
	}
	
	//divWrap.setStyle('top',wrapT);
	//divWrap.setStyle('left',wrapL);
	
	var mTop = mnuHome.getStyle('top');
	var mLeft = mnuHome.getStyle('left');
	if (mTop.contains('%')) {
	}else{
		topPrc = (mTop.toInt() / docHeight) * 100;
		mnuHome.setStyle('top', topPrc+'%');
		leftPrc = (mLeft.toInt() / docWidth) * 100;
		mnuHome.setStyle('left', leftPrc+'%');
	}
	
	var curPage = document.getElement('body').get('id');
	//alert(mnuHome.getStyle('top') + '--' + document.getSize().y);

	mnuHome.addEvent('click',function(event){
		if (curPage != 'bodyhome') {
			curPage = 'bodyhome';
			//mnuHome.setStyle('margin-left','-30px');
			//mnuHome.setStyle('margin-top','-12px');
			mnuHome.set('class','mainmnu homehome');
			homeM.start('.homehome');
			mnuHome.morph('#bodyhome #mnuhome');
			mnuWork.setStyle('opacity', '.4');
		
			wrapFx.start('#bodyhome #wrap');
			logoanim.start('width',146);
			bgMethod.start('background-position','100px -100px');
			document.getElement('.curmenu').removeClass('curmenu');
			mnuHome.addClass('curmenu');
			
			bigBG.morph('.bghome');
			
			cornerfx.start({
				opacity: '0'
			});
		}
		event.preventDefault();
	});
	
	mnuAbou.setStyle('opacity','.4');
	mnuAbou.addEvent('mouseenter',function(){
		if (curPage != 'bodyabout') {
			mnuAbou.tween('opacity', '1');
		}
	});
	mnuAbou.addEvent('mouseout',function(){
		if (curPage != 'bodyabout') {
			mnuAbou.tween('opacity', '.4');
		}
	});
	mnuAbou.addEvent('click',function(event){
		if (curPage != 'bodyabout') {
			document.getElement('.curmenu').removeClass('curmenu');
			mnuAbou.addClass('curmenu');
			
			curPage = 'bodyabout';
			//mnuHome.setStyle('margin-left','-60px');
			//mnuHome.setStyle('margin-top','0px');
			mnuHome.set('class','mainmnu homeabou');
			mnuAbou.tween('opacity', '.4');
			mnuWork.setStyle('opacity', '.4');
			
			mnuHome.morph('#bodyabout #mnuhome');
			homeM.start('.homeabou');
			logoanim.start('width',60);
			bgMethod.start('background-position','200px -186px');
			
			wrapFx.start('#bodyabout #wrap');
			bigBG.morph('.bgabou');
			
			$('corner').setStyles({'top': '0px', 'bottom' : 'auto' , 'right': '0px', 'left' : 'auto' , 'margin' : '-260px -260px 0 0', 'display' : 'block'});
			cornerfx.start({'opacity':['0','1'], margin: '-130px -130px 0 0'});
		}
		event.preventDefault();
	});
	
	mnuBlog.addEvent('click',function(event){
		if (curPage != 'blogpage') {
			document.getElement('.curmenu').removeClass('curmenu');
			mnuBlog.addClass('curmenu');
			
			curPage = 'blogpage';
			//mnuHome.setStyle('margin-left','0px');
			//mnuHome.setStyle('margin-top','-12px');
			mnuHome.set('class','mainmnu homecont');
			homeM.start('.homeblog');
			mnuHome.morph('#blogpage #mnuhome');
			logoanim.start('width',60);
			bgMethod.start('background-position','0px -100px');
			
			wrapFx.start('#blogpage #wrap');
			bigBG.morph('.bgblog');
			//return false;
		}
		event.preventDefault();
	});
	
	if (curPage != 'bodyworks'	) {
		mnuWork.setStyle('opacity', '.4');
	}
	mnuWork.addEvent('mouseenter',function(){
		if (curPage != 'bodyworks'	) {
			mnuWork.tween('opacity', '1');
		}
	});
	mnuWork.addEvent('mouseout',function(){
		if (curPage != 'bodyworks'	) {
			mnuWork.tween('opacity', '.4');
		}
	});
	mnuWork.addEvent('click',function(event){
		if (curPage != 'bodyworks') {
			document.getElement('.curmenu').removeClass('curmenu');
			mnuWork.addClass('curmenu');
			
			curPage = 'bodyworks';
			//mnuHome.setStyle('margin-left','-30px');
			//mnuHome.setStyle('margin-top','-24px');
			mnuHome.set('class','mainmnu homework');
			//mnuWork.tween('opacity', '.4');
			
			homeM.start('.homework');
			mnuHome.morph('#bodyworks #mnuhome');
			logoanim.start('width',60);
			bgMethod.start('background-position','100px 0px');
			
			wrapFx.start('#bodyworks #wrap');
			bigBG.morph('.bgwork');
			
			$('corner').setStyles({'top': 'auto', 'bottom' : '0px' , 'right': 'auto', 'left' : '50%' , 'margin' : '0 0 -260px -130px', 'display' : 'block'});
			cornerfx.start({'opacity':['0','1'], margin: '0 0 -130px -130px'});
		}
		event.preventDefault();
	});
	
	mnuCont.setStyle('opacity','.4');
	mnuCont.addEvent('mouseenter',function(){
		if (curPage != 'bodycontact') {
			mnuCont.tween('opacity', '1');
		}
	});
	mnuCont.addEvent('mouseout',function(){
		if (curPage != 'bodycontact') {
			mnuCont.tween('opacity', '.4');
		}
	});
	mnuCont.addEvent('click',function(event){
		if (curPage != 'bodycontact') {
			document.getElement('.curmenu').removeClass('curmenu');
			mnuCont.addClass('curmenu');
			
			curPage = 'bodycontact';
			//mnuHome.setStyle('margin-left','0px');
			//mnuHome.setStyle('margin-top','0px');
			mnuHome.set('class','mainmnu homeblog');
			mnuCont.tween('opacity', '.4');
			mnuWork.setStyle('opacity', '.4');
			
			homeM.start('.homecont');
			mnuHome.morph('#bodycontact #mnuhome');
			logoanim.start('width',60);
			bgMethod.start('background-position','0px -186px');
			
			wrapFx.start('#bodycontact #wrap');
			bigBG.morph('.bgcont');
			
			$('corner').setStyles({'top' : '0px' , 'bottom' : 'auto' , 'right': 'auto', 'left' : '0px' , 'margin' : '-260px 0 0 -260px', 'display' : 'block'});
			cornerfx.start({'opacity':['0','1'], margin: '-130px 0 0 -130px'});
		}
		event.preventDefault();
	});
	
	$('works').set('load', {onSuccess: function(){
		initSlide();
	}});
	//$('wrap').morph({top:'400%'});
	//alert(divWrap.getStyle('top') + '  ' + divWrap.getStyle('height'));
}

function initSlideItem(item){
	
	item.set('morph', {duration: 300,unit: 'px'});
	//alert(item.getStyle('width'));
	
	var slideshower;
	
	item.addEvent('mouseenter',function(){
		if (item.getStyle('top').toInt() == 0) {
			//alert(item.getSize().x)
			
			//var weblink = item.getElement('a');
			//var webdesc = item.getElement('p');
			
			$$('#workdesc li').setStyle('z-index', '0');
			item.setStyle('z-index', '100');
			//item.setStyle('width','33%');
			//alert(item.getStyle('width'));
			item.morph({
				width: '290',
				marginTop: '-5',
				marginLeft: '-6'
			});
			//item.set('tween', {duration: 300, unit: '%'}).tween('width',[33,35]);
			item.getElement('a').fade('.9');
			item.getElement('p').fade('in');
			slideshower = slideshow.periodical(1500, item);
		}
		

	});
	item.addEvent('mouseleave',function(){
		//item.set('tween', {duration: 300, unit: '%'}).tween('width',[35,33]);
		item.morph({width: '276', marginTop: '0', marginLeft: '0'});
		$clear(slideshower);
		item.setStyle('z-index','1');
		item.getElement('a').fade('out');
		item.getElement('p').fade('out');
	});
}

function initSlide(){
	if($chk($('workdesc'))){
		
		
		$$('.btnslide').each(function(item){
			
			item.setStyle('opacity','.4');
			item.addEvent('mouseenter',function(){
				item.tween('opacity','1');
			});
			item.addEvent('mouseout',function(){
				item.tween('opacity','.4');
			});
			
		});
		
		var liz = $$('.slide');
		var maxItem = 3;
		
		//var itemW = $('workdesc').getElement('li').getSize().x;
		//var itemLeft = $('workdesc').getElement('li').getStyle('left');
		//topPrt = (bgTop.toInt() / docHeight) * 100;
		//bigBG.setStyle('top', topPrt+'%');
		//widthPrt = (itemW.toInt() / $('workdesc').getSize().x.toInt()) * 100;
		
		$('slidey').store('direction', 'next');
		$('slidey').store('curIdx', 1);
		$('slidey').store('numItems', maxItem);
		$('slidey').store('allowClick', true);
		var numTemp = $('slidetemp').getLast().get('id').substr(5).toInt();
		
		
		var dele = 200;
		liz.each(function(item, index){
			//item.removeEvents();
			dele = dele+200;
			//newli.inject(item,'after').addClass('slidebox');
			item.set('tween', {duration: dele });
			//el.morph({height: 100, width: 100});
			//dele = dele +200;
			item.get('tween').addEvents({
				'start': function(){
					item.morph({opacity: 0.2});
				},
				'complete': function() {
					
					var curIdx = $('slidey').retrieve('curIdx');
					itemId = item.get('id').substr(5).toInt();
				
					
					//because curIdx is the ID of the first slide, nextId => +maxItem
					
					nextId = curIdx + itemId - 1;
					
					if(nextId > numTemp){
						nextId = nextId - numTemp;
					}
					var newli = $('folio'+ nextId);
					item.set('html', newli.get('html'));
					//newli.inject($('slidetemp'),'bottom');
				
					
					item.morph({top: ['30px','0px'], opacity : 1});
					//alert(index+'done');
					if(itemId == maxItem){
						$('slidey').store('allowClick', true);
						//$('slidey').store('curIdx', $('slide1').get('id').substr(5).toInt());
					}
	   			 }
			});
			//item.tween('top', '-30px');
			
			
			//item.inject($('slidetemp'),'top');
			initSlideItem(item);
		});
		
	}
}

function getbatch(dir){
	if($('slidey').retrieve('allowClick') == true){
		$('slidey').store('direction', dir);
		$('slidey').store('allowClick', false);
		
		curIdx = $('slidey').retrieve('curIdx');
		numItems = $('slidey').retrieve('numItems');
		numTemp = $('slidetemp').getLast().get('id').substr(5).toInt();
		startId = 1;
		
		if(dir == 'next'){
			startId = curIdx + numItems;
			if(startId > numTemp){
				startId = startId - numTemp;
			}
		}else{
			startId = curIdx - numItems;
			if(startId < 1){
				startId = numTemp + startId;
			}
		}
		
		$('slidey').store('curIdx', startId);
		
		//alert(startId + '----' + numTemp);
		
		$$('#workdesc li').each(function(item, index){	
			item.tween('top', '-30px');
		});
	}else{
		alert('wewek');
	}
}

function initFont(){
	//alert (document.body.getSize().y);
	var dw = document.body.getSize().y;
	if(dw<500){
		document.body.setStyle('font-size','.6em');
	}else if(dw<600){
		document.body.setStyle('font-size','.7em');
	}else if(dw<700){
		document.body.setStyle('font-size','.8em');
	}else if(dw<800){
		document.body.setStyle('font-size','.9em');
	}else{
		document.body.setStyle('font-size','1em');
	}
	
}

window.addEvent('domready', function(){
    //alert('JS ON');
	initmnu();
	//initFont();
	//initSlide();
	
	//alert(document.getElement('.curmenu').innerHTML);
	
	var sc = false;
	document.addEvent('keydown', function(event){
		
	    //The passed event parameter is already an instance of the Event class.
	    //alert(event.key);   //Returns the lowercase letter pressed.
	    //alert(event.shift); //Returns true if the key pressed is shift.
	    if (event.key == 'space') event.preventDefault(); //Executes if the user hits Ctr+S.
	});
});

window.addEvent('resize', function(){
    //alert('JS ON');
	//initFont();
	//initmnu();
	//initSlide();
});
