	window.onload = function () {
	
		var i = 0,
			max = 0,
			o = null,
	
			// list of stuff to preload
			preload = [
				'/img/back_r1.jpg',
				'/img/back_r2.jpg',
				'/img/back_r3.jpg',
				'/img/back_r4.jpg',
				'/img/back_r5.jpg',
				'/img/back_r6.jpg',
				'/img/back_r7.jpg',
				'/img/back_r8.jpg',
				'/img/back_r9.jpg'
			],
			isIE = navigator.appName.indexOf('Microsoft') === 0;
	
		for (i = 0, max = preload.length; i < max; i += 1) {
	
			if (isIE) {
				new Image().src = preload[i];
				continue;
			}
			o = document.createElement('object');
			o.data = preload[i];
	
			// IE stuff, otherwise 0x0 is OK
			//o.width = 1;
			//o.height = 1;
			//o.style.visibility = "hidden";
			//o.type = "text/plain"; // IE 
			o.width  = 0;
			o.height = 0;
	
			// only FF appends to the head
			// all others require body
			document.body.appendChild(o);
		}
	
	};

var slideshowSpeed = 16000;

// Variable to store the images we need to set as background
// which also includes some text and url's.
var photos = [ {
		"image" : "/img/back_r1.jpg"
	}, {
		"image" : "/img/back_r2.jpg"
	}, {
		"image" : "/img/back_r3.jpg"
	}, {
		"image" : "/img/back_r4.jpg"
	}, {
		"image" : "/img/back_r5.jpg"
	}, {
		"image" : "/img/back_r6.jpg"
	}, {
		"image" : "/img/back_r7.jpg"
	}, {
		"image" : "/img/back_r8.jpg"
	}, {
		"image" : "/img/back_r9.jpg"
	}
];



$(document).ready(function() {
	$("a[rel='cb1']").colorbox({transition:"fade"});
	$("a[rel='cb2']").colorbox({width:"700px", height:"460px"});
//	$(".example8").colorbox({width:"50%", inline:true, href:"#inline_example1"});
//	$("a[rel='cb3']").colorbox({transition:"fade"});
//	$("a[rel='cb4']").colorbox({slideshow:true});
	var interval;
	$("#control").toggle(function(){
		stopAnimation();
	}, function() {
		// Show the next image
		navigate("next");
		
		// Start playing the animation
		interval = setInterval(function() {
			navigate("next");
		}, slideshowSpeed);
	});
	
	
	var activeContainer = 1;	
	var currentImg = 0;
	var animating = false;
	var navigate = function(direction) {
		// Check if no animation is running. If it is, prevent the action
		if(animating) {
			return;
		}
		
		// Check which current image we need to show
		if(direction == "next") {
			currentImg++;
			if(currentImg == photos.length + 1) {
				currentImg = 1;
			}
		} else {
			currentImg--;
			if(currentImg == 0) {
				currentImg = photos.length;
			}
		}
		
		// Check which container we need to use
		var currentContainer = activeContainer;
		if(activeContainer == 1) {
			activeContainer = 2;
		} else {
			activeContainer = 1;
		}
		
		//showImage(photos[currentImg - 1], currentContainer, activeContainer);
		showImage(photos[Math.floor(Math.random()*photos.length)], currentContainer, activeContainer);
	};
	
	var currentZindex = -1;
	var showImage = function(photoObject, currentContainer, activeContainer) {
		animating = true;
		
		// Make sure the new container is always on the background
		currentZindex--;
		
		// Set the background image of the new active container
		$("#headerimg" + activeContainer).css({
			"background-image" : "url(" + photoObject.image + ")",
			"display" : "block",
			"z-index" : currentZindex
		});
		
		// Hide the header text
		$("#headertxt").css({"display" : "none"});
		
		
		// Fade out the current container
		// and display the header text when animation is complete
		$("#headerimg" + currentContainer).fadeOut(function() {
			setTimeout(function() {
				$("#headertxt").css({"display" : "block"});
				animating = false;
			}, 500);
		});
	};
	
	var stopAnimation = function() {
		clearInterval(interval);
	};
	
	// We should statically set the first image
	navigate("next");
	
	// Start playing the animation
	interval = setInterval(function() {
		navigate("next");
	}, slideshowSpeed);
	
});

var tabFlip = {
	callBackFuncs:new Array(),
	selector:null,
	selectedIndex:0,
	makeTabs:function(selector){
		this.selector = selector;
		$(this.selector+' div:first').css({'display':'block'});
		$(this.selector+' div:first').addClass('current');
		$('#flipnavs a:first').addClass('current');
		if($.address.parameter('tab') == null){
			$.address.parameter('tab',$('#flipnavs a:first').attr('rel'));
		}
		$('#flipnavs li a').click(function(event){
			event.preventDefault();
			$.address.parameter('tab',$(this).attr('rel'));
			tabFlip.setTab($(this).attr('rel'));
		});
	},
	onChange:function(func,args){
		this.callBackFuncs.push(func,args);
	},
	setTab:function(label){
		$(tabFlip.selector+' .current').css({'display':'none'});
		$('#flipnavs .current').removeClass('current');
		$('#flipnavs a.current').removeClass('current');
		$('#'+label).css({'display':'block'});
		$('#'+label).addClass('current');
		$('#flipnavs a[rel="'+label+'"]').addClass('current');
		tabFlip.selectedIndex = $('#flipnavs a').index($('#flipnavs a[rel="'+label+'"]'));
		$(tabFlip.callBackFuncs).each(function(){
			if(typeof($(this)) == 'function'){
				$(this)();
			}
		});
	}
}



$(function () {
	var tabContainers = $('div.tabs > div');
	tabContainers.hide().filter(':first').show();
	
	$('div.tabs ul.tabNavigation a').click(function () {
		tabContainers.hide();
		tabContainers.filter(this.hash).show();
		$('div.tabs ul.tabNavigation a').removeClass('selected');
		$(this).addClass('selected');
		return false;
	}).filter(':first').click();
});







