// -----------------------------------------------------------------------------------
//
//	zortbox v2.04
//	by Jacek Partyka
//	Last Modification: 2010-01-27
// based on Lightbox
//
//	For more information, visit:
//	http://lokeshdhakar.com/projects/lightbox2/
//
//	Licensed under the Creative Commons Attribution 2.5 License - http://creativecommons.org/licenses/by/2.5/
//  	- Free for use in both personal and commercial projects
//		- Attribution requires leaving author name, author link, and the license info intact.
//	
//  Thanks: Scott Upton(uptonic.com), Peter-Paul Koch(quirksmode.com), and Thomas Fuchs(mir.aculo.us) for ideas, libs, and snippets.
//  		Artemy Tregubenko (arty.name) for cleanup and help in updating to latest ver of proto-aculous.
//
// -----------------------------------------------------------------------------------

//
//  Configurationl
//
zortboxOptions = Object.extend({
    fileLoadingImage:        'img/loading.gif',     
    filezbottomNavCloseImage: 'img/closelabel.gif',

    zortoverlayOpacity: 0.8,   // controls transparency of shadow zortoverlay

    animate: true,         // toggles resizing animations
    resizeSpeed: 7,        // controls the speed of the image resizing animations (1=slowest and 10=fastest)

    borderSize: 10,         //if you adjust the padding in the CSS, you will need to update this variable

	// When grouping images this is used to write: Image # of #.
	// Change it for non-english localization
	labelImage: "Zdjęcie",
	labelOf: "z"
}, window.zortboxOptions || {});

// -----------------------------------------------------------------------------------

var zortbox = Class.create();

zortbox.prototype = {
    imageArray: [],
    activeImage: undefined,
    
    // initialize()
    // Constructor runs on completion of the DOM loading. Calls updateImageList and then
    // the function inserts html at the bottom of the page which is used to display the shadow 
    // zortoverlay and the image container.
    //
    initialize: function() {   
 
        if (zortboxOptions.resizeSpeed > 10) zortboxOptions.resizeSpeed = 10;
        if (zortboxOptions.resizeSpeed < 1)  zortboxOptions.resizeSpeed = 1;

	    this.resizeDuration = zortboxOptions.animate ? ((11 - zortboxOptions.resizeSpeed) * 0.15) : 0;
	    this.zortoverlayDuration = zortboxOptions.animate ? 0.2 : 0;  // shadow fade in/out duration

        // When zortbox starts it will resize itself from 250 by 250 to the current image dimension.
        // If animations are turned off, it will be hidden as to prevent a flicker of a
        // white 250 by 250 box.
        var size = (zortboxOptions.animate ? 250 : 1) + 'px';
        

        // Code inserts html at the bottom of the page that looks similar to this:
        //
        //  <div id="zortoverlay"></div>
        //  <div id="zortbox">
        //      <div id="zortboxMessage">
        //              </div>
        //  </div>


        var objBody = $$('body')[0];

		objBody.appendChild(Builder.node('div',{id:'zortoverlay'}));
	
        objBody.appendChild(Builder.node('div',{id:'zortbox'}, [
             Builder.node('div',{id:'zortboxMessage'})
        ]));


		$('zortoverlay').hide().observe('click', (function() { this.end(); }).bind(this));
		$('zortbox').hide().observe('click', (function(event) { if (event.element().id == 'zortbox') this.end(); }).bind(this));
		
        var th = this;
        (function(){
            var ids = 
                'zortoverlay zortbox zortboxMessage';   
            $w(ids).each(function(id){ th[id] = $(id); });
        }).defer();
    },

 
    //
    //  start()
    //  Display zortoverlay and zortbox. If image is part of a set, add siblings to imageArray.
    //
    start: function(content, width, height) {    

        $$('select', 'object', 'embed').each(function(node){ node.style.visibility = 'hidden' });

        // stretch zortoverlay to fill page and fade in
        var arrayPageSize = this.getPageSize();
        $('zortoverlay').setStyle({ width: arrayPageSize[0] + 'px', height: arrayPageSize[1] + 'px' });

        new Effect.Appear('zortoverlay', { duration: this.zortoverlayDuration, from: 0.0, to: zortboxOptions.zortoverlayOpacity });

		  $('zortbox').setStyle({ width: width + 'px', height: height + 'px' });

        // calculate top and left offset for the zortbox 
        var arrayPageScroll = document.viewport.getScrollOffsets();
			
		  var Zbwidth = (arrayPageSize[0] - width) / 2;
		  var Zbheight = (document.viewport.getHeight() - height) / 2;
			
        var zortboxTop = Zbheight; //arrayPageScroll[1] + (document.viewport.getHeight() / 10);
        var zortboxLeft = Zbwidth; //arrayPageScroll[0];
        $('zortbox').setStyle({ top: zortboxTop + 'px', left: zortboxLeft + 'px' }).show();
		  $('zortboxMessage').setStyle({ width: width + 'px', height: height + 'px' });	
        $('zortboxMessage').innerHTML = content;
			
		  //this.resizezimageContainter(width, height);	
        //this.changeImage(imageNum);
    },


    //
    //  resizezimageContainter()
    //
    resizezimageContainter: function(imgWidth, imgHeight) {

        // get current width and height
        var widthCurrent  = $('zouterzimageContainter').getWidth();
        var heightCurrent = $('zouterzimageContainter').getHeight();

        // get new width and height
        var widthNew  = (imgWidth  + zortboxOptions.borderSize * 2);
        var heightNew = (imgHeight + zortboxOptions.borderSize * 2);

        // scalars based on change from old to new
        var xScale = (widthNew  / widthCurrent)  * 100;
        var yScale = (heightNew / heightCurrent) * 100;

        // calculate size difference between new and old image, and resize if necessary
        var wDiff = widthCurrent - widthNew;
        var hDiff = heightCurrent - heightNew;

        if (hDiff != 0) new Effect.Scale('zouterzimageContainter', yScale, {scaleX: false, duration: this.resizeDuration, queue: 'front'}); 
        if (wDiff != 0) new Effect.Scale('zouterzimageContainter', xScale, {scaleY: false, duration: this.resizeDuration, delay: this.resizeDuration}); 

        // if new and old image are same size and no scaling transition is necessary, 
        // do a quick pause to prevent image flicker.
        var timeout = 0;
        if ((hDiff == 0) && (wDiff == 0)){
            timeout = 100;
            if (Prototype.Browser.IE) timeout = 250;   
        }

        (function(){
            $('zouterzimageContainter').setStyle({ width: widthNew + 'px' });

            this.showImage();
        }).bind(this).delay(timeout / 1000);
    },
    
    //
    //  showImage()
    //  Display image and begin preloading neighbors.
    //
    showImage: function(){
        $('zouterzimageContainter').hide();
        new Effect.Appear('zortboxMessage', { 
            duration: this.resizeDuration, 
            queue: 'end'
        });
        
    },

    //
    //  updateDetails()
    //  Display zcaption, image number, and bottom nav.
    //
    updateDetails: function() {
    

        new Effect.Parallel(
            [ 
                new Effect.SlideDown('zzimageDataContainer', { sync: true, duration: this.resizeDuration, from: 0.0, to: 1.0 }), 
                new Effect.Appear('zzimageDataContainer', { sync: true, duration: this.resizeDuration }) 
            ], 
            { 
                duration: this.resizeDuration, 
                afterFinish: (function() {
	                // update zortoverlay size and update nav
	                var arrayPageSize = this.getPageSize();
	                $('zortoverlay').setStyle({ height: arrayPageSize[1] + 'px' });
	                
                }).bind(this)
            } 
        );
    },

    //
    //  end()
    //
    end: function() {
        $('zortbox').hide();
        new Effect.Fade('zortoverlay', { duration: this.zortoverlayDuration });
        $$('select', 'object', 'embed').each(function(node){ node.style.visibility = 'visible' });
    },

    //
    //  getPageSize()
    //
    getPageSize: function() {
	        
	     var xScroll, yScroll;
		
		if (window.innerHeight && window.scrollMaxY) {	
			xScroll = window.innerWidth + window.scrollMaxX;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
		
		var windowWidth, windowHeight;
		
		if (self.innerHeight) {	// all except Explorer
			if(document.documentElement.clientWidth){
				windowWidth = document.documentElement.clientWidth; 
			} else {
				windowWidth = self.innerWidth;
			}
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}	
		
		// for small pages with total height less then height of the viewport
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else { 
			pageHeight = yScroll;
		}
	
		// for small pages with total width less then width of the viewport
		if(xScroll < windowWidth){	
			pageWidth = xScroll;		
		} else {
			pageWidth = windowWidth;
		}

		return [pageWidth,pageHeight];
	}
}
var zortbo;
document.observe('dom:loaded', function () { zortbo = new zortbox(); });