/* Mootools 1.11 est obligatoire */

function PopupSondage(id, altClose, startTitle, startContent, altClose2, endTitle, endContent, urlSondage, urlRessources){
	this.id = id;
	this.startTitle = startTitle;
	this.altClose = altClose;
	this.startContent = startContent;
	this.altClose2 = altClose2;
	this.endTitle = endTitle;
	this.endContent = endContent;
	this.urlSondage = urlSondage;
	this.urlRessources = urlRessources;
	this.outgoingLink = '';
	this.askMe = false;
	this.sondageWindow = null;
	
	
	this.show = function(title, content, isStart){
		if(window.ie6){
			var iFrame = new Element('iframe',{'id':'popup-sondage-iframe', 'src':'javascript:false;'});
			iFrame.setOpacity(0.000001);
		}
			var PopupOverlay = new Element('div', {'class':'popup-fond', 'id':'popup-sondage'});
			
			PopupOverlay.setOpacity(0.75);
			
			var popup = new Element('div', {'class':'popup'});
			
			// Header
			var popupHeader = new Element('div',{'class':'popup-titre'});
			var popupTitle = new Element('h1',{'tabindex':'-1'}).setHTML(title);
			var popupClose = new Element('p',{'class':'popup-fermer'});
			var textClose = '';
			if(isStart){
				textClose = this.altClose;
			}else{
				textClose = this.altClose2;	
			}
			var popupCloseLink = new Element('a',{'href':'#', 'events':{'click':(this.close).bindWithEvent(this)}}).setHTML('<img src="'+ this.urlRessources +'images/popup-btn-fermer.gif" alt="'+ textClose +'" />');
			
			popupTitle.injectInside(popupHeader);
			
			// Content
			
			popupContent = new Element('div', {'class':'popup-contenu'}).setHTML(content);
			
			// end
			popupHeader.injectInside(popup);
			popupContent.injectInside(popup);
			new Element('div', {'class':'popup-bas'}).injectInside(popup);
			
			popupCloseLink.injectInside(popupClose);
			popupClose.injectInside(popup);
			

			// Add to page
			var pageBody = $$('body');
			if(iFrame){
			   iFrame.injectTop(pageBody[0]);
			}
			PopupOverlay.injectTop(pageBody[0]);
			popup.injectAfter(PopupOverlay);
			
			
			// Move the content box
			popupSize = popup.getSize().scrollSize.y;
			windowHeight = window.getHeight();
			popup.setStyle('top',((windowHeight / 2) - (popupSize / 2)))
			
			this.resizeOverlay();
			this.positionOverlay();
			$$('.popup h1')[0].focus();
	}
	
	this.positionOverlay = function(){
		$('popup-sondage').setStyle('top',Window.getScrollTop()+'px');
		if($('popup-sondage-iframe')){
		  $('popup-sondage-iframe').setStyle('top',Window.getScrollTop()+'px');
		}
	}
			
	this.resizeOverlay = function(){
		if($('popup-sondage')){
			windowHeight = window.getHeight();
			$('popup-sondage').setStyle('height',windowHeight);
			if($('popup-sondage-iframe')){
			$('popup-sondage-iframe').setStyle('height',windowHeight);
			}
		}
	}
	
	this.showStart = function(){
		if(Cookie.get('sondage-'+this.id) !== 'nevershow'){
			this.show(this.startTitle, this.startContent, true);
			$$('.popup .popup-oui').addEvent('click', (this.wantToBeAsk).bindWithEvent(this));
			$$('.popup .popup-non').addEvent('click', (this.dontAskMe).bindWithEvent(this));
		}
	}
	
	this.showEnd = function(event){
		if(this.askMe === true){
			event = new Event(event);
			event.stopPropagation();
			event.preventDefault();
			this.outgoingLink = event.target.href;
			this.show(this.endTitle, this.endContent, false);
			$$('a.lien-goSondage').addEvent('click', (function(event){
			event = new Event(event);
				this.setCookie()
				try{
					this.sondageWindow.close();
				}catch(e){}
			}).bindWithEvent(this))
		}
	}
	
	this.close = function(event){
		event = new Event(event);
		event.stopPropagation();
		event.preventDefault();
		if($('popup-sondage')){
			$('popup-sondage').remove();
			$$('.popup')[0].remove();
			if($('popup-sondage-iframe')){
			$('popup-sondage-iframe').remove();
			}
		}
		if(this.outgoingLink !== ''){
			this.sondageWindow.close();
			window.location = this.outgoingLink;
		}
	}
	
	this.setCookie = function(){
		Cookie.set('sondage-'+this.id, 'nevershow', {'path':'/'});
	}
	
	this.dontAskMe = function(event){
		event = new Event(event);
		this.close(event);
		this.setCookie();
	}
	
	this.wantToBeAsk = function(event){
		event = new Event(event);

		this.sondageWindow = window.open(this.urlSondage,'SondageWindow','width='+window.getSize().scrollSize.x+',height='+(window.getSize().scrollSize.y -50));
		this.sondageWindow.blur();
		window.focus();
		this.askMe = true;
		this.close(event);
	}
	
	
	window.addEvent('resize', this.resizeOverlay);
	window.addEvent('scroll', this.positionOverlay);
	

	var liens = $$('a[href]')
	for(i=0; i<liens.length; i++){
		if(liens[i].href.test("^http://") && !liens[i].href.test("^http://www\.cnt\.gouv\.qc\.ca/") && !liens[i].href.test("#")){
			liens[i].addEvent('click', (this.showEnd).bindWithEvent(this));
			liens[i].addEvent('focus', (this.close).bindWithEvent(this));
		}
	}
	
	
	return this;
}				
