var islide;

Init = function (){

	if ($('bigSlide') && $$('p.hint').first()) {
		new ImageHint('content', 'p.hint');
	}
	if ($('bigSlide') && $$('.bottom').size()) {
		islide = new ImageSlide('content', 'div.bottom', 4);
	}


	if ($$('#imgprev a')) {
	   $$('#imgprev a').each(function(a){a.onclick=function(){document.location.href=a.href}.bind(a)})
	}

	$$('a').each ( function (a){
		a.onfocus = function(){this.blur();};
	});

	if ($$('.scrollable').first()) {
		runScroll($$('.scrollable').first())
	}




}
window.onload = Init;

var ImageSlide = Class.create({
	initialize: function(image, slide, timeout) {
		this.slide = $$(slide).first();

		this.seconds = timeout;
		this.image = $(image);
		Event.observe(this.image, 'mouseover', this.showSlide.bind(this));
		Event.observe(this.image, 'mousemove', this.showSlide.bind(this));
		Event.observe(this.image, 'mouseout', this.startHide.bind(this));
        this.show=false;
		this.timer = setTimeout(this.hideSlide.bind(this), this.seconds*100*2);

	},
	startHide: function(e){
		clearTimeout(this.timer)
		this.show = false;
		this.timer = setTimeout(this.hideSlide.bind(this), this.seconds*100*2);
	},
	showSlide: function(e) {
		clearTimeout(this.timer)
		if (e.element() != this.image) return;
		this.slide.show();
		this.show = false;
		this.timer = setTimeout(this.hideSlide.bind(this), this.seconds*100*5);
	},
	hideSlide: function() {
		clearTimeout(this.timer)
		if (this.show == false) {
			this.slide.hide();
			this.show = true;
		}
	}
});


var ImageHint = Class.create();
Object.extend(ImageHint.prototype, {
	initialize: function(image, hint, timeout, type) {
		this.hint = $$(hint).first();

		this.hint.hide();
		this.image = $(image);

		Event.observe(this.image, 'mouseover', this.showHint.bind(this));
		Event.observe(this.image, 'mouseout', this.hideHint.bind(this));
	},
	showHint: function() {
		this.hint.show();
	},
	hideHint: function() {
		this.hint.hide();
	}
});




// scroll the element vertically based on its width and the slider maximum value
function scrollVertical(value, element, slider) {

//	console.log('value = ' + value + ', element= ' + element + ', slider' + slider);
	element.scrollTop = Math.round(value/slider.maximum*(element.scrollHeight-element.offsetHeight));
}


var slider;
function runScroll(element) {

	element.scrollTop = 0;
	// vertical slider control
	slider = new Control.Slider('handle', 'track', {
		axis: 'vertical',
		onSlide: function(v) { scrollVertical(v, element, slider);  },
		onChange: function(v) { scrollVertical(v, element, slider); }
	});

	// disable vertical scrolling if text doesn't overflow the div
	if (element.scrollHeight <= element.offsetHeight) {
		slider.setDisabled();
		$('wrap').hide();
	}
	
				// mozilla
			Event.observe(element, 'DOMMouseScroll', wheel);
			
			// IE/Opera
			Event.observe(element, 'mousewheel', wheel);
			

}



		// mouse wheel code from http://adomas.org/javascript-mouse-wheel/
			function handle(delta) {
				slider.setValueBy(-delta);
			}

			/** Event handler for mouse wheel event. */
			function wheel(event){
				var delta = 0;
				if (!event) /* For IE. */
					event = window.event;
				if (event.wheelDelta) { /* IE/Opera. */
					delta = event.wheelDelta/1200;
					/** In Opera 9, delta differs in sign as compared to IE. */
					if (window.opera)
						delta = -delta;
				} else if (event.detail) { /** Mozilla case. */
					/** In Mozilla, sign of delta is different than in IE.
					* Also, delta is multiple of 3.
					*/
					delta = -event.detail/30;
				}
		
				/** If delta is nonzero, handle it.
				* Basically, delta is now positive if wheel was scrolled up,
				* and negative, if wheel was scrolled down.
				*/
				if (delta)
					handle(delta);
		
				/** Prevent default actions caused by mouse wheel.
				* That might be ugly, but we handle scrolls somehow
				* anyway, so don't bother here..
				*/
				if (event.preventDefault)
					event.preventDefault();
				
				event.returnValue = false;
			}


			
			
var isCtrl = false; 
document.onkeyup=function(e){ 
	if (e.keyCode) {
		code = e.keyCode;
	} else if (e.which) {
		code = e.which;
	} 
	if(code == 17) isCtrl=false; 
} 
document.onkeydown=function(e){ 
	if (e.keyCode) {
		code = e.keyCode;
	} else if (e.which) {
		code = e.which;
	} 
	if(code == 17) isCtrl=true; 
	if(code == 37 && isCtrl == true) { //37: left arrow
		//Previous image
		showSlide('prev');
		
	} 
	if(code == 39 && isCtrl == true) { //39: right arrow
		//Next image
		showSlide('next')
	} 
	
} 			
