// JavaScript Document
//
// ==================================================================
//
// JAVASCRIPT
// Stephane Chanay-Savoyen
// http://www.chanaysavoyen.com
//
// ==================================================================

(function(){
	var
	// Before doing anything, be sure to have a debug
	debug=function(a){
		var f=function(a){ 
			var r=true, t,
				s='t=typeof '+a+';';eval(s);
			if( t=="undefined" ){ r=false; } 
			return r; }
		if( f("fun") && 
			f("fun.debug") )
			{ fun.debug(a); }
		else if( f("console") && 
			f("console.log") )
			{ console.log(a); }
	}
	,
	// Main function 
	full_background=function(f,a)
	{	debug("function full_background is running");
		
        if( f ){
			if( f=="init" ){ var r=full_background_init(a); }
			else if( f=="resize" ){ var r=full_background_resize(a); }
		}; if(typeof r!=="undefined"){ return r; }                      
		
	}
	,
	// Check window size after event (depend: jQuery)
	full_background_checksize=function()
	{	debug("function full_background_checksize is running");
		
		var winsize=full_background_winsize||{};
		try{var winW=$(window).width(),
				winH=$(window).height();
		}catch(e){ debug("function full_background_checksize error: "+e.message); }
		if( !winW||!winH ){
			var d=document,
				winW=d.documentElement.clientWidth,
				winH=d.documentElement.clientHeight;
		}
		
		if( winsize=={} ||
			winsize.width!=winW ||
			winsize.height!=winH ){
			winsize.width=winW;
			winsize.height=winH;
			full_background_resize();
			full_background_winsize=winsize;
		}
		
	}
	,
	// Resize (depend: jQuery)
	full_background_resize=function(a) 
	{	debug("function full_background_resize is running");
		
		var self=this,
			elts=$('.full_background'); 
			if(a){ elts=$(a); }
			
		elts.each(function(){
			
			var elt=this;
			try{var winW=$(window).width(),
					winH=$(window).height();
			}catch(e){ 
				//debug(e);
			}
			if( !winW||!winH ){ var w=window;
				if( typeof w.innerWidth=='number' )
				{ winW=w.innerWidth; winH=w.innerHeight; }
			}
			if( !winW||!winH ){ var d=document;
				winW=d.documentElement.clientWidth,
				winH=d.documentElement.clientHeight;
			}
			
			$(elt).css({ 
				width:winW,
				height:winH
			});
			
			if( $('> img',elt).length>0 ){
				var img = $('> img',elt);
					img.css({minWidth:0,minHeight:0});
				var iw = parseInt(img.attr('width'))||0,
			    	ih = parseInt(img.attr('height'))||0;
				if( iw==0 ){ iw=img.width(); }	
				if( ih==0 ){ ih=img.height(); }
				
				// image not ready
				if( iw==0||ih==0 ){
					// hide image
					img.css({ visibility:"hidden" });
					// try later
					var f=function(){ 
						try{ self.resize(elt); }
						catch(e){} 
					};	window.setTimeout(f,200);
					return; // break each loop
				}
				// image is ready
				else{
					
					img.attr('width',iw);
					img.attr('height',ih);
						
					// adjust size to window size
				    var rw = iw/ih,
				    	rh = ih/iw,
				    	sc = winH*rw,
						nh, nw;
				    if( sc >= winW ){
				        nh = winH;
				        nw = sc;
				    }else{
				        sc = winW*rh;
				        nh = sc;
				        nw = winW;
				    };
				    img.css({
						height:Math.floor(nh)+"px",
						width:Math.floor(nw)+"px",
						minHeight:Math.floor(nh)+"px",
						minWidth:Math.floor(nw)+"px"
					});
					// adjust position to center
					var pt = (winH-nh)/2,
						pl = (winW-nw)/2;
					img.css({
						position:"relative",
						top:Math.floor(pt)+"px",
						left:Math.floor(pl)+"px"
					});
					// show image
					img.css({visibility:"visible"});
					// hide overflow
					$(elt).css({
						display:'block', 
						'overflow':'hidden' 
					});
					
				}
			};
		});
		
	}
	,
	// Initialize (depend: jQuery)
	full_background_init=function()
	{	debug("function full_background_init is running");
		
		var self=this;
		$(document).ready(function(){ self.checksize(); });
		$(window).resize(function(){ self.checksize(); });
		$(window).scroll(function(){ self.checksize(); });
		$("body").ready(function(){ self.resize();
			// next line below is used to run after other jquery ready functions:
			window.setTimeout(function(){ self.resize(); },1);
		});
		
	}
	,
	// Keep memory of the last window size
	full_background_winsize={}
	,
	// Lets run
	run=function()
	{   
		// Create constructor
		try{ var f=full_background; }
		catch(e){ var f=function(){}; }
		
		// Add to constructor
		f.prototype.init=full_background_init;
		f.prototype.resize=full_background_resize;
		f.prototype.winsize=full_background_winsize;
		f.prototype.checksize=full_background_checksize;
		
		// Add to public container for sharing (if needed...)
		// IE bug with __proto__ so used constructor.prototype instead
		window._fun_.prototype.full_background=f; // function // YES
		//window._fun_.prototype.full_background=new full_background(); // object // NO
		
		// Initialize new
		var n=new f(); n.init();
		// Better not use previous line to initialize, use next line in your main script:
		// var full_background = new fun.full_background(); full_background.init();
	}
	;
	var w=window; // shortcut

	// Make public container accessible
	w._fun_=w._fun_||function(){};

	// Create private instance
	var fun=new w._fun_();

	// autorun
	run();
	
	////// TRASH //////
	/*
	
	// Before doing anything, 
	// be sure to have a debug
	if( !debug ){
		if( !fun.debug ){
			// IE bug with __proto__ 
			// so we use _fun_.prototype
			_w._fun_.prototype.debug=function(a){
				if( typeof console!="undefined" ){ 
					try{ console.log(a); }
					catch(e){}
				}
			}
		} var debug=fun.debug; // shortcut
	}
	
	// Return arguments
	function args()
	{
		var r = null;
		var a=arguments.callee.caller; if(a){
			try{ r=Array.prototype.slice.call(a.arguments); }
			catch(e){ };
		}; return r; 
		
	}; // end function args
	                
	// Send arguments to main function
	if( arguments.callee.caller && 
		typeof args=="function" && 
		args()!=null && a!="" ){ 
	   var a=args();
	}; var r=full_background(a);                   
	if(typeof r!="undefined"){ return r; }
	*/
	
})();
