YAHOO.namespace("ibe.deals");

YUI({
      modules :{
        'slideshow' :{},
        'ibe-anim' : {}
      }
    }).use('node', 'anim', 'overlay', 'slideshow', 'ibe-anim', function(Y) {
             var DEAL = YAHOO.ibe.deals;

             Y.on('domready', function() {
               DEAL.menuManager = new IBEMenuManager();
               DEAL.compHotelDetails = new Array();
               DEAL.hotelDetailOverlays = new Array();
               DEAL.fetchedHotelDetailsViews = new Array();
               DEAL.tripAdvisorOverlays = new Array();
               DEAL.currentTripAdvisorOverlayId = undefined;
               DEAL.selectedTab = undefined;
               DEAL.setUpTabViews();
             });

             YAHOO.ibe.deals.setUpTabViews = function() {
               DEAL.hotelImagesTabView = new IBEWebComponentView('/ajax.component.view.holidaydeal.hotelImagesTabView.action');
               DEAL.hotelImagesTabView.fetchView();
             };

             YAHOO.ibe.deals.openHotelDetailsTabView = function(index, code) {
               DEAL.closeOpenedSlideshow();
               DEAL.closeOpenedHotelDetails();

               var deal = DEAL.getDeal(index);

               DEAL.currentIndex = index;
               DEAL.currentHotelSystemCode = code;
               DEAL.currentUniqueId = index + "_" + code;

               DEAL.hotelDetailsOverlay = 'hotelDetailsOverlay_' + DEAL.currentUniqueId;
               DEAL.hotelDetailsTabsDiv = 'tabsForHotelDetails_' + DEAL.currentUniqueId;
               DEAL.hotelDetails = 'hotelDetails_' + DEAL.currentUniqueId;
               DEAL.hotelDetailsLoading = 'hotelDetailsLoading_' + DEAL.currentUniqueId;
               DEAL.pageDealItemElementId = 'holidayDealsListView_' + DEAL.currentUniqueId;
               DEAL.pageDetailsOverlayElementId = 'pageDetailsOverlayDiv_' + DEAL.currentUniqueId;

               var menu = new IBETabMenu({
                                           id : 'menuForHotel_' + DEAL.currentHotelSystemCode,
                                           placeHolderId : DEAL.hotelDetailsTabsDiv,
                                           leftCss : 'hotelBigListTabLeft hotelBigListTab',
                                           middleCss : 'hotelBigListTabMiddle hotelBigListTab',
                                           rightCss : 'hotelBigListTabRight hotelBigListTab',
                                           leftCssSelected : 'hotelBigListTabLeftSelected hotelBigListTab hotelBigListTabSelected',
                                           middleCssSelected : 'hotelBigListTabMiddleSelected hotelBigListTab hotelBigListTabSelected',
                                           rightCssSelected : 'hotelBigListTabRightSelected hotelBigListTab hotelBigListTabSelected',
                                           separatorCss : 'hotelBigListTabSeparator'
                                         });
               DEAL.menuManager.addMenu(menu);

               menu.addTab(new IBETab({id:'details', title:UiText.get('Hotel.Result.List.Tabs.Description.Label'), callback:DEAL.tabHasBeenSelected}));
               menu.getLastTab().parentObject = deal;
               menu.addTab(new IBETab({id:'images', title:UiText.get('Hotel.Result.List.Tabs.Photos.Label'), callback : DEAL.tabHasBeenSelected}));
               menu.getLastTab().parentObject = deal;
               menu.addTab(new IBETab({id:'reviews', title:UiText.get('Hotel.Result.List.Tabs.Reviews.Label'), callback:DEAL.tabHasBeenSelected}));
               menu.getLastTab().parentObject = deal;

               // Override menu CSS for first tab. First tab (differs on left side)
               menu.getFirstTab().leftCss = 'hotelBigListTabFirstLeft hotelBigListTab';
               menu.getFirstTab().leftCssSelected = 'hotelBigListTabFirstLeftSelected hotelBigListTab hotelBigListTabSelected';

               menu.render();

               DEAL.selectedTab = 'details_' + DEAL.currentUniqueId;

               DEAL.fetchAndDisplayHotelDetailsView();
             };

             YAHOO.ibe.deals.tabHasBeenSelected = function(index, tab, deal) {
               if (deal && tab) {
                 var tabName = tab.id;
                 var tabId = tabName + '_' + DEAL.currentUniqueId;
                 if (tabId != DEAL.selectedTab) {
                   DEAL.hide(DEAL.selectedTab);
                   DEAL.selectedTab = tabId;
                   if (tabName == 'images') {
                     DEAL.renderHotelImagesInTabView = true;
                     DEAL.fetchAndRenderHotelImages(deal);
                     DEAL.selectThumbnailImage();
                   } else {
                     if (tabName == 'reviews') {
                       DEAL.displayTripAdvisorInfo();
                       DEAL.display(DEAL.selectedTab);
                     } else {
                       DEAL.display(DEAL.selectedTab);
                     }
                   }
                 }
               }
             };

             YAHOO.ibe.deals.fetchAndDisplayHotelDetailsView = function () {
               if (DEAL.fetchedHotelDetailsViews[DEAL.currentUniqueId] === undefined) {
                 DEAL.fetchedHotelDetailsViews[DEAL.currentUniqueId] = new IBEWebComponentView('/ajax.component.view.holidaydeal.hotelDetailsView.action');
                 DEAL.fetchedHotelDetailsViews[DEAL.currentUniqueId].fetchView();
               }
               DEAL.displayHotelDetailsContainer();
               DEAL.createAndShowOverlay();
             };

             YAHOO.ibe.deals.displayHotelDetailsContainer = function() {
               if (DEAL.hotelDetailOverlays[DEAL.currentUniqueId] === undefined) {
                 DEAL.hotelDetailOverlays[DEAL.currentUniqueId] = new Y.Overlay({
                                                                                  contentBox : '#' + DEAL.hotelDetailsOverlay,
                                                                                  visible : true,
                                                                                  width: '550px',
                                                                                  zIndex : 6,
                                                                                  align : {
                                                                                    node : '#' + DEAL.pageDealItemElementId,
                                                                                    points:[Y.WidgetPositionAlign.TC, Y.WidgetPositionAlign.CC]
                                                                                  }
                                                                                });
               }
               DEAL.hotelDetailOverlays[DEAL.currentUniqueId].render('#' + DEAL.pageOverlayDiv);

               DEAL.display(DEAL.hotelDetailsOverlay);

               DEAL.display(DEAL.hotelDetailsLoading);

               DEAL.renderHotelDetailsView();
             };

             YAHOO.ibe.deals.showImagesInTabContainer = function() {
               DEAL.display(DEAL.selectedTab);
             };

             YAHOO.ibe.deals.renderHotelDetailsView = function() {
               var deal = DEAL.getDeal(DEAL.currentIndex);
               if (deal) {
                 var component = new IBEWebComponent({id:'hotelDetailsId', view:DEAL.fetchedHotelDetailsViews[DEAL.currentUniqueId],model:deal, placeHolderId:DEAL.hotelDetails, renderCallback:DEAL.showHotelDetails});
                 component.showLoadingWhileRendering = true;
                 component.render();
                 DEAL.compHotelDetails[DEAL.currentUniqueId] = component;
               }
             };

             YAHOO.ibe.deals.showHotelDetails = function() {
               DEAL.hide(DEAL.hotelDetailsLoading);
               DEAL.display(DEAL.hotelDetails);
               DEAL.scrollPage();
             };

             YAHOO.ibe.deals.scrollPage = function() {
               var body = Y.one(document.body);
               var details = Y.one('#' + DEAL.hotelDetailsOverlay);
               var detailsYPos = details.getXY()[1];
               var scrollTopPos = parseInt(detailsYPos - 100);

               var anim = new Y.Anim({
                                       node: body,
                                       to: { scroll:  [0, scrollTopPos]},
                                       easing: Y.Easing.easeOut
                                     });

               // Used by IE. Must set scrollTop position on the document.documentElement.scrollTop and NOT on the
               // document.scrollTop.
               document.documentElement.scrollTop = scrollTopPos;

               anim.run();
             };

             YAHOO.ibe.deals.closeOpenedHotelDetails = function() {
               if (DEAL.hotelDetailsOverlay !== undefined) {
                 DEAL.hide(DEAL.hotelDetailsOverlay);
                 DEAL.hide(DEAL.pageDetailsOverlayElementId);
               }
             };

             YAHOO.ibe.deals.displayTripAdvisorInfo = function() {
               var tripAdvisorInfoNodes = Y.all('.tripAdvisorWhatIs');

               var showInfo = function(e) {
                 var id = e.currentTarget.get('id');
                 var displayDiv = id + '_itIs';

                 if (DEAL.tripAdvisorOverlays[id] === undefined) {
                   DEAL.tripAdvisorOverlays[id] = new Y.Overlay({
                                                                  contentBox : '#' + displayDiv,
                                                                  visible : true,
                                                                  align : {
                                                                    node : '#' + id,
                                                                    points:[Y.WidgetPositionAlign.BL, Y.WidgetPositionAlign.RC]
                                                                  }
                                                                });
                 }
                 DEAL.tripAdvisorOverlays[id].render();

                 DEAL.display(displayDiv);

                 new Y.IBEFade(displayDiv, {
                   from : { opacity : 0,width : '180px',height : '15px' },
                   to : { opacity : 1, width : '180px', height : '15px' },
                   duration: 0.3
                 }).fadeIn();

                 DEAL.currentTripAdvisorOverlayId = displayDiv;
               };

               var hideInfo = function(e) {
                 var id = Y.Event.getEvent(e).target.get('id');
                 if (id && id.match('tripAdvisor_')) return;
                 if (DEAL.currentTripAdvisorOverlayId !== undefined) {
                   if (Y.one('#' + DEAL.currentTripAdvisorOverlayId).getStyle('opacity') == 1) {
                     new Y.IBEFade(DEAL.currentTripAdvisorOverlayId).fadeOut();
                   }
                 }
               };

               tripAdvisorInfoNodes.on('click', showInfo, this);
               Y.one(document.body).on('click', hideInfo);
             };

             YAHOO.ibe.deals.createAndShowOverlay = function() {
               var pageDetailsOverlay = Y.one('#' + DEAL.pageDetailsOverlayElementId);
               var pageOverlayDiv = Y.one('#' + DEAL.pageOverlayDiv);
               var mainContent = Y.one('#mainContentHolidayDeals');
               var filtersElement = Y.one('#holidayDealsFiltersId');

               if (pageDetailsOverlay === null) {
                 var width = parseInt(mainContent.get('offsetWidth'));
                 var height = parseInt(mainContent.get('offsetHeight')) + 20;
                 var div = '<div id="' + DEAL.pageDetailsOverlayElementId +
                           '" class="hotelDetailsTransparentOverlayDiv"' +
                           ' style="position:abosulute; width:' + width + 'px; height:' + height + 'px;' + '"></div>';
                 pageOverlayDiv.append(div);
               } else {
                 DEAL.display(DEAL.pageDetailsOverlayElementId);
               }
             };

             YAHOO.ibe.deals.selectThumbnailImage = function() {
               DEAL.hotelImagesInTabDiv = 'hotelImagesInTabDiv_' + DEAL.currentUniqueId;
               DEAL.hotelImageBig = 'hotelImageBig_' + DEAL.currentUniqueId;

               var thumbnailImages = undefined;
               var selectedThumbnailImage = undefined;

               var select = function(e) {
                 var thumbnailImageDiv = e.currentTarget;
                 if (thumbnailImageDiv) {
                   selectedThumbnailImage.removeClass('thumbnailSelected'); // unselect
                   thumbnailImageDiv.addClass('thumbnailSelected'); // selected
                   // Change the big picture.
                   var thumbnailImageHTML = thumbnailImageDiv.get('innerHTML');
                   var bigImg = Y.one('#' + DEAL.hotelImageBig);
                   if (bigImg) {
                     bigImg.set('innerHTML', thumbnailImageHTML);
                   }
                   selectedThumbnailImage = thumbnailImageDiv;
                 }
               };

               Y.on('available', function() {
                 thumbnailImages = Y.all('.hotelImagesInTabView');
                 if (thumbnailImages && thumbnailImages.size() > 0) {
                   thumbnailImages.on('click', select);
                   // Set the first image selected
                   if (selectedThumbnailImage === undefined) {
                     selectedThumbnailImage = thumbnailImages.item(0);
                     selectedThumbnailImage.addClass('thumbnailSelected');
                   }
                 }
               }, '#' + DEAL.hotelImagesInTabDiv, this);

             };

             YAHOO.ibe.deals.generateHtmlForThumbnailImageList = function(images) {
               var html = "";
               for (var i = 0; i < images.length; i++) {
                 if ((i % 7) == 0 && i != 0) {
                   html += '<br/>';
                 }
                 html += '<div class="hotelImagesInTabView pointer"><img src="' + images[i].url + '" title="Image ' + i + '"/></div>';
               }
               return html;
             };

           });

