/** COPYRIGHT 2011 ADMX V.O.F., BAARLE-NASSAU, SCHIJF, THE NETHERLANDS  */
PhotoStack = Class.create({data: [],filesArray: [],preCssClass: null,inMotion: false,pathFiles: [],enableText : false,initialize: function (){},init: function (preCssClass, id, data){if (data != null){var triggerItem = $(id);Event.observe(triggerItem, 'click', this.eShow.bind(this, this.filesArray.length -1));this.filesArray = this.filesArray.concat(data.filesArray);this.data = data;this.pathFiles = this.data.filesResized;}},eShow: function (activeId){this.activeId = activeId;if (this.inMotion) {this.renderThumbs();return;} else {this.inMotion = true;}if (this.frameIsSet == true){}else{this.parseFrameSet();this.renderThumbs();}this.eReposition();this.ePlay();Event.observe(window, 'resize', this.eReposition.bind(this));},renderThumbs: function (){while (this.loadedThumbsArray.length > 0){var obj = this.loadedThumbsArray.pop();Dom.un(obj.item);}for(var i = 0 ; i < this.filesArray.length; i++){var obj = this.filesArray[i];obj.id = i;var item = null;if ((obj.stackTitle) || (obj.stackText)){var item = Dom.div(this.itemThumbStripScroll, {'class':'stackThumbstackTitle', 'style':'float: left; cursor: pointer;'});item.style.height = this.data.thumbHeight + 'px';item.innerHTML = obj.stackTitle;obj.item = item;this.eThumbLoaded(item, obj);this.loadedThumbsArray.push(obj);}else{if (obj.thumb){var image = new Image();image.src = this.pathFiles + obj.thumb;var item = Dom.div(this.itemThumbStripScroll, {'class':'stackThumb', 'style':'float: left; cursor: pointer;'});item.appendChild(image);obj.item = item;if (image.complete){this.eThumbLoaded(image, obj);}else{Event.observe(image, 'load', this.eThumbLoaded.bind(this, image, obj));}this.loadedThumbsArray.push(obj);}}if (item){Event.observe(item, 'click', this.eLoad.bind(this, obj));}}},parseFrameSet : function (){var viewport = document.viewport.getDimensions();this.itemBackground = Dom.div(document.body, {'style':'position: fixed; z-index: 1000; cursor: pointer; ', 'class':'zoomImageBackground'});this.tweenBT = new Tween(this.itemBackground.style,'top',Tween.regularEaseOut,viewport.height/2, 0, 0.8,'px').start();this.tweenBB = new Tween(this.itemBackground.style,'bottom',Tween.regularEaseOut,viewport.height/2, 0, 0.8,'px').start();this.tweenBL = new Tween(this.itemBackground.style,'right',Tween.regularEaseOut,viewport.width/2, 0, 0.8,'px').start();this.tweenBR = new Tween(this.itemBackground.style,'left',Tween.regularEaseOut,viewport.width/2, 0, 0.8,'px').start();this.tweenBA = new OpacityTween(this.itemBackground, Tween.regularEaseOut,0, 80, 0.8).start();Event.observe(this.itemBackground, 'click', this.eHide.bind(this));this.itemCenter = Dom.div(document.body, {'style':'position: fixed; z-index: 1001; top: 50%; left: 50%; ', 'class':'zoomImageBackground'});this.tweenCA = new OpacityTween(this.itemCenter, Tween.regularEaseOut,0, 100, 0.8).start();this.tweenCenterHeight = new Tween(this.itemCenter.style,'top',Tween.regularEaseOut,viewport.height/2, 0, 0.8,'px').start();this.itemBox = Dom.div(this.itemCenter, {'class':'stackImageContainer', 'style':' width: 200px; height: 200px; position: absolute; top: -100px; left: -100px;'});this.tl = Dom.div(this.itemBox, {'class':'TL'});this.tl = Dom.div(this.itemBox, {'class':'T'});this.tl = Dom.div(this.itemBox, {'class':'TR'});this.tl = Dom.div(this.itemBox, {'class':'ML'});this.tl = Dom.div(this.itemBox, {'class':'MR'});this.tl = Dom.div(this.itemBox, {'class':'BL'});this.tl = Dom.div(this.itemBox, {'class':'B'});this.tl = Dom.div(this.itemBox, {'class':'BR'});this.tweenBoxWidth = new Tween(this.itemBox.style,'width',Tween.regularEaseOut,0, 0, 0.8,'px').start();this.tweenBoxWidth.onMotionChanged = function (){this.obj['left'] = -Math.round(this._pos * 0.5) + 'px';};this.tweenBoxHeight = new Tween(this.itemBox.style,'height',Tween.regularEaseOut,0, 0, 0.8,'px').start();this.tweenBoxHeight.onMotionChanged = function (){this.obj['top'] = -Math.round(this._pos/2) + 'px';};this.textBox = Dom.div(this.itemBox, {'class':'stackSlideCaptionBox', 'style':' position: absolute; bottom: 0px; z-index: 10; '});this.itemImageTitle = Dom.div(this.textBox, {'class':'stackSlideCaptionTitle', 'style':''});this.itemImageDescription = Dom.div(this.textBox, {'class':'stackSlideCaptionBody', 'style':' '});this.buttonClose = Dom.div(this.itemBox, {'class':'zoomImageClose', 'style':'cursor: pointer;'});Event.observe(this.buttonClose, 'click', this.eHide.bind(this));this.itemThumbStrip = Dom.div(document.body, {'class':'stackThumbStrip', 'style':'position: fixed; z-index: 1002; bottom: 0px; left: 0px; right: 0px; overflow: hidden; '});this.tweenTH = new Tween(this.itemThumbStrip.style,'height',Tween.regularEaseOut,0, this.data.thumbHeight, 0.8,'px').start();this.itemActiveImage = Dom.div(this.itemThumbStrip, {'class':'stackThumbActiveBoundry', 'style':'position: absolute; z-index: 1000;  top: 0px; left: 0px; width: 10px; height: 10px;  '});this.itemActiveImage.style.height = this.data.thumbHeight + 'px';this.tweenActiveLeft = new Tween(this.itemActiveImage.style,'left',Tween.regularEaseOut,0, 0, 0.8,'px').start();this.tweenActiveWidth = new Tween(this.itemActiveImage.style,'width',Tween.regularEaseOut,0, 0, 0.8,'px').start();this.itemControls = Dom.div(document.body, {'class':'stackControls','style':'position: fixed; z-index: 1002; '});this.tweenControlsAlpha = new OpacityTween(this.itemControls, Tween.regularEaseOut,0, 80, 0.8).start();this.buttonPrev = Dom.div(this.itemControls, {'class':'stackButtonPreviuos', 'style':'cursor: pointer;'}, '<<');Event.observe(this.buttonPrev, 'click', this.ePrev.bind(this));this.buttonPlay = Dom.div(this.itemControls, {'class':'stackButtonStop', 'style':'cursor: pointer;'}, '||');Event.observe(this.buttonPlay, 'click', this.ePlay.bind(this));this.buttonNext = Dom.div(this.itemControls, {'class':'stackButtonNext', 'style':'cursor: pointer;'}, '>>');Event.observe(this.buttonNext, 'click', this.eNext.bind(this));var self = this;this.tweenTH.onMotionChanged = function (){self.itemControls.style.bottom = Math.round(this._pos) + 'px';};this.itemThumbStripScroll = Dom.div(this.itemThumbStrip, {'style':'position: absolute; left: 0px;'});this.tweenScroll = new Tween(this.itemThumbStripScroll.style,'left',Tween.regularEaseOut,0, 0, 0.8,'px').start();this.frameIsSet = true;},isPlaying: false,playTimer: null,activeId : -1,ePlay : function (){if (this.isPlaying == false){this.isPlaying = true;this._setPlay();this.eNext();this.buttonPlay.innerHTML = ">";}else{this.isPlaying = false;this._setPlay();this.buttonPlay.innerHTML = "||";}},_setPlay : function (){if (this.playTimer != null){clearTimeout(this.playTimer);this.playTimer = null;}if (this.isPlaying == true){this.playTimer = setTimeout(this.eNext.bind(this), 4000);}},ePrev : function (){this.activeId -= 1;if (this.activeId < 0){this.activeId = this.loadedThumbsArray.length -1;}if (this.filesArray[this.activeId]){this.eLoad(this.filesArray[this.activeId]);}else{}},eNext : function (){this.activeId += 1;if (this.activeId > this.loadedThumbsArray.length -1){this.activeId = 0;}if (this.filesArray[this.activeId]){this.eLoad(this.filesArray[this.activeId]);}else{}},thumbsTotalWidth: 0,eThumbLoaded : function (image, obj){var item = obj.item;var w = Element.getWidth(item);this.thumbsTotalWidth += Number((w)?(w):(0));this.itemThumbStripScroll.style.width = this.thumbsTotalWidth + 'px';},eReposition : function (){var viewport = document.viewport.getDimensions();var viewHeight = viewport.height - this.data.thumbHeight;this.tweenCenterHeight.continueTo(Math.round(viewHeight * .5), 0.8);},eCenterThumbs : function (obj){var off = Dom.findPos(this.itemThumbStripScroll).left;left = Dom.findPos(obj.item).left - off;var viewport = document.viewport.getDimensions();var max = Element.getWidth(this.itemThumbStrip);var center = (max /2);var imageWidth = Element.getWidth(obj.item);var imageCenter = imageWidth * .5;var pos = 0;var actLeft = 0;switch(true){case (left > center):pos = (left + imageCenter) - center;actLeft = left - pos;break;default:pos = 0;actLeft = left;}var actOff = parseFloat(Element.getStyle(this.itemActiveImage, 'borderLeftWidth'));this.tweenActiveWidth.continueTo(imageWidth, 0.8);this.tweenActiveLeft.continueTo(actLeft - actOff, 0.8);this.tweenScroll.continueTo(-pos, 0.8);},loadedImagesArray: [],loadedThumbsArray: [],_eUnItems: function (){for (var i = 0; i < this.loadedImagesArray.length; i++){var item = this.loadedImagesArray.pop();if (item.tweenA){item.tweenA.continueTo(0, 0.8);item.tweenA.onMotionFinished = function (){this.stop();Dom.un(item);};}}},eLoad: function (obj){this._setPlay();this.activeId = obj.id;this._eUnItems();this.eCenterThumbs(obj);switch(true){case ((obj.stackTitle != undefined) || (obj.stackText != undefined)):var item = Dom.div(null, {'class':'stackTextSlideBox', 'style':'position: absolute; '});var itemTitle = Dom.div(item, {'class':'stackTextSlideTitle', 'style':''});itemTitle.innerHTML = obj.stackTitle;var itemText = Dom.div(item, {'class':'stackTextSlideBody', 'style':''});itemText.innerHTML = obj.stackText;this.loadedImagesArray.push(item);Element.setOpacity(item, 0);this.eImageLoaded(item, obj);break;case ((obj.slide != undefined)&&(obj.slide != 0)):var image = new Image();image.src = this.pathFiles + obj.slide;image.className = 'stackImage';image.style.position = 'absolute';Element.setOpacity(image, 0);this.loadedImagesArray.push(image);if (image.complete){this.eImageLoaded(image, obj);}else{Event.observe(image, 'load', this.eImageLoaded.bind(this, image, obj));}}},eImageLoaded: function(item, obj){item.tweenA = new OpacityTween(item, Tween.regularEaseOut,0, 100, 0.8).start();var w = (item.width > 0)?(item.width):this.data.slideWidth;this.tweenBoxWidth.continueTo(w, 0.8); var h = (item.height > 0)?(item.height):this.data.slideHeight;this.tweenBoxHeight.continueTo(h, 0.8);this.itemBox.appendChild(item);this.itemImageTitle.innerHTML = (obj.title)?obj.title:'';this.itemImageDescription.innerHTML = (obj.description)?obj.description:'';},eHide : function (){if (this.isPlaying == true){this.ePlay();}var viewport = document.viewport.getDimensions();this.tweenBT.continueTo(viewport.height/2, 0.8);this.tweenBB.continueTo(viewport.height/2, 0.8);this.tweenBL.continueTo(viewport.width/2, 0.8);this.tweenBR.continueTo(viewport.width/2, 0.8);this.tweenBA.continueTo(0, 0.8);this.tweenCA.continueTo(0, 0.8);this.tweenBA.onMotionFinished = this.eUnload.bind(this);this.tweenControlsAlpha.continueTo(0, 0.5);this.tweenTH.continueTo(0, 0.8);this.tweenBoxWidth.continueTo(0, 0.8);this.tweenBoxHeight.continueTo(0, 0.8);this.tweenCenterHeight.continueTo(viewport.height/2, 0.8);},eUnload: function (){this.isPlaying = false;this.playTimer = Dom.timer(this.playTimer, null, null);this.frameIsSet = false;this.inMotion = false;this._eUnItems();this.tweenBT.stop();this.tweenBB.stop();this.tweenBL.stop();this.tweenBR.stop();this.tweenBA.stop();this.tweenCA.stop();this.tweenControlsAlpha.stop();this.tweenTH.stop();this.tweenBoxWidth.stop();this.tweenBoxHeight.stop();this.tweenCenterHeight.stop();Dom.un(this.itemBackground);Dom.un(this.itemControls);Dom.un(this.itemBox);Dom.un(this.itemThumbStrip);for(var i = 0; i < this.loadedThumbsArray.length; i++){Dom.un(this.loadedThumbsArray[i]);}}});

