// -----------------------------------------------------------------------------------
//
//	Textbox v2.04
//	by Lokesh Dhakar - http://www.lokeshdhakar.com
//	Last Modification: 2/9/08
//
//	For more information, visit:
//	http://lokeshdhakar.com/projects/Textbox2/
//
//	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.
//
// -----------------------------------------------------------------------------------
/*

    Table of Contents
    -----------------
    Configuration

    Textbox Class Declaration
    - initialize()
    - updateImageList()
    - start()
    - changeImage()
    - resizeImageContainer()
    - showImage()
    - updateDetails()
    - updateNav()
    - enableKeyboardNav()
    - disableKeyboardNav()
    - keyboardAction()
    - preloadNeighborImages()
    - end()
    
    Function Calls
    - document.observe()
   
*/
// -----------------------------------------------------------------------------------

//
//  Configurationl
//
TextboxOptions = Object.extend({
    
    overlayOpacity: 0.5,   // controls transparency of shadow overlay

    animate:  true,         // toggles resizing animations
    resizeSpeed: 9,        // 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

}, window.TextboxOptions || {});

// -----------------------------------------------------------------------------------

var Textbox = Class.create();

Textbox.prototype = {
    
    // 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 
    // overlay and the image container.
    //
    initialize: function() {    
       	   	
        this.updateImageList();
                
        if (TextboxOptions.resizeSpeed > 10) TextboxOptions.resizeSpeed = 10;
        if (TextboxOptions.resizeSpeed < 1)  TextboxOptions.resizeSpeed = 1;

	    this.resizeDuration = TextboxOptions.animate ? ((11 - TextboxOptions.resizeSpeed) * 0.15) : 0;
	    this.overlayDuration = TextboxOptions.animate ? 0.2 : 0;  // shadow fade in/out duration

        // When Textbox 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 = (TextboxOptions.animate ? 250 : 1) + 'px';
        var objBody = $$('body')[0];

		objBody.appendChild(Builder.node('div',{id:'overlay2'}));
	
		var myHTML = Builder.node('div',{id:'Textbox'},[
						Builder.node('div',{id:'textboxContainer'},[
							Builder.node('div',{id:'textboxContent'})
						])
					]);
	
        objBody.appendChild(myHTML);

		


		$('overlay2').hide().observe('click', (function() { this.end(); }).bind(this));
		$('Textbox').hide().observe('click', (function(event) { if (event.element().id == 'Textbox') this.end(); }).bind(this));
		
        var th = this;
        (function(){
            var ids = 
                'overlay2 Textbox outerImageContainer imageContainer TextboxImage hoverNav prevLink nextLink loading loadingLink ' + 
                'imageDataContainer imageData imageDetails caption numberDisplay bottomNav bottomNavClose';   
            $w(ids).each(function(id){ th[id] = $(id); });
        }).defer();
    },

    //
    // updateImageList()
    // Loops through anchor tags looking for 'Textbox' references and applies onclick
    // events to appropriate links. You can rerun after dynamically adding images w/ajax.
    //
    updateImageList: function() {   
       
		this.updateImageList = Prototype.emptyFunction;

        document.observe('click', (function(event){
            var target = event.findElement('a[rel^=Textbox]') || event.findElement('area[rel^=Textbox]');
            if (target) {
                event.stop();
                this.start(target);
            }
        }).bind(this));
    },
    
    //
    //  start()
    //  Display overlay2 and Textbox. If image is part of a set, add siblings to imageArray.
    //
    start: function(imageLink) {    
		
		//alert(imageLink.firstChild.tagName);
		//var myContent = imageLink.select("[class='textboxcontent']");
		
		var myContent = imageLink.childElements()[1];
		
        $$('select', 'object', 'embed').each(function(node){ node.style.visibility = 'hidden' });

        // stretch overlay2 to fill page and fade in
        var arrayPageSize = this.getPageSize();
				
        $('overlay2').setStyle({ width: arrayPageSize[0] + 'px', height: arrayPageSize[1] + 'px' });

        new Effect.Appear(this.overlay2, { duration: this.overlayDuration, from: 0.0, to: TextboxOptions.overlayOpacity });

       	this.content = myContent.innerHTML;  
		
        // calculate top and left offset for the Textbox 
        var arrayPageScroll = document.viewport.getScrollOffsets();
        var TextboxTop = arrayPageScroll[1] + (document.viewport.getHeight() / 10);
        var TextboxLeft = arrayPageScroll[0];
        $('Textbox').setStyle({ top: TextboxTop + 'px', left: TextboxLeft + 'px' }).show();
        				
      	this.showImage();
    },

    //
    //  showImage()
    //  Display image and begin preloading neighbors.
    //
    showImage: function(){
        this.loading.hide();
        this.updateDetails(); 
	},

    //
    //  updateDetails()
    //  Display caption, image number, and bottom nav.
    //
    updateDetails: function() {
    
        // if caption is not null
        $('textboxContent').update(this.content).show();
                
       /* new Effect.Parallel(
            [ 
                new Effect.SlideDown(this.imageDataContainer, { sync: true, duration: this.resizeDuration, from: 0.0, to: 1.0 }), 
                new Effect.Appear(this.imageDataContainer, { sync: true, duration: this.resizeDuration }) 
            ], 
            { 
                duration: this.resizeDuration, 
                afterFinish: (function() {
	                // update overlay size and update nav*/
	                var arrayPageSize = this.getPageSize();
	                this.overlay2.setStyle({ height: arrayPageSize[1] + 'px' });
               /* }).bind(this)
            } 
        );*/
    },
 	
	//
    //  end()
    //
    end: function() {
        $('textboxContent').hide();
        new Effect.Fade(this.overlay2, { duration: this.overlayDuration });
        $$('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];
	}
}

document.observe('dom:loaded', function () { new Textbox(); });