var guestRoomQuickView = {
	// Keep track of fetched quick views so we don't get them unecessarily
	fetchedQuickViews : Array,
	quickViewTabs : Array,
	init: function(){		
		//setup quickview
		if(document.getElementById("guest-rooms-list")) {
			var quickViewToggleHeadings = YAHOO.util.Dom.getElementsByClassName("open-quick-view-heading");
			for (var i=0; i<quickViewToggleHeadings.length; i++)
			{
				quickViewToggleHeadings[i].onclick = guestRoomQuickView.toggleQuickView;
			}
	        var quickViewToggles = YAHOO.util.Dom.getElementsByClassName("open-quick-view");
	        for (var m=0; m<quickViewToggles.length; m++) {
	        	quickViewToggles[m].onclick = guestRoomQuickView.toggleQuickView;
	        }		
	        var quickViewToggles = YAHOO.util.Dom.getElementsByClassName("quick-view-hide");
	        for (var n=0; n<quickViewToggles.length; n++) {
	        	quickViewToggles[n].onclick = guestRoomQuickView.toggleQuickView;
	        }
	        var quickViews = YAHOO.util.Dom.getElementsByClassName("quick-view");
	        for (var o=0; o<quickViews.length; o++) {
	        	guestRoomQuickView.quickViewTabs[quickViews[o].id] = new YAHOO.widget.TabView(quickViews[o].id);
	        }
        }
	},	
	toggleQuickView: function(){
		var propertyMarshaCode = document.getElementById("property-marshaCode").value;
		var quickViewRoomCode;
		if(this.className == "open-quick-view-heading" || this.className == "close-quick-view-heading")
		{
			quickViewRoomCode = this.id.substring(19,this.id.length);
		}
		else{
			quickViewRoomCode = this.id.substring(18,this.id.length);
		}		
    	var quickViewToShow = "quick-view-" + quickViewRoomCode;
    	var quickViewToggle = "quick-view-toggle-" + quickViewRoomCode;
		var quickViewToggleHeading = "quick-view-heading-"+quickViewRoomCode;
    	var roomRecord = "guest-room-record-" + quickViewRoomCode;
    	var siteLocale = document.getElementById("site-locale").value;		
    	if(this.className == "open-quick-view" || this.className == "open-quick-view-heading") {
    		// The user wants to open the quick view    		
    		var attributes = {height: { to: 347 }};    		
			YAHOO.util.Dom.addClass(document.getElementById(roomRecord), "active-record");
    		if(this.className == "open-quick-view")
			{
				this.className = "close-quick-view";
				document.getElementById(quickViewToggleHeading).className = "close-quick-view-heading";
			}
			else if(this.className == "open-quick-view-heading")
			{
				this.className = "close-quick-view-heading";
				document.getElementById(quickViewToggle).className = "close-quick-view";				
			}	
    		if(!guestRoomQuickView.fetchedQuickViews[quickViewRoomCode]) {
    			// function sends image request to Omniture
    			// with data of first quick view opened only
    			if(document.getElementById("omniSiteTrackingOn") && document.getElementById("omniSiteTrackingOn").value == "true") {
	    			sendExpandQuickView("hws", propertyMarshaCode.toUpperCase(), quickViewRoomCode);
	    		}
    			
    		// If we haven't already, fetch the data from the backend    							
    			YAHOO.util.Dom.addClass(quickViewToShow, "loading");    					
	    		ReservationRemoteService.retrieveKORDetails(propertyMarshaCode, quickViewRoomCode, siteLocale, function(data) {
	    			if(data == {}){
	    				return true;
	    			}	

					var isRoomOverviewAvailable = false;
					var isPhotoCarouselAvailable = false;
									
					//put together Photo Carousel
					if(data.carouselPhotoList && data.carouselPhotoList.length > 0) {						
						//imgPath pulled from global namespace
						var imagePath = imgPath;
						var photoList = quickViewRoomCode + "-carousel-list";
						var imageList = [];
						var urlList = [];
						var altList = [];
						for(var i = 0; i < data.carouselPhotoList.length; i++){
							imageList.push(imagePath + data.carouselPhotoList[i].thumbnailImageFile);
							urlList.push(data.carouselPhotoList[i].photoTourPopupPgURL);
							altList.push(data.carouselPhotoList[i].imageAltTag);	
						};
						var carouselObj = {
							imgPathList:imageList,
							urlList: urlList,
							altTextList: altList
						};
					
						var carouselId = quickViewRoomCode + "-carousel";
						var previousButton = quickViewRoomCode + '-prev-arrow';
						var nextButton = quickViewRoomCode + '-next-arrow';
						var carousel = new Carousel(carouselId, {prevElement:previousButton, nextElement:nextButton}, carouselObj);
						isPhotoCarouselAvailable = true;
					}
					else
					{					
						//remove photo carousel section, if no photos are available.
						YAHOO.util.Dom.addClass(quickViewRoomCode+"-image-disclaimer", "hidden");
						YAHOO.util.Dom.addClass(quickViewRoomCode+"-room-photos", "hidden");
						var guestRoomInfoContainer = document.getElementById(quickViewRoomCode+"-guest-room-info-container");
						YAHOO.util.Dom.removeClass(guestRoomInfoContainer, "guest-room-information");
					}					
					if (data.korDataErrorMessages && data.korDataErrorMessages.length > 0)
					{
						var noKORDataErrorMessage = "";
						for ( i=0; i< data.korDataErrorMessages.length; i++)
						{
							if(i == 0)
							{
								noKORDataErrorMessage = noKORDataErrorMessage+ data.korDataErrorMessages[i];
							}
							else{
								noKORDataErrorMessage = noKORDataErrorMessage+", "+data.korDataErrorMessages[i];
							}
						}
						if(noKORDataErrorMessage != "")
						{
							$("#"+quickViewRoomCode+"-room-overview").html(noKORDataErrorMessage);
						}
						
						 //remove Amenties Tab
						var roomAmenitiesTab = guestRoomQuickView.quickViewTabs["quick-view-"+ quickViewRoomCode].getTab(1);
						guestRoomQuickView.quickViewTabs["quick-view-"+ quickViewRoomCode].removeTab(roomAmenitiesTab);
						//remove in-room-media tab
						var roomMediaTab = guestRoomQuickView.quickViewTabs["quick-view-"+ quickViewRoomCode].getTab(1);
						guestRoomQuickView.quickViewTabs["quick-view-"+ quickViewRoomCode].removeTab(roomMediaTab);
											
					}
					else
					{
						//put together room overview text.
						var roomOverviewText ="";
						if(data.roomFeatureDetails.roomOverview && data.roomFeatureDetails.roomOverview.featuresList.length > 0)
						{
							for ( i=0; i< data.roomFeatureDetails.roomOverview.featuresList.length; i++)
							{
								if(i == 0)
								{
									roomOverviewText = roomOverviewText+ data.roomFeatureDetails.roomOverview.featuresList[i];
								}
								else{
									roomOverviewText = roomOverviewText+", "+data.roomFeatureDetails.roomOverview.featuresList[i];
								}
							}
						}
						if(roomOverviewText != "")
						{
							isRoomOverviewAvailable = true;
							$("#"+quickViewRoomCode+"-room-overview").html(roomOverviewText);
						}
						else{
							YAHOO.util.Dom.addClass(quickViewRoomCode+"-room-overview", "hidden");
						}	
							
						guestRoomQuickView.populateContent(quickViewRoomCode, isRoomOverviewAvailable, isPhotoCarouselAvailable, data.roomFeatureDetails.conciergeFeatures, data.roomFeatureDetails.roomFeaturesList);
						
					}										
									
					// Get rid of the loading class
					YAHOO.util.Dom.removeClass(quickViewToShow, "loading");					
				});				
				guestRoomQuickView.fetchedQuickViews[quickViewRoomCode] = true;
			}
    	}
    	else {
    		// Close the quick view
    		var attributes = {height: { to: 0 }};    		
    		if(this.className == "quick-view-hide") {
    			document.getElementById(quickViewToggle).className = "open-quick-view";
				document.getElementById(quickViewToggleHeading).className = "open-quick-view-heading";
    		}
			else if(this.className == "close-quick-view-heading")
			{
				this.className = "open-quick-view-heading";
				document.getElementById(quickViewToggle).className = "open-quick-view";
			}
    		else {	
    			this.className = "open-quick-view";
				document.getElementById(quickViewToggleHeading).className = "open-quick-view-heading";
    		}    		
    		setTimeout(function setIt() {document.getElementById(roomRecord).className = ""}, 200);
    	}    	
    	var anim = new YAHOO.util.Anim(quickViewToShow, attributes, .1);
		anim.animate();
    	return false;
	},
	
	populateContent : function(roomTypeCode, isRoomOverviewAvailable,isPhotoCarouselAvailable, specialFeatureList, otherFeatureList)
	{
		//tab 1 content
		var specialFeaturesInnerHtml="";
		var roomFeaturesInnerHtml="";
		var accessibleRoomFeaturesInnerHtml="";
		var hospitalityServiceFeaturesInnerHtml="";
		var roomOverviewFeatureContent="";
		var mainRoomOverFeatureContainer= document.getElementById(roomTypeCode+"-room-overview-tab-features");
		
		//tab 2 content
		var bedsFeatureInnerHtml="";
		var bathFeatureInnerHtml="";
		var furnitureFeatureInnerHtml="";
		var foodFeatureInnerHtml="";
		var kitchenFeatureInnerHtml="";
		var roomAmenitiesFeatureContent="";
		var roomAmenitiesFeatureContainer= document.getElementById(roomTypeCode+"-guest-room-amenities-tab");
		
		//tab 3 Content
		var internetFeatureInnerHtml="";
		var entertainmentFeatureInnerHtml="";
		var inRoomMediaFeatureContent="";
		var inRoomMediaFeatureContainer= document.getElementById(roomTypeCode+"-guest-room-media-tab");
		
		//content population
		if(specialFeatureList && specialFeatureList.featuresList.length > 0)
		{
			//special features.
			specialFeaturesInnerHtml=guestRoomQuickView.populateFeatures(specialFeatureList);
		}
		
		if(otherFeatureList && otherFeatureList.length > 0)
		{
			
			for (i= 0; i<otherFeatureList.length; i++)
			{				
				//room features: FEATURE TYPE: 8
				if(otherFeatureList[i] && otherFeatureList[i].featureType == 8)
				{
					if(otherFeatureList[i].featuresList.length > 0)
					{
						roomFeaturesInnerHtml = guestRoomQuickView.populateFeatures(otherFeatureList[i]);
					}
				}
				//put tegether Accessible Room Features list : FEATURE TYPE: 12
				else if(otherFeatureList[i] && otherFeatureList[i].featureType == 12)
				{
					if(otherFeatureList[i].featuresList.length > 0)
					{
						accessibleRoomFeaturesInnerHtml = guestRoomQuickView.populateFeatures(otherFeatureList[i]);
					}
				}
				//put together hospitality feature list: FEATURE NAME :10
				else if(otherFeatureList[i] && otherFeatureList[i].featureType == 10)
				{
					if(otherFeatureList[i].featuresList.length > 0)
					{
						hospitalityServiceFeaturesInnerHtml = guestRoomQuickView.populateFeatures(otherFeatureList[i]);
					}
				}
				//put together Beds and Bedding Feature List : FEATURE TYPE: 3
				else if(otherFeatureList[i] && otherFeatureList[i].featureType == 3)
				{
					if(otherFeatureList[i].featuresList.length > 0)
					{
						bedsFeatureInnerHtml = guestRoomQuickView.populateFeatures(otherFeatureList[i]);
					}
				}
				//put together Bathroom features - FEATURE TYPE: 4
				else if(otherFeatureList[i] && otherFeatureList[i].featureType == 4)
				{
					if(otherFeatureList[i].featuresList.length > 0)
					{
						bathFeatureInnerHtml = guestRoomQuickView.populateFeatures(otherFeatureList[i]);
					}
				}
				//put together furniture Features: FEATURE TYPE: 5
				else if(otherFeatureList[i] && otherFeatureList[i].featureType == 5)
				{
					if(otherFeatureList[i].featuresList.length > 0)
					{
						furnitureFeatureInnerHtml = guestRoomQuickView.populateFeatures(otherFeatureList[i]);
					}
				}
				//put together Food and Beverages Feature List: FEATURE TYPE: 6
				else if(otherFeatureList[i] && otherFeatureList[i].featureType == 6)
				{
					if(otherFeatureList[i].featuresList.length > 0)
					{
						foodFeatureInnerHtml = guestRoomQuickView.populateFeatures(otherFeatureList[i]);
					}
				}
				//put together Kitchen Features : FEATURE TYPE : 7
				else if(otherFeatureList[i] && otherFeatureList[i].featureType == 7)
				{
					if(otherFeatureList[i].featuresList.length > 0)
					{
						kitchenFeatureInnerHtml = guestRoomQuickView.populateFeatures(otherFeatureList[i]);
					}
				}
				//put together Internet Features : FEATURE TYPE: 9
				else if(otherFeatureList[i] && otherFeatureList[i].featureType == 9)
				{
					if(otherFeatureList[i].featuresList.length > 0)
					{
						internetFeatureInnerHtml = guestRoomQuickView.populateFeatures(otherFeatureList[i]);
					}
				}
				//put together Entertainment Features : FEATURE TYPE: 11
				else if(otherFeatureList[i] && otherFeatureList[i].featureType == 11)
				{
					if(otherFeatureList[i].featuresList.length > 0)
					{
						entertainmentFeatureInnerHtml = guestRoomQuickView.populateFeatures(otherFeatureList[i]);
					}
				}
			
			}
			
		}
		//put together Tab 1 content:
		var isRoomOverViewTabDeleted = false;
		roomOverviewFeatureContent = guestRoomQuickView.populateFeatureColumn(new Array(specialFeaturesInnerHtml,roomFeaturesInnerHtml), new Array(accessibleRoomFeaturesInnerHtml,hospitalityServiceFeaturesInnerHtml));
		if(roomOverviewFeatureContent!="" && roomOverviewFeatureContent.length >0)
		{
			mainRoomOverFeatureContainer.innerHTML = roomOverviewFeatureContent;
		}
		else{
			if(!isRoomOverviewAvailable && !isPhotoCarouselAvailable)
			{
				var roomOverviewTab = guestRoomQuickView.quickViewTabs["quick-view-"+ roomTypeCode].getTab(0);
				guestRoomQuickView.quickViewTabs["quick-view-"+ roomTypeCode].removeTab(roomOverviewTab);
				isRoomOverViewTabDeleted = true;
			}
		}
		//put together Tab 2 content
		var isAmenitiesTabDeleted = false;
		roomAmenitiesFeatureContent = guestRoomQuickView.populateFeatureColumn(new Array(bedsFeatureInnerHtml ,bathFeatureInnerHtml), new Array(furnitureFeatureInnerHtml, foodFeatureInnerHtml, kitchenFeatureInnerHtml));
		if(roomAmenitiesFeatureContent!= "" && roomAmenitiesFeatureContent.length >0)
		{
			roomAmenitiesFeatureContainer.innerHTML = roomAmenitiesFeatureContent;			
		}
		else{
			var roomAmenitiesTab;
			if(isRoomOverViewTabDeleted){
				roomAmenitiesTab = guestRoomQuickView.quickViewTabs["quick-view-"+ roomTypeCode].getTab(0);
			}
			else{
				roomAmenitiesTab = guestRoomQuickView.quickViewTabs["quick-view-"+ roomTypeCode].getTab(1);
			}			
			guestRoomQuickView.quickViewTabs["quick-view-"+ roomTypeCode].removeTab(roomAmenitiesTab);
			isAmenitiesTabDeleted = true;
		}
		//put together Tab 3 content
		inRoomMediaFeatureContent = guestRoomQuickView.populateFeatureColumn(new Array(internetFeatureInnerHtml), new Array(entertainmentFeatureInnerHtml));
		if(inRoomMediaFeatureContent != "" && inRoomMediaFeatureContent.length >0)
		{
			inRoomMediaFeatureContainer.innerHTML = inRoomMediaFeatureContent;
		}
		else{
			var roomMediaTab;
			if(isRoomOverViewTabDeleted && isAmenitiesTabDeleted)
			{
				roomMediaTab = guestRoomQuickView.quickViewTabs["quick-view-"+ roomTypeCode].getTab(0);
			}
			else if(isRoomOverViewTabDeleted || isAmenitiesTabDeleted)
			{
				roomMediaTab = guestRoomQuickView.quickViewTabs["quick-view-"+ roomTypeCode].getTab(1);
			}
			else{
				roomMediaTab = guestRoomQuickView.quickViewTabs["quick-view-"+ roomTypeCode].getTab(2);
			}
			guestRoomQuickView.quickViewTabs["quick-view-"+ roomTypeCode].removeTab(roomMediaTab);
		}
	
	},
	
	populateFeatureColumn: function(column1, column2)
	{
		var tabContent = "";
		
		if(column1.length > 0)
		{
			var column1Text = "";			
			for(a=0; a<column1.length; a++)
			{
				column1Text = column1Text+column1[a];
			}
			if(column1Text != ""){
				tabContent = tabContent+"<div class=\"column\">"+column1Text+"</div>";
			}
						
		}
		if(column2.length > 0)
		{
			var column2Text = "";
			for(j=0; j<column2.length; j++)
			{
				column2Text = column2Text+column2[j];
			}
			if(column2Text != ""){
				tabContent = tabContent+"<div class=\"column\">"+column2Text+"</div>";
			}
			
		}
		return tabContent;
	},
	//function to poplulate features;
	populateFeatures : function(featureList)
	{
		var featureInnerHtml = "";
		if(featureList.featuresList.length > 0)
		{
			featureInnerHtml = featureInnerHtml+"<h3>"+featureList.featureName+"</h3>";		
			featureInnerHtml = featureInnerHtml+"<ul>";
			for(j = 0 ; j < featureList.featuresList.length; j++)
			{
				featureInnerHtml= featureInnerHtml+"<li>"+featureList.featuresList[j]+"</li>";
			}
			featureInnerHtml = featureInnerHtml+"</ul>";
			
		}	
		return featureInnerHtml;
	}
	
}
YAHOO.util.Event.onDOMReady(guestRoomQuickView.init);