$(document).ready(function() {
	$(function(){
		var backgroundheight = 2300;
		var now = new Date();
		var hour = now.getHours();
		var minute = now.getMinutes();
		// work out how far through the day we are as a percentage - e.g. 6pm = 75%
		var hourpercent = hour / 24 * 100;
		var minutepercent = minute / 60 / 24 * 100;
		var percentofday = Math.round(hourpercent + minutepercent);
		// calculate which pixel row to start graphic from based on how far through the day we are
		var offset = backgroundheight / 100 * percentofday;
        
		// graphic starts at approx 6am, so adjust offset by 1/4
		var offset = offset - (backgroundheight / 4);

		function scrollbackground() {
			offset = (offset < 1) ? offset + (backgroundheight + 1) : offset + 1;
			$('#header').css("background-position", offset + "px" + " 0");
			$('html').css("background-position", offset + "px" + " 0");
			setTimeout(function() {scrollbackground();}, 150);
		}
		scrollbackground();
	});
	
	$(function(){
		$('.c3').hover(
			function() {
				var pos = $(this).offset();
				$(this).addClass('hover');
				$('#bottom_light').animate({
					opacity: 1,
					backgroundPosition: (pos.left - 115) + "px" + " 0"
				},{ queue:false, duration:500 });
			},
			function() {
				$(this).removeClass('hover');
				$('#bottom_light').animate({opacity: 0}, 200);
			}
		);
	});

	$('#tabs').tabs({
		cookie: { expires: 30 },
		fx: {opacity: 'toggle'},
		show: function(event, ui) {
			elem = $(".ui-state-active a");
			if (elem.is (".brief")) {
				var current = $(".ui-state-active a").attr("href");
				var image = current.replace(/#/g, "_");
				$('#download').css("background" , "transparent url('briefs/" + image + ".png') no-repeat bottom center");
				$('#download').html('<a class="pdf_link" href="briefs/' + image + '.pdf"> </a>');
			}
			else {
				var current = $(".ui-state-active a").attr("href");
				var image = current.replace(/#/g, "_");
				$('#download').css("background" , "transparent url('briefs/none.png') no-repeat bottom center");
				$('#download').html('<p> </p>');
			}
		}
	});
	
	$('.ui-tabs-nav li a').click(function() {
	    val = new String("/tab/" + $(this).attr('href')).replace('#', '_');
		console.log(val);

	    res = urchinTracker(val);
	});
	
	

	$("#open_drawer").click(
		function () {
			$('#drawer').slideDown(700);
			$(this).fadeOut(500)
		}
	);
	$("#close_drawer").click(
		function () {
			$('#drawer').slideUp(500);
			$('#open_drawer').fadeIn(1000)
		}
	);

	
	$(".bios ul li").each(function() {
		var name = $(this).attr("class");
		var next = $(this).next().attr("class");
		var previous = $(this).prev().attr("class");
		$('#' + name).dialog({
			bgiframe: true,
			autoOpen: false,
			modal: true,
			resizable: true,
			stack: false,
			width: 650,
			open: function(event, ui) {
				$('.next_button').click(function() {
					$('#' + next).dialog('open');
					$('#' + name).dialog('close');
					$('#' + previous).dialog('close');
			    });
				$('.previous_button').click(function() {
					$('#' + previous).dialog('open');
					$('#' + name).dialog('close');
					$('#' + next).dialog('close');
				});
				next = $('.' + name).next().attr("class");
				previous = $('.' + name).prev().attr("class");
			}
		});
		
	    $(this).click(function() {
	        $('#' + name).dialog('open');
	    });
	});

});