var INFO = 0; 
var ERROR = 1;
var QUESTION = 2;
var HELP = 3;

function displayMessage(type, title, text, work) { 
	placeBackdrop();
	
	var elm = document.getElementById('message_ok');
	
	if ( elm.addEventListener ) {
	document.getElementById('message_ok').addEventListener('click', function() { 
		clearMessage();
		work();
	}, false);
	} else if ( elm.attachEvent ) { 
		var r = elm.attachEvent('onclick', function() { 
			clearMessage();
			work();		
		});
	}
	
	var message = document.getElementById('message_dialog');
	
	var arrayPageSize = getPageSize();
	var leftPos = (arrayPageSize[0] / 2) - 200+'px';
	message.style.zIndex = 99;
	message.style.left = leftPos;
	
	var icon = document.getElementById('message_icon');
	if ( type == INFO ) { 
		icon.src = buildURL('/common/images/messages/info.png');
	} else if ( type == ERROR ) { 
		icon.src = buildURL('/common/images/messages/error.png');		
	} else if ( type == QUESTION ) {
		icon.src = buildURL('/common/images/messages/question.png');
		var no = document.getElementById('message_no');
		no.style.display = '';
		no.addEventListener('click', function() { 
			document.getElementById('message_no').style.display = 'none';
			clearMessage();
		}, false);
		document.getElementById('message_ok').value = 'Yes';
		
	} else if ( type == HELP ) { 
		icon.src = buildURL('/common/images/messages/help.png');
	}
	
	document.getElementById('message_title').innerHTML = title;
	document.getElementById('message_text').innerHTML = text;
	
	message.style.display = '';
	document.getElementById('message_icon').focus();	
}

function clearMessage() {
	document.getElementById('message_dialog').style.display = 'none';
	removeBackdrop();
}


function shiftOpacity(id, work, opacMin, opacMax) {
	if ( timerInterval ) { 
		clearTimeout(timerInterval);
	}
	
	var start = 0;
	var end = 80;
	
	if ( opacMin ) { 
		start = opacMin;
	}
	
	if ( opacMax ) { 
		end = opacMax;
	}

    //if an element is invisible, make it visible, else make it invisible
    if(document.getElementById(id).style.opacity == start) {
        opacity(id, start, end, work);
    } else {
        opacity(id, end, start, work);
    }
} 

var timerInterval;

function opacity(id, opacStart, opacEnd, work) {

    //speed for each frame
    millisec = 1000;
    var speed = Math.round(millisec / 100);
    var timer = 0;

    //determine the direction for the blending, if start and end are the same nothing happens
    if(opacStart > opacEnd) {
        for(i = opacStart; i >= opacEnd; i--) {
            timerInterval = setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    } else if(opacStart < opacEnd) {
        for(i = opacStart; i <= opacEnd; i++) {
            timerInterval = setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    }

	setTimeout(work ,(timer * speed));
}

//change the opacity for different browsers
function changeOpac(opacity, id) {
    var object = document.getElementById(id).style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
} 

//
// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.org
//
function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}



//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

function placeBackdrop() {
	var backdrop = document.getElementById('backdrop');
	if ( backdrop == null ) { 
		backdrop = document.createElement('div');
		backdrop.id = 'backdrop';
		backdrop.style.position = 'absolute';
		backdrop.style.top = '0px';
		backdrop.style.left = '0px';
		backdrop.style.backgroundImage = "url('"+buildURL('common/images/backdrop.png')+"')";
		backdrop.style.backgroundRepeat = 'repeat';
		backdrop.style.zIndex = '98';
		
		var arrayPageSize = getPageSize();
		var arrayPageScroll = getPageScroll();
	
		// set height of Overlay to take up whole page and show
		backdrop.style.height = (arrayPageSize[1] + 'px');
		backdrop.style.width = (arrayPageSize[0]+'px');
	}
	
	document.getElementsByTagName('body')[0].appendChild(backdrop);
}

function removeBackdrop() { 
	if ( document.getElementById('backdrop') ) {
		document.getElementsByTagName('body')[0].removeChild(document.getElementById('backdrop'));
	}
}