/**
  * Define the base POKE namespace
 **/
POKE = {};

/**
  * Define Image Gallery namespace
 **/

POKE.ImageFadeGallery = {
  init : function(id, imageArray, options){
		this.Gallery.init(id, imageArray, options);
		this.Controller.init(this.Gallery);
	}
};

/**
 * Define Gallery widget
 **/

POKE.ImageFadeGallery.Gallery = {
    init : function(id, imageArray, options) {
	//init the carousel (uses carousel lite)
	$("#carouselLite").jCarouselLite({
        btnNext: ".cnext",
        btnPrev: ".cprev",
		circular: false,
		speed: 300,
		scroll: 7,
		visible: 7
    });
    this.obj = $("#" + id)[0];
    this.imageArray = imageArray;
    this.opacity = 100;
    this.fadeFrame = 0;
    this.timerID = null;
    this.apTimerID = null;
    this.imagesLoaded = new Array(this.imageArray.length);
    this.options = options;
    
    // checking options
    this.current = (this.options.startNum) ? this.options.startNum : 0;
   
	
    var self = this;
    // bind events to controls
    var control;
    for (var i = 0; i < this.imageArray.length; i++) {
      if (control = $("#" + id + "_goto" + i)) {
		$('#' + control[0].id).bind("click", {element: control[0].id }, function(e){
			POKE.ImageFadeGallery.Controller.showImg(e);
			return false;
		});
      }
    }

    if (this.options.preload) {
         window.setTimeout(function() {
           self.preloadImgs();
         }, 50);
       }
       if (this.options.autoplay > 0) {
         var delay = this.options.autoplay * 1000 + 1000; // the additional 1000 compensates for the fade in time of the next image.
         this.apTimerID = window.setTimeout(function() {
           self.controller.autoplayImgs(delay);
         }, delay);
       }
  },
  
  preloadImgs: function() {
    for (var i = 0; i < this.imageArray.length; i++) {
      this.loadImageNumber(i);
    }
  },
  
  loadImageNumber: function(imgNum) {
    // check if already loaded
    if (!this.imagesLoaded[imgNum]) {
      this.imagesLoaded[imgNum] = new Image();
      this.imagesLoaded[imgNum].src = this.imageArray[imgNum].src;
    }
  },

  FADE_UP: new Array(0, 6, 17, 30, 47, 66, 75, 84, 92, 100),
  FADE_DOWN: new Array(0, 6, 17, 30, 47, 56, 66, 75, 84)
};

/**
 * Define Gallery widget controller
 **/
POKE.ImageFadeGallery.Controller = {
	
	init: function(gallery) {

	  this.gallery = gallery;
	  this.imageArray = this.gallery.imageArray;
	  this.id = this.gallery.obj.id;
	var that = this;
	$(this.gallery.obj).load(function () {
		that.startFade("up");
	});
	
  
	  // set initial image
	  this.gallery.obj.src = this.imageArray[this.gallery.current].src;
	  $('#'+this.id).attr({ 
	          src: this.gallery.obj.src,
	          title: "current gallery image",
	          alt: "gallery image"
	        }).css({ height: 443 });
	  this.onFadeEnd();
	},
	
	/* ------------- CALLBACKS ------------- */
	/* ------------- EDITABLE -------------- */

	onFadeStart: function() {
	  //this.setTitle("...");
	  //this.setCaption("&nbsp;");
	},

	onFadeEnd: function() {
	  var title = (this.imageArray[this.gallery.current].title) ? this.imageArray[this.gallery.current].title : "";
	  var caption = (this.imageArray[this.gallery.current].caption) ? this.imageArray[this.gallery.current].caption : "";
	},

	changeImg: function(imgNum) {
	  if (!this.gallery.imagesLoaded[imgNum]) {
	    this.gallery.loadImageNumber(imgNum);
	  }
	  this.gallery.obj.src = this.gallery.imagesLoaded[imgNum].src;
	},

	showImg: function(evt) {
	  var elem = evt.data.element;
	  var imgNum = parseInt(elem.substr(elem.indexOf('goto')+4));
	  if (this.gallery.current != imgNum) {
	    if (this.gallery.current < 0) {
	      this.gallery.current = this.imageArray.length - 1;
	    }
	    else if (this.gallery.current > this.imageArray.length - 1) {
	      this.gallery.current = 0;
	    }
	    else this.gallery.current = imgNum;
	    this.doFade();
	  }
	},

	doFade: function() {
	  if (!this.fading) {
	    this.fading = true;
	    this.onFadeStart();
	    this.startFade("down");
	  } else {
	    this.fading = false;
	    var self = this;
	    window.setTimeout(function() {
	      self.changeImg(self.gallery.current);
	    }, 50);
	    //this.startFade("up");
	    //this.gallery.obj.show();
	  }
	},

	startFade: function(dir) {
	  if (this.timerID) {
	    window.clearTimeout(this.timerID);
	    this.timerID = null;
	  }
	  // place delay before fade
	  var self = this;
	  switch(dir) {
	    case "up":
	      this.timerID = window.setTimeout(function() {
	        self.fadeUp();
	      }, 50);
	      break;
	    case "down":
	      this.timerID = window.setTimeout(function() {
	        self.fadeDown();
	      }, 50);
	  }
	},

	fadeUp: function() {
	  if (this.timerID) {
	    window.clearTimeout(this.timerID);
	    this.timerID = null;
	  }
	  this.calcOpacity(this.gallery.fadeFrame);
	  this.setOpacity(this.gallery.opacity);
	  this.gallery.fadeFrame++;

	  if (this.gallery.fadeFrame < POKE.ImageFadeGallery.Gallery.FADE_UP.length) {
	    var self = this;
	    this.timerID = window.setTimeout(function() {
	      self.fadeUp();
	    }, 10);
	  }
	  else {
	    this.gallery.fadeFrame = 0;
	    this.onFadeEnd();
	  }
	},

	fadeDown: function() {
	  if (this.timerID) {
	    window.clearTimeout(this.timerID);
	    this.timerID = null;
	  }
	  this.calcOpacity(POKE.ImageFadeGallery.Gallery.FADE_DOWN.length-1 - this.gallery.fadeFrame);
	  this.setOpacity(this.gallery.opacity);
	  this.gallery.fadeFrame++;
	  if (this.gallery.fadeFrame < POKE.ImageFadeGallery.Gallery.FADE_DOWN.length) {
	    var self = this;
	    this.timerID = window.setTimeout(function() {
	      self.fadeDown();
	    }, 10);
	  }
	  else {
	    this.gallery.fadeFrame = 0;
	    //this.gallery.obj.hide();
	    this.doFade();
	  }
	},

	calcOpacity: function(frameNumber) {
	  if (!this.fading)
	    this.gallery.opacity = POKE.ImageFadeGallery.Gallery.FADE_UP[frameNumber];
	  else
	    this.gallery.opacity = POKE.ImageFadeGallery.Gallery.FADE_DOWN[frameNumber];
	},

	setOpacity: function(opacity) {
	  var obj = this.gallery.obj.style;
	  opacity = (opacity == 100) ? 99.999 : opacity;
	  obj.filter = "alpha(opacity:"+opacity+")";
	  obj.KHTMLOpacity = opacity/100;
	  obj.MozOpacity = opacity/100;
	  obj.opacity = opacity/100;
	}
};