var Portfolio = new Class({
	status: false,
	frames: 25,
	initialize: function() {
		this.container = $('slideshow');
		this.container.getElement('a').addEvent('click', function(e){ e.stop(); portfolio.action(); });
		this.button = null;
		$(document.body).getElement('div.swicth').getElements('a').each(function(element){
			element.addEvent('click', function(e){
				e.stop();
				if (this.get('href')=='#previous') portfolio.procede(-1);	
				else if (this.get('href')=='#next') portfolio.procede(+1);	
				else portfolio.action();
			});
		});
		this.button = $(document.body).getElement('div.swicth').getElement('a[href$=action]');
		this.theTimer = null;
		this.pos = 0;
	},
	procede: function(adv) {
		this.status = true;
		this.button.setStyle('background-position', '0 -19px');
		clearTimeout(this.theTimer);
		var all = this.container.getElements('img');
		all.each(function(element){
			if (element.get('id')!='slide'+portfolio.pos)
				element.setStyle('z-index', '10');
		});
		if (this.pos>0) {
			var last = $('slide'+this.pos);
			last.setStyles({'z-index': '100', 'opacity': 1});
		}
		
		if (!adv) {
			this.pos = (this.pos+1)>this.frames ? 1 : this.pos+1;
		} else {
			this.pos = (this.pos+adv)>this.frames ? 1 : this.pos+adv;
			this.pos = (this.pos<1) ? this.frames : this.pos;
		}
				
		if (!$chk($('slide'+this.pos))) {
		  if ( iPhone == 'iPhone' && iResolution < 532 )
			var img = new Element('img', {
				'src': '/images/portfolio/image-'+this.pos+'.jpg', 
				'width': iResolution + 'px', 
				'height': (324*iResolution/532) + 'px',  
				'alt': '', 	 
				'id': 'slide'+this.pos, 
				'styles': {'position': 'absolute', 'opacity': 0, 'top': 0, 'left': 0}
			});
		  else
			var img = new Element('img', {
				'src': '/images/portfolio/image-'+this.pos+'.jpg', 
				'width': '532px', 
				'height': '324px', 
				'alt': '', 
				'id': 'slide'+this.pos, 
				'styles': {'position': 'absolute', 'opacity': 0, 'top': 0, 'left': 0}
			});
			
			img.inject(this.container);
			var cur = $('slide'+this.pos);
			cur.setStyle('z-index', '200');
			img.addEvent('load', function() { portfolio.fade(cur); });
		} else  {
			var cur = $('slide'+this.pos);
			cur.setStyles({'opacity': 0, 'z-index': '200'});
			this.fade(cur);
		}
	},
	fade: function(cur) {
		var fx = new Fx.Tween(cur, {duration: 2500});
		fx.start('opacity', 1);
				
		this.theTimer = setTimeout('portfolio.procede(null);', 5000);
	},
	action: function() {
		if (portfolio.status==false) {
			portfolio.status = true;
			portfolio.procede()
		} else {
			clearTimeout(portfolio.theTimer);
			portfolio.status = false;
			portfolio.button.setStyle('background-position', '0 0');
		}
	}
});

var portfolio;

window.addEvent('domready', function() {
	portfolio = new Portfolio;
});