var Photographs = {

	currentIndex: 0,

	currentPhotograph: null,

    nextPhotograph: null,

	fadeDuration: 1000,

	list: null,

	timer: null,

	timerDuration: 7000,
	
	init: function() {
		// photograph block exist?
		if ($('photographBlock')) {
			var el = $('otherItem');
			
			Photographs.list = $('photographBlock').getElements('.bigShow').setStyle('opacity', 0);
			if (Photographs.list.length > 0) {
				// set up controls initially
				Photographs.list[0].setStyle('opacity', 1);
				Photographs.currentPhotograph = Photographs.list[0].get('id');

				if (el) {
					$(el).getElements('a').addEvent('click', function(e) {
						e.stop();

						$clear(Photographs.timer);

						var id = this.get('id').match(/[a-z]+\-(\d+)/i)[1];
						Photographs.showPhotographVague(id);
					});

					new VerticalSlider(el, {
						height: 190,
						scrollStep: 195
					});

					if (Photographs.list.length > 1) {
						Photographs.timer = Photographs.switchPhotos.periodical(Photographs.timerDuration);
					}
				}
			}
		}	
	},

	
	showPhotographVague: function(id) {
        Photographs.nextPhotograph = $('bigPhotograph-' + id);
        $(Photographs.nextPhotograph).removeClass('hidden').addClass('show');

 		// swap photo information panel
		$(Photographs.currentPhotograph).getNext('.photographVague').removeClass('show').addClass('hidden');
		$(Photographs.nextPhotograph).getNext('.photographVague').removeClass('hidden').addClass('show');

        var inFx = new Fx.Tween($(Photographs.nextPhotograph), {
            duration: Photographs.fadeDuration,
			link: 'cancel',
			property: 'opacity'
		}).start(1);

        var outFx = new Fx.Tween($(Photographs.currentPhotograph), {
			duration: Photographs.fadeDuration,
			link: 'cancel',
			property: 'opacity',
            onComplete: function(el) {
                el.removeClass('show').addClass('hidden');
            }
		}).start(0);

        Photographs.currentPhotograph = Photographs.nextPhotograph;
	},

	switchPhotos: function() {
		var listLength = Photographs.list.length;
		if (Photographs.currentIndex >= listLength - 1) {
			Photographs.currentIndex = 0;
		} else {
			Photographs.currentIndex++;
		}

		var nextId = Photographs.list[Photographs.currentIndex].get('id').match(/[a-z]+\-(\d+)/i)[1];
		Photographs.showPhotographVague(nextId);
	}
};

var DragAndDrop = {
	
	dragObject: null,
	
	mouseOffset: null,
	
	init: function(){
		document.onmousemove = DragAndDrop.mouseMove;
		document.onmouseup   = DragAndDrop.mouseUp;
	},
	
	mouseMove: function(ev){
		ev = ev || window.event;
		var mousePos = DragAndDrop.mouseCoords(ev);
		
		if (this.dragObject){
			this.dragObject.style.position = 'absolute';
			this.dragObject.style.top      = mousePos.y - this.mouseOffset.y;
			this.dragObject.style.left     = mousePos.x - this.mouseOffset.x;
			
			return false;
		}
	},
	
	mouseCoords: function(ev){
		if (ev.pageX || ev.pageY){
			return {x:ev.pageX, y:ev.pageY};
		}
		return {
			x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
			y:ev.clientY + document.body.scrollTop  - document.body.clientTop
		};
	},
	
	makeClickable: function(object){
		object.onmousedown = function(){
			DragAndDrop.dragObject = this;
		};
	},
	
	mouseUp: function(ev){
		this.dragObject = null;
	},
	
	getMouseOffset: function(target, ev){
		ev = ev || window.event;
		
		var docPos   = getPosition(target);
		var mousePos = this.mouseCoords(ev);

		return {x:mousePos.x - docPos.x, y:mousePos.y - docPos.y};
	},
	
	getPosition: function(e){
		var left = 0;
		var top  = 0;
		
		while (e.offsetParent){
			left += e.offsetLeft;
			top  += e.offsetTop;
			e     = e.offsetParent;
		}
		
		left += e.offsetLeft;
		top  += e.offsetTop;
		
		return {x:left, y:top};
	},
	
	makeDraggable: function(item){
		if (!item) return;
		item.onmousedown = function(ev){
			DragAndDrop.dragObject  = this;
			DragAndDrop.mouseOffset = DragAndDrop.getMouseOffset(this, ev);
			return false;
		};
	}
	
};

window.addEvent('domready', Photographs.init);