// CONTENTS ====================================================================
/*
- GLOBAL VARS						- 
- INIT	 							- common code that runs on every page.
- BG / PALLETTE						- code to handle bg loading & associated pallettes
- BROG								- manipulate img sizes to be consistent
*/

// GLOBAL VARS ====================================================================
var DoBgs		= true;
var HoldTime	= 10000;
var CurrentBg 	= -1;
var BgMatrix 	= [
	{bg:"window.jpg", 		dark:"#471100", medDark:"#681a01", med:"#915f4b", medLight:"#d1ee63", light:"#edf9f0"},
	{bg:"graf.jpg", 		dark:"#1f2933", medDark:"#efc614", med:"#0c7226", medLight:"#2ca0b9", light:"#dcefed"},
	{bg:"cupcakes.jpg", 	dark:"#2b3150", medDark:"#e6bf00", med:"#709990", medLight:"#fa0a00", light:"#f8f3d8"},
	{bg:"bear.jpg", 		dark:"#1f1d4b", medDark:"#459290", med:"#00bed0", medLight:"#99d4d6", light:"#e7eadc"},
	// {bg:"door.jpg", 		dark:"#3b2900", medDark:"#b67128", med:"#dea475", medLight:"#d6d3cf", light:"#fffff8"},
	{bg:"checks.jpg", 		dark:"#1d1626", medDark:"#364971", med:"#4fa7b1", medLight:"#c73e5d", light:"#d8f5f5"},
	{bg:"ross-trees.jpg", 	dark:"#271c05", medDark:"#504d12", med:"#9fbc00", medLight:"#ccce3e", light:"#ede5b1"},
	{bg:"fence.jpg", 		dark:"#1a100f", medDark:"#625b55", med:"#9fb435", medLight:"#5988bc", light:"#e2e4e1"}
];

// INIT ====================================================================
function Init(){
	
	// resize cols to make the most of screen real estate
	var mainwidth = $("body").width() / 2.2;
	$("#main").width(mainwidth);
	// $("#brog").width(mainwidth);
	
	// fix the position of the main div
	$("#main").css("position", "fixed");
	$("#brog").css("position", "relative");
	$("#brog").css("left", mainwidth+120);
	
	// call other Init functions
	InitBgs();
	
	// only start loading the big bg imgs once all the indow content has been loaded
	$(window).load(function () {
  		NewBg();
  		//InitBrog();
	});
	
	// open external links in new windows
	$(document).ready(function() {
		$("a[href^=http]").each(function(){
			if(this.href.indexOf(location.hostname) == -1) {
				$(this).click(function(){
					window.open(this.href); return false;
				});
				$(this).attr('rel', 'nofollow');
			}
		});
	});
	
}

// BG / PALLETTE ====================================================================
function InitBgs(){
	//alert( BgMatrix[0].bg );
	$("#bg").width( $(window).width() );
	$("#bg").height( $(window).height() );
	$(".btn-bg-load").click(CancelBg);
}

function NewBg(){
	
	if(DoBgs){
		
		$("#message .loading").show();
		$("#message .loaded").hide();
		ShowMessage();
		
		CurrentBg ++;
		if(CurrentBg > (BgMatrix.length - 1) ) CurrentBg = 0;
		
		var img = new Image();
	  
		$(img)
		.load(function () {										// on img load
			
			if(DoBgs){
				HideMessage();										// hide loading message
				$("#bg")											// with the holding div (#bg)
				.fadeOut(300, function(){ 							// fade it out
					$(this)
					.html("")										// clear any existing <img>s 
					.append(img)									// then insert our image
					.fadeIn(1000, SetPalette); 						// fade it in and call to set the palette
				})
			}
			
		})
		
		.error(function () {
			alert("img could not be loaded");
		})
		
		// *finally*, set the src attribute of the new image to our image, triggering the load
		.attr("src", "/images/bgs/"+BgMatrix[CurrentBg].bg);
	
	}
	
}

function SetPalette(){
	
	if(DoBgs){
		
		for(var i in BgMatrix[CurrentBg]) {
			$("."+i).animate({color:BgMatrix[CurrentBg][i]}, 1000)
			$(".bg-"+i).css("background", BgMatrix[CurrentBg][i])
			// fade in bg colour for logomark
			$(".lm-bg-"+i).animate({backgroundColor:BgMatrix[CurrentBg][i]},1000)
		}
		
		$("#posts li .img-wrapper").animate({backgroundColor:BgMatrix[CurrentBg].dark}, 1000)
		
		$("#message .loading").hide();
		$("#message .loaded").show();
		ShowMessage();
		
		window.setTimeout(HideMessage, HoldTime / 2);
		window.setTimeout(NewBg, HoldTime);
		
	}
}

function ShowMessage(){
	$("#message").animate({top:0}, 500)
}

function HideMessage(){
	$("#message").animate({top:-50}, 500)
}

function CancelBg(){
	DoBgs = false;
	HideMessage();
	
}

// BROG ====================================================================
function InitBrog(){
	$("#posts li .img-wrapper img").each(function(){
		//alert( $(this).width() + " / " + $("#brog").width() )
		if( ($(this).width() + 20) > $("#brog").width() ){
			$(this).width( $("#brog").width() - 20 );
		}else{
			$(this).parent().width( $(this).width() );
		}
	})
}




