$(document).ready(function() {

	var pages=[
		new page('intro_cover',675,900,'4c4c4c'),
		new page('intro_bio',900,675,'4c4c4c'),
		new page('intro_biblio',675,900,'4c4c4c'),

		new page('alkaseltzer',614,900,'006484'),
		new page('americanapparel',675,900,'00e376'),
		new page('escota',780,780,'3f4041'),
		
		new page('skema_cdc',900,600,'3c738b'),
		new page('skema_cdf',900,600,'f8452b'),
		new page('skema_ccm',900,600,'006a88'),
		new page('skema_cwp',900,600,'004077'),
		new page('skema_cwm',900,600,'5a573f'),
		new page('skema_cwl',900,600,'66615a'),
		new page('skema_cws',900,600,'39565b'),
		new page('skema_cwc',900,600,'606957'),
		//new page('skema_cwv',900,600,'4a423e'),
		new page('skema_cbr',636,900,'7a857c'),
		new page('skema_ip',900,675,'818181'),
		new page('skema_ipb',900,675,'932a04'),
		new page('skema_ish',900,741,'ff00a2'),
		new page('skema_isp',900,741,'ff00a2'),
		//new page('skema_s1b',844,720,'ff00a2'),

		new page('ceram_icb',900,602,'ff1495'),
		new page('ceram_ice',900,640,'283f5c'),
		new page('ceram_ccg',636,900,'525437'),
		new page('ceram_cgh',636,900,'2c3b4b'),
		new page('ceram_cec',636,900,'53626b'),
		new page('ceram_cmr',636,900,'a6a5a5'),
		new page('ceram_cbq',634,900,'010104'),
		
		new page('sephora_cwp',675,900,'00deff'),
		new page('sephora_cwr',675,900,'5f0000'),
		new page('sephora_cwc',675,900,'00f08d'),

		new page('merc_il',900,675,'7e00c2'),
		new page('merc_cd',900,675,'7001a7'),
		new page('palmolive',900,675,'e10097'),
		new page('apr',900,675,'4f0200'),
		new page('gazelle_cl',900,675,'e10003'),
		
		new page('sacla_ib',675,900,'e87800'),
		new page('sacla_ipa',675,900,'6c0043'),
		new page('sacla_ipp',675,900,'e10000'),
		new page('sacla_iwb',780,780,'f26d00'),
		new page('sacla_iwj',900,675,'e9000a'),
		new page('sacla_iwr',900,675,'e9000a'),
		//new page('sacla_iws',780,780,'e60004'),
		//new page('sacla_iwj',780,780,'e30613'),
		
		new page('mane_cc',675,900,'74878e'),
		new page('mane_cp',675,900,'7220a1'),
		new page('mane_isl',900,675,'02c4f2'),
		new page('mane_isf',900,675,'f631bd'),
		//new page('mane_ip',900,675,'77d900'),

		new page('espalu_il',900,675,'1b1c20'),
		new page('espalu_if',900,675,'dc0004'),
		//new page('espalu_ipe',900,675,'d60004'),
		new page('espalu_ipb',900,675,'ee000f'),
		new page('espalu_ipc',900,675,'130a0a'),
		//new page('espalu_ipp',900,675,'240bbd'),

		new page('molinard_ic',900,675,'3f220b'),
		new page('molinard_cc',900,675,'68deff'),
		//new page('molinard_pic',608,900,'4b4b4b'),

		//new page('topsol',614,900,'ffe600'),

		new page('boregart_dg',675,900,'ff4aed'),
		//new page('boregart_dv',675,900,'4dc900'),
		new page('boregart_db',675,900,'d4f500'),
		new page('boregart_dr',675,900,'e50000'),

		new page('urw_meca',900,675,'00deff'),
		new page('urw_scri',675,900,'00ff00'),
		//new page('urw_scrn',675,900,'ff00d4'),

		//new page('3m1',656,900,'868d8f'),
		//new page('3m2',656,900,'868d8f'),
		//new page('3m3',656,900,'446273'),
		//new page('3m4',900,675,'868d8f'),

		//new page('alutech_cxa',900,675,'522c1e'),
		new page('alutech_cxv',900,675,'00bbdd'),
		new page('alutech_cxb',900,675,'5f5f68'),
		new page('alutech_cbf',900,675,'ff0000'),
		//new page('alutech_cbb',900,675,'ff0000'),
		new page('alutech_ipc',900,675,'1d486b'),
		new page('alutech_ipi',900,675,'00ea00'),

		//new page('topfit_ccz',900,675,'203400'),
		//new page('topfit_ccm',900,675,'01acb3'),

		//new page('arles_cic',636,900,'fb926d'),
		//new page('arles_cie',636,900,'c12f2f'),
		//new page('arles_cir',636,900,'cdc6b4'),

		//new page('host',900,675,'0e0e0e'),

		//new page('wak',900,675,'78f900')
	];
	var imgDir='img/';
	var slidePrefix='pepkarsten_';
	var slideExt='.jpg';
	var homePageNumber=1;
	var fadeTime=250;
	var fadeEasing='swing';
	var slideTime=250;
	var slideEasing='swing';

	function page(slide,width,height,color) {
		this.slide=slide;
		this.width=width;
		this.height=height;
		this.color=color;
	}	
	function displayPage(n) {
		var $slide=$('#slide');
		var $slideimg=$slide.find('img');
		var $homeimg=$('#home img');
		var $pagenumber=$('#pagenumber');
		var $body=$('body');
		var availableHeight=$(window).height()-parseInt($body.css('marginTop'))-parseInt($body.css('marginBottom'));
		var slideHeight=(availableHeight<pages[n-1].height&&$slideimg.hasClass('resized'))?availableHeight:pages[n-1].height;
		var slideWidth=Math.round(slideHeight*pages[n-1].width/pages[n-1].height);
		$slide.addClass('loading');
		$slideimg.hide();
		$slide.css({width:slideWidth,height:slideHeight});
		$homeimg.css({backgroundColor:'#a8a8a8'});
		$slideimg
			.attr('src','')
			.one('load',function() {
				$slide.removeClass('loading');
				$slideimg.fadeIn(fadeTime,fadeEasing);
				$slide.css({width:'',height:''});
				$homeimg.stop(true).animate({backgroundColor:'#'+pages[n-1].color},fadeTime,fadeEasing);
				$.preloadImg(imgDir+slidePrefix+pages[n-1].slide+slideExt);
				if(n<pages.length) $.preloadImg(imgDir+slidePrefix+pages[n].slide+slideExt);
				if(n>1) $.preloadImg(imgDir+slidePrefix+pages[n-2].slide+slideExt);
			})
			.attr('src',imgDir+slidePrefix+pages[n-1].slide+slideExt);
		n==pages.length?
			$('#next a').hide():
			$('#next a').show();
		n==1?
			$('#prev a').hide():
			$('#prev a').show();
		$pagenumber.text(n+'/'+pages.length);
		Cufon.replace($pagenumber);
	}

	$(window).hashchange(function() {
		if(location.hash.match(/^#\d{1,3}$/)) {
			var pn=(+location.hash.substr(1));
			(pn>0&&pn<=pages.length)? displayPage(pn): location.hash=homePageNumber;
		}
		else location.hash=homePageNumber;
	});
	$(window).hashchange();
	$.autoMouseOver();
	$.blurAllLinks();
	$('#home a')
		.onclick(function() {(+location.hash.substr(1)==homePageNumber)? displayPage(homePageNumber): location.hash=homePageNumber})
		.shortcut('up')
		.hover(
			function() {$('#tip-home').stop(true,true).slideDown(slideTime,slideEasing)},
			function() {$('#tip-home').hide()});
	$('#next a')
		.onclick(function() {location.hash=(+location.hash.substr(1))+1})
		.shortcut('right')
		.hover(
			function() {$('#tip-next').stop(true,true).slideDown(slideTime,slideEasing)},
			function() {$('#tip-next').hide()});
	$('#prev a')
		.onclick(function() {location.hash=(+location.hash.substr(1))-1})
		.shortcut('left')
		.hover(
			function() {$('#tip-prev').stop(true,true).slideDown(slideTime,slideEasing)},
			function() {$('#tip-prev').hide()});
	$('#contact a')
		.onclickEmail('info','pepkarsten.com')
		.hover(
			function() {$('#tip-contact').stop(true,true).slideDown(slideTime,slideEasing)},
			function() {$('#tip-contact').hide()});
	$('#download a')
		.onclickOpen('http://download.pepkarsten.com/pepkarsten_artdirection.pdf')
		.hover(
			function() {$('#tip-download').stop(true,true).slideDown(slideTime,slideEasing)},
			function() {$('#tip-download').hide()});
	$('#linkedin a')
		.onclickOpen('http://www.linkedin.com/in/pepkarsten')
		.hover(
			function() {$('#tip-linkedin').stop(true,true).slideDown(slideTime,slideEasing)},
			function() {$('#tip-linkedin').hide()});
	$('').shortcut('down');
	$('#slide img').resizer();
});



// JQUERY PKTOOLS PLUG-IN V1.0 - COPYRIGHT 2010 PEP KARSTEN

(function($){
	imgCache=new Array();
	$.preloadImg=function() {
		for(var i=0; i<arguments.length; i++) {
			var img=new Image();
			img.src=arguments[i];
			imgCache[img.src]=img;
		}
	}
	$.autoMouseOver=function(outStr,overStr) {
		var outStr=(typeof outStr=='undefined')?'-out.':outStr;
		var overStr=(typeof overStr=='undefined')?'-over.':overStr;
		$("img[src*='"+outStr+"']")
			.each(function() {$.preloadImg(this.src.replace(outStr,overStr))})
			.hover(
				function() {this.src=this.src.replace(outStr,overStr)},
				function() {this.src=this.src.replace(overStr,outStr)}
			);
	}
	$.blurAllLinks=function() {
		$('a').focusin(function() {
			this.blur();
		});
	}
	$.address=function(u,d) {
		return u+'@'+d;
	}
	$.fn.onclick=function(f) {
		this.click(function() {
			f();
			return false;
		});
		return this;
	}
	$.fn.onclickOpen=function(l) {
		this.onclick(function() {
			open(l);
		});
		return this;
	}
	$.fn.onclickEmail=function(u,d,s,b) {
		var l='mailto:'+$.address(u,d);
		if(s||b) {
			l+='?';
			if(s) {
				l+='subject='+s;
				if(b) l+='&';
			}
			if(b) l+='body='+b;
		}
		this.onclickOpen(l);
		return this;
	}
	$.fn.shortcut=function(k) {
		var code={left:37,up:38,right:39,down:40};
		var $this=this;
		$(document).keydown(function(e) {
			if(e.keyCode==code[k]) {
				$this.filter(':visible').click();
				return false;
			}
		});
		window.focus();
		return this;
	}
	$.extend($.easing, {
		swing2: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;
		},
		swing3: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;
		},
		swing4: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;
		},
		swing5: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;
		},
		swingx: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;
		},
		swingc: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;
		}
	});
})(jQuery);



// JQUERY PKRESIZER PLUG-IN V1.0 - COPYRIGHT 2010 PEP KARSTEN

(function($){
	$.fn.resizer=function(settings) {
		var s=$.extend({
			defaultResize:false,
			thresh:24,
			animSpeed:400,
			animEasing:'swing', //'swing4',
			curZoomIn:'url(img/zoom-in.gif) 6 6,url(img/zoom-in.cur),pointer',
			curZoomOut:'url(img/zoom-out.gif) 6 6,url(img/zoom-out.cur),pointer',
			container:$('body')
		},settings);
		var $this=this;
		var preloadCursor=(s.defaultResize)?s.curZoomOut:s.curZoomIn;
		$this.css({cursor:preloadCursor});
		if(s.defaultResize) $this.addClass('resized');
		$(window).resize(function() {
			var originalHeight=$.extend({},$this).height('').attr('height');
			var availableHeight=$(this).height()-parseInt(s.container.css('marginTop'))-parseInt(s.container.css('marginBottom'))-parseInt(s.container.css('paddingTop'))-parseInt(s.container.css('paddingBottom'));
			var duration=Math.round(Math.sqrt((originalHeight-availableHeight)*100000/s.animSpeed));
			if(availableHeight<originalHeight-s.thresh) {
				$this.hasClass('resized')?
					$this.css({height:availableHeight,cursor:s.curZoomIn}):
					$this.css({height:originalHeight,cursor:s.curZoomOut});
				$this.unbind('click').onclick(function() {
					$this.hasClass('resized')?
						$this
							.stop(true).animate({height:originalHeight},duration,s.animEasing)
							.css({cursor:s.curZoomOut})
							.removeClass('resized'):
						$this
							.stop(true).animate({height:availableHeight},duration,s.animEasing)
							.css({cursor:s.curZoomIn})
							.addClass('resized');
				});
			}
			else $this
				.height('')
				.unbind('click')
				.css({cursor:''});
		});
		$this.load(function() {if($this.attr('src')!='') $(window).resize()});
		$(window).resize();
		return this;
	}
})(jQuery);



// JQUERY HASHCHANGE PLUG-IN V1.3 - COPYRIGHT 2010 BEN ALMAN

(function($,e,b){var c="hashchange",h=document,f,g=$.event.special,i=h.documentMode,d="on"+c in e&&(i===b||i>7);function a(j){j=j||location.href;return"#"+j.replace(/^[^#]*#?(.*)$/,"$1")}$.fn[c]=function(j){return j?this.bind(c,j):this.trigger(c)};$.fn[c].delay=50;g[c]=$.extend(g[c],{setup:function(){if(d){return false}$(f.start)},teardown:function(){if(d){return false}$(f.stop)}});f=(function(){var j={},p,m=a(),k=function(q){return q},l=k,o=k;j.start=function(){p||n()};j.stop=function(){p&&clearTimeout(p);p=b};function n(){var r=a(),q=o(m);if(r!==m){l(m=r,q);$(e).trigger(c)}else{if(q!==m){location.href=location.href.replace(/#.*/,"")+q}}p=setTimeout(n,$.fn[c].delay)}$.browser.msie&&!d&&(function(){var q,r;j.start=function(){if(!q){r=$.fn[c].src;r=r&&r+a();q=$('<iframe tabindex="-1" title="empty"/>').hide().one("load",function(){r||l(a());n()}).attr("src",r||"javascript:0").insertAfter("body")[0].contentWindow;h.onpropertychange=function(){try{if(event.propertyName==="title"){q.document.title=h.title}}catch(s){}}}};j.stop=k;o=function(){return a(q.location.href)};l=function(v,s){var u=q.document,t=$.fn[c].domain;if(v!==s){u.title=h.title;u.open();t&&u.write('<script>document.domain="'+t+'"<\/script>');u.close();q.location.hash=v}}})();return j})()})(jQuery,this);



// JQUERY COLOR ANIMATIONS PLUG-IN V1.1 - COPYRIGHT 2007 JOHN RESIG

(function(d){d.each(["backgroundColor","borderBottomColor","borderLeftColor","borderRightColor","borderTopColor","color","outlineColor"],function(f,e){d.fx.step[e]=function(g){if(!g.colorInit){g.start=c(g.elem,e);g.end=b(g.end);g.colorInit=true}g.elem.style[e]="rgb("+[Math.max(Math.min(parseInt((g.pos*(g.end[0]-g.start[0]))+g.start[0]),255),0),Math.max(Math.min(parseInt((g.pos*(g.end[1]-g.start[1]))+g.start[1]),255),0),Math.max(Math.min(parseInt((g.pos*(g.end[2]-g.start[2]))+g.start[2]),255),0)].join(",")+")"}});function b(f){var e;if(f&&f.constructor==Array&&f.length==3){return f}if(e=/rgb\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*\)/.exec(f)){return[parseInt(e[1]),parseInt(e[2]),parseInt(e[3])]}if(e=/rgb\(\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*\)/.exec(f)){return[parseFloat(e[1])*2.55,parseFloat(e[2])*2.55,parseFloat(e[3])*2.55]}if(e=/#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})/.exec(f)){return[parseInt(e[1],16),parseInt(e[2],16),parseInt(e[3],16)]}if(e=/#([a-fA-F0-9])([a-fA-F0-9])([a-fA-F0-9])/.exec(f)){return[parseInt(e[1]+e[1],16),parseInt(e[2]+e[2],16),parseInt(e[3]+e[3],16)]}if(e=/rgba\(0, 0, 0, 0\)/.exec(f)){return[255,255,255]}}function c(g,e){var f;do{f=d.curCSS(g,e);if(f!=""&&f!="transparent"||d.nodeName(g,"body")){break}e="backgroundColor"}while(g=g.parentNode);return b(f)}})(jQuery);



// MINION FONT - COPYRIGHT 1990 ADOBE SYSTEMS INCORPORATED

Cufon.registerFont({"w":53,"face":{"font-family":"Minion","font-weight":400,"font-stretch":"normal","units-per-em":"360","panose-1":"4 1 5 0 0 0 0 0 0 0","ascent":"288","descent":"-72","bbox":"-51 -236 166 59","underline-thickness":"17.9297","underline-position":"-27.0703","unicode-range":"U+002F-U+0039"},"glyphs":{" ":{},"\/":{"d":"107,-239r-78,255r-20,0r78,-255r20,0","w":80},"0":{"d":"91,-165v-43,0,-74,35,-74,85v0,49,32,84,74,84v42,0,74,-35,74,-84v0,-50,-31,-85,-74,-85xm91,-154v60,1,60,145,0,147v-63,-4,-60,-144,0,-147","w":181},"1":{"d":"49,-30v4,25,-16,19,-33,22r0,8r95,0r0,-8v-17,-3,-34,2,-34,-22r0,-135v-22,6,-44,11,-67,15r0,8v20,2,39,-7,39,22r0,90","w":119},"2":{"d":"99,-106v0,49,-60,79,-86,98r0,8r130,0r9,-42v-19,-2,-9,21,-34,19v-21,-2,-45,2,-64,-1v38,-38,66,-38,74,-90v8,-48,-65,-68,-93,-32r-20,26r7,6v8,-11,21,-30,41,-31v25,-2,36,17,36,39","w":156},"3":{"d":"114,-121v3,-44,-61,-57,-86,-28v-4,9,-21,19,-7,26v8,-28,68,-35,65,8v-2,28,-28,36,-52,50v0,13,15,1,23,3v25,1,42,16,42,47v0,54,-54,71,-81,38v-21,3,-15,28,2,32v55,12,108,-29,108,-86v0,-28,-23,-47,-49,-52v16,-10,33,-17,35,-38","w":143},"4":{"d":"32,-36r73,-96r0,96r-73,0xm132,-165r-16,0r-105,135r0,13r94,0r0,74r27,0r0,-74r30,0r4,-19r-34,0r0,-129","w":168},"5":{"d":"125,-27v-1,-52,-39,-69,-90,-70r8,-36r77,-7r5,-25r-91,8r-14,84v41,0,78,8,77,50v-1,50,-47,66,-90,73r2,9v55,-1,116,-34,116,-86","w":142},"6":{"d":"55,-119v10,-41,53,-78,98,-88r-1,-9v-65,7,-135,67,-135,137v0,50,27,82,69,83v40,1,71,-33,71,-74v0,-58,-60,-80,-102,-49xm93,-7v-39,0,-52,-56,-42,-101v43,-21,77,6,77,54v0,28,-9,47,-35,47","w":172},"7":{"d":"146,-160r-134,0r0,25r111,0v-35,63,-74,126,-117,189v4,10,19,0,29,0v55,-105,93,-175,114,-210","w":151},"8":{"d":"81,-205v38,0,37,68,10,79v-16,-12,-42,-20,-42,-46v1,-17,12,-33,32,-33xm83,-7v-50,-1,-52,-79,-11,-98v21,11,47,26,47,56v0,26,-13,42,-36,42xm79,4v34,0,66,-26,66,-63v0,-37,-22,-49,-46,-62v28,-17,35,-19,37,-46v2,-29,-24,-49,-54,-49v-34,0,-58,27,-58,57v0,25,21,38,39,49v-23,14,-46,23,-47,58v0,36,27,56,63,56","w":160},"9":{"d":"118,-42v-16,52,-60,82,-109,93r1,8v82,-10,143,-54,143,-141v0,-43,-22,-85,-68,-83v-37,2,-72,32,-69,76v3,52,51,79,102,47xm78,-154v42,2,44,48,44,91v0,15,-12,17,-27,17v-30,0,-52,-23,-50,-59v0,-23,11,-49,33,-49","w":170},"\u00a0":{}}});
