
var xmlHttpPopups;
var savedTopPos;
var savedLeftPos;
var savedShadowOffset;
var savedCallingObjId;
var savedPopupBgcolor;
var popupIsOpen = false;


function doPopupWithShow(topPos, leftPos, shadowOffset, docPath, callingObjId, popupBgcolor){
	savedCallingObjId = callingObjId;
	savedPopupBgcolor = popupBgcolor;
	doPopup(topPos, leftPos, shadowOffset, docPath);
}//end function


function doPopup(topPos, leftPos, shadowOffset, docPath){
	if(popupIsOpen == true){
		return false;
	}
	popupIsOpen = true;
	savedTopPos = topPos;
	savedLeftPos = leftPos;
	savedShadowOffset = shadowOffset;
	startRequestPopups(docPath);	
	return false;
}//end function


function startRequestPopups(docPath){
	xmlHttpPopups = createXMLHttpRequest();
	xmlHttpPopups.onreadystatechange = handleStateChangePopups;
	
	var URL = docPath;	
	
	xmlHttpPopups.open("GET", URL, true);
	xmlHttpPopups.send(null);
}//end function

function handleStateChangePopups(){
    if(xmlHttpPopups.readyState == 4){
		if(xmlHttpPopups.status == 200){
		
			var housePopupsObj = document.getElementById("housePopups");
			housePopupsObj.style.visibility = "hidden";
			housePopupsObj.innerHTML = xmlHttpPopups.responseText;

			housePopupsObj.style.top = savedTopPos + "px";
			housePopupsObj.style.left = savedLeftPos + "px";
			//in IE the border didn't disappear when there was no text left,
			//so we put the border on and take it off later
			housePopupsObj.style.borderWidth="1px";
			housePopupsObj.style.borderColor="#999999";
			housePopupsObj.style.backgroundColor="#ffffff";
			housePopupsObj.style.zIndex=2;
			
			
			if($defined(savedCallingObjId)){
				var openingDuration = 500;

				var shower = new Element('div');
				shower.set('id','shower');
				shower.setStyle('borderStyle','solid');			
				shower.setStyle('borderColor','#cccccc');			
				shower.setStyle('borderWidth','1px');			
				shower.setStyle('backgroundColor',savedPopupBgcolor);
				shower.setStyle('width','0px');			
				shower.setStyle('height','0px');			
				shower.setStyle('zIndex',2);			
				shower.setStyle('position','absolute');
				shower.inject($(savedCallingObjId), 'after');
				shower.setStyle('top',$(savedCallingObjId).getPosition().y);
				shower.setStyle('left',$(savedCallingObjId).getPosition().x);
 									
				var myEffect = new Fx.Morph(shower,{
					duration: openingDuration
				});
	
				myEffect.start({
					height: housePopupsObj.clientHeight,
					width: housePopupsObj.clientWidth,
					top: savedTopPos,
					left: savedLeftPos
				}).chain(function(){
					housePopupsObj.style.visibility = 'visible';
					//housePopupsShadowObj.style.visibility = 'visible';
					shower.destroy();
				});
				
				var myDrag = new Drag.Move(housePopupsObj);

				removeDragFromObj('commentsBit', myDrag);
				removeDragFromObj('houseServicesTable', myDrag);

				
			}else{
				housePopupsObj.style.visibility = 'visible';
			}//end if
			
			
		}//end if
    }//end if
}//end function

function removeDragFromObj(objectId, myDrag){
	if($defined($(objectId))){
     	$(objectId).addEvents({
     		'mouseover': function(){
					myDrag.detach();
			},
			'mousedown': function(){
				myDrag.detach();
			},
			'mouseup': function(){
				myDrag.detach();
			},
 			'mouseout': function(){
 				myDrag.attach();
 			}
     	});     						
	}//end if
	
}//end function()

function addPopupsShadow(housePopupsShadowObj, housePopupsObj){

    housePopupsShadowObj.style.visibility = "hidden";
	var shadowImg = document.createElement("img");
	housePopupsShadowObj.appendChild(shadowImg);
	shadowImg.src = "/gifimages/maps/shadow.png";
	
	housePopupsShadowObj.style.position = "absolute";
	housePopupsShadowObj.style.top = (housePopupsObj.offsetTop + savedShadowOffset) + "px";
	housePopupsShadowObj.style.left = (housePopupsObj.offsetLeft + savedShadowOffset) + "px";
	
	shadowImg.style.height = housePopupsObj.offsetHeight + "px";
	shadowImg.style.width = housePopupsObj.offsetWidth + "px";
	
	return false;
}//end function


function closePopups(){

	var housePopupsObj = document.getElementById("housePopups");
	if(housePopupsObj != null && housePopupsObj.innerHTML.length > 6){
		var housePopupsShadowObj = document.getElementById("housePopupsShadow");
		housePopupsObj.innerHTML = "";
		housePopupsShadowObj.innerHTML = "";
		//in IE the border didn't disappear when there was no text left..
		housePopupsObj.style.borderWidth="0px";
	}//end if
	popupIsOpen = false;
	return false;
}//end function

	window.addEvent('domready', function(){
		$$('a[id^="currency."]').each(function(link){
			link.addEvent('click', function(evt){
				evt.stop();
				link.blur();
				var noOfDaysForPrices = 0;
				if($defined($('noOfDaysForPrices'))){
					noOfDaysForPrices = ($('noOfDaysForPrices')).value.toInt();
				}
				var noOfDaysPercentageRise = 0;
				if($defined($('noOfDaysPercentageRise'))){
					noOfDaysPercentageRise = ($('noOfDaysPercentageRise')).value.toInt();
				}
				
				priceTableFinder.getPriceTable(link, noOfDaysForPrices, noOfDaysPercentageRise);
			});
		});

		//$('noOfDaysButton').addEvent('click', priceTableFinder.getSoggiornoMinimoPriceTable);
		if($defined($('noOfDaysForPrices'))){
			$('noOfDaysForPrices').addEvent('change', priceTableFinder.getSoggiornoMinimoPriceTable);
		}

	});
	
	/**
	 * After clicking on the mailto: link we always do Google urchin
	 */
	function doUrchinStuff(){
		alert('hello');
		document.location = 'http://localhost';
	}



var priceTableFinder = {
		
		
	getSoggiornoMinimoPriceTable: function(){
		priceTableFinder.getPriceTable(null, ($('noOfDaysForPrices')).value.toInt(), ($('noOfDaysPercentageRise')).value.toInt());
	},
	
	/**
	 * There are two ways to get the price table, one from the four currency links and another from the
	 * noOfDaysForPrices select object, which is the number of days the user has chosen for a soggiorno
	 * minimo less than 7 days
	 */
	getPriceTable: function(currencyLink, noOfDaysForPrices, noOfDaysPercentageRise){

		//save the link for later
		priceTableFinder.currentLink = currencyLink;
		priceTableFinder.noOfDaysForPrices = noOfDaysForPrices;
		
		var myRequest = new Request({
			url: '/includedocs/houseInclude/PriceTable.jsp;jsessionid=' + $('jsessionid').value,
			method: 'get',
			onSuccess: function(responseText, responseXML){
				
				$('topPriceTableDiv').set('html', responseText);
				
				if(priceTableFinder.currentLink != null){
					priceTableFinder.currentLink.set('color', '#d5791a');
					priceTableFinder.resetCurrencyLinks();
				
				}//end if
				
				if($chk(priceTableFinder.noOfDaysForPrices)){
					//if it wasn't called from a currency link
					if(priceTableFinder.currentLink == null){
						var pricesTitleText = 'Prices for ' + priceTableFinder.noOfDaysForPrices + ' days';
						priceTableFinder.showTickerTitle(pricesTitleText);
					}
				}
				
				//add the events to the .gif in the price table which sets off the popup for descriptions
				shower.addDescriptionGifEvents();
				
				setTimeout(function(){
					priceTableFinder.currentLink = null;
				}, 250);
			},
			onFailure: function(responseText, responseXML){
				//do nothing, it's unlikely to happen and any message will just add confusion
			}
		});
		
		var parms = 'name=' + encodeURIComponent($('nomecasa').value);
		parms += '&' + 'stagione=' + $('stagione').value;
		//this signals to PriceTable.jsp that we're calling it direct, not from an include in House.jsp
		parms += '&independentCall=true'
		if(currencyLink != null){
			parms += '&' + 'overrideValuta=' + priceTableFinder.getCurrencyName(currencyLink);
		}
		if(noOfDaysForPrices > 0){
			parms += '&' + 'noOfDaysForPrices=' + noOfDaysForPrices;			
			parms += '&' + 'noOfDaysPercentageRise=' + noOfDaysPercentageRise;			
		}
		//for extended stays we need to know houseType, so we've put houseType id on the PricesAndDetails page
		var houseTypeObj = $('houseType');
		if(houseTypeObj){
			var houseType = $('houseType').value;
			if(houseType != null && houseType.length > 0){
				parms += '&' + 'houseType=' + $('houseType').value;
			}
		}
		
		myRequest.send(parms);
		
		if(currencyLink != null){
			currencyLink.set('color', '#cccccc');
		}	
	},
	
	showTickerTitle: function(pricesTitleText){
		//$('pricesTitle').set('html', pricesTitleText);
		var pricesTitleTextArray = pricesTitleText.split('');
		var tempTitle = '';
		var delayTime = 0;
		pricesTitleTextArray.each(function(value, index){
			setTimeout(function(){
				tempTitle += value;
				var thisTitle = tempTitle;
				$('pricesTitle').set('html', thisTitle);				
			}, delayTime);
			delayTime = delayTime + 50;
		});
	},
	
	
	/**
	 * The four currency links need resetting, underlining only the link for the currently selected currency
	 */
	 resetCurrencyLinks: function(){
	 	$$('a[id^="currency."]').each(function(link){
			if(link.id === priceTableFinder.currentLink.id){
				link.setStyle('color', '#000000');
				link.setStyle('textDecoration', 'underline');
			}else{
				link.erase('style');
			}//end if		
		});
	 	
	 },
	
	/**
	 * Link ids are in the form 'currency.AUD', and we want the bit after the dot
	 * @return the name of the currency (after the dot '.')
	 */
	getCurrencyName: function(currencyLink){
		var linkId = currencyLink.get('id');
		var linkIdArray = linkId.split('.');
		return linkIdArray[1];
	}
	
}//end var priceTableFinder


//:::::::::::::::: Prices description popup section ::::::::::

var DescriptionShower = new Class({

	showDescription: function(imgObj){
		var index = this.getIndexFromId(imgObj);
		
		//if it already exists just show it
		if($defined($('accommodationDiv.' + index))){
			$('accommodationDiv.' + index).setStyle('display','block');
			return;
		}

		var accommodationText = $('accommodationText.' + index).value;
		if(!$defined(accommodationText) || accommodationText.length < 4){
			return;
		}


		//the replaceString must come before the unescape otherwise any real + chars,
		//eg in 12+8pax, will get converted to spaces
		var title = replaceString($('accommodationTitle.' + index).value, '+', ' ');
		title = unescape(title);

		var accommodation = replaceString($('accommodationText.' + index).value, '+', ' ');
		accommodation = unescape(accommodation);
		
		var accommodationTitleDiv = new Element('div', {
			'class': 'paraHeadingBlackTxt',
			styles: {
				paddingLeft: '5px'
			},
			html: title
		});
		var accommodationTextDiv = new Element('div', {
			styles: {
				padding: '5px',
				backgroundColor: '#ffffff'
			},
			html: accommodation
		});
		var innerShadowDiv = new Element('div', {
			'class': 'inner'
		});
		var ydsfShadowDiv = new Element('div', {
			'class': 'ydsf'
		});
		innerShadowDiv.inject(ydsfShadowDiv);
		
		var divToShow = new Element('div', {
			styles: {
				border: '1px #999999 solid',
				backgroundColor: '#ffffff',
				width: $('pricesTr.' + index).getStyle('width'),
				position: 'absolute',
				top: $('pricesTr.' + index).getCoordinates().bottom,
				left: $('pricesTr.' + index).getPosition().x
			},
			'class': 'txt',
			id: 'accommodationDiv.' + index
		});
		ydsfShadowDiv.inject(divToShow);
		accommodationTitleDiv.inject(innerShadowDiv);
		accommodationTextDiv.inject(innerShadowDiv);

		divToShow.inject(document.body);

	},
	
	
	hideDescription: function(imgObj){
		var index = this.getIndexFromId(imgObj);
		if($defined($('accommodationDiv.' + index))){
			$('accommodationDiv.' + index).setStyle('display','none');
		}
	},

	/**
	 * ids are in the form descriptionGif.0 so we just split at the dot
	 * and return the second half
	 */
	getIndexFromId: function(imgObj){
		var theId = $(imgObj).get('id');
		theIdSplit = theId.split('.');
		return theIdSplit[1].toInt();
	},
	
	/**
	 * Attaches mouseover and mouseout events to the description gif for the popups
	 */
	addDescriptionGifEvents: function(){
		$$('img[id^="descriptionGif."]').each(function(img){
			img.addEvents({
				mouseover: function(){
				shower.showDescription(img);
			},
			mouseout: function(){
				shower.hideDescription(img);
			}
			});
		});
	}

	
});

var shower = new DescriptionShower();
window.addEvent('domready', function(){

	shower.addDescriptionGifEvents();
});


