/**
 * 
 * @author JMutsaerts <jmutsaerts@zigwebsoftware.nl>
 * @version 1.0
 * @since 1 sep 2010 
 * @copyright (c) 2010, Zig Websoftware
 */
var oEtalage =
{
	/**
	 * The maximum amout of items to show
	 * @var int
	 */
	iLimit 			: 5,	
		
	/**
	 *  This array holds info objects about each item
	 *  @var Array
	 */
	aItems			: new Array(),

	/**
	 * The time in ms before an item switches
	 * @var int
	 */
	iInterval		: 5000,
	
	/**
	 * The time in s an effect takes.
	 * Happens in the time of iInterval
	 * @var int
	 */
	iEffectDuration	: 1.0,
	
	/**
	 * The time in ms before playing starts after it was by a mouse over/out
	 * @var int
	 */
	iResetTimeout	: 500,
	
	/**
	 * The ID of the interval
	 * @var int
	 */
	iIntervalID		: -1,
	
	/**
	 * The index of the currently active item
	 * @var int
	 */
	iCurrentItem	: -1,
	
	iMaxZ			: 15,
	
	/**
	 * Whether the 'animation' is active
	 * @var bool
	 */
	bPlaying		: false,	

	/**
	 * Whether the 'animation' is paused
	 * @var bool
	 */
	bPaused			: false,	

        setOptions              : function(oOptions)
        {
            if(oOptions)
            {
                   Object.extend(this, oOptions);
            }
        },

	/**
	 * Add an item to the collection if the length is below limit
	 * @param	string sButtonID	The HTML ID of the button that selects this item
	 * @param	string sTitle		The title of the item
	 * @param	string Intro		The contents of HTML ID of the image container
	 * @param	string sLink		The link URI of the item
	 */
	addItem			: function(sButtonID, sContentID, sImageID, sTitle, sIntro, sLink)
	{
		if(oEtalage.aItems.length >= oEtalage.iLimit)
		{
			return;
		}
		
		oEtalage.aItems.push
		({
			sButtonID: sButtonID,
			sContentID: sContentID,
			sImageID: sImageID,
			sTitle: sTitle,
			sIntro: sIntro,
			sLink: sLink
		});
		
		var iItemID = oEtalage.aItems.length - 1;
		$(sButtonID).observe
		(
			'mouseover',
			function()
			{
				oEtalage.showItem(iItemID, true);
				oEtalage.pause(); 
			}
		);
		
		$(sButtonID).observe
		(
			'mouseout',
			function()
			{
				setTimeout
				(
						
					function()
					{
						oEtalage.iIntervalID = -1;						
						oEtalage.bPlaying = false;	
						oEtalage.bPaused = false;
					},
					oEtalage.iResetTimeout
				);
			}
		);
		
		if($('etalageBarSpacer'))
		{
			$('etalageBarSpacer').style.width = (473 - oEtalage.aItems.length * 30) + 'px';			
		}
	},
	
	/**
	 * Stop an item by ID
	 * @param int	iID			ID in the item array
	 * @param bool	bSkipEffect	Used with mouse over
	 */
	showItem : function(iID, bSkipEffect)
	{
		var oEffectOptions = {duration: oEtalage.iEffectDuration};
		
		if(oEtalage.iEffectDuration <= 0)
		{
			bSkipEffect = true;
		}
		
		if(oEtalage.iCurrentItem != iID)
		{			
			for(var i = 0; i < oEtalage.aItems.length; i++)
			{
				var oItem = oEtalage.aItems[i];
				// Item to be active
				if(iID == i)
				{	
					$(oItem.sImageID).style.zIndex = oEtalage.iMaxZ;
					$(oItem.sContentID).style.zIndex = oEtalage.iMaxZ + 2;
					
					
					$(oItem.sButtonID).addClassName('activeEtalageButton');
					$(oItem.sButtonID).removeClassName('etalageButton');
					
					if(bSkipEffect)
					{
						
						$(oItem.sImageID).style.display = 'block';						
						$(oItem.sContentID).style.display = 'block';
					}
					else
					{
						Effect.Appear(oItem.sImageID, oEffectOptions);
						Effect.Appear(oItem.sContentID, oEffectOptions);
						setTimeout
						(
							function()
							{
								oEtalage.iCurrentItem = iID
								
							},
							oEtalage.iEffectDuration * 1000
						);
					}
				}
				// Current active item	
				else if(oEtalage.iCurrentItem == i)
				{
					$(oItem.sImageID).style.zIndex = oEtalage.iMaxZ - 1;
					$(oItem.sContentID).style.zIndex = oEtalage.iMaxZ + 1;
					
					
					$(oItem.sButtonID).addClassName('etalageButton');
					$(oItem.sButtonID).removeClassName('activeEtalageButton');						
					
					
					if(bSkipEffect)
					{						
						$(oItem.sImageID).style.display = 'none';
						$(oItem.sContentID).style.display = 'none';
					}
					else
					{
						
						Effect.Fade(oItem.sImageID, oEffectOptions);
						Effect.Fade(oItem.sContentID, oEffectOptions);
					}
				}	
				// Inactive items
				else
				{
					$(oItem.sContentID).style.display = 'none';					
					$(oItem.sImageID).style.display = 'none';
					$(oItem.sButtonID).addClassName('etalageButton');
					$(oItem.sButtonID).removeClassName('activeEtalageButton');				
					$(oItem.sImageID).style.zIndex = i+1;
					$(oItem.sContentID).style.zIndex = i+1;
					
				}
			}
			if(bSkipEffect)
			{
				oEtalage.iCurrentItem = iID
			}
		}
		
	},
	
	/**
	 * Pause playing (if playing)
	 */
	pause : function()
	{
		if(!oEtalage.bPaused)
		{
			if(oEtalage.iIntervalID > -1)
			{
				clearInterval(oEtalage.iInterval);
			}
			oEtalage.bPaused = true;
		}
	},
	
	/**
	 * Stop playing (if not playing or paused)
	 */
	play : function(iInterval)
	{
		if((!oEtalage.bPlaying || oEtalage.bPaused) && oEtalage.aItems.length)
		{
			if(!oEtalage.bPaused)
			{
				oEtalage.showItem(0);
			}
			oEtalage.iIntervalID = setInterval(oEtalage.showNext, oEtalage.iInterval);
			oEtalage.bPlaying = true;
			oEtalage.bPaused = false;
		}
	},
	
	/**
	 * Stop playing (if playing)
	 */
	stop : function()
	{
		if(oEtalage.bPlaying)
		{
			clearInterval(oEtalage.iIntervalID);
			oEtalage.bPlaying = false;
		}
		
	},
	
	/**
	 * Show next item
	 */
	showNext : function()
	{
		if(!oEtalage.bPaused)
		{		
			var iNextID = oEtalage.iCurrentItem + 1;
			if(oEtalage.iCurrentItem >= oEtalage.aItems.length - 1)
			{
				iNextID = 0;
				
			}
			
			oEtalage.showItem(iNextID);
		}
	}
	
}

// Call the etalage to start after document is loaded:
document.observe('dom:loaded', oEtalage.play);

function XPath(sPath,oXml)
{
	var oReturn = new Array();
	
	if (window.ActiveXObject)
	{
		oReturn = oXml.selectNodes(sPath);
	}
	else if (document.implementation && document.implementation.createDocument)
	{
		var oNodes	= oXml.evaluate(sPath, oXml, null, XPathResult.ANY_TYPE,null);

		var oResult = oNodes.iterateNext();
		
		while(oResult)
		{
			oReturn.push(oResult);
		  	oResult = oNodes.iterateNext();
		}
	}	

	return oReturn;
}

var DwellingViewer = Class.create({
	
	sType : '',

	oElement : null,

	iAmountOfDwellings : 0,

	iRandomID : null,
	
	oDwellings : null,

	initialize: function(sType,sElement,oXmlDoc) 
	{
		this.sType 		= sType;
		this.oDwellings = new Array();
		this.oElement 	= $(sElement);
		this.iRandomID  = null;

		this.iAmountOfDwellings = this.getChildNodeValue(oXmlDoc.getElementsByTagName('amountOfRecords')[0],1);

		if(this.iAmountOfDwellings > 0)
		{
			switch(this.sType)
			{
				case "rent":
					var sPath = '/data/dwellings/dwelling[model/value=\'Huur\']';
				break;
				case "buy":
					var sPath = '/data/dwellings/dwelling[model/value=\'Koop\']';
				break;
				default:
					var sPath = '/data/dwellings/dwelling';
			}
			
			var oRows = XPath(sPath, oXmlDoc);
			
			/* Thanh-2k11jul01: Added the check. Thanh-2k11sep01: Modified */
			if(oRows && oRows.length > 0)
			{
				this.iAmountOfDwellings = oRows.length;
				this.iRandomID 	= Math.floor(Math.random()*this.iAmountOfDwellings);
	
				var iCounter = 0;
				for(iCounter;iCounter<this.iAmountOfDwellings;iCounter++)
				{
					this.registerDwelling(iCounter,oRows[iCounter]);
				}
		
				this.showDwelling(this.iRandomID,false);
			}
			else
			{
				this.iAmountOfDwellings = 0;
			}
		}
	},
	
	registerDwelling: function(iID,oDwelling)
	{
		this.oDwellings[iID] = oDwelling;
	},

	showDwelling: function(iID,bShowMore)
	{
		var oDwelling = this.oDwellings[iID];

		var sHtml = '<div id="dwellingView-'+this.sType+'" class="dwellingView">';
		
		var sPicture = this.getDwellingArrayProperty('picture',oDwelling);
		
		if(!sPicture)
		{
			sPicture = 'imageNotFound.png';
		}
		
		var iID = this.getDwellingProperty('ID',oDwelling);
		var sUrl = this.getDwellingUrl(oDwelling,iID);
		sHtml += '<span class="image"><a href="'+sUrl+'" onclick="return etalage_toggle(\''+this.sType+'\')" id="'+this.sType+'-image"><img src="/portal/uploads/'+sPicture+'.160f120" alt="Afbeelding woning" /></a></span>';
		
		var sMoreClassName = (bShowMore == true) ? '' : ' hidden';
		
		sHtml += '<div id="dwellingViewMore-'+this.sType+'" class="dwellingViewMore'+sMoreClassName+'">'; 
		sHtml += '<div class="information">';
		sHtml += '<span class="address"><a href="'+sUrl+'">'+this.getDwellingProperty('street',oDwelling)+' '+this.getDwellingProperty('houseNumber',oDwelling);
		var sAddition = this.getDwellingProperty('houseNumberAddition',oDwelling);
		if ((sAddition != null) && (sAddition != 'null')) sHtml += sAddition;
		sHtml += '</a></span>';
		
		/* Thanh-2k11jul01: Added the check */
		sHtml += '<span class="quarter">Wijk:&nbsp;';
		if(this.getDwellingProperty('quarter',oDwelling) != null)
		{
			sHtml += this.getDwellingProperty('quarter',oDwelling);		
		}
		sHtml += '</span>';	
		
		sHtml += '<span class="dwellingType"> &nbsp;';
		if(this.getDwellingProperty('dwellingType',oDwelling) != null)
		{
			sHtml += this.getDwellingProperty('dwellingType',oDwelling);		
		}
		sHtml += '</span>';	
		
		sHtml += '<span class="sleepingRoom"> &nbsp;';
		if(this.getDwellingProperty('sleepingRoom',oDwelling) != null)
		{
			sHtml += this.getDwellingProperty('sleepingRoom',oDwelling);		
		}
		sHtml += '</span>';	
		
		switch(this.sType)
		{
			case "buy":
				sHtml += '<span class="price">&euro; '+ this.getDwellingProperty('sellingPrice',oDwelling) +'</span>';
			break;
			default:
				sHtml += '<span class="price">&euro; '+ this.getDwellingProperty('rent',oDwelling) +' p/m</span>';
		}
		
		sHtml += '</div>';
		
		if(this.iAmountOfDwellings > 1)
		{	
			sHtml += '<div id="dwellingViewNavigation-'+this.sType+'" class="dwellingViewNavigation">';
			sHtml += '<span class="dwellingViewNavigationItem dwellingViewNavigationItemPrev"><a id="dwellingViewNavigationPrev'+this.sType+'" href="#">&laquo;</a></span>';
			sHtml += '<span class="dwellingViewNavigationItem dwellingViewNavigationItemNext"><a id="dwellingViewNavigationNext'+this.sType+'" href="#">&raquo;</a></span>';
			sHtml += '<span class="dwellingViewNavigationItem dwellingViewNavigationItemAll"><a id="dwellingNiewNavigationAll'+this.sType+'" href="' + this.getDwellingUrl() + '">Alle ' + this.iAmountOfDwellings + ' &raquo;</a></span>'
			sHtml += '</div>'; 
		}

		sHtml += '</div>'; 
		sHtml += '</div>';
		sHtml += '</div>'; 

		this.oElement.innerHTML = sHtml;
		
		if(this.iAmountOfDwellings > 1)
		{
			if($('dwellingViewNavigation-'+this.sType))
			{
				$('dwellingViewNavigationPrev'+this.sType).onclick = function(){this.showPrevDwelling();}.bind(this);
				$('dwellingViewNavigationNext'+this.sType).onclick = function(){this.showNextDwelling();}.bind(this);
			}
		}
	},

	getDwellingUrl: function(oDwelling,iID)
	{
		switch(this.sType)
		{
			case "buy":
				switch(oDwelling == null ? -1 : this.getDwellingProperty('sellingTypeID',oDwelling))
				{
					case "1":
						return "/kopenkoopaanbod/huurhuistekoop/details?ID="+iID;
					break;
					case "2":
						return "/kopenkoopaanbod/nieuwbouwtekoop/details?ID="+iID;
					break;
					case "3":
						return "/kopenkoopaanbod/koopgarant/details?ID="+iID;
					break;
					default:
						return "/kopenkoopaanbod/actueelaanbod";
				}
			break;
			case "rent":
				switch(oDwelling == null ? -1 : this.getDwellingProperty('cityID',oDwelling))
				{
					case "200":
						return "/hurenhuuraanbod/actueelaanbodwaalre/details?ID="+iID;
					break;
					case "100":
						return "/hurenhuuraanbod/actueelaanbodveldhoven/details?ID="+iID;
					break;
					default:
						return "/hurenhuuraanbod/actueelaanbod";
				}
			break;
			default:
				return "/test/details?ID="+iID;
				
		}
		
		
		
	},
	
	getAmountOfDwellings: function()
	{
		return this.iAmountOfDwellings;
	},
	
	getDwellingProperty: function(sProperty,oDwelling)
	{
		var oElement = oDwelling.getElementsByTagName(sProperty)[0];
		
		if(oElement)
		{
			return this.getChildNodeValue(oElement,2);
		}	
	},
	
	getDwellingArrayProperty: function(sProperty,oDwelling)
	{
		var oElement = oDwelling.getElementsByTagName(sProperty)[0];
		
		if(oElement)
		{
			return this.getChildNodeValue(oElement,4);
		}
	},
	
	showNextDwelling: function()
	{
		var iNext = (this.iRandomID == (this.iAmountOfDwellings -1)) ? 0 : (this.iRandomID + 1);
		this.iRandomID = iNext;
		this.showDwelling(iNext,true);
	},
	
	showPrevDwelling: function()
	{
		var iPrev = (this.iRandomID == 0) ? (this.iAmountOfDwellings -1) : (this.iRandomID - 1);
		this.iRandomID = iPrev;
		this.showDwelling(iPrev,true);
	},

	getChildNodeValue: function(oElement,iDepth)
	{
		for(iCounter=0;iCounter<iDepth;iCounter++)
		{
			if(oElement)
			{
				oElement = oElement.childNodes[0];
			}
		}
		
		if(oElement)
		{
			return oElement.nodeValue;
		}
		
		return null;
	}
});

