//ideon namespace object
ideon = {};
// jQuery's onDOMready function
$(document).ready( function(){

	//set up elements once (for speed in execution)
	ideon.nav = $("#nav").children("ul");
	ideon.wrapper = $("#wrapper");
	ideon.content = $("#content");
	ideon.titleNav = $("#titleNav");
	ideon.shim = $("#shim");
	ideon.blindTop = $("#topBlind");
	ideon.blindBottom = $("#bottomBlind");
	ideon.blindTopShim = $( ideon.blindTop.children(".gradientShim") );
	ideon.blindBottomShim = $( ideon.blindBottom.children(".gradientShim") );
	ideon.blindMaxHeight = 180;
	ideon.blindMinHeight = 60;
	ideon.topOffsetPercentage = .2;
	ideon.accordion();
	// because ie6 is crappy, but we still have to deal with it... 
	if (document.all && document.styleSheets && document.styleSheets[0] && document.styleSheets[0].addRule && typeof document.body.style.maxHeight == "undefined"){
		ideon.fixIE6();
	}
	// browsers arent so hot on last-child, this was preventing validation so we set it here...
	$(".post:first-child").css( "margin-top", 0 );
	$(".post p:last-child").css( "margin-bottom", 0 );

	ideon.setupAnchorSmoothScroll();

	// set up resize behavior and run it once
	if (navigator.userAgent.indexOf("Firefox") != -1){
		$(document).bind( "mouseover", ideon.clearMozillaResize );
		$(document).bind( "mouseout", ideon.mozillaResize );
	}else{
		$( window ).resize( ideon.onResize );
	}
	ideon.onResize();
	ideon.fadetagline();

});

ideon.onNavSelectBlur = function(){
	var navLinks = $("#nav a");
	navLinks.each( function( anIndex ){
		this.blur();
	});
}
ideon.setupAnchorSmoothScroll = function(){
	if(!ideon.titleNav) return false;
	var titleLinks = $("#titleNav a");
	titleLinks.each( function( anIndex ){
		var targetId = this.href.substring( this.href.indexOf("#"), this.href.length );
		var target = $(targetId);
		$(this).bind( "click", target, function(e){
			ideon.scrollTo( target );
			this.blur();
			return false;
		});
	});
}

ideon.scrollTo = function( aTarget ){
	if( typeof document.body.style.maxHeight != "undefined"){
		$.scrollTo( aTarget, 550, { offset: { top: - ideon.getNavPositionTop() } } );
	}else{
		$("#wrapper").scrollTo( aTarget, 550, { offset: { top: - ideon.getNavPositionTop() } } );
	}	
}

ideon.getNavPositionTop = function(){
	var val = $(window).height();
	val = ideon.topOffsetPercentage* $(window).height();
	return val;
}

ideon.setBodyBottomPad = function( h ){
	if( ideon.getNavPositionTop() + ideon.content.height() + 100 < h ) return false;
	var bottomPadding = ( h - ( ideon.getNavPositionTop()  + ideon.nav.height() ) );
	ideon.content.css( "padding-bottom", bottomPadding + "px");
}

ideon.onResize = function(){
	
//	console.log( "onResize" );
	var w = $( window ).width();
	var h = $( window ).height();
	ideon.setBodyBottomPad( h );
	var blindHeightRatio = 2*ideon.blindMaxHeight/h;
	var blindHeight = ( ideon.blindMaxHeight * ( 1-blindHeightRatio ) < ideon.blindMaxHeight )?  ideon.blindMaxHeight * ( 1-blindHeightRatio ) : ideon.blindMaxHeight;
	blindHeight = ( blindHeight > ideon.blindMinHeight )? blindHeight : ideon.blindMinHeight;
	ideon.blindTopShim.css( "height", blindHeight );
	ideon.blindBottomShim.css( "height", blindHeight );
	ideon.shim.css( "height", h*ideon.topOffsetPercentage );
	
	if( w < 1000 ){
		ideon.wrapper.css("margin-left", "490px" );
		ideon.titleNav.children("ul").css("margin-left", "490px" );
		ideon.blindBottom.children(".gradientShim").css("margin-left", "490px" );
		ideon.blindBottom.children(".fade").css("margin-left", "490px" );
		ideon.blindTop.children(".gradientShim").css("margin-left", "490px" );
		ideon.blindTop.children(".fade").css("margin-left", "490px" );
	}else{
		ideon.wrapper.css("margin-left", "49%" );		
		ideon.titleNav.children("ul").css("margin-left", "49%" );
		ideon.blindBottom.children(".gradientShim").css("margin-left", "49%" );
		ideon.blindBottom.children(".fade").css("margin-left", "49%" );
		ideon.blindTop.children(".gradientShim").css("margin-left", "49%" );
		ideon.blindTop.children(".fade").css("margin-left", "49%" );
	}
};

ideon.fixIE6 = function(){
	// fix ie6 double margin bug
	// ideon.content.css( "display", "inline" );
	// Fix ie6's lack of png support
	document.styleSheets[0].addRule('#wrapper .shim', 'behavior: url(/iepngfix.htc)');
	document.styleSheets[0].addRule('#topBlind .shim', 'behavior: url(/iepngfix.htc)');
	document.styleSheets[0].addRule('#topBlind .fade', 'behavior: url(/iepngfix.htc)');
	document.styleSheets[0].addRule('#bottomBlind .shim', 'behavior: url(/iepngfix.htc)');
	document.styleSheets[0].addRule('#bottomBlind .fade', 'behavior: url(/iepngfix.htc)');
}

ideon.clearMozillaResize = function(){
	clearTimeout( ideon.timeoutId );
}

ideon.mozillaResize = function(fn) {
    ideon.timeoutId = setInterval( ideon.onResize, 70 );
}

ideon.accordion = function(){
		$('.accordion').click(function() {
/* 			ideon.scrollTo($(this)); */
			$(this).next('.collapse').slideToggle(300);
				var htmlStr = $(this).children('span').html();
				var newHTML = htmlStr == '+' ? '&ndash;' : '+';
				$(this).children('span').html(newHTML);
			return false;1	
		}).next().hide();
}

ideon.fadetagline = function(){
  // find the div.fade elements and hook the hover event
	 if ($('div#tagline').length > 0){
	 //	 $('body').css('opacity', '0');
	//  $('body').animate({opacity: 1}, 1000);
	 $('div.post:last').css('visibility', 'visible');
	  $('div#tagline').animate({opacity: 1}, 4000).fadeOut(3000, function() {
      $(this).remove();
    });
  	}
}

