var $m = (function () {
	return {
		finestra: {
			w: 0,
			h: 0,
			cw: 0,
			ch: 0
		},
		init: function() {
//			var oldw = $m.readCookie('w');
//			var oldh = $m.readCookie('h');
			$j(window).resize($m.resize);
			$m.resize();
			
/*			if ( oldh == $m.finestra.h && oldw == $m.finestra.w ) {
				// do nothing, size is same
			} else if ( ( typeof(oldw) == 'string' && oldw > 0 && oldw != $m.finestra.w ) || ( typeof(oldh) == 'string' && oldh > 0 && oldh != $m.finestra.h ) ) {
				// refresh, size has changed
				document.location.href = document.location.href;
			} else {
				// no size read, first time loaded?  -->  test if cookies saved properly (from above)
				var testw = $m.readCookie('w');
				var testh = $m.readCookie('h');
				if ( testw == $m.finestra.w && testh == $m.finestra.h ) {
					// newly saved value reads properly, reload if wide enough for more than 3 columns
					if ( $m.finestra.w >= 1350 ) {
						document.location.href = document.location.href;
					}
				}
			}
			var t = 250;
			for ( var z = 1; z <= 100; z++ ) {
				if ( $j('#box'+z).length ) {
					$j('#box'+z).delay(t).fadeIn(600);
					t = t + 150;
				}
			}
			$j('#footer').delay(t).fadeIn(800);
			$j('.post').delay(t).fadeIn(1000);
*/			window.scrollTo(0,1);	// hide url bar
			if ( isHome ) {
				$j('#side h1').delay(1000).fadeIn(1000);
				$j('#side #menu').delay(2000).fadeIn(1000);
				$j('#photo').delay(3000).fadeIn(1000);
				$j('#footer').delay(5000).fadeIn(1000);
			} else {
				$j('#photo').delay(1000).fadeIn(1000);
			}
			setTimeout( $m.preloadImages, 3500 );
		},
		readCookie: function(name) {
			var cookiename = name + "=";  
			var ca = document.cookie.split(';');  
			for(var i=0;i < ca.length;i++) {
				var c = ca[i];
				while (c.charAt(0)==' ')
					c = c.substring(1,c.length);
				if (c.indexOf(cookiename) == 0)
					return c.substring(cookiename.length,c.length);  
			}
			return null;  
		},
		resize: function() {
			$m.finestra.w = $m.getWindowSize('w');
			$m.finestra.h = $m.getWindowSize('h');
			var today = new Date();
			var expire = new Date();
			expire.setTime(today.getTime() + (1000*60*60*24*365));
			document.cookie = "w="+escape($m.finestra.w)+"; expires="+expire.toGMTString()+"; path=/";
			document.cookie = "h="+escape($m.finestra.h)+"; expires="+expire.toGMTString()+"; path=/";

			if ( $m.finestra.w < 1010 )
				$m.finestra.w = 1010;
			if ( $m.finestra.h < 665 )
				$m.finestra.h = 665;
				
			var paddingWidth = ( $m.finestra.w > 1030 ? 40 : 20 );
			var paddingHeight = 105; 	// 120		// 20 + 1 + 20 + 20 + 2 + 1 + 10 + 10 + 10 + 4;
			var sideWidth = 295;
			
			var extraWidth = sideWidth + paddingWidth;
			var extraHeight = paddingHeight;
			
//			$j('#footer').html($m.finestra.w+'x'+$m.finestra.h);
			
			$j('#all').width( ($m.finestra.w - paddingWidth) );
			$j('#main').height( ($m.finestra.h - extraHeight) );
			$j('#side').height( ($m.finestra.h - extraHeight) );
			$j('#photo').width( ($m.finestra.w - extraWidth) );
			$j('#photo').height( ($m.finestra.h - extraHeight) );
			$j('#photo img').css('max-width', ($m.finestra.w - extraWidth) + 'px' );
			$j('#photo img').css('max-height', ($m.finestra.h - extraHeight) + 'px' );
			$j('#text').width( ($m.finestra.w - extraWidth) );
			$j('#text').height( ($m.finestra.h - extraHeight) );
			
			var x = Math.floor(($m.finestra.w - extraWidth) * 0.45);
			$j('#bioColumnTwo').width( (x > 350 ? x : 350) + 'px' );
			
			// give smaller screens a little padding
			$j('#side').width( ($m.finestra.w <= 1024 ? sideWidth-5 : sideWidth) );
			$j('#side').css('padding-left', ($m.finestra.w <= 1024 ? '3px' : '0') );
			$j('#photo').css('padding-right', ($m.finestra.w < 1024 ? '2px' : '0') );
			$j('#side #menu').css('top', ($m.finestra.h < 680 ? '5px' : '20px') );
		},
		getWindowSize: function(whichDimension) {
			if( typeof( window.innerWidth ) == 'number' ) {
				thebrowserwidth = window.innerWidth;
				thebrowserheight = window.innerHeight;
			} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
				thebrowserwidth = document.documentElement.clientWidth;
				thebrowserheight = document.documentElement.clientHeight;
			} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
				thebrowserwidth = document.body.clientWidth;
				thebrowserheight = document.body.clientHeight;
			}
			if(whichDimension=='w') {
				var toReturn=thebrowserwidth;
			} else {
				var toReturn=thebrowserheight;
			}
			return(toReturn);
		},
		preloadImages: function() {
			if ( document.images && (typeof(imagesToPreload) != undefined) && imagesToPreload.length > 0 ) {
				var preloadedImages = new Array();
				for ( i = 0; i < imagesToPreload.length; i++ ) {
					preloadedImages[i] = new Image();
					preloadedImages[i].src = imagesToPreload[i];
				}
			}
			
		}/*,
		track: function(url) {
			try {
				pageTracker._trackPageview('link.php?link='+url);
			} catch(err) {}
		}*/
	};
}());




// ***** INIT *****
var $j = '';
var $j = jQuery.noConflict();
var mmLoaded=false;
var RecaptchaOptions = {
	theme : 'clean'
};

var fileref=document.createElement("link");
fileref.setAttribute("rel", "stylesheet");
fileref.setAttribute("type", "text/css");
fileref.setAttribute("href", "/wp-content/themes/joncancelino.com/init.css");
if (typeof fileref!="undefined") {
	document.getElementsByTagName("head")[0].appendChild(fileref);
}

if ( isHome ) {
	var fileref=document.createElement("link");
	fileref.setAttribute("rel", "stylesheet");
	fileref.setAttribute("type", "text/css");
	fileref.setAttribute("href", "/wp-content/themes/joncancelino.com/init2.css");
	if (typeof fileref!="undefined") {
		document.getElementsByTagName("head")[0].appendChild(fileref);
	}	
}

jQuery(document).ready(function(){
	var $j = jQuery.noConflict();
	//var $j = jQuery;
	if(mmLoaded==false) {
		$m.init();
		mmLoaded=true;
	}		
});
// ***** init *****


