/********************************************
 * Name: Lightbox.js
 * Author: Rob Griffiths
 * Email: r.griffiths@bigroominternet.co.uk
 *******************************************/

var WindowObjectReference = null;

YAHOO.namespace('enlarge');
YAHOO.enlarge = function() {
    $E = YAHOO.util.Event;
    $D = YAHOO.util.Dom;
    $ = $D.get;
    
    var $L = new YAHOO.widget.Panel("lightbox",
                    { 
                        modal: true,
                        fixedcenter: true,
                        underlay: "shadow", 
                        draggable: false, 
                        constraintoviewport:true, 
                        close: false, 
                        zindex: 200,
                        effect: {effect:YAHOO.widget.ContainerEffect.FADE, duration:0.5 }
                    } 
                );
    
    return {
        lightbox : $L,
        currentId : 0,
    
        init : function() {
            var elements = $D.getElementsByClassName('enlarge');
            this.lightboxImages = new Array();
            
            for(i=0;i<elements.length;i++) {
                var key = $D.generateId(elements[i],'image-');
                
                var id = (elements[i].className.match(/id-([0-9]*)/))[1];
                
                this.lightboxImages[key] = new Image;
                this.lightboxImages[key].src = elements[i];
                this.lightboxImages[key].content_id = id;
                
                $E.on(elements[i], 'click', this.show);
            }
        },

        show : function(e) {
            $E = YAHOO.util.Event;
            $D = YAHOO.util.Dom;
            $ = $D.get;
        
            
            $E.stopEvent(e); //stop the link's true location
            var currentImage = YAHOO.enlarge.lightboxImages[this.id];
            YAHOO.enlarge.currentId = this.id;
            
            var viewportHeight = YAHOO.util.Dom.getViewportHeight();
            var viewportWidth = YAHOO.util.Dom.getViewportWidth();
            
            if(((viewportHeight/100) * 80) < currentImage.height) {
                var ratio = (viewportHeight/100) * 80 / currentImage.height;
                currentImage.height = currentImage.height * ratio;
                currentImage.width = currentImage.width * ratio;
            }
            
            if(((viewportWidth/100) * 80) < currentImage.width) {
                var ratio = (viewportWidth/100) * 80 / currentImage.width;
                currentImage.height = currentImage.height * ratio;
                currentImage.width = currentImage.width * ratio;
            }
            
            YAHOO.enlarge.lightbox.setBody('<img src="'+currentImage.src+'" width="'+currentImage.width+'" height="'+currentImage.height+'" /><a id="print-lightbox" href="#">print</a><a id="hide-lightbox" href="#">close</a>');          
            YAHOO.enlarge.lightbox.render(document.body);
            YAHOO.enlarge.lightbox.show();                
                
            $E.on('hide-lightbox', 'click', YAHOO.enlarge.hideLightbox, YAHOO.enlarge, true);
            $E.on('print-lightbox', 'click', YAHOO.enlarge.printLightbox, YAHOO.enlarge, true);
        },
        
        hideLightbox : function(e) {
            $E.stopEvent(e);
            this.lightbox.hide();
        },
        
        printLightbox : function(e) {
            $E.stopEvent(e);
            
            var currentImage = YAHOO.enlarge.lightboxImages[YAHOO.enlarge.currentId];
            
            if (WindowObjectReference == null || WindowObjectReference.closed) {
	            WindowObjectReference = window.open();
			} else {
                WindowObjectReference.focus();
			};
			
            WindowObjectReference.location.href = window.baseUrl + "product.popup/content_id/"+currentImage.content_id;
            //WindowObjectReference.document.write('<img src="'+currentImage.src+'" width="'+currentImage.width+'" height="'+currentImage.height+'" /><p>Internet Explorer 7 users need to press Control+p to print.</p>');
            WindowObjectReference.print();
            //WindowObjectReference.close();
        }
    };
}();

YAHOO.util.Event.onDOMReady(YAHOO.enlarge.init, {}, YAHOO.enlarge); 
//YAHOO.util.Event.on(window, 'load', YAHOO.enlarge.init, YAHOO.enlarge, true);