jQuery.preloadImages = function()
{
  for(var i = 0; i<arguments.length; i++)
  {
    jQuery("<img>").attr("src", arguments[i]);
  }
};

function crossFade(image, target) {
	$(image).fadeTo(400,0,function() {
        $(image).attr({src : target});
	}).fadeTo(400, 1);
}

function resizeBox() {
	$("#boxes")
		.animate({height:
			$("#content").height()
		});
}

function showContent(id) {
	var moods = {
		"philosophy": 	{"image": "images/mood1.jpg", "color": "#5e224f", "color2": "#EAC8E2"},
		"company": 		{"image": "images/mood2.jpg", "color": "#9a2211", "color2": "#CFF6B6"},
		"clients": 		{"image": "images/mood5.jpg", "color": "#62563c", "color2": "#DFD5F6"},
		 "contact": 		{"image": "images/mood3.jpg", "color": "#0f4497"}
		// 	"contact": 		{"image": "images/mood4.jpg", "color": "#359618", "color2": "#BAE4F3"}
	};

	var css_header_id = "#header_" + id;
	var css_text_id = "#text_" + id;

	if (!$(css_header_id).is('.selected')) {
		// old title out
		$("#header h1.selected").removeClass("selected").fadeOut("def");
	}


	if (!$(css_text_id).is('.selected')) {
		// replace text
		$(".selected.text").removeClass("selected").slideUp("def").fadeOut("def");
		$(css_text_id).addClass("selected").slideDown("def").fadeIn("def");

		setTimeout(resizeBox, 500);
	} else {
		setTimeout(resizeBox, 0);
	}

	// adjust boxes and image
	$(".colorify").animate({backgroundColor: moods[id].color}, 1000);
	// $(".colorify2").animate({backgroundColor: moods[id].color2}, 1000);

	if (!$(css_header_id).is('.selected')) {
		crossFade("#header img", moods[id].image);

		// new title in
		$(css_header_id).addClass("selected").fadeIn("def");

	}
}

function showSubContent(id) {
	var css_title_id = "#subtitle_" + id;
	var css_text_id = "#subtext_" + id;

	if (!$(css_text_id).is('.selected')) {
		// replace title
		$(".selected.subtitle").removeClass("selected");
		$(css_title_id).addClass("selected");

		// replace text
		$(".selected.subtext").removeClass("selected").slideUp("def").fadeOut("def");
		$(css_text_id).addClass("selected").slideDown("def").fadeIn("def");

		// adjust boxes and image
		setTimeout(resizeBox, 500);
	}
}


$.preloadImages("images/mood2.jpg", "images/mood5.jpg", "images/mood3.jpg");

$(document).ready(function() {
	// links
	$("#nav a").click(function() {
		showContent($(this).attr("href").substring(1));
	});

	$(".subtitle a").click(function() {
		showSubContent($(this).attr("href").substring(1));
	});

	showContent('philosophy');
});

