
//preload
window.onload = function(){
if(! $.cookie("more")) {
	window.setTimeout(
		function(){
    	var cache = new Array();
		var imgcount = numposts-3;
    	for(i = 1; i <= numposts; i++) {
        	cache[i] = new Image();
			cache[i].onload = function() {
			imgcount--;
			if(imgcount==0) {$.cookie("more","on");}
			}
    		cache[i].src = 'http://boscode.com/library_images/' + i + '_1s.jpg';
    	}

		$("<div>").load('/more/ #content', function() {
				$("div.clear").remove();
				$("#content").append($(this).find("#content").html())
				.find("img").hover(
				function () {
					$(this).fadeTo("fast",0.6);
  				},
  				function () {
    				$(this).fadeTo("fast",1);
				});
			});
	},500);
}
}


$(function() {

var loading = $("#loading");
var content = $("#content");
var buttonMore = $("#button-more");
var ddcat = $("dd.category");

//loading		
loading.show();

//hover
$("img, #button-more",content).hover(
	function () {
    $(this).fadeTo("fast",0.6);
	},
	function () {
    $(this).fadeTo("fast",1);
});
	
//hash
$(window).hashchange(function() {
	if (location.hash) {
		var path = location.hash.replace('#', '');
		
//more click
		if (path=="more") {
			if($.cookie("more")) {
    			buttonMore.hide();
				ddcat.removeClass("selected");
				loading.hide();
				$("div.img-more, div.img-show").show();
				content.fadeIn(800);
			}
			else {
				buttonMore.replaceWith('<div id="loading2"><img src="http://boscode.com/img/loading2.gif" alt="Loading..." /></div>');
				tid = setInterval(function () {
					if ($.cookie("more")) {
						clearInterval(tid), ( tid = null ), setTimeout(function() {
						$("div.clear").remove();
						ddcat.removeClass("selected");
						$("#loading2").hide();
						loading.hide();
						$("div.img-more").fadeIn(800);
						}, 0);
					}
				}, 200);
			}
		}
		
//cat click
		else if(path != "") {
		
			if(! $.cookie("more")) {
			if($.cookie("show")) {
				ddcat.removeClass("selected");
				$('#b_'+path).addClass("selected");
				content.hide();
				loading.show();
				tid = setInterval(function () {
					if ($.cookie("more")) {
						clearInterval(tid), ( tid = null ), setTimeout(function() {
							$("div.img-more, div.img-show").hide();
							$('div.'+path).show();
							buttonMore.hide();
							loading.hide();
							content.fadeIn(800);
						}, 0);
					}
				}, 200);
			}
			}
	
			else {
				ddcat.removeClass("selected");
				$('#b_'+path).addClass("selected");
				content.hide();
				$("div.img-show, div.img-more").hide();
				buttonMore.hide();
				$('div.'+path).show();
				loading.hide();
				content.fadeIn(800);
			}

		}
		
		else {
			ddcat.removeClass("selected");
			buttonMore.hide();
			content.hide();
			$("div.img-show, div.img-more").show();
			loading.hide();
			content.fadeIn(800);
		}
		
	}
	
	else {
	if($.cookie("more")) {
		ddcat.removeClass("selected");
		buttonMore.hide();
		content.hide();
		$("div.img-show, div.img-more").show();
		loading.hide();
		content.fadeIn(800);
	}
	}
	
});
	
$(window).hashchange();

	if(! $.cookie("more")) {
		jQuery.event.add(window, "load", function(){
		loading.hide();
		content.fadeIn(800);
		$.cookie("show","on");
		});
	}

});

