// globals: stores the gallery definitions and provides some utility methods
var NEI = function() {
  return { 
    Galleries: [
    /* New Stuff */
      {
        IsPublic: true,
        Abbrev: "BSU ILC",
        Name:  "BSU Interactive Learning Center",
        Owner: "State of Idaho",
        Architect: "LCA Architects, P.A. - Project Architect",
        Pictures: ['BSULearningCenter02', 'BSULearningCenter']
      },
      {
        IsPublic: true,
        Name:  "BSU Stadium",
        Owner: "State of Idaho",
        Architect: "Synergy Engineering",
        Pictures: ['bsuStadium02', 'bsuStadium']
      },
      {
        IsPublic: true,
        Name:  "BSU Student Union",
        Owner: "State of Idaho",
        Architect: "LCA Architects, P.A.",
        Pictures: ['BSUStudentUnion', 'BSUStudentUnion02', 'BSUStudentUnion03']
      },
      {
        IsPublic: false,
        Name:  "Bown Crossing",
        Owner: "Albertsons, Inc.",
        Architect: "Erstad Thornton Architects",
        Pictures: ['brownCrossing04', 'brownCrossing03', 'brownCrossing02', 'brownCrossing01']
      },
      {
        IsPublic: false,
        Name:  "Burlington Coat Factory",
        Owner: "Milan Properties",
        Architect: "CSHQA",
        Pictures: ['Burlington', 'Burlington02', 'Burlington03']
      },
      {
        IsPublic: false,
        Name:  "Cambria Suites",
        Owner: "Choice Hotels",
        Architect: "Mind's Eye Architecture",
        Pictures: ['cambria']
      },
      {
        IsPublic: false,
        Name:  "Cost Plus World Market",
        Owner: "Langly Properties/Kimco Developers",
        Architect: "CTA Architects/Engineers",
        Pictures: ['CostPlus', 'CostPlus02', 'CostPlus03', 'CostPlus04']
      },
      {
        IsPublic: false,
        Abbrev:  "D.L. Evans Bank, TI",
        Name:  "D.L. Evans Bank Nampa, Tenant Improvement",
        Owner: "D.L. Evans Bank (An Idaho Corporation)",
        Architect: "Erstad Thorton Architects",
        Pictures: ['DLEvans', 'DLEvans02']
      },
      {
        IsPublic: false,
        Name:  "DA Warehouse",
        Owner: "D & A Commercial Warehousing, LLC",
        Architect: "Alderson, Karst & Mitro Architects P.A.",
        Pictures: ['DAWarehouse', 'DAWarehouse02', 'DAWarehouse03', 'DAWarehouse04']
      },
      {
        IsPublic: false,
        Name:  "Direct Buy",
        Owner: "BK West, Inc.",
        Architect: "B R S Architects",
        Pictures: ['directBuy']
      },
      {
        IsPublic: true,
        Name:  "Eagle City Hall",
        Owner: "City of Eagle",
        Architect: "BRS Architects",
        Pictures: ['EagleCityHall', 'EagleCityHall02', 'EagleCityHall03']
      },
      {
        IsPublic: false,
        Name:  "Eagle Executive Suites",
        Owner: "Kittyhawk Investments",
        Architect: "BRS Architects",
        Pictures: ['EagleExecutiveSuites', 'EagleExecutiveSuites02', 'EagleExecutiveSuites03']
      },
      {
        IsPublic: false,
        Abbrev:  "Five Diamond Properties",
        Name:  "Five Diamond Properties",
        Owner: "Office Value",
        Architect: "Architecture Northwest, P.A.",
        Pictures: ['OfficeValue', 'OfficeValue02']
      },
      {
        IsPublic: true,
        Name:  "Harney Hospital",
        Owner: "Harney District Hospital",
        Architect: "CTA Architects/Engineers",
        Pictures: ['HarneyHospital','HarneyHospital02']
      },
      {
        IsPublic: true,
        Name:  "Hispanic Cultural Center",
        Owner: "The Hispanic Cultural Center of Idaho, Inc.",
        Architect: "Olson & Associates Architects PA",
        Pictures: ['hispanicCenter', 'hispanicCenter02']
      },
      {
        IsPublic: true,
        Abbrev:  "Idaho Center Rodeo Club",
        Name:  "Idaho Center Rodeo Club",
        Owner: "City of Nampa",
        Architect: "Olson & Associates",
        Pictures: ['RodeoClub', 'RodeoClub02', 'RodeoClub03']
      },
      {
        IsPublic: true,
        Name:  "Idaho History Center",
        Owner: "State of Idaho",
        Architect: "ZGA Architects and Planners, Chartered",
        Pictures: ['IdahoHistoryCenter', 'IdahoHistoryCenter02']
      },
      {
        IsPublic: false,
        Name:  "Kohl's",
        Owner: "Kohl's Department Stores, Inc.",
        Architect: "Schroeder and Holt Architects, LTD",
        Pictures: ['kohls', 'kohls02']
      },
      {
        IsPublic: true,
        Name:  "Kuna Elementary School No. 2",
        Owner: "Kuna Joint School District No. 3",
        Architect: "Leatham-Krohn-VanOcker Architects",
        Pictures: ['KunaElementary', 'KunaElementary02']
      },
      {
        IsPublic: false,
        Name:  "LDS Paramount",
        Owner: "Corporation of the Presiding Bishop of the Church of Jesus Christ of Latter-Day Saints",
        Architect: "Lombard-Conrad Architects PA",
        Pictures: ['ldsParamount', 'ldsParamount02']
      },
      {
        IsPublic: false,
        Name:  "LDS River Heights Ward",
        Owner: "Church of Jesus Christ of Latter-day Saints",
        Architect: "Lystrup-Jensen Architects",
        Pictures: ['LDSRiverHeights', 'LDSRiverHeights02', 'LDSRiverHeights03']
      },
      {
        IsPublic: true,
        Name:  "Library! at Cole and Ustick",
        Owner: "City of Boise",
        Architect: "Fletcher, Farr, Ayotte, PC",
        Pictures: ['ColeLibrary', 'ColeLibrary02']
      },
      {
        IsPublic: false,
        Name:  "Lowe's",
        Owner: "Lowe's Home Centers, Inc.",
        Architect: "SSOE, Inc.",
        Pictures: ['lowes']
      },
      {
        IsPublic: false,
        Name:  "Marcon",
        Owner: "Marcon",
        Architect: "B R S Architects",
        Pictures: ['marcon']
      },
      {
        IsPublic: false,
        Name:  "MOB VII at St. Alphonsus",
        Owner: "Cameron Investments, L.C.",
        Architect: "CSHQA",
        Pictures: ['StAlMob7', 'StAlMob702', 'StAlMob703']
      },
      {
        IsPublic: true,
        Abbrev:  "Meridian Water Dept.",
        Name:  "Meridian Water Department Bldg. 06-021",
        Owner: "City of Meridian",
        Architect: "BRS Architects",
        Pictures: ['MeridianWater']
      },
      {
        IsPublic: true,
        Abbrev:  "Meridian Water Dept.",
        Name:  "Meridian Water Department Bldg. 06-022",
        Owner: "City of Meridian",
        Architect: "BRS Architects",
        Pictures: ['MeridianWater02']
      },
      {
        IsPublic: false,
        Name:  "Michaels",
        Owner: "Langly Properties/Kimco Developers",
        Architect: "CTA Architects/Engineers",
        Pictures: ['Michaels', 'Michaels02', 'Michaels03']
      },
      {
        IsPublic: true,
        Abbrev:  "Nampa High School",
        Name:  "Nampa High School Bldgs.  250-500",
        Owner: "Nampa School District # 131",
        Architect: "Design West Architects, P.A.",
        Pictures: ['NampaHigh', 'NampaHigh02', 'NampaHigh03', 'NampaHigh04', 'NampaHigh05', 'NampaHigh06']
      },
      {
        IsPublic: true,
        Abbrev:  "Neighborhood Housing",
        Name:  "Neighborhood Housing Services Renovation",
        Owner: "Neighborhood Housing Services, Inc.",
        Architect: "Trout Architects, Chartered",
        Pictures: ['NHSReno', 'NHSReno02', 'NHSReno03']
      },
      {
        IsPublic: false,
        Name:  "Norco Fill Plant",
        Owner: "Norco, Inc.",
        Architect: "Larson Architects, P.A.",
        Pictures: ['NorcoFill', 'NorcoFill02']
      },
      {
        IsPublic: false,
        Name:  "OBGYN",
        Owner: "OB/GYN Associates, P.A.",
        Architect: "ZGA Architects and Planners Chartered",
        Pictures: ['obgyn', 'obgyn02']
      },
      {
        IsPublic: false,
        Abbrev:  "Our Lady of The Valley",
        Name:  "Our Lady of The Valley Catholic Church Expansion",
        Owner: "Catholic Diocese of Idaho",
        Architect: "Hummel Architects, PLLC",
        Pictures: ['OurLadyExpansion', 'OurLadyExpansion02', 'OurLadyExpansion03', 'OurLadyExpansion04']
      },
      {
        IsPublic: false,
        Abbrev:  "Petco Nampa",
        Name:  "Petco - Nampa, ID Store #1229",
        Owner: "Langly Properties/Kimco Developers",
        Architect: "Karina T. Szymkowiak",
        Pictures: ['PetcoNampa', 'PetcoNampa02']
      },
      {
        IsPublic: false,
        Name:  "Portico MOB A",
        Owner: "Gardner Ahlquist Development",
        Architect: "VCBO Architecture",
        Pictures: ['PorticoMobA', 'PorticoMobA02', 'PorticoMobA03']
      },
      {
        IsPublic: false,
        Abbrev:  "Target Stores",
        Name:  "Target Stores Western Region",
        Owner: "Target Stores T-0699",
        Architect: "Mulvanny G2 Architects",
        Pictures: ['Target', 'Target02', 'Target03']
      },
      {
        IsPublic: false,
        Name:  "Victory Plaza",
        Owner: "Victory Properties, LLC",
        Architect: "B R S Architects",
        Pictures: ['victoryPlaza']
      },
      {
        IsPublic: false,
        Name:  "Walgreens",
        Owner: "Walgreens Co.",
        Architect: "BRS Architects",
        Pictures: ['Walgreens', 'Walgreens02', 'Walgreens03']
      },
      {
        IsPublic: false,
        Name: "Washington Trust Bank",
        Owner: "Washington Trust Bank",
        Architect: "Wolfe Architectural Group, P.S.",
        Pictures: ['trustBank', 'trustBank02']
      },
      {
        IsPublic: false,
        Name:  "WinCo Corporate Office Remodel",
        Owner: "WinCo Foods, LLC",
        Architect: "Tait & Associates, Inc.",
        Pictures: ['WincoCorp', 'WincoCorp02']
      },
      {
        IsPublic: false,
        Abbrev:  "Wyndstone Place",
        Name:  "Wyndstone Place Business Park",
        Owner: "Wyndstone Place Business Owners Association, Inc.",
        Architect: "Hummel Architects",
        Pictures: ['Wyndstone', 'Wyndstone02', 'Wyndstone03']
      }
    ],
    
    // filter public/private galleries: set index on returned elements so we know where they
    // are in the Galleries list
    getPublicGalleries: function() {
      return $.grep(NEI.Galleries, function(value, index) {
        value.Index = index;
        return value.IsPublic;
      });
    },
    
    getPrivateGalleries: function() {
      return $.grep(NEI.Galleries, function(value, index) {
        value.Index = index;
        return !value.IsPublic;
      });
    },
    
    // load images into cache
    preloadImages: function() {
      $.each(NEI.Galleries, function() {
        $.each(this.Pictures, function(){
          $.get('images/portfolio/' + this + '.jpg');
          $.get('magesimages/portfolio/' + this + 'SM.jpg');
        });
      });  
    }
  };
}();

// sugar
Array.prototype.first = function() { return this[0]; };

// startup method: invoked when page is loaded.
$(function() {
  // look for container div; exit if not present
  if (!$('#portfolio-container').length)
    return;
  
    var contentContainer = $('#portfolio-container').closest('table').hide();

    // don't need a back button - but thanks anyway!
    $('.back_button').remove();
  
    // reset css
    $('div#contentholder').css('padding-left', '0px');
    $('div#contentholder').css('padding-right', 0);
  
    // set up the portfolio div
    var portfolio = $('#portfolio-container');
    var index = portfolio.galleryNavigation();
    portfolio.gallery(index);
    // ensure that the content is displayed, no matter what...
    contentContainer.show();
  // since caching is now enabled, this makes sense
  // NEI.preloadImages();
});

// api functions.
(function($) {
  
  // build public/private gallery links
  $.fn.galleryNavigation = function() {
    var portfolio = $(this);
    
    var buildNavHtml = function(generator) {
      return "<ul>" + $.map(generator(), function(value, index) {
        var liClass = (index == 0) ? "class='first-real'" : "";
        var name = value.Abbrev || value.Name;
        return "<li " + liClass + "><a class='gallery-link' rel='" + value.Index + "' href='#'>" + name + "</a></li>";
      }).join(' ') + "</ul>";
    };
    
    var gallery = portfolio.hasClass('private') ? NEI.getPrivateGalleries : NEI.getPublicGalleries;
    portfolio.append("<div class='navigation-container'>" + buildNavHtml(gallery) + "</div>");
    // var li = $('li:first', portfolio).remove();
    
    // show/hide links under the header: each one is opened individually (as opposed to a div opening/closing)
    $('.header-open', portfolio).click(function() {
      $this = $(this);
      $this.toggleClass('is-open');
      $this.text(($this.hasClass('is-open') ? $this.text().replace('+', '-') : $this.text().replace('-', '+')));
		  $this.closest('ul').children('li').toggle('fast');
		  return false;
	  }).next().hide();

    // load the gallery data into the image container
    $('.gallery-link', portfolio).click(function(evt) {
      // bind the portfolio as a closure - could also bind and pass as data (evt.data.portfolio)
      var portfolio = portfolio;
      // extract the id from the link
      var id = $(evt.target).attr('rel');
      if (!id)
        return;
      // and gallerize
      $(this.portfolio).gallery(id);
      return false;
    });
    
    return gallery().first().Index;
  };
  
  // abusing the jquery api paradigm: this will gallerize the referenced element
  $.fn.gallery = function(id) {
    
    // method to build the description div contents
    var getDescription = function(elt) {
      return "<h4>" + elt.Name + "</h4><p class='owner'>Owner: " + elt.Owner + "</p><p class='architect'>Architect: " + elt.Architect + "</p>";
    };
    
    var gallery = $(this);
    if (id === undefined)
      return;
    
    // if not gallerized, set up structure
    if ($('.image-container', gallery).length === 0)
      gallery.append("<div class='image-container'><div class='main-image'></div><div class='description'></div><div class='sub-gallery'></div></div>");
    
    var imageContainer = $('.image-container', gallery);
    var elt  = NEI.Galleries[id];
    // set up description div
    $('.description', imageContainer).empty().append(getDescription(elt));
    // and load the main (first) image associated with this project
    $('.main-image', imageContainer).galleryImage(elt.Pictures[0]);
    
    // load the sub gallery images
    var subGallery = $('.sub-gallery');
    subGallery.empty();
    $.each(elt.Pictures, function(index, value){
      subGallery.append("<img class='sub-gallery-image' rel='" + value + "' src='images/portfolio/" + value + "SM.jpg'/>");
    });
    
    // when the sub-gallery image is clicked, load the main image (image name is in rel attribute)
    $('.sub-gallery-image', imageContainer).click(function(evt) {
      // bind the portfolio as a closure - could also bind and pass as data (evt.data.portfolio)
      var imageContainer = imageContainer;
      // extract the id from the link
      var image = $(evt.target).attr('rel');
      if (!image)
        return;
      // and gallerize
      $('.main-image', this.imageContainer).galleryImage(image);
      return false;
    });
  };
  
  // load the main image
  $.fn.galleryImage = function(image) {
    var imageContainer = $(this);
    var img = "<img src='images/portfolio/" + image + ".jpg'/>";
    imageContainer.empty().append(img);
  };
  
  // useful...log to console, but only if console is defined.  prevents IE issues.
  $.extend({log: function (msg) { if (window.console && window.console.log) window.console.log('debug: ' + msg);}});
  
})(jQuery);
