// global variables //
var TIMER = 3;
var SPEED = 10;
var WRAPPER = 'dialog_content';

// calculate the current window width //
function pageWidth() {
  return window.innerWidth != null ? window.innerWidth : document.documentElement && document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body != null ? document.body.clientWidth : null;
}

// calculate the current window height //
function pageHeight() {
  return window.innerHeight != null? window.innerHeight : document.documentElement && document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body != null? document.body.clientHeight : null;
}

// calculate the current window vertical offset //
function topPosition() {
  return typeof window.pageYOffset != 'undefined' ? window.pageYOffset : document.documentElement && document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop ? document.body.scrollTop : 0;
}

// calculate the position starting at the left of the window //
function leftPosition() {
  return typeof window.pageXOffset != 'undefined' ? window.pageXOffset : document.documentElement && document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft ? document.body.scrollLeft : 0;
}

// build/show the dialog box, populate the data and call the fadeDialog function //
function showDialog(title,whatMessage,param_1,param_2,type,autohide) {
  if (whatMessage == 'MSG_NEW_ACCOUNT') {
    //var theMessage = '<br /><div style=\"padding-right:115px; font-size:18px; font-family: Arial, Helvetica, sans-serif;\">Selecting to sign-up with Facebook Connect will create a NEW account for you on 10to1. &nbsp; <input type="button" value="  OK  " onclick="document.location.href=\'http://www.10to1.com/fb_connect.php\';"></div>';

  } else if (whatMessage == 'MSG_GET_EMAIL') {
    var theMessage = '<br /><div style=\"padding-right:5px; font-size:18px; font-family: Arial, Helvetica, sans-serif;\">      <form name="form_email" method="GET" action="http://www.10to1.com/fb_emailPrompt.php" onsubmit="if(document.form_email.emailAddr.value!=\'\' && document.form_email.userName.value!=\'\'){return true;}else{return false;}">   Please enter your details to associate your new 10to1 account with Facebook. <br />       Email: <input type="text" name="emailAddr">     <br />User Name: <input type="text" name="userName" value="' + param_1 + '">      <input type="submit" name="userButt" value="Accept">        </form>    </div>';

  } else if (whatMessage == 'MSG_LOGIN') {
    var theMessage = '<div style=\"padding:3px; font-size:14px; font-family: Arial, Helvetica, sans-serif;\">			<form name="form_login" action="http://www.10to1.com/login.php" method="POST" style="display:inline;">		<table width="100%" border="0" padding="0" style="font-size:14px;"> <tr> <td>		<nobr>Username:&nbsp;<input type="text" name="username" size="10" maxlength="30" tabindex="1">			<br />Password:&nbsp;<input type="password" name="password" size="10" maxlength="20" style="margin-left:2px;" tabindex="2">	</nobr>		<br /><span style="padding-left:1px;">Remember Me? <input type="checkbox" name="remember" value="t" tabindex="3">  &nbsp;&nbsp;	<input type="submit" name="standardLogin" value="Login" tabindex="4">	</td>			<td align="right" valign="top"> 	<a href="#" onclick="document.location.href=\'http://www.10to1.com/login.php?forgotpw=1#pw\'">Forgot Password?</a>	    </td></tr>	</table>		</form>			<hr size="3" width="63%" align="left" style="color:#d4dadf; margin-top:6px; margin-bottom:6px;" noshade>			Or <b>login</b> with Facebook:<br />			<a href="http://www.10to1.com/fb_connect.php"><img style="padding-top:4px;" id="fb_login_image" src="http://www.10to1.com/Images/Site_Images/fb_logo_connect.gif" alt="Connect"/></a>     <br />	<br />	<a href="#" onclick="document.location.href=\'http://www.10to1.com/register.php\'">New to 10to1? Create an account here...</a>	</div>';
  }


  if(!type) {
    type = 'error';
  }
  var dialog;
  var dialogheader;
  var dialogclose;
  var dialogtitle;
  var dialogcontent;
  var dialogmask;
  if(!document.getElementById('dialog')) {
    dialog = document.createElement('div');
    dialog.id = 'dialog';
    dialogheader = document.createElement('div');
    dialogheader.id = 'dialog-header';
    dialogtitle = document.createElement('div');
    dialogtitle.id = 'dialog-title';
    dialogclose = document.createElement('div');
    dialogclose.id = 'dialog-close'
    dialogcontent = document.createElement('div');
    dialogcontent.id = 'dialog-content';
    dialogmask = document.createElement('div');
    dialogmask.id = 'dialog-mask';
    document.body.appendChild(dialogmask);
    document.body.appendChild(dialog);
    dialog.appendChild(dialogheader);
    dialogheader.appendChild(dialogtitle);
    dialogheader.appendChild(dialogclose);
    dialog.appendChild(dialogcontent);;
    dialogclose.setAttribute('onclick','hideDialog()');
    dialogclose.onclick = hideDialog;
  } else {
    dialog = document.getElementById('dialog');
    dialogheader = document.getElementById('dialog-header');
    dialogtitle = document.getElementById('dialog-title');
    dialogclose = document.getElementById('dialog-close');
    dialogcontent = document.getElementById('dialog-content');
    dialogmask = document.getElementById('dialog-mask');
    dialogmask.style.visibility = "visible";
    dialog.style.visibility = "visible";
  }
  dialog.style.opacity = .00;
  dialog.style.filter = 'alpha(opacity=0)';
  dialog.alpha = 0;
  var width = pageWidth();
  var height = pageHeight();
  var left = leftPosition();
  var top = topPosition();
  var dialogwidth = dialog.offsetWidth;
  var dialogheight = dialog.offsetHeight;
  var topposition = top + (height / 3) - (dialogheight / 2);
  var leftposition = left + (width / 2) - (dialogwidth / 2);
  dialog.style.top = topposition + "px";
  dialog.style.left = leftposition + "px";
  dialogheader.className = type + "header";
  dialogtitle.innerHTML = title;
  dialogcontent.className = type;
  dialogcontent.innerHTML = theMessage;
  var dialog_content = document.getElementById(WRAPPER);
  dialogmask.style.height = dialog_content.offsetHeight + 'px';
  dialog.timer = setInterval("fadeDialog(1)", TIMER);
  if(autohide) {
    dialogclose.style.visibility = "hidden";
    window.setTimeout("hideDialog()", (autohide * 1000));
  } else {
    dialogclose.style.visibility = "visible";
  }
}

// hide the dialog box //
function hideDialog() {
  var dialog = document.getElementById('dialog');
  clearInterval(dialog.timer);
  dialog.timer = setInterval("fadeDialog(0)", TIMER);
}

// fade-in the dialog box //
function fadeDialog(flag) {
  if(flag == null) {
    flag = 1;
  }
  var dialog = document.getElementById('dialog');
  var value;
  if(flag == 1) {
    value = dialog.alpha + SPEED;
  } else {
    value = dialog.alpha - SPEED;
  }
  dialog.alpha = value;
  dialog.style.opacity = (value / 100);
  dialog.style.filter = 'alpha(opacity=' + value + ')';
  if(value >= 99) {
    clearInterval(dialog.timer);
    dialog.timer = null;
  } else if(value <= 1) {
    dialog.style.visibility = "hidden";
    document.getElementById('dialog-mask').style.visibility = "hidden";
    clearInterval(dialog.timer);
  }
}