//************************************************************************************
// 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 isMozilla;
var objDiv = null;
var originalDivHTML = "";
var DivID = "";
var over = false;

//
// dinamically add a div to 
// dim all the page
//
function buildDimmerDiv()
{
    //document.write('<div id="dimmer" class="dimmer" ></div>');
}

function displayFloatingDiv(divId, title, width, height, left, top) 
{
	DivID = divId;

	//document.getElementById('dimmer').style.visibility = "visible";
	document.getElementById ('dimmer').style.display='';
    document.getElementById ('dimmer').style.visibility='visible';
    document.getElementById ('dimmer').style.position='relative';

    document.getElementById ('dimmer').style.top='0px';
    document.getElementById ('dimmer').style.left='0px';
    document.getElementById ('dimmer').style.width= '100%';
    document.getElementById ('dimmer').style.height= '100%';

    document.getElementById('dimmer').style.backgroundColor = "Gray";
    document.getElementById('dimmer').style.filter = "alpha(opacity=60)";
    document.getElementById('dimmer').style.opacity = "0.6";
	
	//hide map div on home

	$(".map_div").hide();
	/*
	document.getElementById ('dimmer').style.display='none';
    document.getElementById ('dimmer').style.visibility='hidden';

    document.getElementById ('dimmer').style.top='0px';
    document.getElementById ('dimmer').style.left='0px';
    document.getElementById ('dimmer').style.width= '0px';
    document.getElementById ('dimmer').style.height= '0px';
	*/
    document.getElementById(divId).style.width = width + 'px';
    //document.getElementById(divId).style.height = height + 'px';
    document.getElementById(divId).style.left = '50px';
    document.getElementById(divId).style.top = '50px';

	var addHeader;
	originalDivHTML="";
	if (originalDivHTML == "")
	    originalDivHTML = document.getElementById(divId).innerHTML;
		
	
	
	addHeader = '<div class="floatingHeader">' +
				'<a href="javascript:hiddenFloatingDiv(\'' + divId + '\');void(0);">' + 
	            '<img alt="Close..." title="Close..." src="http://www.rentmycarpark.com.au/wp-content/plugins/BusinessLink/float_div/b-close.gif" border="0"></a></div>';
	

    // add to your div an header	
	document.getElementById(divId).innerHTML = addHeader + originalDivHTML;
	
	
	document.getElementById(divId).className = 'dimming';
	document.getElementById(divId).style.visibility = "visible";
	document.getElementById(divId).style.display = "";
	//document.body.disabled=true;
	
	

}


//
//
//
function hiddenFloatingDiv(divId) 
{
	document.getElementById(divId).innerHTML = originalDivHTML;
	document.getElementById(divId).style.visibility='hidden';
	document.getElementById('dimmer').style.visibility = 'hidden';
	document.getElementById('dimmer').style.display = 'none';
	document.getElementById(divId).style.display='none';
	DivID = "";
	
	//show map div
	$(".map_div").show();

}

//
//
//
function MouseDown(e) 
{
    if (over)
    {
        if (isMozilla) {
            objDiv = document.getElementById(DivID);
            X = e.layerX;
            Y = e.layerY;
            return false;
        }
        else {
            objDiv = document.getElementById(DivID);
            objDiv = objDiv.style;
            X = event.offsetX;
            Y = event.offsetY;
        }
    }
}


//
//
//
function MouseMove(e) 
{
    if (objDiv) {
        if (isMozilla) {
            objDiv.style.top = (e.pageY-Y) + 'px';
            objDiv.style.left = (e.pageX-X) + 'px';
            return false;
        }
        else 
        {
            objDiv.pixelLeft = event.clientX-X + document.body.scrollLeft;
            objDiv.pixelTop = event.clientY-Y + document.body.scrollTop;
            return false;
        }
    }
}

//
//
//
function MouseUp() 
{
    objDiv = null;
}


//
//
//
function init()
{
    // check browser
    isMozilla = (document.all) ? 0 : 1;


    if (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
init();


function clearFloatingDiv() 
{
	for(i=0;i<5;i++)
	{
	
	$(".floatingHeader").hide();
	document.getElementById("adsdetailpopup"+i).style.visibility='hidden';
	document.getElementById('dimmer').style.visibility = 'hidden';
	document.getElementById('dimmer').style.display = 'none';
	document.getElementById("adsdetailpopup"+i).style.display='none';

	}
}

	
		    function displayWindow(window, i, loc)
		    {
				//clearFloatingDiv();
		        var w, h, l, t;
		        w = 550;
		        h = 400;
		        l = 0;//((screen.width-w)/2)-160;
		        t = 100;
                // with title
				/*				
				if(jQuery("#"+window).is(":hidden"))
				{
		        displayFloatingDiv(window, '', w, h, l, t);
				}
				else{
				hiddenFloatingDiv(window);
				}
				*/
				if (document.getElementById(window).style.visibility == "hidden"){
					//alert('display');
			        //newWin = displayFloatingDiv(window, '', w, h, l, t);		
					clearFloatingDiv();
			        displayFloatingDiv(window, '', w, h, l, t);		
					//newWin.focus();		
				
				}
				else{
					//alert('hide');
					hiddenFloatingDiv(window);
				}
				
				showmap(loc,'small_map_canvas'+i);
		    }
