/*
---
name: XtLightbox.Adaptor.Image

description: extendable lightbox Image Adaptor class

license: MIT-style

authors:
- Anton Suprun <kpobococ@gmail.com>

requires:
- XtLightbox.Adaptor

provides: XtLightbox.Adaptor.Image

...
*/
XtLightbox.Adaptor.Image = new Class(
{
    Extends: XtLightbox.Adaptor,

    $name: 'Image',

    options: {
        extensions: ['jpg', 'png', 'gif'],
        lightboxCompat: true
    },

    initialize: function(options)
    {
        this.parent(options);
        var e = this.options.extensions || [];
        if (e.contains('jpg') && !e.contains('jpeg')) e.push('jpeg');
    },

    check: function(element)
    {
        return this.options.lightboxCompat ? this.parent(element) : element.href.test('\\.(?:' + this.options.extensions.join('|') + ')$', 'i');
    },

    getContent: function(element)
    {
        if (!XtLightbox.Adaptor.cached(element)) throw new Error('Element content must be loaded first');
        return XtLightbox.Adaptor.load(element);
    },

    getSize: function(element)
    {
        if (!XtLightbox.Adaptor.cached(element)) throw new Error('Element content must be loaded first');
        var img = XtLightbox.Adaptor.load(element);
		
			/*if( img.naturalWidth >= (window.getSize().x*0.9) )
			{
				r = (window.getSize().x*0.9)/img.naturalWidth;
				
				return {
					x: img.naturalWidth*r,
					y: img.naturalHeight*r
				}
			} else {
			}*/
				return {
					x: img.naturalWidth,
					y: img.naturalHeight
				}
    },

    load: function(element, callback)
    {
        callback = callback || function() {}
        if (XtLightbox.Adaptor.cached(element)) {
            callback(element);
            return this;
        }
        new Element('img').addEvent('load', function() {
			
            if (!this.naturalWidth) this.naturalWidth = this.width;
            if (!this.naturalHeight) this.naturalHeight = this.height;
			
				/*									 
			if( img.naturalWidth >= (window.getSize().x*0.9) )
			{
				r = (window.getSize().x*0.9)/img.naturalWidth;
					this.width *=r,
					this.height *=r
			}*/
			
			
            XtLightbox.Adaptor.cache(element, this);
            callback(element);
        }).set({
            src: element.href,
            alt: ''
        });
        return this;
    }
});

