$(document).ready(function() {
	error404();
	navigationPrimary();
	socialNetworks();
	microPage();
	archiveList();
	commentsForm();
	contactForm();
	commentsLink();
	
	var billboard_timer = null;
	billboardPrep();
	setTimeout(function() {
		billboardLoop(); 
	}, 10000);
});

function error404() {
	$("#error_404").css({
		height: $("body").height(),					
		width: $("body").width()					
	});
}

function navigationPrimary() {
	$('#navigation_primary li').removeClass('hover');
	$('#navigation_primary li a')
		.css({ opacity: 1.0 })
		.mouseover(function() {				
			$(this).stop().animate(
				{ opacity: 0.0 }
			, 300);
		})
		.mouseout(function() {
			$(this).stop().animate(
				{ opacity: 1.0 }
			, 1000);
		});
}

function socialNetworks() {
	$("#social_area").append('<div id="hide_social_networks"><p><a href="#">Hide social networks</a></p></div>');
	$("#social_area").hide();
	$("#socialT").click(function() {
		$("#social_area").slideToggle("normal");
		return false;
	});
	$("#hide_social_networks a").click(function() {
		$("#social_area").slideUp("normal");
		return false;
	});
}

function ie7DialogFix(id) {
	//Fix for IE7 css rendering bug
	var parent = $(id).parent(".ui-dialog");
	var width = $(id).width() + 10;
	$(parent).find(".ui-dialog-titlebar").css({ width: ""+width+"px" });
	//$(parent +"p").css({display: "block"});
}

function microPage() {
	$('a[rel=me]').click(function(){
		var url = $(this).attr('href')+' #micro_page';
		$('body').append('<div id="scratch_content"></div>');
		$('#scratch_content').hide();
		$('#scratch_content').load(url, '', function() {
			$this = $('#scratch_content').dialog({
						title: ''+$('#scratch_content #micro_page_contentT').text()+'',
						modal: true,
						width: '620px';
						open: function(event, ui) {
							ie7DialogFix($(this));	
						},
						close: function(event, ui) {
							$this.remove();
						}
					});											 
		});
		return false;
	});
};

function archiveList() {
	$('#archive_list li').removeClass('hover');
	$('#archive_list li a')
	.mouseover(function() {				
		$(this).stop().animate(
			{ backgroundColor:'#083151' }
		, 200);
	})
	.mouseout(function() {
		$(this).stop().animate(
			{ backgroundColor:'#222' }
		, 400);
	});
}

function commentsForm() {
	$("#comment_form").validate();
}

function contactForm() {
	$("#contact_form").validate();
}

function commentsLink() {
	$("body#blog a.comments").click(function() {
		$.scrollTo('#comments', 800);
		return false;				 
	});
}

function billboardLoop() {
	
	var active_billboard = $('#billboard_mask div.billboard_item.active');
	var active_nav = $('#billboard_navigation li.active');
	
    if ( active_billboard.length == 0 ) { active_billboard = $('#billboard_mask div.billboard_item:last'); }
    if ( active_nav.length == 0 ) { active_nav = $('#billboard_navigation li:last'); }
	
    var next_billboard = active_billboard.next().length ? active_billboard.next() : $('#billboard_mask div.billboard_item:first');
	var next_nav = active_nav.next().length ? active_nav.next() : $('#billboard_navigation li:first');
	
    active_billboard.addClass('last_active');
	
	next_nav.addClass('active');
    active_nav.removeClass('active');
	
    next_billboard.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
	    	active_billboard.removeClass('active last_active');     
        });
        
    billboard_timer = setTimeout( "billboardLoop()", 10000 );

}

function billboardPrep() {
	
	$("#billboard")
	  .mouseover(function(){
		clearTimeout(billboard_timer);
		$("#billboard_paused").show();	
	}).mouseout(function(){
		billboard_timer = setTimeout( "billboardLoop()", 10000 );
		$("#billboard_paused").hide();
	});
	
	
	$("#billboard_navigation a").click(function(){
		clearTimeout(billboard_timer);
		$("div.billboard_item").stop(true, true);
		
		var parent = $(this).parent("li");
		
		if(!parent.hasClass("active")) {
			var active_billboard = $('#billboard_mask div.billboard_item.active');
			var active_nav = $('#billboard_navigation li.active');
			var number = $(this).text();
			
			var selected = $("#billboard div.billboard_"+number);
			
    		active_billboard.addClass('last_active');
			
			parent.addClass('active');
    		active_nav.removeClass('active');
			
    		selected.css({opacity: 0.0})
        		.addClass('active')
        		.animate({opacity: 1.0}, 1000, function() {
				active_billboard.removeClass('active last_active');  
        	});
        }
		
		return false;
	});
        
}
