//QUESTIONNAIRE OPENS AFTER A GIVEN TIME
var questionnaireTimeLoad = {
  params: {
    title: 'Questionnaire on Vestas.com - Feedback on Vestas.com',
	content: 'Dear visitor,<br /><br />We hope you will spend 5-7 minutes answering a few questions to help us make the website even better. <br /><br />If you complete the survey and sign up to our user panel we will draw 5 winners and send them a LEGO turbine.<br /><br />You are of course anonymous. <br /><br /><br /><br /><a href="http://vovici.com/wsb.dll/s/1198bg4d3fd" target="_blank" style="margin-left:120px; padding: 4px 15px; margin-right: 5px; background: #666; color: #fff; text-decoration: none;">Participate in survey</a>&nbsp;<span onclick="questionnaireTimeLoad.later();" style="cursor: pointer; margin-right: 5px; padding: 4px; background: #555; font-weight: bold; color: #888;">Ask me later</span>&nbsp;<span onclick="questionnaireTimeLoad.close()" style="cursor:pointer; padding: 4px; background: #555; font-weight: bold; color: #888;">No thanks</span>',
    campaign: 'KAMPAGNE01',
    expireDays: 90,
    button: '/Files/System/poponexit/images/popup_close.png',
    transBg: '/Files/System/poponexit/images/trans_bg.png',	
    elm: 'questionnaireTimeLoad'
  },
  load: function() {
    	var date = new Date();
		
		if(this.testCookie(date)) {
      		var ca = document.cookie.split(';'),
       		shown = false;
			
			for(var i = 0; i < ca.length; i++) {
				if(ca[i].indexOf(this.params.campaign) >= 0) {
					shown = true;
					
					break;
				}
			}

      		if(!shown) {
				date.setDate(date.getDate() + this.params.expireDays);
				
				if(this.getCookie("timeCheck")){
					var now = new Date();
					var then = new Date(this.getCookie("timeCheck"));

					if(this.getTimeDifference(now, then) > 120) {
						document.cookie = this.params.campaign +'=true; expires='+ date.toGMTString() +'; path=/;';
						questionnaireTimeLoad.open();		
					} else {
					}
				} else {
					var creationDate = new Date();
					document.cookie = "timeCheck="+ creationDate;
				}
      		}
    	}
  },
  getCookie: function(name) {
	var results = document.cookie.match ( '(^|;) ?' + name + '=([^;]*)(;|$)' );

	if ( results )
		return ( unescape ( results[2] ) );
	else
		return null;	
  },
  testCookie: function(date) {
		date = escape(date);
		document.cookie = '_'+ date;
		
    	if(document.cookie.indexOf(date) > 0) {
			return true;
    	}	
	return false;
  },
  later: function () {
	var dt = new Date(), expiryTime = dt.setTime( dt.getTime() + 3600000 );
	document.cookie = this.params.campaign +'=true; expires='+ dt.toGMTString() +'; path=/;';
  	questionnaireTimeLoad.close();
  },
  getTimeDifference: function(then, now) {
		var date1_ms = then.getTime();
		var date2_ms = now.getTime();
		var difference_ms = Math.abs(date1_ms - date2_ms)
		return Math.floor(difference_ms/1000);
  },
  open: function() {
		var bgElm = document.createElement('div');
		var bgElmName = 'surveyPopupLayer';  
		bgElm .innerHTML = '<div style="width: 100%; height: 100%; float: left; background: transparent url('+ this.params.transBg +') 0 0; position: fixed; top: 0; left: 0; zoom: 1"></div>';
		bgElm.setAttribute('id', bgElmName);
		var bgElemGrab = document.getElementById(this.params.elm); 
		bgElemGrab.parentNode.insertBefore(bgElm, bgElemGrab);  
  
    var elm = document.getElementById(this.params.elm),
        height = window.innerHeight ? window.innerHeight : (document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight);
    
elm.innerHTML = '<iframe width="600" id="'+this.params.elm +'_iframe" frameborder="0" style="display:none"></iframe><h3 onclick="questionnaireTimeLoad.close()" style="height: 23px;margin:0;padding: 2px 8px;font: bold 11px/21px Verdana, Sans-Serif;color: #ffffff;background: #666 url('+ this.params.button +') no-repeat 474px 6px;"><span style="float: right !important;cursor: pointer;margin: -5px -2px 0 0;padding: 5px 5px 5px 8px;">&nbsp;</span>'+ this.params.title +'</h3><div style="margin: 0; background: #333333; padding: 10px 10px 15px 10px;font: normal 11px/16px Verdana, Sans-Serif;color: #fff">'+ this.params.content +'</div>';
    
    var links = elm.getElementsByTagName('a');
    for(var i = 0; i < links.length; i++) {
      var link = links[i];
      link.style.fontWeight = 'bold';
      link.style.color = '#fff';
      link.style.textDecoration = 'none';
      link.onclick = function() { questionnaireTimeLoad.close(); };
    }
    
    elm.style.position = 'absolute';
    elm.style.zIndex = '99999999';
    elm.style.left = '50%';
    elm.style.display = 'block';
    elm.style.width = '600px';
    elm.style.marginLeft = '-300px';
    elm.style.textAlign = 'left';
    elm.style.background = '#333333';
    elm.style.border = '2px solid #fff';
    elm.style.top = (height/2) - (elm.clientHeight/2) +'px';
    
    var iframe = document.getElementById(this.params.elm +'_iframe');
    iframe.height = elm.clientHeight;
    iframe.style.position = 'absolute';
    iframe.style.zIndex = '-1';
    iframe.style.display = 'block';
  },
  close: function() {
    document.getElementById('surveyPopupLayer').style.display = 'none';
	document.getElementById('questionnaireTimeLoad').style.display = 'none';
    return false;
  }
}

/*window.onload = function() {
  QUESTIONNAIRE.load();
}*/
