// JavaScript Document
var References = {
	
	  init: function(){
			
			  var items = $$('#references .item');
				items.each(function(element){
				  var img = element.getElement('img');
				  var imgFx = new Fx.Tween(img, { duration: 200, wait: false });
					element.addEvent('mouseenter', function(){
					  imgFx.start('opacity', 0);
						element.setStyle('cursor','pointer');
					});
					element.addEvent('mouseleave', function(){
					  imgFx.start('opacity', 1);
					});
					element.addEvent('click', function(){
					  window.location = element.getElement('a').getProperty('href');
					});
				});
				
		}
		
};

var Slideshow = {
	
	init: function(){
	  
		var slides = $$('#references .wrapper');
		var position = $('references').getElement('.position');
		var current = 0;
		var slideCount = slides.length;
		var slideWidth = 945;
		var display;
		slideFocus = slideFocus || 0;
				
		// focus
		if (slideFocus){
		  display = $('page-'+slideFocus);
			// id exists
			if (display){
				current = slides.indexOf(display.getParent('div'));
			}
		}
		
		// width
		position.setStyle('width', slideWidth*slideCount);
		
		// slide effect
		var slideFx = new Fx.Tween(position, {property: 'left', duration: 1000, wait: false, transition: Fx.Transitions.Cubic.easeInOut });
		slideFx.set(-current*slideWidth);
		
		// button fx
		var buttonPrev = $('ref-prev');
		var buttonPrevFx = new Fx.Tween(buttonPrev, { property: 'opacity', duration: 200, wait: false });
		if (current > 0) {
			buttonPrevFx.set(1);
		} else {
			buttonPrevFx.set(0);
		}
		var buttonNext = $('ref-next');
		var buttonNextFx = new Fx.Tween(buttonNext, { property: 'opacity', duration: 200, wait: false });
		if (slideCount > 1 && current != slideCount-1){
			buttonNextFx.set(1);
		} else {
			buttonNextFx.set(0);
		}
		
		// events
		$('ref-next').addEvent('click', function(event){
		  event.stop();
			if (current != slideCount-1){
				current++;
				slideFx.start(-current*slideWidth);
				if (current == slideCount-1){
					buttonNextFx.start(0);
				}
				if (slideCount > 1){
					buttonPrevFx.start(1);
				}
			}
			
		});
		
		$('ref-prev').addEvent('click', function(event){
		  event.stop();
			if (current > 0){
				current--;
				slideFx.start(-current*slideWidth);
				if (current == 0){
					buttonPrevFx.start(0);
				}
				if (slideCount > 1){
					buttonNextFx.start(1);
				}
			}
		});
		
	}
	
};

var AntiSpam = {
	
	  init: function(){
			var email = 'jan@pivonka.eu';
			$$('.email').each(function(element){
			   element.set('text', email);
			});
			
			if ($('form-contact')){
				$('form-contact').getElement('noscript').destroy();
				var anti = new Element('input', { 'type': 'hidden', 'name': 'anti', 'value': 'pivonka' });
				anti.inject('form-contact');
			}
		}
};



var FileCz = {
	init: function(){
		 
    // Reference
		References.init();
		
		// SmoothScroll
		new SmoothScroll({ duration: 1500, wait: false,  transition: Fx.Transitions.Quint.easeOut });
		
		// Slideshow
		if ($('references')){
			Slideshow.init();
		}
		
		// Antispam
		AntiSpam.init();
		
		// Homepage Flash
		if ($('flash')){
			$('img').destroy();
			var obj = new Swiff('hp-flash.swf', {
					container: $('flash-container'),
					id: 'hp-flash',
					width: 610,
					height: 420,
					params: {
							wmode: 'transparent'
					}
			});
		}
	}
};


window.addEvent('domready', function(){
																		 
  FileCz.init();

	
});