(function(){
	//if ($('body').hasClass('portfolio') && $('body').hasClass('domain')){
	if(document.getElementById('works') && $('#works > ul').size() > 1) {
		var scroller = document.createElement('div');
		scroller.className = 'js-scroller';
		scroller.style.position = 'relative';
		scroller.style.width = '100%';
		$('#works').wrapInner(scroller);

		if ($('body').hasClass('portfolio') && $('body').hasClass('domain')){
			gpx.fn.doSlideShowNav('#primary');
		}
		else {
			gpx.fn.doSlideShowNav('#works');
		}
		var portfolioScroller = function() {
			var fx = {
				targetSelector: '#works > .js-scroller',
				childrenSelector: '> ul',
				prevSelector: '.js-controller > a.prev',
				nextSelector: '.js-controller > a.next',
				scrollToOptions: {
					axis:'x',
					margin: true,
					onAfter: function() {
						fx.inProgress = false;
					}
				},
				fxItems: 1,
				fxTime: 800,
				fxTimeReset: 1200,
				fxTimeAuto: false,
				idx: 0,
				max: false,
				inProgress: false,
				elements: false,
				nextFX: function() {
					if(this.idx+this.fxItems < this.max) {
						this.idx = this.idx+this.fxItems;
						this.inProgress = true;
						$(this.targetSelector).scrollTo(this.elements[this.idx], this.fxTime, this.scrollToOptions);						
						switch(this.idx) {
							case 1:
								$(this.prevSelector).removeClass('prevdisabled');
								if(this.max == 2) {
									$(this.nextSelector).addClass('nextdisabled');	
								}
								break;
							case (this.max - this.fxItems):
								$(this.nextSelector).addClass('nextdisabled');
								break;
						}
					}
					return true;
				},
				prevFX: function() {
					if(this.idx > 0) {
						this.idx = this.idx-this.fxItems;
						this.inProgress = true;
						$(this.targetSelector).scrollTo(this.elements[this.idx], this.fxTime, this.scrollToOptions);						
						switch(this.idx) {
							case 0:
								$(this.prevSelector).addClass('prevdisabled');
								if(this.max == 2) {
									$(this.nextSelector).removeClass('nextdisabled');
								}
								break;
							case (this.max - this.fxItems*2):
								$(this.nextSelector).removeClass('nextdisabled');
								break;
						}
					}
					return true;
				},
				resetFX: function() {
					if(this.idx > 0) {
						this.idx = 0;
						$(this.targetSelector).scrollTo(this.elements[this.idx],fx.fxTimeReset, this.scrollToOptions);
					}
					return true;
				},
				autoFX: function() {
					if(this.idx+1 < this.max) {
						this.nextFX();
					}
					else {
						this.resetFX();
					}
					return true;
				},
				cancelAutoFX: function() {
					if(this.fxTimeAuto !== false) {
						clearInterval(this.timer);
					}
				},
				init: function() {
					var scrollWrapper = document.createElement('div');
					scrollWrapper.style.width = '10000px';
					scrollWrapper.style.postion = 'relative';

					this.elements = $(this.targetSelector+' '+this.childrenSelector).wrapAll(scrollWrapper);
					this.max = this.elements.size();
					scrollWrapper = null;

					if(this.fxTimeAuto !== false) {
						this.timer = setInterval(function() {
							fx.autoFX();
						}, this.fxTimeAuto);
					}
					
					$(this.nextSelector).bind('click', function(e) {
						//thix.cancelAutoFX();
						fx.nextFX();
						return false;
					});
					$(this.prevSelector).bind('click', function(e) {
						//fx.cancelAutoFX();
						fx.prevFX();
						return false;
					})
					.addClass('prevdisabled');
				}
			}
			fx.init();
			gpx.rdx = fx;
		};
		portfolioScroller();
	}
})();