// JavaScript Document

//larghezza colonna destra
var colw = 200;
//opacità sfondo
var overlay_opacity = 0.8;

var iehack = 0;
if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){
var iehack = 0;
}

function mylightbox() {

	var links = $('a[rel^=lightbox]');
    var overlay = $( jQuery( '<div id="overlay" style="display:none"></div>' ) );
    var container = $( jQuery( '<div id="lightbox" style="display:none"></div>' ) );
	var right = $( jQuery( '<div class="right" id="right" style="width:'+(colw-16-iehack)+'px;"></div>' ) );
    var target = $( jQuery( '<div class="target"></div>' ) );
	var nav = $( jQuery( '<div class="nav" style="width:'+(colw-16-iehack)+'px;"></div>' ) );
	var text = $( jQuery( '<div class="text"></div>' ) );
	var images = $( jQuery( '<div class="images"></div>' ) );
    var close = $( jQuery( '<div class="closediv"><a href="#close" class="close"><img src="http://www.travelrevolution.com/images/lightbox_closelabel.gif" alt="Close" /></a></div>' ) );
    var prev = $( jQuery( '<div class="prevdiv"><a href="#prev" class="prev"><img src="http://www.travelrevolution.com/images/lightbox_prevlabel.gif" alt="Prev" /></a></div>' ) );
    var next = $( jQuery( '<div class="nextdiv"><a href="#next" class="next"><img src="http://www.travelrevolution.com/images/lightbox_nextlabel.gif" alt="Next" /></a></div>' ) );
	
	$( 'body' ).append( overlay ).append( container );
	container.append( right ).append( target );
	right.append( images ).append( close ).append( text ).append( nav );
	nav.append( prev ).append( next );
	container.show().css({
	'top': Math.round(($( window ).height() - container.outerHeight() ) / 2 ) + 'px', 
	'left': Math.round( ($( window ).width() - container.outerWidth() ) / 2 ) + 'px', 
	'margin-top': 0, 'margin-left': 0
	}
	).hide();
	right.children().hide();
	
	close.click(function(c) {
    c.preventDefault();
    overlay.add( container ).fadeOut( 'normal' );
	});
	overlay.click(function(c) {
    c.preventDefault();
    overlay.add( container ).fadeOut( 'normal' );
	});
	
	next.click(function(c) {
    c.preventDefault();
    var current = parseInt(links.filter( '.selected' ).attr( 'lb-position' ),10 );
    var to = links.eq( current + 1 );
    if( !to.size() ) {
        to = links.eq( 0 );
    }
    if( to.size() ) {
        to.click();
    }
	});
	
	prev.click(function(c) {
    c.preventDefault();
    var current = parseInt(links.filter( '.selected' ).attr( 'lb-position' ),10 );
    var to = links.eq( current - 1 );
    if( !to.size() ) {
        to = links.eq( links.size() - 1 );
    }
    if( to.size() ) {
        to.click();
    }
	});
	
	target.click(function(c) {
    c.preventDefault();
    var current = parseInt(links.filter( '.selected' ).attr( 'lb-position' ),10 );
    var to = links.eq( current + 1 );
    if( !to.size() ) {
        to = links.eq( 0 );
    }
    if( to.size() ) {
        to.click();
    }
	});

	links.each(function( index ) {
    var link = $(this);
	link.click( function( c ) {
        c.preventDefault();
        open( link.attr( 'href' ) );
        links.filter( '.selected' ).removeClass( 'selected' );
        link.addClass( 'selected' );
		
		text.html(link.attr('title'));
		images.html((index+1)+' of '+links.length);
		
    });
    link.attr({'lb-position': index});
	});
	
	var open = function( url ) {
    if( container.is( ':visible' ) ) {
        target.children().fadeOut( 'normal', function() {
        target.children().remove();
        loadImage( url );
        });
		right.children().fadeOut( 'normal' );

    } else {
        target.children().remove();
		overlay.fadeTo('normal',overlay_opacity);
        container.fadeIn( 'normal',function(){
        loadImage( url );
    });
    }
	}


	var loadImage = function( url ) {
	
    	if( container.is( '.loading' ) ) { return; }
    	container.addClass( 'loading' );
   		var img = new Image();
   		img.onload = function() {
        	img.style.display = 'none';
        	var maxWidth = ( $( window ).width() - parseInt( container.css( 'padding-left' ),16 ) - parseInt( container.css( 'padding-right' ), 16 ) )  - 10 ;
        	var maxHeight = ( $( window ).height() - parseInt( container.css( 'padding-top' ),16 ) - parseInt( container.css( 'padding-bottom' ), 16) ) - 100 ;
        	if( img.width > maxWidth || img.height > maxHeight ) { // One of these is larger than the window
            	var ratio = img.width / img.height;
            	if(img.height >= maxHeight) {
                	img.height = maxHeight;
                	img.width = maxHeight * ratio;
            	} else {
                	img.width = maxWidth;
                	img.height = maxWidth * ratio;
            	}
        	}
        	container.animate( {'width': img.width+colw,'height': img.height+(iehack), 'top': Math.round( ($( window ).height() - img.height - parseInt( container.css( 'padding-top' ),16 ) - parseInt( container.css( 'padding-bottom' ),16 ) ) / 2 ) + 'px', 'left': Math.round( ($( window ).width() - img.width - parseInt( container.css( 'padding-left' ),16 ) - parseInt( container.css( 'padding-right' ),16 ) ) / 2 ) - parseInt(colw/2) + 'px'},'normal', function(){
            target.append( img );
			right.children().fadeIn('normal');
            $( img ).fadeIn( 'normal', function() {
                container.removeClass( 'loading' );
            } );
        } )
    }
    img.src = url;
}

}

$(document).ready(function(){

mylightbox();

});
