// JavaScript Document
// KWOKBOX VERSION 1.0 beta max
(function($){
	$.fn.fullSizeImage = function(options) {
		// this is to keep from overriding our "defaults" object.
		var opts = $.extend({}, $.fn.fullSizeImage.defaults, options);
		var base = this;
		var baseImg = this.find('img');
		var origHeight = baseImg.height();
		var origWidth = baseImg.width();
		
        base.init = function(){
	        base.resizeImage();
	        $(window).bind('resize',function(){base.resizeImage()});
	        if(opts.offset!=null) {
	        	if ($.browser.msie && $.browser.version.substr(0,1)<7) {
					base.css({
						'position':'absolute',
						'left':opts.offset
					});
					$(window).bind('scroll',function(){base.scrollPage()});
				} else {
					base.css('position','fixed');
				}
	        }
		};

        base.scrollPage = function(){
	        base.css({
				'top':$(window).scrollTop()
			});
		};	
		
        base.repositionImage = function(){
	        if(opts.offset!=null) {
		        if(opts.position!='fixed') {
		        	baseImg.css({
			        	'left':(($(window).width() - opts.offset) - baseImg.width()) / 2,
			        	'top':($(document).height() - baseImg.height()) / 2
			        });
			    } else {
		        	baseImg.css({
			        	'left':(($(window).width() - opts.offset) - baseImg.width()) / 2,
			        	'top':($(window).height() - baseImg.height()) / 2
			        });
			    }
	        } else {
		        if(opts.position!='fixed') {
		        	baseImg.css({
			        	'left':($(window).width() - baseImg.width()) / 2,
			        	'top':($(document).height() - baseImg.height()) / 2
			        });
			    } else {
		        	baseImg.css({
			        	'left':($(window).width() - baseImg.width()) / 2,
			        	'top':($(window).height() - baseImg.height()) / 2
			        });
			    }
	        }
		};	
		
        base.resizeImage = function(){
        	imageRatio = baseImg.width() / baseImg.height();
        	
        	if( baseImg.width() > $(window).width() ||  baseImg.width() < $(window).width() ){
		        if(opts.offset!=null) {
		        	baseImg.css({
			        	'width': ($(window).width()-opts.offset),
			        	'height': ($(window).width()-opts.offset) / imageRatio
			        });
			    } else {
		        	baseImg.css({
			        	'width': $(window).width(),
			        	'height': $(window).width() / imageRatio
			        });
			    }
        	}
        	
        	if( baseImg.height() < $(document).height() ) {
		        if(opts.position!='fixed') {
		        	baseImg.css({
			        	'width': $(document).height() * imageRatio,
			        	'height': $(document).height()
			        });
			    } else {
		        	if( $(window).width()-opts.offset <  $(window).height() * imageRatio ){
			        	baseImg.css({
				        	'width': $(window).height() * imageRatio,
				        	'height': $(window).height()
				        });		
		        	}	    	
			    }
        	}

	        if(opts.offset!=null) {
	        	base.css({
	        		'left':opts.offset,
	        		'width':($(window).width()-opts.offset),
	        		'height':$(window).height()
	        	})
	        } else {
	        	base.css({
	        		'width':$(window).width(),
	        		'height':$(window).height()
	        	})
	        }

	        if(opts.repositionImage==true) {
	        	base.repositionImage();
	        	$(window).bind('resize',function(){base.repositionImage()});
	        }

	        if(opts.overlayLine==true) {
	        	if(baseImg.width() > origWidth && $('#overlay').size() < 1) {
	        		$('#full-screen-image').append('<div id="overlay">&nbsp;</div>');
	        	}

	        	if(baseImg.width() < origWidth) {
	        		$('#overlay').remove();
	        	}

        		$('#overlay').css({
        			'width':base.width(),
        			'height':base.height()
        		})
	        }
		};		

        base.refeshPlugin = function(){
        	alert('');
        }

		base.init();
	};
	
	$.fn.fullSizeImage.defaults = {
		repositionImage : true,
		overlayLine : false,
		offset : null,
		position : 'fixed'
	};
})(jQuery);
