//-------------------------------------------------------------------------------------
//	SimpleView Gallery Deluxe v2.1
//	(c) 2007 Chris Jaure
//	SimpleView Gallery is freely distributable under the terms of an MIT-style license.
//	website: http://www.chromasynthetic.com/
//
//	svgscript.js
//-------------------------------------------------------------------------------------
var SimpleView = {
	files : [],
	offset : 1,
	objects : {},
	effects : [],
	buttons : {},
	change : function(num){
		if (num >=1 && num <= this.files.length && num!=this.offset){
			this.loading();
			(function(){
				this.objects.image.setProperties({
					'src' : this.files[num-1],
					'title' : this.files[num-1],
					'alt' : this.files[num-1]});
				this.offset = +num;
				this.objects.number.setText('[ '+num+' / '+this.files.length+' ]');
				this.preload();
				if (num <= 1){
					this.disable([this.buttons.first, this.buttons.previous]);
				} else {
					this.enable([this.buttons.first, this.buttons.previous]);
				}
				if (num >= this.files.length){
					this.disable([this.buttons.next, this.buttons.last]);
				} else{
					this.enable([this.buttons.next, this.buttons.last]);
				}
			}).delay(400, this);
			if (window.opera && this.objects.image.complete){
				this.loaded.delay(400, this);
			}
		}
	},
	step : function(num){
		this.change(this.offset+num);
	},
	disable : function(objs){
		objs.each(function(el) {
			this.effects[el.getProperty('id')].start({'color':'#cccccc'});
			el.addClass('disabled');
		}.bind(this));
	},
	enable : function(objs){
		objs.each(function(el) {
			this.effects[el.getProperty('id')].start({'color':'#666666'});
			el.removeClass('disabled');
		}.bind(this));
	},
	loading : function(){
		this.objects.loadDiv.setStyle('visibility', 'visible');
		document.body.style.cursor = 'wait';
		this.effects['image'].start(0);
	},
	loaded : function(){
		this.objects.loadDiv.setStyle('visibility', 'hidden');
		document.body.style.cursor = 'default';
		this.effects['image'].start(1);
		if (window.getScrollTop() < this.objects.nav_container.offsetTop && this.objects.nav.getStyle('position') == 'fixed'){
			this.objects.nav.setStyle('position', 'static');
		}
	},
	preload : function(){
	  if(this.files.length > this.offset){
			this.objects.nextImage = new Element('img', {'src':this.files[this.offset]});
		}
		if(this.offset > 1){
			this.objects.previousImage = new Element('img', {'src':this.files[this.offset-2]});
		}
	},
	floatNav : function(){
		window.addEvent('scroll', function(){
			if (window.getScrollTop() > this.objects.nav.getPosition().y && this.objects.nav.getStyle('position')=='static'){
				this.objects.nav.setStyles({'position':'fixed', 'top':0, 'left': ((window.getWidth() / 2) - (this.objects.nav.offsetWidth / 2))});
			} else if (window.getScrollTop() < this.objects.nav_container.offsetTop && this.objects.nav.getStyle('position') == 'fixed'){
				this.objects.nav.setStyle('position', 'static');
			}
		}.bind(this));
	},
	initialize : function(files, offset){
		this.files = files;
		this.offset = offset;
		this.objects.image = $('sv_image').addEvent('load', this.loaded.bind(this));
		this.effects['image'] = new Fx.Style(this.objects.image, 'opacity', {duration:400, wait:false});
		this.buttons.first = $('sv_first').addEvent('click', this.change.pass(1, this));
		this.buttons.previous = $('sv_previous').addEvent('click', this.step.pass(-1, this));
		this.buttons.next = $('sv_next').addEvent('click', this.step.pass(1, this));
		this.buttons.last = $('sv_last').addEvent('click', this.change.pass(this.files.length, this));
		for (var button in this.buttons){
			this.effects[this.buttons[button].getProperty('id')] = new Fx.Styles(this.buttons[button], {duration:200, wait:false});
			this.buttons[button].addEvent('click', function(event){
				this.blur();
				var event = new Event(event).stop();
			});
		}
		if ($('sv_archive')){
			this.objects.form = $('sv_archive').addEvent('submit', function(event){
				this.change(+this.objects.form.elements['sv_archive_dd'].value);
				event.stop();
			}.bindWithEvent(this));
		}
		this.objects.loadDiv = new Element('div', {'id':'sv_loading'})
			.setHTML('Loading...')
			.injectInside('simpleView');
		this.objects.number = $('sv_number');
		this.objects.nav = $('sv_nav');
		this.objects.nav_container = $('sv_nav_container');
		if ($('sv_thumbs')){
			this.objects.thumb_container = $('sv_thumbs');
			this.objects.thumb_container.getChildren().each(function(el){
				el.addEvent('click', function(event){					
					this.change(event.target.parentNode.rel);
					event.target.parentNode.blur();
					event.stop();
				}.bindWithEvent(this));
				/*var img = el.getFirst();
				img.setOpacity(0.65);
				var fx = new Fx.Style(img, 'opacity', {duration:300, wait:false});
				img.addEvent('mouseover', function(){
					fx.start(1);
				}).addEvent('mouseout', function(){
					fx.start(0.65);
				});*/
			}.bind(this));
		}
		this.floatNav();
	}
};

