/*
 * Terry Doherty (uncompressed)
 * Copyright (c) 2008 Terry Doherty Partnership
 */

$.fn.replace = function(){
  return this.domManip(arguments, true, 1, function(a){
    this.parentNode.replaceChild(a, this);
  });
};

$.fn.loadImage = function(src, title, alt, callback){
  return this.each(function(){
    var i = new Image();
        i.src = src;
        i.alt = alt;
        i.title = title;
        i.onload = callback;
    $(this.firstChild).replace(i);
  });
};

var textArray = new Object();
    textArray.home = "<img src='images\/portfolio_legend.gif' \/><br \/>We trust the selection of projects and clients in this portfolio demonstrates to you the wealth and scope of our experience and may encourage you to <a href='contact_us.html'>contact us<\/a> regarding your specific project requirements and needs. Our goal will not be to meet your needs but to exceed them.<br \/><br \/>In order to browse our work <strong>click the coloured bars<\/strong> below to change between the various sectors we work within. For further help on using our portfolio please <a href='help.html'>view our site help<\/a>.";
    textArray.retail = "<img src='images\/retail_legend.gif' \/><br \/>We have developed an awareness and understanding of the specific requirements of the retail sector having worked throughout Ireland in all areas of the sector, including shop fitting, shopping centre development, retail parks and major store development and refurbishment. We provided quantity surveying services on the new build and refurbishment of &pound;80 million worth of retail space for Safeway Stores.";
    textArray.offices = "<img src='images\/offices_legend.gif' \/><br \/>Terry Doherty Partnership have developed particular expertise in the provision of major office developments for the private sector.";
    textArray.hotels = "<img src='images\/hotels_legend.gif' \/><br \/>We have been involved in various aspects of the tourism and leisure sector in the construction of new hotels, swimming pools and spa complexes and major extensions to existing hotels.";
    textArray.bars = "<img src='images\/br_legend.gif' \/><br \/>We have considerable expertise in the development and refurbishment of bars and nightclub complexes for both individual owners and well known groups within the licensed sector throughout Northern Ireland and the Republic of Ireland.";
    textArray.industrial = "<img src='images\/industrial_legend.gif' \/><br \/>Our extensive experience covers the provision of facilities across the industrial base in Northern Ireland and includes modernisation and redevelopment as well as new-build projects.";
    textArray.education = "<img src='images\/education_legend.gif' \/><br \/>The practice has undertaken a broad spectrum of education assignments having worked throughout the province in all areas, including universities, higher education, secondary, primary, nursery and special need schools.";
    textArray.residential = "<img src='images\/res_legend.gif' \/><br \/>In both the public and private sectors our experience ranges from new build houses and apartments to the repair, maintenance and refurbishment of existing housing stock.";
    textArray.churches = "<img src='images\/churches_legend.gif' \/><br \/>Our experience includes new church buildings, extensions and restoration &amp; refurbishments of older churches for all denominations and faiths.";

var imageArray = new Object();
    imageArray.retail = [
      'images/retail/retail_17_',
      'images/retail/retail_3_',
      'images/retail/retail_6_',
      'images/retail/retail_7_',
      'images/retail/retail_9_',
      'images/retail/retail_10_',
      'images/retail/retail_13_',
      'images/retail/retail_14_',
      'images/retail/retail_15_',
      'images/retail/retail_16_',
      'images/retail/retail_18_'
    ];
    imageArray.offices = [
      'images/office/office_1_',
      'images/office/office_2_',
      'images/office/office_3_',
      'images/office/office_6_',
      'images/office/office_8_',
      'images/office/office_9_',
      'images/office/office_10_'
    ];
    imageArray.hotels = [
      'images/hotels/hotels_1_',
      'images/hotels/hotels_2_',
      'images/hotels/hotels_3_',
      'images/hotels/hotels_4_',
      'images/hotels/hotels_9_',
      'images/hotels/hotels_10_',
      'images/hotels/hotels_14_'
    ];
    imageArray.bars = [
      'images/bars/bars_2_',
      'images/bars/bars_3_',
      'images/bars/bars_6_',
      'images/bars/bars_8_',
      'images/bars/bars_9_',
      'images/bars/bars_12_',
      'images/bars/bars_14_',
      'images/bars/bars_17_'
    ];
    imageArray.industrial = [
      'images/industrial/industrial_1_',
      'images/industrial/industrial_2_',
      'images/industrial/industrial_3_',
      'images/industrial/industrial_4_',
      'images/industrial/industrial_5_',
      'images/industrial/industrial_6_',
      'images/industrial/industrial_7_'
    ];
    imageArray.education = [
      'images/education/education_5_',
      'images/education/education_7_',
      'images/education/education_8_',
      'images/education/education_9_',
      'images/education/education_10_',
      'images/education/education_11_',
      'images/education/education_13_',
      'images/education/education_15_',
      'images/education/education_16_'
    ];
    imageArray.residential = [
      'images/residential/residential_1_',
      'images/residential/residential_2_',
      'images/residential/residential_4_',
      'images/residential/residential_5_',
      'images/residential/residential_6_'
    ];
    imageArray.churches = [
      'images/churches/church_1_',
      'images/churches/church_2_'
    ];
    imageArray.home = [
      'images/churches/church_1_',
      'images/churches/church_2_'
    ];

var captionArray = new Object();
    captionArray.retail = [
      'Sligo Retail Park',
      'Saltmarine Motors, Dungannon',
      'Bow Street Mall, Lisburn',
      'Lesley Plaza, 406-408 Lisburn Road, Belfast',
      'Letterkenny Retail Park',
      'Letterkenny Retail Park',
      'Flagship Centre, Interior, Bangor',
      'McDonalds province wide',
      'Safeway Stores, Northern Ireland',
      'KFC throughout Ireland',
      'Sligo Retail Park'
    ];
    captionArray.offices = [
      'City Exchange, Belfast',
      'City Exchange, Belfast',
      'Glengall Exchange, Belfast',
      'Lesley Exchange, Belfast',
      'Lesley Studios, Belfast',
      'Lesley Suites, Belfast',
      'Derryvolgie House, Belfast'
    ];
    captionArray.hotels = [
      'Armagh City Hotel, Armagh',
      'The Manor House, Killedas',
      'The Bushmills Inn, Bushmills',
      'Hillgrove Hotel, Monaghan',
      'Canal Court Hotel, Newry',
      'Killerig Hotel, Co. Carlow',
      'Old Inn, Crawfordsburn'
    ];
    captionArray.bars = [
      'The M Club, Belfast',
      'Harbour Point, Carrickfergus',
      'The Point, Belfast',
      'Cafe Vaudeville, Belfast',
      'Cafe Vaudeville, Belfast',
      'Hunting Lodge, Belfast',
      'The Edge, Belfast',
      'The Chelsea Bar, Belfast'
    ];
    captionArray.industrial = [
      'Regeneration Project Moygashel',
      'Sperrin Metals Factory, Draperstown',
      'Olympic Lifts, Lisburn',
      'Irish News Interpress, Belfast',
      'Warehouse Unit, Livingston, Scotland',
      'Industrial Units, Armagh',
      'Industrial Units, Enterprise Crescent, Lisburn'
    ];
    captionArray.education = [
      'St. Patricks High School, Keady, Co. Armagh',
      'Harberton School, Belfast',
      'Larne AEC',
      'Larne AEC',
      'Larne AEC Interior',
      'Royal School, Dungannon',
      'Royal School Courtyard, Dungannon',
      'University of Ulster Interior',
      'St. Colmans College, Newry'
    ];
    captionArray.residential = [
      'Windsor Park, Belfast',
      'Apartment Development, Strand Road, Derry',
      'Old Station, Dunadry',
      'Cranmore Park, Belfast',
      'Golf Lodges, Killerig Golf Country Club, Carlow'
    ];
    captionArray.churches = [
      'Church Restoration',
      'Church of the Annunciation, Belfast'
    ];
    captionArray.home = [
      'Church Restoration',
      'Church of the Annunciation, Belfast'
    ];

$(document).ready(function(){

  // state
  var current = $("#home");
  var slideTimer = null;
  var currentCaption;
  var slideShowSpeed = 9000;

  if(document.location.toString().match('#')){
    $(current).css({width:"15px"});
    var g = document.location.toString().split('#')[1];
    loadGallery(g);
  }else{
    $(current).css({width:"260px"});
  }

  init();

  function init(){
    $("#loader").bind("click", function(e){
      $('#ploader').empty();
      $('#preloader').fadeIn();
      $('#text').hide();
      var link = $("#loader").attr("loadimage");
      var img = new Image();
      $(img).load(function(){
        $(this).hide();
        $('#ploader').append(this);
        $('#preloader').hide();
        $(this).fadeIn(function(){
          $('#text').fadeIn();
        });
      }).error(function(e) {
        alert("Image load error, press OK and refresh the page: err. " + e);
      }).attr('src', link);
      $("#blackout").show();
      $("#image-display").show();
      return false;
    });
    $("#loader").bind("mouseover", function(e){$(this).css({'cursor':'pointer'});});
    $("#loader").bind("mouseout",  function(e){$(this).css({'cursor':'pointer'});});
    // menu - mouse click event
    $('a').each(function(){
      if(this.getAttribute('href') && (this.getAttribute('rel') == 'menuItem')){
        $(this).bind("click", function(e){
          if($(this).attr('id') != $(current).attr('id')){
            $("#menu").block({message: null});
            $("#portfolioMenu").block({message: null});
            var g = $(this).attr('id');
            clearInterval(slideTimer);
            $("#pagination").fadeOut(350, function(e){
              $("#pagination").empty();
              $("p#portfolioText").fadeOut(350, function(e){
                $(current).animate({width:"15px"}, 300, function(e){ loadGallery(g); }); //cb
              }); //cb
            }); //cb
          }
        });
      }
      // side links
      if(this.getAttribute('href') && (this.getAttribute('rel') == 'sideLink')){
        $(this).bind("click", function(e){
          $("#menu").block({message: null});
          $("#portfolioMenu").block({message: null});
          var g = $(this).attr('href');
          clearInterval(slideTimer);

          $("#pagination").fadeOut(350, function(e){
            $("#pagination").empty();
            $("p#portfolioText").fadeOut(350, function(e){
              $(current).animate({width:"15px"}, 300, function(e){ loadGallery(g.replace(/#/, '')); }); //cb
            }); //cb
          }); //cb

        });
      }
    });
  }

  // 1
  function loadGallery(g){
    var displayText = textArray[g];
    var gallery     = imageArray[g];
    var captions    = captionArray[g];
    var targetDiv   = "#" + g;
    $("#menu").unblock({message: null});
    $("#portfolioMenu").unblock({message: null});
    $(".target").animate({ backgroundColor: g.toString() }, 150);
    $("#banner").animate({ backgroundColor: g.toString() }, 150);
    $("p#portfolioText").html(displayText);
    $("#" + g).animate({width:"260px"}, 250, function(e){
      current = $(this);
      $("p#portfolioText").fadeIn(300, function(e){
        for(var i=0; i<gallery.length; i++){
          $("#pagination").append("<a class='" + captions[i] + "' href='" + gallery[i] + "'rel='button' id='page" + i + "'>" + (i+1) + "<\/a>");
        }
        $("#pagination").fadeIn(300, function(e){
          initGallery();
          startSlideshow(captions, gallery);
          loadPreview(gallery[0]);
          currentCaption = captions[0];
          $("#page0").css({'background-color':'#fff' , 'color':'#2a8c93'});
        }); // cb
      }); //cb
    }); //cb
  }

  function startSlideshow(c, g){
    indexCurrent = 0;
    slideTimer = setInterval(function(){
      indexCurrent++;
      if(indexCurrent >= g.length) indexCurrent = 0;
      // reset
      $('a').each(function(){if(this.getAttribute('href') && (this.getAttribute('rel') == 'button')){$(this).css({'background-color':'#a7c6c8' , 'color':'#2a8c93'});}});
      $("#page" + indexCurrent).css({'background-color':'#fff' , 'color':'#2a8c93'});
      currentCaption = c[indexCurrent];
      loadPreview(g[indexCurrent]);
    }, slideShowSpeed);
  }

  // 2
  function initGallery(){
    // pagination - mouse click event
    $('a').each(function(){
      if(this.getAttribute('href') && (this.getAttribute('rel') == 'button')){
        // click
        $(this).bind("click", function(e){
          clearInterval(slideTimer);
          // reset
          $('a').each(function(){if(this.getAttribute('href') && (this.getAttribute('rel') == 'button')){$(this).css({'background-color':'#a7c6c8' , 'color':'#2a8c93'});}});
          $(this).css({'background-color':'#fff' , 'color':'#2a8c93'});
          $("#previousImage").empty();
          currentCaption = $(this).attr('class');
          loadPreview($(this).attr('href'));
          return false;
        });
      }

    /*
    if(this.getAttribute('href') && (this.getAttribute('rel') == 'lb')){
            this.onclick = function(){
              $('#ploader').empty();
              $('#preloader').fadeIn();
              $('#text').hide();
              var link = $("#loader").attr("loadimage");
              var img = new Image();
              $(img).load(function(){
                $(this).hide();
                $('#ploader').append(this);
                $('#preloader').hide();
                $(this).fadeIn(function(){
                  $('#text').fadeIn();
                });
              }).error(function(e) {
                alert("Image load error, press OK and refresh the page: err. " + e);
              }).attr('src', link);
              $("#blackout").show();
              $("#image-display").show();
              return false;
            }
      }
    */

    });


    $("#image-display").click(function(e) {
      $("#blackout").hide();
      $("#image-display").hide();
    });

  }

  // 3
  function loadPreview(img){
    var imageSrc     = img + "preview.jpg";
    var mainImageSrc = img + "main.jpg";
    $('#loader').fadeOut(300, function(e){
      $('#portfolioImage').block({ message: currentCaption });
      $("#loader").loadImage(imageSrc, "Terry Doherty Partnership", "www.tdoherty.com", function(){
        $('#loader').fadeIn(500);
        $('#portfolioImage').unblock();
        $("#loader").attr({loadimage: mainImageSrc});
      }); //cb
    }); //cb
  }

}); // document(ready)

