/// <reference path="jquery-1.2.3.js" />

var Gallery = {
  gIdStr:'gallery',
  gJQIDSTR:'#gallery',
  galleryId:0,
  endIndex:0,
  totalFiles:0,
  _mainImageId: 'galleryMainImg',
  _singleItemWidth: 115,
  _visibleWidth:690,
  _animationDuration:1200,
  _itemsetLength:6,
  _itemsetWidth:0,
  _galleryWidth:0,
  maxLeft:0,
  pageSize:20,

  init: function() {
    if (document.getElementById(Gallery.gIdStr)) {
        this.constructList();
        this.getGalleryImages();
        this.setFirstImageActive();
    }
  },
  constructList: function() {
    $(Gallery.gJQIDSTR+" #galleryImages").html("<ul></ul>");
  },

/* Sample request result
{ "__type":"Atwww.PhotoGallery.Web.Controls.Messaging.ImageItemCollection",
  "TotalFiles":6,   "StartIndex":25,   "EndIndex":30, 
  "ImageItems":[{
    "Id":20,
    "ImagePath":"/_uploads/Images/PhotoGallery/gallery_Delatite-7th-Dec2006-1341.jpg",
    "MediumThumbPath":"/_uploads/Images/PhotoGallery/MediumThumb_gallery_Delatite-7th-Dec2006-1341.jpg",
    "SmallThumbPath":"/_uploads/Images/PhotoGallery/SmallThumb_gallery_Delatite-7th-Dec2006-1341.jpg",
    "AltText":"gallery_Delatite-7th-Dec2006-1341",
    "Caption":""
  },{},{}]
}


  */
  appendImage: function(objDetails) {
    var img = objDetails;
    //alert(img);
    
    f="Gallery.setMainImage({MediumThumbPath:'"+img.MediumThumbPath+"',Caption:'"+img.Caption+"',AltText:'"+img.AltText+"'},this);"
    var h = '<li id="galleryImage_'+img.Id+'"><a href="javascript:;" onclick="'+f+'"><img src="'+img.SmallThumbPath+'" alt="'+img.AltText+'" /></a></li>';
    $('#gallery #galleryImages ul').append(h);
    this.galleryReConstructor();
  },
  
  setFirstImageActive: function() {
    if (Gallery.images){
      //Gallery.setMainImageSource(Gallery.images.ImageItems[0].MediumThumbPath);
      liId = $(Gallery.gJQIDSTR + " li:first").attr('id');
      l = document.getElementById(liId).getElementsByTagName('a')[0];
      Gallery.setMainImage(Gallery.images.ImageItems[0],l);
      
    }
  },
  
  buildGallery: function(images) {
    firstRequest=false;
    if (images) {
      if (Gallery.totalFiles == 0) {
        firstRequest=true;
      }
      Gallery.images = images;
      Gallery.totalFiles = images.TotalFiles;
      Gallery.endIndex = images.EndIndex;
      currentSet = images.ImageItems;
      if (currentSet && currentSet.length>0){
        csl=currentSet.length;
        for (var i=0;i<csl;i++) {
          if (currentSet[i]) {
            Gallery.appendImage(currentSet[i]);
          }
        }
      }
      Gallery.endIndex=images.EndIndex;
    }
    if (firstRequest) {
      Gallery.setFirstImageActive();
    }
  },
  
  getGalleryImages: function() {
    $.ajax({
      type: "POST",
      url: "/Messaging/PhotoGallery.asmx/GetGalleryImages",
      contentType: "application/json; charset=utf-8",
      dataType: "json",
      data: '{"galleryId":"' + this.galleryId + '",'
          + '"pageSize":"'+this.pageSize+'",'
          + '"startIndex":"' + (parseInt(this.endIndex) + 1) + '"}',
      success: this.buildGallery
    });
    
  },
  galleryReConstructor: function() {
    this.setWidth();
    this._galleryWidth = this._calcWidth();
    this._itemsetWidth = this._singleItemWidth*this._itemsetLength;
    this.maxLeft = (this._galleryWidth-this._itemsetWidth);
  
  },

  addLinks: function() {
    $("#gallery #galleryImages li img").wrap("<a href=\"javascript:;\" onclick=\"Gallery.set(this);false;\"></a>");
  },

  setMainImage: function(imgObj, el) {
    this.setActive(el);
    imgSrc = imgObj.MediumThumbPath;
    imgEl = el.getElementsByTagName('img')[0];
    imgAlt = imgEl.alt; 
    mi = document.getElementById(this._mainImageId).getElementsByTagName('img')[0];
    mi.alt = imgAlt;
    if (isIE6OrLess()) {
      setTimeout("mi.src=\""+imgSrc+"\"",0);
    }
    else {
      mi.src = imgSrc;
    }
    var capt=imgObj.Caption;
    if (imgObj.Caption =='') {
      if(imgObj.AltText=='') {
        capt='';
      }
      else{
        capt=imgObj.AltText;
      }
    }
    
    $("#"+this._mainImageId+" .caption p").html(capt);
    
    
  },
  setMainImageSource: function setMainImageSource(strSrc) {
    im = document.getElementById(this._mainImageId).getElementsByTagName('img')[0];
    im.src=strSrc;
  },
//  setMainImage: function(src, el) {
//  },

  setActive: function(el) {
    //alert(el.firstChild.src);
    items = el.parentNode.parentNode.getElementsByTagName('li');
    for (var i=0;i<items.length;i++){
      imgEl = items[i];
      $(imgEl).removeClass('active');
    }
    el.parentNode.className='active';

  },
  slideLeft: function() {

    leftVal = this._getLeft()+this._itemsetWidth;
      //this.notice(this._getLeft() + ' ' + (this._galleryWidth-this._itemsetWidth));
    if ((leftVal*-1) < 0) {
      leftVal = 0;
      $('#galleryImages ul').stop().animate( { left:leftVal+'px' },this._animationDuration/2);
    }
    if (((leftVal*1)-1)+(this._visibleWidth*(Math.floor(this._getImageCount()/this._itemsetLength))) < this._calcWidth()) {
      $('#galleryImages ul').stop().animate( { left:leftVal+'px' },this._animationDuration);
    }

  },
  slideRight: function() {
    if(Gallery.totalFiles<=Gallery._itemsetLength) {
      // the total files are less than we need to slide...
      return;
    }
    rightVal = this._getLeft()-this._itemsetWidth;
    if ((rightVal*-1) > this.maxLeft) {
      rightVal = -this.maxLeft;
      $('#galleryImages ul').stop().animate( { left:rightVal+'px' },this._animationDuration/2);
    }
    if (rightVal*-1 < this._calcWidth()) {
      $('#galleryImages ul').stop().animate( { left:rightVal+'px' },this._animationDuration);
    }
    //if end reached, fetch more images.
    if ((rightVal-this._visibleWidth)<-this.maxLeft) {
      if ($('#galleryImages ul').children().length <= (Gallery.totalFiles-this._itemsetLength)) {
        this.getGalleryImages();
      }
    }
  },
  _getLeft: function() {
    return parseInt($('#galleryImages ul').css('left'));
  },
  _calcWidth: function() {
    return $('#galleryImages ul').children('li').length * this._singleItemWidth;
  },

  setWidth:function() {
    $('#galleryImages ul').css('width',this._calcWidth()+"px");
  },

  _getImageCount:function() {
    return $('#galleryImages ul').children().length;
  }
}

$(document).ready(init);
function init() {
  Gallery.init();  
  Menu.init();
}

var Menu = {
  init:function() {
    $("ul.Isite-Menu li.Isite-Menu-WithChildren ul").prepend("<li class=\"topFade\">&nbsp;</li>");
    $("ul.Isite-Menu li a").wrapInner("<span></span>");
  }

}

/** isIE6OrLess()
 *  for some of our methods its neccesary to know if we're dealing with an old browser
 *
 *  @returns {bool}
 */
function isIE6OrLess() {
    //Detect < IE6
    version=0
    if (navigator.appVersion.indexOf("MSIE")!=-1){
        temp=navigator.appVersion.split("MSIE");
        version=parseFloat(temp[1]);
    }
    if (version<=6.0 && version !=0) { //NON IE browser will return 0
      return true;
    }
    return false;
}