var Session = new Class({	
	initialize : function() {
		// handle all https links and add session to it
		
		var session = Cookie.read('session');
		//console.log(session);
		
		$$('a[href^=https://]').each(function(el){
			el.addEvents({
				'click': function(ev){
					new Event(ev).stop();
					var url = el.get('href') + '&session='+session;
					// console.log(url);
					document.location = url;
				}.bind(this)
			});
		}); 
	}
});


var SetImages = new Class ({
	initialize : function() {
		this.images = [];
		this.els = $$('.i100, .i125, .i160, .i225, .i350');
		if (this.els.length == 0) return false;
		
		this._getElements();
		
		this._center();
	},
	
	_getElements : function() {
		this.els.each(function(el) {
			el.getElements('img').each(function(el2) {
				this.images.push(el2);
			},this);
		}, this);
	},
	
	_center : function() {
		this.images.each(function(el) {
			if(h = el.getCoordinates().height){
				var ha = ((h - el.getParent().getCoordinates().height)*-1)/2;
				el.setStyle('margin-top', ha.toInt())
			}
			el.fade('in');
		});
	}
});


var	handleSlideshow = new Class ({
	
	Implements: [Options],
	
	options: {
		duration: 1500, 
		delay: 2000
	},
	
	initialize : function(slideshows,options){	
		this.setOptions(options);
		
		var self = this;
		
		slideshows.each(function(slideshow) {
			
			var img = new Array();
			var url = new Array();
			
			var cnt = 0;
			
			var data = new Hash();
			var data_container = $('slideshowdata');
			
			var r=/nophoto/g;
		
			data_container.getChildren().each(function(d){
				var src = d.get('html');
				var info = {
					'href': d.get('href')
				};
				
				if (!src.match(r)) {
					data.include(src,info);
					cnt++;
				}
			});
			
			if (cnt > 0) {
		
				new Slideshow('slideshow', data, { 
					duration: self.options.duration, 
					delay: self.options.delay, 
					resize:false,
					loop: true,
					center: false
				});
			} else {
				console.log(data);
				slideshow.getElement('img').set('src',data[0]);
			}
			
			
		});
		

	}
});


window.addEvent('domready', function() {	
	// create a formcheck object for each form class formcheck
	
	var forms = $$('form.formcheck');
	if (forms.length > 0) {
		forms.each(function(form) {
			new FormCheck(form);
			
		})
	}
	
	var tooltip = $$('a.tooltip1');
	if (tooltip.length) {	
		tooltip.each(function(element,index) {
			var content = element.get('title').split('::');
			element.store('tip:title', content[0]);
			if (content[1] == 'CHF 0.00') { content[1] = 'prix sur demande'}
			element.store('tip:text', content[1]);
		});								 
										 
		new Tips($$('.tooltip1'),{
				className : 'tool-tip'
			}).addEvents({
			show: function(tip, el){
				tip.fade('in');
			},
			hide: function(tip, el){
				tip.fade('out');
			}
		});
	};
	
	
	
	//new Session();
});



window.addEvent('load', function() {
	new SetImages();
	
	var slideshows = $(document.body).getElements('.slideshow-images');
	if (slideshows) 
		new handleSlideshow(slideshows);
});

