var ajaxPopups = new Array();











/*
*       FUNCTIONS
*/










function getBody()
{
	return document.getElementsByTagName('body').item(0);
}










function AjaxPopup(url)
{
	this.id = 'ajax_popup'+AjaxPopup.prototype.currentId;
	this.zIndex = 400;
	this.url = url;
	
	ajaxPopups[this.id] = this;
	AjaxPopup.prototype.currentId++;
	
	this.createDiv();
	this.requestContents();	
}

AjaxPopup.prototype.currentId = 0;

AjaxPopup.prototype.createDiv = function()
{
	var closeImg = new Image();
	closeImg.src = '/images/close.gif';
	
	
	var code = '';
	code += '<div id="'+this.id+'" class="ajax_popup" style="visibility: hidden; z-index: '+this.zIndex+';">';
	code += '<div class="ajax_popup_head">';
	code += '<span class="title"></span>';
	code += '<a class="close" href="javascript:ajaxPopups[\''+this.id+'\'].close()"><img src="'+closeImg.src+'" /></a>';
	code += '</div>';
	code += '<div class="ajax_popup_body"></div>';
	code += '</div>';
	
	
	appendToBody(code);
}

AjaxPopup.prototype.requestContents = function()
{
	var options = 
	{
		asynchronous: true, 
		onSuccess: this.processContents.bind(this),
		onFailure: this.ajaxError.bind(this)	
	};
	
	
	new Ajax.Request(this.url, options);
	loadingPopup.show('Loading...');
}

AjaxPopup.prototype.processContents = function(req)
{
	try
	{
		eval('var data = '+req.responseText+';');
	}
	catch (err)
	{
		alert('Unknown error');
		loadingPopup.hide();
		this.closeFinally();
		return;		
	}
	
	this.setTitle(data.title);
	this.setBody(data.body);
	data.body.evalScripts();
	
	
	this.show();
	loadingPopup.hide();
}

AjaxPopup.prototype.ajaxError = function(req)
{
	alert('Unknown Error');
	loadingPopup.hide();
}

AjaxPopup.prototype.getDiv = function()
{
	return $(this.id);
}

AjaxPopup.prototype.show = function()
{
	screenLocker.lock(this.zIndex - 1);
	var div = this.getDiv();
	Element.setOpacity(div, 0);
	div.style.visibility = 'visible';
	new Effect.Opacity(div, {duration: 1.0, from: 0.0, to: 1.0});
}


AjaxPopup.prototype.close = function()
{
	var div = this.getDiv();
	new Effect.Opacity(div, {duration: 1.0, from: 1.0, to: 0.0, afterFinish: this.closeFinally.bind(this)});
}

AjaxPopup.prototype.closeFinally = function()
{
	Element.remove($(this.id));
	screenLocker.unlock();
}


AjaxPopup.prototype.setTitle = function(text)
{
	var span = getElementByPath('div[0]/span[0]', this.getDiv());
	span.innerHTML = text;
}

AjaxPopup.prototype.setBody = function(html)
{
	var div = getElementByPath('div[last]', this.getDiv());
	div.innerHTML = html;
}


/*
*            IMAGE VIEWER
*/

function ImageViewer(src)
{
	this.id = 'image_viewer'+ImageViewer.prototype.currentId;
	this.src = src;
	this.zIndex = 450;
	
	ajaxPopups[this.id] = this;
	ImageViewer.prototype.currentId++;
	
	
	this.resizeDivHandler = this.resizeDiv.bind(this);
	
	
	this.createDiv();
	this.setTitle();
	this.setImage();
	this.show();
}

ImageViewer.prototype.currentId = 0;


ImageViewer.prototype.createDiv = function()
{
	var code = '';
	code += '<div id="'+this.id+'" class="ajax_popup" style="visibility: hidden; z-index: '+this.zIndex+';">';
	code += '<div class="ajax_popup_head">';
	code += '<span class="title"></span>';
	code += '<a class="close" href="javascript:ajaxPopups[\''+this.id+'\'].close()"><img src="/images/close.gif" /></a>';
	code += '</div>';
	code += '<div class="ajax_popup_body"></div>';
	code += '</div>';
	
	
	appendToBody(code);
	
	var div = this.getDiv();
	div.style.left = '200px';
	div.style.top = '140px';
	div.style.width = '225px';
	
	
	div = getElementByPath('div[last]', this.getDiv());
	div.style.overflow = 'auto';
	div.style.padding = '10px';
	div.style.textAlign = 'center';
	
	
}

ImageViewer.prototype.show = AjaxPopup.prototype.show;
ImageViewer.prototype.close = AjaxPopup.prototype.close;
ImageViewer.prototype.closeFinally = AjaxPopup.prototype.closeFinally;
ImageViewer.prototype.getDiv = AjaxPopup.prototype.getDiv;





ImageViewer.prototype.setTitle = function()
{
	var title = this.src.match(/([^\/]+)$/);
	title = title[1];
	
	var span = getElementByPath('div[0]/span[0]', this.getDiv());
	span.innerHTML = title;
	
}

ImageViewer.prototype.resizeDiv = function(img)
{
	var div = this.getDiv();
	div.style.width = (img.width + 25) + 'px';
}





ImageViewer.prototype.setImage = function()
{
	var code = '<img style="position: relative;" src="'+this.src+'" width="200" onload="fitImage(this, 650); ajaxPopups[\''+this.id+'\'].resizeDivHandler(this);" />';
	
	var div = getElementByPath('div[last]', this.getDiv());
	div.innerHTML = code;
}


/*
*    VIRTUAL TOUR VIEWER
*/
function VtourViewer(src)
{
	this.id = 'vtour_viewer'+VtourViewer.prototype.currentId;
	this.src = src;
	this.zIndex = 450;
	
	ajaxPopups[this.id] = this;
	VtourViewer.prototype.currentId++;
	
	
	
	this.createDiv();
	this.setTitle();
	this.setImage();
	this.show();
}

VtourViewer.prototype.currentId = 0;


VtourViewer.prototype.createDiv = function()
{
	var code = '';
	code += '<div id="'+this.id+'" class="ajax_popup" style="visibility: hidden; z-index: '+this.zIndex+';">';
	code += '<div class="ajax_popup_head">';
	code += '<span class="title"></span>';
	code += '<a class="close" href="javascript:ajaxPopups[\''+this.id+'\'].close()"><img src="/images/close.gif" /></a>';
	code += '</div>';
	code += '<div class="ajax_popup_body"></div>';
	code += '</div>';
	
	
	appendToBody(code);
	
	var div = this.getDiv();
	div.style.left = '200px';
	div.style.top = '140px';
	div.style.width = '650px';
	
	
	div = getElementByPath('div[last]', this.getDiv());
	div.style.overflow = 'auto';
	div.style.padding = '10px';
	div.style.textAlign = 'center';
	
	
}


VtourViewer.prototype.close = ImageViewer.prototype.close;
VtourViewer.prototype.getDiv = ImageViewer.prototype.getDiv;





VtourViewer.prototype.setTitle = function()
{
	var span = getElementByPath('div[0]/span[0]', this.getDiv());
	span.innerHTML = 'Virtual tour';
	
}


VtourViewer.prototype.setImage = function()
{
	var code = '';
	code += '<applet archive="/ptviewer.jar" code="ptviewer.class" width="600" height="407">';
	code += '<param name="bar_x" value="92">';
	code += '<param name="bar_y" value="270">';
	code += '<param name="bar_width" value="305">';
	code += '<param name="bar_height" value="16">';
	code += '<param name="barcolor" value="3333ff">';
	code += '<param name="quality" value="3">';
	code += '<param name="maxspeed" value="7">';
	code += '<param name="bgcolor" value="ff9999">';
	code += '<param name="shotspot0" value=" x355 y287 a385 b298 u\'ptviewer:startAutoPan(0,-1.0,1.0)\' ">';
	code += '<param name="shotspot1" value= " x401 y278 a417 b298 u\'ptviewer:startAutoPan(0,0,0.99)\' ">';
	code += '<param name="file" value = "ptviewer:0">';
	code += '<param name="pano0" value=';
	code += '"{file='+this.src+'} ';
	code += '{auto=0.3} ';
	code += '{panmin=-180} ';
	code += '{panmax=180} ';
	code += '{pan=-20} ';
	code += '{tiltmin=-35} ';
	code += '{tiltmax=31} ';
	code += '{tilt=1} ';
	code += '{fovmin=30} ';
	code += '{fovmax=99} ';
	code += '{fov=86}"> '
	code += '</applet>';
	
	code += '<div style="border-top: dotted 1px #333333; margin-top: 5px; padding-top: 5px;">';
	code += 'Click and drag over image to move around, while panning use the "control" and "shift" keys to zoom in and out.';
	code += '</div>';
	
	
	var div = getElementByPath('div[last]', this.getDiv());
	div.innerHTML = code;
}


VtourViewer.prototype.show = function()
{
	screenLocker.lock(this.zIndex - 1);
	var div = this.getDiv();
	div.style.visibility = 'visible';
}


VtourViewer.prototype.close = function()
{
	var div = this.getDiv();
		div.innerHTML = '';
	
	Element.remove($(this.id));
	screenLocker.unlock();	
}




/*
*     FLOOR PLAN VIEWER
*/

function FloorPlanViewer(src, title)
{
	this.id = 'fplan_viewer'+FloorPlanViewer.prototype.currentId;
	this.src = src;
	this.title = title;
	this.zIndex = 450;
	
	ajaxPopups[this.id] = this;
	FloorPlanViewer.prototype.currentId++;
	
	
	
	this.createDiv();
	this.setTitle();
	this.setImage();
	this.show();
}

FloorPlanViewer.prototype.currentId = 0;

FloorPlanViewer.prototype.createDiv = function()
{
	var code = '';
	code += '<div id="'+this.id+'" class="ajax_popup" style="visibility: hidden; z-index: '+this.zIndex+';">';
	code += '<div class="ajax_popup_head">';
	code += '<span class="title"></span>';
	code += '<a class="close" href="javascript:ajaxPopups[\''+this.id+'\'].close()"><img src="/images/close.gif" /></a>';
	code += '</div>';
	code += '<div class="ajax_popup_body"></div>';
	code += '</div>';
	
	appendToBody(code);
	
	var div = this.getDiv();
	div.style.left = '200px';
	div.style.top = '140px';
	div.style.width = '800px';
	
	
	div = getElementByPath('div[last]', this.getDiv());
	div.style.overflow = 'auto';
	div.style.padding = '5px';
	div.style.textAlign = 'center';
}


FloorPlanViewer.prototype.setImage = function()
{
	var code = '';
	code += '<iframe style="border: 0px; padding: 0px; margin: 0px;" frameborder="0" src="'+this.src+'" width="780" height="600"></iframe>';
	
	code += '<div style="border-top: dotted 1px #333333; margin-top: 5px; padding-top: 5px;">';
	code += 'Please click <a target="_blank" href="'+this.src+'" onclick="javascript:ajaxPopups[\''+this.id+'\'].close(); return true;">here</a> if you can not see the floor plan';
	code += '</div>';
	
	var div = getElementByPath('div[last]', this.getDiv());
	div.innerHTML = code;
}

FloorPlanViewer.prototype.setTitle = function()
{
	var span = getElementByPath('div[0]/span[0]', this.getDiv());
	span.innerHTML = this.title;
	
}

FloorPlanViewer.prototype.show = function()
{
	screenLocker.lock(this.zIndex - 1);
	var div = this.getDiv();
	div.style.visibility = 'visible';
}

FloorPlanViewer.prototype.getRelativeSrc = function()
{
	return this.src.match(/^\/(.+)/)[1];
}



FloorPlanViewer.prototype.close = VtourViewer.prototype.close;
FloorPlanViewer.prototype.getDiv = AjaxPopup.prototype.getDiv;



/*
*
*/

function PropertyDetailsViewer(propId)
{
	this.id = 'vtour_viewer'+VtourViewer.prototype.currentId;
	this.propId = propId
	this.zIndex = 450;
	
	ajaxPopups[this.id] = this;
	PropertyDetailsViewer.prototype.currentId++;
	
	this.createDiv();
	this.setTitle();
	this.setImage();
	this.show();
}






/*
*         AJAX WRAPPERS
*/

function popupVillaTypeOverview(url)
{
	new AjaxPopup(url+'?action=overview');
}

function popupVillaTypeDetails(url)
{
	new AjaxPopup(url+'?action=details');
}

function popupVillaTypeFeatures(url)
{
	new AjaxPopup(url+'?action=features');
}

function popupVillaTypeArea(url)
{
	new AjaxPopup(url+'?action=area');
}

/*
*   IMAGE VIEWER WRAPPERS
*/

function popupImage(src)
{
	new ImageViewer(src);
}

function popupVtour(src)
{
	new VtourViewer(src);
}

function popupFplan(src, title)
{
	new FloorPlanViewer(src, title);
}


function popupPropertyDetails(url)
{
	new AjaxPopup(url);
}
