var pages = [{indice:1,targetURL:'dossier-special-vacances-d-ete-juillet-et-aout/deja-pret-a-partir/',addParams:{pid:38,qKey:'bd09dc72fa7abe78a6afcebf03624724'}},{indice:3,targetURL:'dossier-special-vacances-d-ete-juillet-et-aout/aoutien-dans-l-ame/',addParams:{pid:38,qKey:'c776ef950ae32f213bd7bffbeccd1aee'}},{indice:4,targetURL:'dossier-special-vacances-d-ete-juillet-et-aout/fan-de-promos/',addParams:{pid:38,qKey:'d3e3ee8be250d7d9d08b5352aa47bf19'}},{indice:5,targetURL:'dossier-special-vacances-d-ete-juillet-et-aout/fan-de-l-ete-indien/',addParams:{pid:38,qKey:'7331462884423996f4bd3bc428d4776a'}}];
var rand = function (min, max) {return Number(Math.round(Math.random()*(max-min)));};

var Bubble = Class.create();
Bubble.prototype = {

	form_id: 0,
	options: null,
	home: false,
	current: 0,
	parallelEffect: [],
	baseUrl: '',
	space:0,

	initialize: function (form_id, options) {
		this.options = {
			'entry': [],
			'y_min': 80,
			'y_max': 110,
			'x_min':350,
			'x_max': 921,
			'w':111,
			'delay':0.2,
			'duration':1
		};
		Object.extend(this.options, options || {});
		this.form_id = form_id;
	},

	start: function () {

		if(typeof tx_jettours_pi1_Engine == 'undefined' || !($('promo_ete'))){return false;}	// Recherche d'un moteur de recherche dans la page
		if(($$('body.dossier-ete').length)){return false;} // Desactiver l'animation pour les pages partez cet ete
		this.baseUrl = $$('base')[0].readAttribute('href'); // Recuperation de BaseURL
		$$('div#panoramic div.right').each(function(elem){Element.remove(elem)}); // Supression du contenu dans la cellule panoramique
		this.home = ($$('body > div.accueil').length)?true:false; // Verification de la page d'accueil

		var numBubbles = this.options.entry.length; // Nombre de vignettes

		if(numBubbles){

			this.space = Math.floor(((this.options.x_max-this.options.x_min)-(this.options.w*numBubbles))/(numBubbles-1)); // Calcul de l'espace entre les vignettes

			if(!this.home){
				this.options.y_min = 65;
				this.options.y_max = 65;
			}

			this.parallelEffect.push(new Effect.Appear($('boxy_title'),{duration:0.9})); // Affichage du titre

			// Affichage des vignettes
			this.options.entry.each(function(a, b){
				b+=1;
				var tmp_x = (b == 1)?(this.options.x_min):(this.options.x_min+((this.options.w+this.space)*(b-1)));
				this.effect(a.indice, tmp_x, (this.options.y_min+rand(0,Number(this.options.y_max-this.options.y_min))), {delay:(this.options.delay*b),duration:this.options.duration});
			}.bind(this));

			// Affichage de l'overlay cas autres pages que l'accueil
			if(!this.home){
				$('promo_ete').insertBefore(Builder.node('div', {id:'boxy_overlay'}), $('boxy_title'))
				var dimensions = $('panoramic').getDimensions();
				Element.setStyle('boxy_overlay', {top:'21px',height:dimensions.height+'px'});
				$('boxy_overlay').setOpacity(0.8);
			}

		}

	},

	effect: function (id, x ,y) {
		var elem = $('boxy_'+id);
		elem.setStyle({left:x+'px',top:y+'px'});
		this.parallelEffect.push(new Effect.Scale(elem, 100, Object.extend({
			beforeStart:function(effect){
				$(effect.element).style.display = 'block';
				$(effect.element).setOpacity(0);
			},
			afterUpdate:function(effect){
				$(effect.element).setOpacity(effect.position);
			},
			scaleFrom:0,
			scaleFromCenter:true,
			afterFinish:function(effect){
				$(effect.element).setStyle({'cursor':'pointer'});
				$(effect.element).onclick=function(){
					this.check($(effect.element).readAttribute('id'));
				}.bind(this)
			}.bind(this)
		}, arguments[3] || {})));
	},

	check: function (id) {
		$$('#promo_ete div.boxy').each(function(elem){
			if(elem.readAttribute('id') != id){ elem.setOpacity(0.5); Element.removeClassName(elem, 'selected'); }
		});
		$(id).setOpacity(1);
		Element.addClassName($(id), 'selected');
		if(!$('boxy_ville').visible()){
			$('boxy_ville').show();
		}
		var expression = /([0-9]+)$/;
		expression.exec(id);
		this.current = (Number(RegExp.$1)-1);
		if(document.forms['tx_jettours_pi1_engine_form_c'+this.form_id].elements['ville_dep'].value){ this.send([null,null,1]); }
	},

	send: function (arg) {
		var indice = Number(arg[2]);
		if(!indice){ return false; }
		var options = {
			'onLoading':function(){$('boxy_loading').show();},
			'onComplete':function(){$('boxy_loading').hide();},
			'targetURL':'',
			'prefixId':'tx_jettours_pi1',
			'lightForm':true,
			'addParams':{'pid':0,'what':'produit','site_diff':'B2C_JETTOURS','score_min':33,'qKey':'','fileNameFormat':'www_%s'}
		};
		options.targetURL = this.baseUrl+this.options.entry[this.current].targetURL;
		options.addParams.qKey = this.options.entry[this.current].addParams.qKey;
		var tx_jettours_pi1_eObj = new tx_jettours_pi1_Engine(this.form_id, options);
		tx_jettours_pi1_eObj.send();
	},

	_void: function () {}

};

var promo_eteObj = null;

var initBubble = function() {

	promo_eteObj = new Bubble(999999, {entry:pages, y_min:115, y_max:115, x_min:386, x_max:957});
	promo_eteObj.start();

};

document.write('<link rel="stylesheet" type="text/css" href="fileadmin/libs/bubble/style.css" />');
document.write('<!--[if IE]><link rel="stylesheet" type="text/css" href="fileadmin/libs/bubble/style-lte-ie6.css" /><![endif]-->');

Event.observe(window, 'load', initBubble, false);