var poonActive = true;




function getPoonStatus(){
	controller.requestController('getPoonStatus', '', poonStatusReceived);
}

function poonStatusReceived(result){
	if(result.status=='hide'){
		hidePoon(true);
	} else {
		startPoonProcess();
	}
}

function startPoonProcess(){
  playPoonWorkflow([10,11], 7000);
}







function getWindowWidthHeight() {
	var myWidth = 0, myHeight = 0;
	if(typeof(window.innerWidth) == 'number'){
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)){
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} else if(document.body && (document.body.clientWidth || document.body.clientHeight)){
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}

	return {'width' : parseFloat(myWidth), 'height' : parseFloat(myHeight)};
}

function openPoonMessage(idMessage, displayNextMessage){
  var message = '';
  var position = {};
  var options = {status:''};
  var processMessage = true;
  var win = getWindowWidthHeight();
  var borderSize = 4;

  if(timeoutHandlePoon!=null){
    clearTimeout(timeoutHandlePoon);
    timeoutHandlePoon = null;
  }
  
  switch(idMessage){
// HELLO
    case 0:
      message = '<h2 style="text-align:center; margin:0; padding:0;">Hi, I am Ptoon</h2>';
      position = {'left':300,'top':200};
      options = {'status':'right', 'noClose':true};
      break;
    case 1:
      message = 'Want to chat with your neighbors, locate your friends, organize a party, hire a babysitter?<br /><br />'
              + 'Want to create a community web site for your school, shop, district, restaurant?';
      position = {'left':300,'top':200};
      options = {'status':'right', msgWidth:350, 'noClose':true};
      break;
    case 2:
      message = '<h2 style="margin:0;margin-bottom:10px; padding:0;">Welcome</h2>'
              + '<b>Publing</b>\'s mission is to help people connect ... in their neighborhood.';
      position = {'left':300,'top':200};
      options = {'status':'right', msgWidth:350, 'noClose':true};
      break;
    case 3:
      message = '<div style="padding:10px;">'
              + ' <img src="/Img/publing.png" /><br /><br />'
              + ' <form onsubmit="return verifBetaPassword();" style="text-align:center; margin:0px; padding:0px;">'
              + '  Beta Tester Code: <input type="password" id="splashScreen.password" /><br /><br />'
              + '  <input type="submit" id="splashScreen.access" value="What\'s up..." style="padding:10px; font-weight:bold;" />'
              + ' </form>'
              + ' <div id="splashScreen.result" style="font-size:0.8em; color:#F00;"></div>'
              + ' <p style="font-size:0.7em;">'
              + '  You want to have a Beta Tester Code please send an email '
              + '  to <a href="mailto:beta@publing.com">beta@publing.com</a>'
              + ' </p>'
              + '</div>';
      position = {'left':Math.floor((win.width-500)/2-100),'top':Math.floor((win.height-300)/2)};
      options = {'status':'right', msgWidth:500, 'noClose':true};
      borderSize = 8;
      break;


// THE MAP
    case 10:
      if(podList.length==0)map.whereToGo('paris');
      message = '<h2 style="margin:0; padding:0;">The Map</h2>'
              + 'On the map you may, click on:<br />'
              + ' - <b>1</b> The <b>PODs</b> (places)<br />'
              + ' - <b>2</b> The <b>Users</b><br />';
      position = {'right':50,'top':250};
      options = {'status':'left'};
      break;
    case 11:
      if(podList.length==0)map.whereToGo('paris');
      message = '<h2 style="text-align:center; margin-top:20px; margin-bottom:20px;">Click on a POD <img width="20" src="/Img/pushpin/pushpin.gif" align="absmiddle" /></h2>';
      position = {'right':50,'top':250};
      options = {'status':'left'};
      break;


// THE POD
    case 20:
      message = '<h2 style="margin:0; padding:0;">The POD</h2>'
              + 'The POD contains a lot of different <b>applications</b>.<br />';
      position = {'left':70,'top':10};
      options = {'status':'bottom'};
      break;
    case 21:
      message = '<h2 style="margin:0; padding:0;">The POD</h2>'
              + 'Each tab (on the left) is an Application.<br /><b>click on them</b>.';
      position = {'left':70,'top':10};
      options = {'status':'bottom'};
      break;
    case 22:
      message = '<h2 style="margin:0; padding:0;">The POD</h2>'
              + 'click on the <b>close button</b> to close the POD.';
      position = {'right':80,'top':25};
      options = {'status':'right'};
      break;


// THE SEARCH TOOL
    case 30:
      message = '<h2 style="margin:0; padding:0;">Search</h2>'
              + 'If you are looking for something, here is the search tool.';
      position = {'left':260,'bottom':50};
      options = {'status':'bottom'};
      break;
    case 31:
      message = '<h2 style="margin:0; padding:0;">Search</h2>'
              + 'Search here for content, contact or POD.';
      position = {'left':120,'bottom':50};
      options = {'status':'bottom'};
      break;


// THE WHERE TO GO
    case 40:
      message = '<h2 style="margin:0; padding:0;">Where to go ?</h2>'
              + 'type here an address to move on the map.';
      position = {'left':440,'bottom':50};
      options = {'status':'bottom'};
      break;


// SIGN UP
    case 50:
      message = '<h2 style="margin:0; padding:0;">Sign up</h2>'
              + 'To build up your own communities, first create your account.';
      position = {'right':100,'top':50};
      options = {'status':'top'};
      break;


// GOODBYE
    case 60:
      message = '<h2 style="text-align:center; margin:0; padding:0;">If you need help, click on my icon.</h2>';
      position = {'left':150,'top':50};
      options = {'status':'top', 'noClose':true};
      break;


// My Publing
    case 70:
      message = '<h2 style="margin:0; padding:0;">My Publing</h2>'
              + 'This is an important button, here you get all your news';
      position = {'right':350,'top':50};
      options = {'status':'top'};
      break;
    case 71:
      message = '<h2 style="margin:0; padding:0;">Chat</h2>'
              + 'click here to open the chat window.';
      position = {'right':140,'bottom':50};
      options = {'status':'bottom'};
      break;
    default:
      processMessage = false;
      break;
  }

  if(poonActive){
    if(processMessage){
      if(!Object.isUndefined(displayNextMessage) && displayNextMessage)message+='<p style="font-size:0.7em; text-align:right; margin:0; padding:0;"><a href="#" onclick="playPoonWorkflow();">next message &gt;</a></p>';
      publingToon.showHint(position, generatePoonMessage(message, borderSize, options), options);
      return true;
    } else {
      publingToon.hide();
      return false;
    }
  }
}

function generatePoonMessage(message, borderSize, options){
  if(Object.isUndefined(options.noClose)) message = '<img width="12px" height="12px" src="./Img/closeButton.gif" class="button" style="float: right; margin-right:-6px; margin-top:-6px;" onclick="hidePoon()" />' + message;
  return generateWindow(message, borderSize)
}


var timeoutHandlePoon = null;
var actionListPoon = null;
var timerListPoon = null;
function playPoonWorkflow(actions, timer){

  if(timeoutHandlePoon != null){
    clearTimeout(timeoutHandlePoon);
    timeoutHandlePoon = null;
  }

  var theTime = 0;

  if(actions) actionListPoon = actions;
  if(timer) timerListPoon = timer;

  if(Object.isArray(timerListPoon)) theTime = timerListPoon.shift();
  else theTime = timerListPoon;
  
  openPoonMessage(actionListPoon.shift(), actionListPoon.length>0);
  if(actionListPoon.length>0) timeoutHandlePoon = setTimeout('playPoonWorkflow();', theTime);
  
  return false;
}

var hidePoonTimeoutHandle = null;
function hidePoon(noDisplay){
	controller.requestController('setPoonStatus', 'status=hide');
  $('poonButton').style.display = 'block';
  openPoonMessage(60);
  if(noDisplay) publingToon.hide();
  else hidePoonTimeoutHandle = setTimeout(publingToon.hide.bindAsEventListener(publingToon), 2000);
  poonActive = false;
}

function showPoon(){
	controller.requestController('setPoonStatus', 'status=show');
  clearTimeout(hidePoonTimeoutHandle);
  $('poonButton').style.display = 'none';
  poonActive = true;
  showPoonRandomMsgOnMap();
}


function showPoonRandomMsgOnMap(){
  var msgs = [30, 40];
  if(myLoginStatus.result!='OK'){
    msgs.unshift(50);
  } else {
    msgs.unshift(70);
    msgs.unshift(71);
  }
  openPoonMessage(msgs[Math.round(Math.random()*(msgs.length-1))]);
}