;(function($) {

	/*function Field(val){
		var value = val;
		this.__defineGetter__("value", function(){
			return value;
		});
		this.__defineSetter__("value", function(val){
			value = 'a'+val;
		});
	}
	var field = new Field(23);
	field.value = 'bar';
	console.log('field',field,field.value);*/


	// private variables
	var oSettings = {};
	var oGetVars = {};
	var iW = 1024;
	var iH = 768;
	var oAp = Array.prototype;

	var iMenuAnimT = 300;

	function trace() { try {console.log.apply(console, arguments);} catch (e) {} };

	// display objects
	var $Document;
	var $Window;
	var $Body;
	var $MenuTrigger;
	var $Menus;
		var $Menu;
		var $Tags;
	var $Colofon;
	var $PageList;

	// misc
	var bHTML5 = !!document.createElement('canvas').getContext;
	//
	var bIOs = !!navigator.userAgent.match(/iPhone|iPad/i);
	//
	var bTouch = false;
	try { document.createEvent("TouchEvent"); bTouch = true; }catch(err){}
	//
	var bPageTransition = false;
	var iBasePageW = 608;
	var iBasePageH = 399; // is average 16:9 and 4:3
	var iSideMargin = 40;
	var iSideHover = 10;
	var aCurrentPages = [];
	var sCenterCssName = 'center';
	var sCenterElements = 'h2,h3,table.about,img.centerImg,ul.imgcnt,div.info,div.fwasotd';
	var oPrepareToFadeIn = {display:'block',opacity:0};

	// default settings
	$.ronvalstar = {
		 id: "Ron Valstar"
		,version: "4.0.0"
		,defaults: {
			 debug:	true
			,start:	"#home"
		}
		// public functions
	};

	// call
	$.fn.extend({
		ronvalstar: function(_settings) {
			oSettings = $.extend({}, $.ronvalstar.defaults, _settings);
			trace($.ronvalstar.id+' '+$.ronvalstar.version+' ('+oSettings.start+')');
			init();
		}
	});

	// PRIVATE FUNCTIONS

	// init
	function init(){
		getvars();
		if (oGetVars.nojs) return;
		//
		// display objects
		$Document = $(document);
		$Window = $(window);
		$Body = $('body');
		$MenuTrigger = $('#menutrigger');
		$Menus = $('#menus');
		$Menu = $('ul#menu');
		$Tags = $('ul#tagmenu');
		//
		$Body.addClass('js');
		//
		initExternalAnchors();
		initRotator();
		initPageModel();
		initMenus();
		initColofon();
		initEvents();
		//
		showRotator();
	}

	// initExternalAnchors
	function initExternalAnchors(){
		$('a[rel="external"]').attr('target','_blank');
	}

	// initRotator
	var bRotator = false;
	var $Rotator;
	var iRotator = 0;
	function initRotator(){
		$Rotator = $('<div class="rotator">0</div>');
	}
	// rotateRotator
	function rotateRotator(){
		iRotator += 1;
		$Rotator.text(iRotator%10);//+'!@#$"'
		if (bRotator) requestAnimFrame(rotateRotator);
	}
	// showRotator
	function showRotator(show){
		if (show===undefined) show = true;
		if (show!==bRotator) {
			bRotator = show;
			if (show) {
				$Rotator.appendTo($Body)
				rotateRotator();
			} else {
				$Rotator.remove();
			}
		}
	}

	// initPageModel
	var oPages = {
		 toString:	function(){return '[pages '+oPages.length+']';}
		,push:		function(o){oAp.push.apply(oPages,[o])}
		,indexOf:	function(o){oAp.indexOf.apply(oPages,[o])}
		,length:	0
	};
	function page(data) {
		var n = null;
		var o = {
			id:					n
			,title:				n
			,imgs:				n
			,currentImg:		0
			,loaded:			-1
			,tags:				n
			,page:				n
			,w:					iBasePageW
			,h:					iBasePageH
			,nr:				-1
			,infoButton:		$()
			,content:			n
			,centerElements:	n
			,toString:			function(){return '[page '+o.id+']';}
		}
		for (var oDat in data) if (o.hasOwnProperty(oDat)) o[oDat] = data[oDat];
		return o;
	}

	function initPageModel(){
		$PageList = $('ul#pages');
		var $Pages = $PageList.find('>li');
		$Pages.each(function(i,el){
			var $Page = $(el);
			$Page.click(clickPage).hover(hoverPage,hoverPage);
			//
			var $Content = $Page.find('div.content');
			//
			// tags
			var aTags = [];
			$.each($Page.attr('class').split(' '),function(i,s){
				if (['','page'].indexOf(s)===-1) aTags.push(s);
			});
			//
			// todo: maybe build ul.imgcnt from js
			$Page.find('ul.imgcnt>li').click(clickImgCnt);
			//
			// imgs
			var aImgs = [];
			$.each($Page.find('img'),function(i,el){
				var $Img = $(el);
				aImgs.push({
					src: $Img.attr('src')
					,alt: $Img.attr('alt')
				});
				if (i>0) $Img.remove();
			});
			//
			// hasImgs
			var bHasImgs = aImgs.length!==0;
			if (!bHasImgs) {
				$Page.addClass('textonly');
				$Content.show();
			}
			//
			// fwa
			if ($Page.hasClass('fwasotd')) $Page.append('<div class="fwasotd"></div>');
			//
			// info button
			if (bHasImgs&&$Content.text()!=='') {
				var $Info = $('<div class="info"></div>').appendTo($Page).click(function() {
					if ($Content.is(':visible')) {
						$Content.animate({opacity:0},{duration:300,queue:false,complete:function() {
							$Content.hide()
						}});
					} else {
						$Content.show();
						$Content.animate({opacity:1},{duration:300,queue:false});
					}
				}).hover(function(){
					$Info.animate({backgroundColor:'#F0A'},{duration:300,queue:false});
				},function(){
					$Info.animate({backgroundColor:'#599CEE'},{duration:300,queue:false});
				});
			}
			//
			// page object
			var oPage = {
				id:			$Page.attr('id')
				,title:		$Page.find('h2,h3').text()
				,imgs:		aImgs
				,currentImg:0
				,loaded:	bHasImgs?0:-1
				,tags:		aTags
				,page:		$Page
				,w:			iBasePageW
				,h:			bHasImgs?iBasePageH:$Page.height()
				,nr:		-1
				,infoButton:		$Info||$()
				,content:			$Content
				,centerElements:	$Page.find(sCenterElements)
			};
			$Page.attr('id','_'+oPage.id);
			oPages.push(oPage);
			oPages[oPage.id] = oPage;
			oPage.nr = oPages.length-1;
		});
		$Pages.detach();
	}

	// initMenus
	function initMenus() {
		//
		// menu trigger
		$MenuTrigger.click(menuAnim).mouseover(menuAnim);
		//
		// add pages
		var iTopPadding = 0;
		$.each(oPages,function(i,o){
			if (o.page.is('.textonly')) {
				iTopPadding += $('<a href="#'+o.id+'" class="home">'+(o.id=='home'?o.id:o.title)+'</a>').insertBefore($Menu).height();
			}
		});
		// add top padding to two menus ($Menu en $Tags)
		//$.merge($Menu,$Tags).css({paddingTop:iTopPadding});
		$Menu.css({paddingTop:iTopPadding});
		$Tags.css({paddingTop:iTopPadding});
		//
		// create shadows
		var iVPadding = eval(($Menu.css('padding-top')+'+'+$Menu.css('padding-bottom')).replace(/[a-z]/g,''));
		$('<div class="shade"></div>').appendTo($Menus).css({
			left: $Menu.css('left')
		}).width($Menu.width()).height($Menu.height()+iVPadding);
		$('<div class="shade"></div>').appendTo($Menus).css({
			left: $Tags.css('left')
		}).width($Tags.width()).height($Tags.height()+iVPadding);
		//
		// menus
		var iMenusTopPos = -($Menu.height()+getExtraMenuHeight());
		$Menus.css({
			top: iMenusTopPos+'px'
		}).mouseleave(function(){
			menuAnim(false);
		});
		//
		// add project tag-classes to menu li
		for (var i=0;i<oPages.length;i++) {
			var oPage = oPages[i];
			var sTagClasses = oPage.page.attr('class');
			// todo: code better
			$Menu.find('>li:has(a[href=#'+oPage.id+'])').addClass(sTagClasses).removeClass('fwasotd').removeClass('page');
		}
		//
		// regular menu
		$Menu.find('li>a').click(clickHash);
		//
		// tag menu
		$Tags.find('>li').click(function(e){
			var $Li = $(e.currentTarget);
			$Li.toggleClass('select').siblings().removeClass('select');
			$Menu.find('li').removeClass('select');
			var sSelect = '';
			$Tags.find('li.select').each(function(i,el){
				sSelect += '.'+$(el).attr('id');
			});
			$Menu.find('li.'+sSelect).each(function(i,el){
				setTimeout(function(){
					$(el).addClass('select');
				},Math.sqrt(i)*200);
			});
			return false;
		});
		//
		// click body to hide
		$Body.click(function(e){
			if (e.target.nodeName!='A'&&isMenuOpen()) $Menu.mouseleave();
		});
	}

	// initColofon
	function initColofon(){
		$Colofon = $('ul#colofon');
		$Colofon.hover(handleColofonHover,handleColofonHover);
//		$Colofon.find('>li.name').mouseenter(function(){
//			menuAnim(true);
//		});
//		$('<li class="logo"></li>').prependTo($Colofon);//.mouseover(function(){
//			menuAnim(true);
//		});
		// qr
		var $LiQr = $Colofon.find('>li.qr');
		var $Qr = $('<div id="qr"></div>').appendTo($Body);
		$LiQr.hover(function(){
			$Qr.fadeIn('fast');
		},function(){
			$Qr.fadeOut('fast');
		});
	}

	// initEvents
	function initEvents() {
		//
		// keyboard
		$Document.keydown(function(e){
			var iWay = {37:4,38:1,39:2,40:3}[e.keyCode];
			if (iWay) processHash(aCurrentPages[iWay].id);
		});
		//
		// touch and drag
		$Body.mousedown(handleDrag);//.bind('mousewheel',handleMouseWheel);
		$Document.mousemove(handleDrag).mouseup(handleDrag);
		if (bTouch) {
			var mBody = $Body.get(0);
			mBody.ontouchstart = handleDrag;
			mBody.ontouchmove = handleDrag;
			mBody.ontouchend = handleDrag;
		}
		//
		// resize
		handleResize();
		$Window.resize(handleResize);
		//
		// hash change
		if ($Window.hashchange) $Window.hashchange(function(){
			processHash(location.hash);
			return false;
		});
		processHash(location.hash==''?oSettings.start:location.hash);
	}

	//////////////////////////////////////////////////////////////////////////////
	//////////////////////////////////////////////////////////////////////////////
	//////////////////////////////////////////////////////////////////////////////

	// handleColofonHover
	function handleColofonHover(e){
		var iTime = 300;
		var bEnter = e.type=='mouseenter';
		$Colofon.animate({opacity:bEnter?1:.2},{duration:iTime,queue:false});
//		if (bEnter) {
//			var oAnim = {duration:300};
//			setTimeout(function(){
//				$Colofon.find('>li.logo').animate({backgroundColor:'#F0A'},oAnim).animate({backgroundColor:'#000'},oAnim);
//				$Colofon.find('>li.name').animate({color:'#F0A'},oAnim).animate({color:'#000'},oAnim);
//			},iTime);
//		}
	}

	// handleDrag
	var pStart;
	var pLast;
	var bDown = false;
	function handleDrag(e){
		bReturn = true;
		var pNow = eventPos(e);
		switch (e.type) {
			case 'mousedown': case 'touchstart':
				pStart = pNow.clone();
				pLast = pNow.clone();
				bDown = true;
			break;
			case 'mouseup': case 'touchend':
				bDown = false;
				var pDst = pNow.clone().subtract(pStart);
				if (pDst.size()>50) {
					var bHor = Math.abs(pDst.x)>Math.abs(pDst.y);
					var bPos = pDst[bHor?'x':'y']>0;
					aCurrentPages[bHor?(bPos?4:2):(bPos?1:3)].page.click();
					bReturn = false;
				}
			break;
			case 'mousemove': case 'touchmove':
				if (bDown) {
					pLast = pNow.clone();
				}
				bReturn = false;
			break;
		}
		return bReturn;
	}
	// eventPos
	function eventPos(e){
		var oTouch = e.targetTouches?e.targetTouches[0]:e;
		return point(
			 oTouch.pageX
			,oTouch.pageY
		);
	}

	// processHash
	function processHash(hashid){
		var sId = hashid.replace('#','');
		if (sId[0]=='_') sId = sId.substr(1);
		var bIsPage = oPages.hasOwnProperty(sId);
		(bIsPage?gotoPage:showTag)(sId);
		return bIsPage;
	}
	
	// gotoPage
	function gotoPage(id){
		var oPage = oPages[id];
		//
		var iUpDown = Math.round((oPages.length-3)/3);
		var iC = oPage.nr;
		var iL = (oPage.nr-1+oPages.length)%oPages.length;
		var iR = (oPage.nr+1+oPages.length)%oPages.length;
		var iT = (oPage.nr-iUpDown+oPages.length)%oPages.length;
		var iB = (oPage.nr+iUpDown+oPages.length)%oPages.length;
		//
		var aPages = [];
		$.each([iC,iT,iR,iB,iL],function(i,nr){
			aPages.push(oPages[nr]);
		});
		//
		var fnMarkPageLoaded = function(){
			iLoaded--;
			if (iLoaded<=0&&aCurrentPages[0]!==aPages[0]&&!bPageTransition) {
				requestAnimFrame(function(){placePages(aPages)});
			}
		};
		//
		var iLoaded = 0;
		$.each(aPages,function(i,page){
			if (page.loaded===0) {
				iLoaded++;
				var oImg = page.imgs[0];
				document.loadImage(oImg.src,function(loadedImg){
					// grayscale image
					var cnGray = Pixastic.process(loadedImg.canvas,'hsl',{hue:0,saturation:-100,lightness:80});
					page.page.css({
						 width:		loadedImg.w
						,height:	loadedImg.h
					});
					var $Img = page.page.find('img');
					if (cnGray.toDataURL) {
						$Img.attr('src',cnGray.toDataURL("image/jpeg"));
					} else {
						$Img.replaceWith(cnGray);
					}

					// update model
					$.extend(page, {
						loaded: 1
						,w:	loadedImg.w
						,h:	loadedImg.h
						,centerImage: $('<img class="centerImg" />').appendTo(page.page).attr('src',oImg.src).click(clickImg).hide()
					});
					fnMarkPageLoaded();
				});
			}
		});
		if (iLoaded===0) fnMarkPageLoaded();
		else showRotator();

	}

	// showTag
	function showTag(id){
		var $LiTag; // find li (hash id cannot be present in page to prevent scroll-y)
		$Tags.find('>li').each(function(){
			if ($LiTag===undefined) {
				var $Li =  $(this);
				if (id==$Li.text().replace(/\s/,'').toLowerCase()) $LiTag = $Li;
			}
		});
		if ($LiTag!==undefined) {
			var bSelected = $LiTag.hasClass('select');
			var bMenuOpen = isMenuOpen();
			if (!bMenuOpen||bSelected) menuAnim();
			$Menus.find('li').removeClass('select');
			history.go(-1);
			if (!$LiTag.hasClass('select')) {
				setTimeout(function(){
					$LiTag.click();
				},iMenuAnimT);
			}
		}
	}

	function getPos(nr,w,h){ // nr = center top right bottom left
		var o;
		switch(nr){
			case 0: o = {	x:parseInt(iW/2-w/2),	y:parseInt(iH/2-h/2)	}; break;
			case 1: o = {	x:parseInt(iW/2-w/2),	y:iSideMargin-h			}; break;
			case 2: o = {	x:iW-iSideMargin,		y:parseInt(iH/2-h/2)	}; break;
			case 3: o = {	x:parseInt(iW/2-w/2),	y:iH-iSideMargin		}; break;
			case 4: o = {	x:iSideMargin-w,		y:parseInt(iH/2-h/2)	}; break;
		}
		return o;
	}

	// placePages // todo: eliminate $.find ea from method
	function placePages(pages){
		showRotator(false);
		//
		var bFirstPage = aCurrentPages.length===0;
		bPageTransition = true;
		var oPage, i, $RemPages = $(), $NewPages = $();
		// staying pages
		var iBaseXDst = bFirstPage?0:2*(iW-(iW-iBasePageW)/2-iSideMargin);
		var oDst = {x:iBaseXDst,y:0};
		for (i=0;i<aCurrentPages.length;i++) {
			oPage = aCurrentPages[i];
			oPage.from = getPos(i,oPage.w,oPage.h);
			var iIndex = pages.indexOf(oPage);
			if (iIndex!==-1) {
				oPage.to = getPos(iIndex,oPage.w,oPage.h);
				oDst.x = oPage.to.x-oPage.from.x;
				oDst.y = oPage.to.y-oPage.from.y;
			} else {
				oPage.to = null;
			}
		}
		// disappearing pages
		for (i=0;i<aCurrentPages.length;i++) {
			oPage = aCurrentPages[i];
			if (!oPage.to) {
				oPage.to = {
					 x:oPage.from.x+oDst.x
					,y:oPage.from.y+oDst.y
				};
			}
			if (pages.indexOf(oPage)===-1) {
				$RemPages.push(oPage.page[0]);
			}
		}
		// appearing pages
		for (i=0;i<pages.length;i++) {
			oPage = pages[i];
			oPage.page.appendTo($PageList);
			if (aCurrentPages.indexOf(oPage)===-1) {
				oPage.to = getPos(i,oPage.w,oPage.h);
				oPage.from = {
					 x:oPage.to.x-oDst.x
					,y:oPage.to.y-oDst.y
				};
				$NewPages.push(oPage.page[0]);
			}
		}
		$NewPages.css({opacity:1}); // todo: maybe animate
		// old center
		if (!bFirstPage) {
			var oOldCenter = aCurrentPages[0];
			var $OldCenter = oOldCenter.page.removeClass(sCenterCssName);//$PageList.find('.'+sCenterCssName).removeClass(sCenterCssName);
			var $OldCenElm = $OldCenter.find(sCenterElements);//oOldCenter.centerElements;//
		}
		// new center
		var oNewCenter = pages[0];
		var $NewCenter = oNewCenter.page;
		var $NewCntElm = $NewCenter.find(sCenterElements).css(oPrepareToFadeIn);//oNewCenter.centerElements.css(oPrepareToFadeIn);//
		// background
		var fBgPos = .1;
		var aBgPos = $Body.css('background-position').replace(/[%px]]/g,'').split(' ');
		var oBgPos = {x:parseInt(aBgPos[0]),y:parseInt(aBgPos[1])};
		// animate
		var iDuration = 900;
		var bMaxEaseOutBack = false;
		$({f:0}).animate({f:1},{
			duration: iDuration
			,easing: 'linear'
			,step:function(f){
				var fEaseOutBack = $.easing.easeOutBack(f,f*iDuration,0,1,iDuration);
				//
				// current pages
				$.each(aCurrentPages.concat(pages),function(i,page){
					page.page.css({
						 left:		page.from.x + fEaseOutBack*(page.to.x-page.from.x)
						,top:		page.from.y + fEaseOutBack*(page.to.y-page.from.y)
					});
				});
				//
				// old center
				if (!bFirstPage) {
					$OldCenter.css({boxShadow:'0px 0px 16px rgba(0,0,0,'+(.1+f*.4)+')'});
					$OldCenElm.css({opacity:Math.max(0,1-2*f)});
				}
				//
				// new center
				$NewCenter.css({boxShadow:'0px 0px 16px rgba(0,0,0,'+(.1+(f-1)*.4)+')'});
				if (f>.5) $NewCntElm.css({opacity:2*(f-.5)});
				//
				// background
				$Body.css('background-position',parseInt(oBgPos.x+fEaseOutBack*fBgPos*oDst.x)+'px '+parseInt(oBgPos.y+fEaseOutBack*fBgPos*oDst.y)+'px');
				//
				// fEaseOutBack is max(1.1) als f=.6
				if (!bMaxEaseOutBack&&f>=.6) {
					// remove corner pages
					$RemPages.detach();
					// hide text info
					if (!bFirstPage&&oOldCenter.loaded>0) $OldCenter.find('div.content').css({opacity:0}).hide();
					bMaxEaseOutBack = true;
				}
			}
			,complete:function(){
				$NewCenter.addClass(sCenterCssName);
				// old center stuff
				if (!bFirstPage) {
					$OldCenElm.hide();
					oOldCenter.currentImg = 0;
					setImg();
				}
				// transition finished
				location.hash = oNewCenter.id;
				aCurrentPages = pages;
				setMenuItem();
				bPageTransition = false;
				//
				for (var j=0;j<6;j++) oNewCenter.infoButton.animate({backgroundColor:j%2?'#599CEE':'#F0A'});
				//
				if (bFirstPage) firstPage();
			}
		});
	}

	// firstPage
	function firstPage(){
		handleResize();
		setTimeout(function(){$Colofon.mouseenter()},40);
		setTimeout(function(){$Colofon.mouseleave()},1200);
	}

	// setMenuItem
	function setMenuItem(){
		$Menu.find('li').removeClass('selected');
		$Menu.find('li:has(a[href='+location.hash+'])').addClass('selected');
		$Tags.find('li').removeClass('selected');
		var sTagIds = aCurrentPages[0].page.attr('class').replace(/\s/gi,',li#');
		$Tags.find(sTagIds).addClass('selected');
	}

	// menuAnim
	var iExtraMenuHeight;
	function getExtraMenuHeight(){
		if (iExtraMenuHeight===undefined){
			iExtraMenuHeight = 0;
			iExtraMenuHeight += parseInt($Menu.css('padding-top').replace('px',''));
			iExtraMenuHeight += parseInt($Menu.css('padding-bottom').replace('px',''));

			var sCSSShade;
			$.each(['box-shadow','-moz-box-shadow','-webkit-box-shadow'],function(i,s){
				if (!sCSSShade) sCSSShade = $('body.js div#menus div.shade').css(s);
			});
			if (sCSSShade) iExtraMenuHeight += parseInt(sCSSShade.replace(/rgba\(.*\)/g,'').split(' ')[3].replace('px',''))/2;
//			iExtraMenuHeight += parseInt($('body.js div#menus div.shade').css('box-shadow').replace(/rgba\(.*\)/g,'').split(' ')[3].replace('px',''))/2;
		}
		return iExtraMenuHeight;
	}
	function menuAnim(animIn){
		if (animIn===undefined) animIn = !isMenuOpen();
		var iMenusTopPos = -($Menu.height()+getExtraMenuHeight()); // + padding and shade
		if (animIn) $Menus.animate({top:'0px'},{duration:iMenuAnimT,queue:false});
		else		$Menus.animate({top:iMenusTopPos+'px'},{duration:iMenuAnimT,queue:false});
		return animIn;
	}

	// menuShown
	function isMenuOpen(){
		return $Menus.position().top===0
	}

	// repositionPages
	function repositionPages(dffw,dffh){
		$.each(aCurrentPages,function(i,page){
			page.to = getPos(i,page.w,page.h);
			if (!bPageTransition) {
				page.page.css({
					left:	page.to.x
					,top:	page.to.y
				});
			}
			// menus
			if (i===0) {
				var iMenuX = iSideMargin+(page.to.x-iSideMargin)/2;
				var iMenuY = iSideMargin+(iH-2*iSideMargin-iBasePageH)/4;
				$MenuTrigger.css('left',iMenuX+'px');
				$Menus.css('left',Math.max(180,iMenuX)+'px');
//				$Colofon.css({top:(iMenuY)-10+'px'});
				$Colofon.css({top:.6*iMenuY-10+'px'});
//				$Colofon.css({top:.7*iMenuY-10+'px'});
			}
		});
		// background
		var aBgPos = $Body.css('background-position').replace(/[%px]]/g,'').split(' ');
		var oBgPos = {x:parseInt(aBgPos[0]),y:parseInt(aBgPos[1])};
		$Body.css('background-position',parseInt(oBgPos.x+dffw/2)+'px '+parseInt(oBgPos.y+dffh/2)+'px');

	}


	// clickImg
	function clickImg(e){
		var bReturn = true;
		var oPage = getPage($(e.currentTarget).parents('.page').get(0)).pageobj;
		var $Page = oPage.page;
		if ($Page.hasClass(sCenterCssName)) {
			oPage.currentImg = (oPage.currentImg+1)%oPage.imgs.length;
			setImg();
			bReturn = false;
		}
		return bReturn;
	}
	// clickImgCnt
	function clickImgCnt(e){
		var $Li = $(e.currentTarget);
		var oPage = aCurrentPages[0];
		oPage.currentImg = $Li.index();
		setImg();
	}
	// setImg
	function setImg(){
		var oPage = aCurrentPages[0];
		if (oPage.centerImage) {
			var oImg = oPage.imgs[oPage.currentImg];
			if (!bHTML5)	oPage.centerImage.attr('src',oImg.src).attr('alt',oImg.alt);
			else			CIMG.tween(oPage.centerImage.get(0),oImg.src,500,CIMG.transition.square,TWEEN.Easing.Quadratic.EaseOut,{toDataURL:true});
			oPage.page.find('.imgcnt>li').removeClass('current').filter(':eq('+oPage.currentImg+')').addClass('current');
		}
	}


	// clickPage
	function clickPage(e){
		var $Page = $(e.currentTarget);
		if (!$Page.hasClass(sCenterCssName)) {
			processHash($Page.attr('id'));
		}
	}

	// hoverPage
	function hoverPage(e){
		if (!bPageTransition) {
			var oHover = getPage(e.currentTarget);
			if (oHover&&oHover.side!==0) {
				var oPage = oHover.pageobj;
				var iSide = oHover.side;
				var bEnter = e.type=='mouseenter';
				var bTop = iSide%2===1;
				var sPrp = bTop?'top':'left';
				var iVal = iSide%4<2?iSideHover:-iSideHover;
				var oAnim = {};
				oAnim[sPrp] = ((bTop?oPage.to.y:oPage.to.x)+(bEnter?iVal:0))+'px';
				oPage.page.animate(oAnim,{duration:300,easing:'easeInOutQuart',queue:false});
			}
		}
	}

	// getPage
	function getPage(mpage){
		var oReturn;
		for (var i=0;i<aCurrentPages.length;i++) {
			var oPage = aCurrentPages[i];
			if (oPage.page.get(0)===mpage) {
				oReturn = {side:i,pageobj:oPage};
				break;
			}
		}
		return oReturn;
	}

	// clickHash
	function clickHash(e){
		var $A = $(e.currentTarget);
		var sHash = $A.attr('href');
		processHash(sHash);
		$Menu.mouseleave();
		return false;
	}

	// handleResize
	function handleResize(){
		var iNewW = $Window.width();
		var iNewH = $Window.height();
		var iDffW = iNewW - iW;
		var iDffH = iNewH - iH;
		iW = iNewW;
		iH = iNewH;
		repositionPages(iDffW,iDffH,iW,iH);
	}

	// getvars
	function getvars(){
		if (location.href.indexOf('?')!==-1) {
			var aGetVars = location.href.split('?').pop().replace(location.hash,'').split('&');
			$.each(aGetVars,function(i,el){
				var aVar = el.split('=');
				oGetVars[aVar.shift()] = aVar.length?aVar.join('').toType():true;
			});
		}
	}

})(jQuery);


// used functions: easeOutBack, easeInOutQuart
jQuery.extend( jQuery.easing, {
	def: 'linear',
	/*swing: function (x, t, b, c, d) {
		//alert(jQuery.easing.default);
		return jQuery.easing[jQuery.easing.def](x, t, b, c, d);
	},
	easeInQuad: function (x, t, b, c, d) {
		return c*(t/=d)*t + b;
	},
	easeOutQuad: function (x, t, b, c, d) {
		return -c *(t/=d)*(t-2) + b;
	},
	easeInOutQuad: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t + b;
		return -c/2 * ((--t)*(t-2) - 1) + b;
	},
	easeInCubic: function (x, t, b, c, d) {
		return c*(t/=d)*t*t + b;
	},
	easeOutCubic: function (x, t, b, c, d) {
		return c*((t=t/d-1)*t*t + 1) + b;
	},
	easeInOutCubic: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t*t + b;
		return c/2*((t-=2)*t*t + 2) + b;
	},
	easeInQuart: function (x, t, b, c, d) {
		return c*(t/=d)*t*t*t + b;
	},
	easeOutQuart: function (x, t, b, c, d) {
		return -c * ((t=t/d-1)*t*t*t - 1) + b;
	},*/
	easeInOutQuart: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t*t*t + b;
		return -c/2 * ((t-=2)*t*t*t - 2) + b;
	},
	/*easeInQuint: function (x, t, b, c, d) {
		return c*(t/=d)*t*t*t*t + b;
	},
	easeOutQuint: function (x, t, b, c, d) {
		return c*((t=t/d-1)*t*t*t*t + 1) + b;
	},
	easeInOutQuint: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t*t*t*t + b;
		return c/2*((t-=2)*t*t*t*t + 2) + b;
	},
	easeInSine: function (x, t, b, c, d) {
		return -c * Math.cos(t/d * (Math.PI/2)) + c + b;
	},
	easeOutSine: function (x, t, b, c, d) {
		return c * Math.sin(t/d * (Math.PI/2)) + b;
	},
	easeInOutSine: function (x, t, b, c, d) {
		return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;
	},
	easeInExpo: function (x, t, b, c, d) {
		return (t==0) ? b : c * Math.pow(2, 10 * (t/d - 1)) + b;
	},
	easeOutExpo: function (x, t, b, c, d) {
		return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
	},
	easeInOutExpo: function (x, t, b, c, d) {
		if (t==0) return b;
		if (t==d) return b+c;
		if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
		return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
	},
	easeInCirc: function (x, t, b, c, d) {
		return -c * (Math.sqrt(1 - (t/=d)*t) - 1) + b;
	},
	easeOutCirc: function (x, t, b, c, d) {
		return c * Math.sqrt(1 - (t=t/d-1)*t) + b;
	},
	easeInOutCirc: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return -c/2 * (Math.sqrt(1 - t*t) - 1) + b;
		return c/2 * (Math.sqrt(1 - (t-=2)*t) + 1) + b;
	},
	easeInElastic: function (x, t, b, c, d) {
		var s=1.70158, p=0, a=c;
		if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
		if (a < Math.abs(c)) { a=c; s=p/4; }
		else s = p/(2*Math.PI) * Math.asin (c/a);
		return -(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
	},
	easeOutElastic: function (x, t, b, c, d) {
		var s=1.70158, p=0, a=c;
		if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
		if (a < Math.abs(c)) { a=c; s=p/4; }
		else s = p/(2*Math.PI) * Math.asin (c/a);
		return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;
	},
	easeInOutElastic: function (x, t, b, c, d) {
		var s=1.70158, p=0, a=c;
		if (t==0) return b;  if ((t/=d/2)==2) return b+c;  if (!p) p=d*(.3*1.5);
		if (a < Math.abs(c)) { a=c; s=p/4; }
		else s = p/(2*Math.PI) * Math.asin (c/a);
		if (t < 1) return -.5*(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
		return a*Math.pow(2,-10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )*.5 + c + b;
	},
	easeInBack: function (x, t, b, c, d, s) {
		if (s == undefined) s = 1.70158;
		return c*(t/=d)*t*((s+1)*t - s) + b;
	},*/
	easeOutBack: function (x, t, b, c, d, s) {
		if (s == undefined) s = 1.70158;
		return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;
	}/*,
	easeInOutBack: function (x, t, b, c, d, s) {
		if (s == undefined) s = 1.70158;
		if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b;
		return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b;
	},
	easeInBounce: function (x, t, b, c, d) {
		return c - jQuery.easing.easeOutBounce (x, d-t, 0, c, d) + b;
	},
	easeOutBounce: function (x, t, b, c, d) {
		if ((t/=d) < (1/2.75)) {
			return c*(7.5625*t*t) + b;
		} else if (t < (2/2.75)) {
			return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
		} else if (t < (2.5/2.75)) {
			return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
		} else {
			return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
		}
	},
	easeInOutBounce: function (x, t, b, c, d) {
		if (t < d/2) return jQuery.easing.easeInBounce (x, t*2, 0, c, d) * .5 + b;
		return jQuery.easing.easeOutBounce (x, t*2-d, 0, c, d) * .5 + c*.5 + b;
	}*/
});



