/*
 * Full Image
 * Full page image plugins
 *
 * © 2011 About Blank Design Office / Lionel Tardy
 */
(function( $ ){
	$.fn.fullImage = function( options ) {  

    	var settings = {
    		'script': 'fullImage.php',
    		'empty' : 'images/empty.png',
    		'file' 	: 'image.jpg',
			'width' : 2560,
			'height': 1440
    	};

	    return this.each(function() {
	    	var self = this;

    		if ( options ) { 
    			$.extend( settings, options );
    		}

    		$(this).css({
    			'position'	: 'absolute',
    			'top'		: 40,
    			'left'		: 387,
    			'width'		: $(window).width()-447,
    			'height'	: $(window).height()-80,
				'overflow'	: 'hidden',
				'z-index'	: 0    			
    		});

    		this.timeout   		= null;
    		this.ratio     		= settings.width / settings.height;
    		this.imageLow  		= $('<img class="fullImageLow"  src="'+settings.script+'?file='+settings.file+'&width=1024" alt="" />').css({
    			'position'	: 'absolute',
    			'top'		: 0,
    			'right'		: 0,
				'z-index'	: 1
    		});

    		this.imageHigh 		= $('<img class="fullImageHigh" src="'+settings.empty+'" alt="" />').css({
    			'position'	: 'absolute',
    			'top'		: 0,
    			'right'		: 0,
				'z-index'	: 2
    		});

			this.windowWidth	= null;
			this.windowHeight	= null;
			this.windowRatio	= null;

    		$(this).append(this.imageLow).append(this.imageHigh);

    		this.resize = function(isResizing) {

	    		$(self).css({
    				'width'		: $(window).width()-447,
    				'height'	: $(window).height()-80
	    		});

		    	$('#backgroundImage').css('height', '0px');

		    	this.windowWidth  = $(window).width()-447;
		    	this.windowHeight = $(window).height()-82;

				if (jQuery.browser.msie) this.windowHeight -= 4;
		    	this.windowRatio	 = this.windowWidth / this.windowHeight;

		    	if (this.windowRatio < this.ratio) {
		    		this.imageLow.css({'width': this.windowWidth, 'height': (this.windowWidth/this.ratio)});
		    		this.imageHigh.css({'width': this.windowWidth, 'height': (this.windowWidth/this.ratio)});
		    	} else {
		    		this.imageLow.css({'width': (this.windowHeight*this.ratio), 'height': (this.windowHeight)});
		    		this.imageHigh.css({'width': (this.windowHeight*this.ratio), 'height': (this.windowHeight)});
		    	}
		    	
		    	if (isResizing) this.imageHigh.hide();

		    	if (isResizing) {
			    	clearTimeout(this.timeout);
			    	this.timeout = setTimeout(function() { self.updatePicture();}, 500);
		    	} else {
		    		this.updatePicture();
		    	}
		    	
		    	$('#Title').css({
		    		left: $(self).width()+$(self).offset().left-372,
		    		top: $(self).height()+$(self).offset().top+5
		    	});
		    	$('#Navigation').css({
		    		left: $(self).width()+$(self).offset().left-62,
		    		top: $(self).height()+$(self).offset().top+5
		    	});
    		};
    		
    		this.updatePicture = function() {
    			this.imageHigh.show();
    			
				if (this.windowRatio > this.ratio) {
					this.imageHigh.attr('src', settings.script+'?file='+settings.file+'&width='+this.windowWidth);
				} else {
					this.imageHigh.attr('src', settings.script+'?file='+settings.file+"&height="+this.windowHeight);
    			}
    		};
    		
    		this.resize(false);
    		$(window).resize(function() { self.resize(true); });
  		});
	};
})( jQuery );
