//************************************************************************************
// Copyright (C) 2006, Massimo Beatini
//
// This software is provided "as-is", without any express or implied warranty. In 
// no event will the authors be held liable for any damages arising from the use 
// of this software.
//
// Permission is granted to anyone to use this software for any purpose, including 
// commercial applications, and to alter it and redistribute it freely, subject to 
// the following restrictions:
//
// 1. The origin of this software must not be misrepresented; you must not claim 
//    that you wrote the original software. If you use this software in a product, 
//    an acknowledgment in the product documentation would be appreciated but is 
//    not required.
//
// 2. Altered source versions must be plainly marked as such, and must not be 
//    misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
//************************************************************************************

//
// global variables
//
var NS6=false;
var IE4=(document.all);
var IE4XHTML = false;
if (!IE4) {
	NS6=(document.getElementById);
} else {
	IE4XHTML = (document.documentElement);
	// && document.documentElement.scrollLeft
	//( (document.documentElement.scrollLeft>=0) || (document.documentElement.scrollTop>=0) );
}
var NS4=(document.layers);

var dimmer_isMozilla;
var dimmer_objDiv = null;
var dimmer_originalDivHTML = "";
var dimmer_DivID = "";
var dimmer_over = false;
var dimmer_disable = false;

var dimmer_Width = 0;
var dimmer_Height = 0;

//
// dinamically add a div to 
// dim all the page
//

function DimmerMoveDisable() {
	dimmer_disable = true;
}
function DimmerMoveEnable() {
	dimmer_disable = false;
}
function IsDimmerEnabled() {
	return true; //(dimmer_disable==false);
}

function buildDimmerDiv() {
    document.write('<div id="dimmer" class="dimmer" style="width:50px; height:50px"></div>');
}

function adjustDimmerDiv() {
	/*
	if ((NS4) || (NS6)) {
		//alert(window.clientWidth);
		theDIMfloat_offsetWidth = document.body.clientWidth;
		theDIMfloat_offsetHeight = document.body.offsetHeight; //window.innerHeight;
	}
	if (IE4) {
		if (IE4XHTML) {
			theDIMfloat_offsetWidth = document.documentElement.offsetWidth;
			theDIMfloat_offsetHeight = document.documentElement.offsetHeight;
		} else {
			theDIMfloat_offsetWidth = document.body.offsetWidth;
			theDIMfloat_offsetHeight = document.body.offsetHeight;
		}
		theDIMfloat_offsetWidth = theDIMfloat_offsetWidth - 25;
	}
	*/
	if ((NS4) || (NS6)) { 
		theDIMfloat_offsetWidth = (document.body.clientWidth);
		theDIMfloat_offsetHeight = (window.outerHeight);
	} else if (IE4) {
		if (IE4XHTML) {
			theDIMfloat_offsetWidth = document.documentElement.offsetWidth;
			theDIMfloat_offsetHeight = document.documentElement.offsetHeight;
		} else {
			theDIMfloat_offsetWidth = document.body.offsetWidth;
			theDIMfloat_offsetHeight = document.body.offsetHeight;
		}
	}
	var d = document.getElementById('dimmer');
	theDIMfloat_offsetWidth = theDIMfloat_offsetWidth-80;
	d.style.width =  theDIMfloat_offsetWidth + 'px';
    d.style.height = theDIMfloat_offsetHeight + 'px';
}


function displayFloatingDivQuickPos() {
	if ((NS4) || (NS6)) { 
		newXCoordinate=Math.round((window.innerWidth-20)/2)-Math.round(thefloat_layerwidth/2);
		newYCoordinate=Math.round((window.innerHeight-20)/2)-Math.round(thefloat_layerheight/2);
	}
	if (IE4) {
		if (IE4XHTML) {
			thefloat_offsetWidth = document.documentElement.offsetWidth;
			thefloat_offsetHeight = document.documentElement.offsetHeight;
		} else {
			thefloat_offsetWidth = document.body.offsetWidth;
			thefloat_offsetHeight = document.body.offsetHeight;
		}
		newXCoordinate=Math.round((thefloat_offsetWidth-20)/2)-Math.round(thefloat_layerwidth/2);
		newYCoordinate=Math.round((thefloat_offsetHeight-20)/2)-Math.round(thefloat_layerheight/2);
	}
	displayFloatingDiv(dimmer_DivID, '', dimmer_Width, dimmer_Height, newXCoordinate, newYCoordinate)
}

function displayFloatingDiv(divId, title, width, height, left, top) {
	dimmer_DivID = divId;
	dimmer_Width = width;
	dimmer_Height = height;

	adjustDimmerDiv();
	document.getElementById('dimmer').style.visibility = "visible";
	
	var divDimmmer = document.getElementById(divId);
	divDimmmer.style.width = width + 'px';
    divDimmmer.style.height = height + 'px';
    divDimmmer.style.left = left + 'px';
    divDimmmer.style.top = top + 'px';
	divDimmmer.className = 'dimming';
	divDimmmer.style.visibility = "visible";
}

function hiddenFloatingDiv(divId) 
{
	document.getElementById(divId).innerHTML = dimmer_originalDivHTML;
	document.getElementById(divId).style.visibility='hidden';
	document.getElementById('dimmer').style.visibility = 'hidden';
	
	dimmer_DivID = "";
}

//
//
//
function MouseDown(e) 
{
	if (dimmer_over)
    {
		if (IsDimmerEnabled()) {
		if (dimmer_isMozilla) {
            dimmer_objDiv = document.getElementById(dimmer_DivID);
            X = e.layerX;
            Y = e.layerY;
            return false;
        }
        else {
            dimmer_objDiv = document.getElementById(dimmer_DivID);
            dimmer_objDiv = dimmer_objDiv.style;
            X = event.offsetX;
            Y = event.offsetY;
        }
		}
    }
}


//
//
//
function MouseMove(e) 
{
	if (dimmer_objDiv) {
        if (IsDimmerEnabled()) {
		if (dimmer_isMozilla) {
            dimmer_objDiv.style.top = (e.pageY-Y) + 'px';
            dimmer_objDiv.style.left = (e.pageX-X) + 'px';
            return false;
        }
        else 
        {
            dimmer_objDiv.pixelLeft = event.clientX-X + document.body.scrollLeft;
            dimmer_objDiv.pixelTop = event.clientY-Y + document.body.scrollTop;
            return false;
        }
		}
	}
}

//
//
//
function MouseUp() 
{
    dimmer_objDiv = null;
}


//
//
//
function dimmer_init()
{
    // check browser
    dimmer_isMozilla = (document.all) ? 0 : 1;


	if (dimmer_isMozilla) 
    {
        document.captureEvents(Event.MOUSEDOWN | Event.MOUSEMOVE | Event.MOUSEUP);
    }
	

    document.onmousedown = MouseDown;
    document.onmousemove = MouseMove;
    document.onmouseup = MouseUp;

    // add the div
    // used to dim the page
	//buildDimmerDiv();

}

// call init
dimmer_init();


function dimmerOSWG_showPopup (targetObjectId, eventObj,targetTitle) {
	
	// hide any currently-visible popups
	hideCurrentPopup();
	// stop event from bubbling up any farther
	
	// move popup div to current cursor position 
	// (add scrollTop to account for scrolling for IE)
	
	if(eventObj)eventObj.cancelBubble = true;
	
	var dimmerP = getObject('dimmer');
	
	var dimmerOSWGpanel = Ext.get(targetObjectId);
	if (dimmerOSWGpanel!=undefined) {
		dimmerOSWGpanel_layerwidth = dimmerOSWGpanel.dom.clientWidth;
		dimmerOSWGpanel_layerheight = dimmerOSWGpanel.dom.clientHeight;

		if ((NS4) || (NS6)) { 
			dimmerOSWGpanel_floatX=Math.round((window.innerWidth-20)/2)-Math.round(dimmerOSWGpanel_layerwidth/2);
			dimmerOSWGpanel_floatY=Math.round((window.innerHeight-20)/2)-Math.round(dimmerOSWGpanel_layerheight/2);
			
			dimmerOSWGpanel_lastX=window.pageXOffset + dimmerOSWGpanel_floatX;
			dimmerOSWGpanel_lastY=window.pageYOffset + dimmerOSWGpanel_floatY;	
		} else if (IE4) {
			if (IE4XHTML) {
				dimmerOSWGpanel_offsetWidth = document.documentElement.offsetWidth;
				dimmerOSWGpanel_offsetHeight = document.documentElement.offsetHeight;
			} else {
				dimmerOSWGpanel_offsetWidth = document.body.offsetWidth;
				dimmerOSWGpanel_offsetHeight = document.body.offsetHeight;
			}
			dimmerOSWGpanel_floatX=Math.round((dimmerOSWGpanel_offsetWidth-20)/2)-Math.round(dimmerOSWGpanel_layerwidth/2);
			dimmerOSWGpanel_floatY=Math.round((dimmerOSWGpanel_offsetHeight-20)/2)-Math.round(dimmerOSWGpanel_layerheight/2);
			if (IE4XHTML) {
				dimmerOSWGpanel_scrollLeft = document.documentElement.scrollLeft;
				dimmerOSWGpanel_scrollTop = document.documentElement.scrollTop;
			} else {
				dimmerOSWGpanel_scrollLeft = document.body.scrollLeft;
				dimmerOSWGpanel_scrollTop = document.body.scrollTop;
			}
			dimmerOSWGpanel_lastX=dimmerOSWGpanel_scrollLeft + dimmerOSWGpanel_floatX;
			dimmerOSWGpanel_lastY=dimmerOSWGpanel_scrollTop + dimmerOSWGpanel_floatY;
		}
		displayFloatingDiv(targetObjectId, targetTitle, dimmerOSWGpanel_layerwidth, dimmerOSWGpanel_layerheight, dimmerOSWGpanel_lastX, dimmerOSWGpanel_lastY);
		// and make it visible
		if( changeObjectVisibility(targetObjectId, 'visible') ) {
			// if we successfully showed the popup
			// store its Id on a globally-accessible object
			window.currentlyVisiblePopup = targetObjectId;
			return false;
		} else {
			// we couldn't show the popup, boo hoo!
			return false;
		}
    }
	/*
	} else {
		// there was no event object, so we won't be able to position anything, so give up
		return false;
	}
	*/
} // dimmerOSWG_showPopup

function hideCurrentPopup() {
    // note: we've stored the currently-visible popup on the global object window.currentlyVisiblePopup
    if(window.currentlyVisiblePopup) {
	changeObjectVisibility(window.currentlyVisiblePopup, 'hidden');
	window.currentlyVisiblePopup = false;
    }
	if (document.getElementById('dimmer')!=undefined) document.getElementById('dimmer').style.visibility = 'hidden';
} // hideCurrentPopup


// ***********************
// hacks and workarounds *
// ***********************

var ie4 = false;
if(document.all) {
	ie4 = true;
}
function getObject(id) {
	if (ie4) {
		return document.all[id];
	} else {
		return document.getElementById(id);
	}
}

function toggle(link, divId) {
	var d = getObject(divId);
	if ( d.style.display == 'none') {
		d.style.display = '';
	} else {
		d.style.display = 'none';
	}
}


function getStyleObject(objectId) {
    // cross-browser function to get an object's style object given its id
    if(document.getElementById && document.getElementById(objectId)) {
	// W3C DOM
	return document.getElementById(objectId).style;
    } else if (document.all && document.all(objectId)) {
	// MSIE 4 DOM
	return document.all(objectId).style;
    } else if (document.layers && document.layers[objectId]) {
	// NN 4 DOM.. note: this won't find nested layers
	return document.layers[objectId];
    } else {
	return false;
    }
} // getStyleObject

function changeObjectVisibility(objectId, newVisibility) {
    // get a reference to the cross-browser style object and make sure the object exists
    var styleObject = getStyleObject(objectId);
    if(styleObject) {
	styleObject.visibility = newVisibility;
	return true;
    } else {
	// we couldn't find the object, so we can't change its visibility
	return false;
    }
} // changeObjectVisibility

function moveObject(objectId, newXCoordinate, newYCoordinate) {
    // get a reference to the cross-browser style object and make sure the object exists
    var styleObject = getStyleObject(objectId);
    if(styleObject) {
	styleObject.left = newXCoordinate;
	styleObject.top = newYCoordinate;
	return true;
    } else {
	// we couldn't find the object, so we can't very well move it
	return false;
    }
} // moveObject

function initializeHacks() {
    // this ugly little hack resizes a blank div to make sure you can click
    // anywhere in the window for Mac MSIE 5
    if ((navigator.appVersion.indexOf('MSIE 5') != -1) 
	&& (navigator.platform.indexOf('Mac') != -1)
	&& getStyleObject('blankDiv')) {
	window.onresize = explorerMacResizeFix;
    }
    resizeBlankDiv();
    // this next function creates a placeholder object for older browsers
    createFakeEventObj();
}

function createFakeEventObj() {
    // create a fake event object for older browsers to avoid errors in function call
    // when we need to pass the event object to functions
    if (!window.event) {
	window.event = false;
    }
} // createFakeEventObj

function resizeBlankDiv() {
    // resize blank placeholder div so IE 5 on mac will get all clicks in window
    if ((navigator.appVersion.indexOf('MSIE 5') != -1) 
	&& (navigator.platform.indexOf('Mac') != -1)
	&& getStyleObject('blankDiv')) {
	getStyleObject('blankDiv').width = document.body.clientWidth - 20;
	getStyleObject('blankDiv').height = document.body.clientHeight - 20;
    }
}

function explorerMacResizeFix () {
    location.reload(false);
}


// initialize hacks whenever the page loads
var newXCoordinate = 0;
var newYCoordinate = 0;
var dimmerOSWGpanel_layerheight = 0;
var dimmerOSWGpanel_layerwidth = 0;
window.onload = initializeHacks;
