// global variables //
var TIMER = 5;
var SPEED = 10;
var WRAPPER = 'alertContent';
var OPACITY = 85;
// 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 //
// needMask = true means:
// the dialog-mask is a grey layer
// the dialog-main has a white bg and black font

//needMask = false means:
//the dialog-mask is a white layer
//the dialog-main has a grey bg and white font 

function showDialog(message, needMask, callBackOK, callBackCancel, overridePosY) {
	var title="BMW AUC alert"
	message = message.replace( new RegExp( "\\n", "g" ), "<br />");
	
  var dialog; 
  var dialogcontent;
  var dialogmask;
  if (!callBackOK){
	  callBackOK="";
  }
  var needCancel= false;
  if (!callBackCancel){
	  callBackCancel="";
  } else{
	  needCancel=true;
  }  
  var footer = document.getElementById('footerstrip-small') || document.getElementById('footerstrip-wide');
  var maskHeight="200%";
  
  if (footer){
	  if (footer.offsetTop+footer.offsetHeight>800){
		  maskHeight = footer.offsetTop+footer.offsetHeight+"px";
	  }
  }
  
  var htm = "<a onclick=\"hideDialog('"+callBackOK+"');return false;\" class=\"auc-blue-button\" href=\"#\">OK</a>";
  var htmlCancel ="";
  if (needCancel){
	  htmlCancel = "<a onclick=\"hideDialog('"+callBackCancel+"'); return false;\" class=\"auc-blue-cancel-button\" href=\"#\">Cancel</a>";
  }
  if(!document.getElementById('dialog-main')) {
	  
    dialog = document.createElement('div');
    dialog.id = 'dialog-main';

    dialogcontent = document.createElement('div');
    dialogcontent.id = 'dialog-content';
    
    dialogbutton = document.createElement('div');
    dialogbutton.id = 'dialog-button';
    dialogbutton.setAttribute("class", "button-on-left");
    if (! needCancel){
    	dialogbutton.style.textAlign="center";
    }
    if (needCancel){
    	dialogbutton.innerHTML=htmlCancel+htm;	
    }else{
    	dialogbutton.innerHTML=htm;
    }
    
    
    dialogmask = document.createElement('div');
    dialogmask.id = 'dialog-mask';
    var browserHeight= 0;
    var isIE = false || (document.all) ;
    if (!isIE) {  
    	browserHeight = document.body.offsetHeight;  
      } else {
    	  browserHeight=document.getElementById('container').scrollHeight;;
      }  	
    
    dialogmask.style.height= maskHeight;
    
    if (needMask){
    	dialog.className='dialog-main-black';
    	dialogcontent.className='dialog-content-black';
    	dialogmask.className='dialog-mask-black';
    }else{
    	dialog.className='dialog-main-white';
    	dialogcontent.className='dialog-content-white';
    	dialogmask.className='dialog-mask-white';
    }    
    
    document.body.appendChild(dialogmask);
    document.body.appendChild(dialog);
    dialog.appendChild(dialogcontent);
    dialog.appendChild(dialogbutton);
    
  }else {
	    dialog = document.getElementById('dialog-main');
	    dialogcontent = document.getElementById('dialog-content');    
		dialogmask = document.getElementById('dialog-mask');	    
		dialogbutton = document.getElementById('dialog-button');
	    if (needCancel){
	    	dialogbutton.innerHTML=htmlCancel+htm;	
	    }else{
	    	dialogbutton.innerHTML=htm;
	    }
	    if (needMask){
	    	dialog.className='dialog-main-black';
	    	dialogcontent.className='dialog-content-black';
	    	dialogmask.className='dialog-mask-black';
	    }else{
	    	dialog.className='dialog-main-white';
	    	dialogcontent.className='dialog-content-white';
	    	dialogmask.className='dialog-mask-white';	    	
	    }
	    dialogmask.style.height= maskHeight;
		dialogmask.style.visibility = "visible";
		dialog.style.visibility = "visible";

	  } 
  
  
  
  dialog.style.opacity = OPACITY/100;
  dialog.style.filter = 'alpha(opacity='+OPACITY+')';
  dialog.alpha = OPACITY;
  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 = (isDolnAucJs ? 200 : topposition) + "px"; - TODO check forms
  if (isDolnAucJs) {
	  dialog.style.top = (overridePosY?overridePosY:200) + "px";
  } else {
	  dialog.style.top = topposition + "px";
  }
  dialog.style.left = leftposition + "px";
  dialogcontent.innerHTML = message;
  var content = document.getElementById(WRAPPER);


}

// hide the dialog box //
// callback is the redirect callback
function hideDialog(callback) {
  var dialog = document.getElementById('dialog-main');
  clearInterval(dialog.timer);
  dialog.timer = setInterval("fadeDialog(0,'"+callback+"')", TIMER);
}

// fade-in the dialog box //
function fadeDialog(flag,callback) {
  if(flag == null) {
    flag = 1;
  }
  var dialog = document.getElementById('dialog-main');
  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 >= OPACITY) {
    clearInterval(dialog.timer);
    dialog.timer = null;
  } else if(value <= 1) {
    dialog.style.visibility = "hidden";
    if (document.getElementById('dialog-mask')){
    	document.getElementById('dialog-mask').style.visibility = "hidden";
    	if (callback){
    		eval(callback);
    	}
    }
    clearInterval(dialog.timer);
  }
  
function showConfirm(message,needMask,callBackOK, callbackCancel){
	
}  
}
