jQuery.cookie = function (key, value, options) {

    // key and value given, set cookie...
    if (arguments.length > 1 && (value === null || typeof value !== "object")) {
        options = jQuery.extend({}, options);

        if (value === null) {
            options.expires = -1;
        }

        if (typeof options.expires === 'number') {
            var days = options.expires, t = options.expires = new Date();
            t.setDate(t.getDate() + days);
        }

        return (document.cookie = [
            encodeURIComponent(key), '=',
            options.raw ? String(value) : encodeURIComponent(String(value)),
            options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
            options.path ? '; path=' + options.path : '',
            options.domain ? '; domain=' + options.domain : '',
            options.secure ? '; secure' : ''
        ].join(''));
    }

    // key and possibly options given, get cookie...
    options = value || {};
    var result, decode = options.raw ? function (s) { return s; } : decodeURIComponent;
    return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null;
};

var cookie_options = { path: '/', expires: 1 };

$(function(){

	// please stop deleting this!
	// show popup
	cid = $("#popup a").attr("id");
	if(typeof cid != 'undefined') {
		c = $.cookie(cid);
		if(!c) {
			$('#popup').lightbox();
			$('#popup').hide();
		}
	}

	// hide popup
	$(".hide-popup").click(function(){
		$.cookie($(this).attr("id"), 'hide', cookie_options);
		hidelightbox();
		//$('#popup').fadeOut();
	})
	
	//<FEATURED CONTENT> ====================================
	
	
	//something of James's
	
	var tabContainers = $('#featured > .article');
	tabContainers.hide().filter(':first-child').show();
	
	
	//prep
	
	var int_interrupt = false;
	var intPermanentlyInterruptedOn = false;
	var barColours = ['48A842', 'e1002c', 'FFAE2E', '00367C', '00BBE3', '005f2f']; //in order
	var autoRotateSpeed = 3000; //miliseconds
	
	
	//set tab as 'on'
	
	setOnTab = function(tabClicked) {
		if (!onPageLoadImageLoaded) onPageLoadImageLoaded = true; else tabContainers.fadeOut();
		with (tabContainers.filter(tabClicked.children('a').get(0).hash)) onPageLoadImageLoaded ? fadeIn() : show();
		$('.hi').removeClass('hi');
		tabClicked.addClass('hi');
		return false;
	}
	var onPageLoadImageLoaded = false;
	setOnTab($('#featured .tabNavigation li:first-child'));
	
	
	//onload, insert a dark mask into each 'featured content' tab. This should be dark by default (except for the one currently on) and vanish on hover
	
	$('#featured .tabNavigation li').each(function(e) {
		var darkMask = document.createElement('div');
		with($(darkMask)) {
			css({background: '#444', opacity: .5, filter: 'alpha(opacity=50)', width: '100%', height: '100%', position: 'absolute'});
			if ($(this).is('.hi')) css({width: 1});
			addClass('featuredContentDarkMask');
		}
		$(this).prepend(darkMask);
	});
	
	
	//on mouseover on tab, interrupt auto-rotate (if running) and turn on tab we're hovering on
	
	var currHoveringOnTabID;
	$('#featured ul li div').mouseover(function(e) {
		if(!$(this).is('.hi') && $(e.target).attr('id') != currHoveringOnTabID && !$(e.target).is('#bar')) {
			int_interrupt = true;
			e.stopPropagation();
			$('.featuredContentDarkMask').each(function() {
				$(this).show();
				$(this).stop();
				$(this).css({width: 182, left: 0});
			});
			setOnTab($(this).parent());
			colourBar($(this).parent(), true);
			currHoveringOnTabID = $(this).parent().attr('id');
			$(this).hide();
		}
	});
	
	$('#featured ul li *').mouseover(function(e) { e.stopPropagation(); });

	
	//on mouseout on tab, either continue auto-rotate or, if not running (due to click on tab), revert to tab that
	//was previously clicked
	
	$('#featured .tabNavigation').mouseout(function(e) {
		if ($(e.relatedTarget).closest('#featured').length == 0) {
			if (intPermanentlyInterruptedOn) {
				$('.tabNavigation .featuredContentDarkMask').show();
				intPermanentlyInterruptedOn.children('.tabNavigation .featuredContentDarkMask').hide();
				setOnTab(intPermanentlyInterruptedOn);
				colourBar(intPermanentlyInterruptedOn, true);
			}
			currHoveringOnTabID = null;
			int_interrupt = false;
		}
	})
	
	
	//click of tab represents permanent interrupt
	
	$('#featured .tabNavigation li').click(function() { clearInterval(featuredInt); intPermanentlyInterruptedOn = $(this); });
	
	
	//sifr config thing
	
	function sifrConfig() {
		var din = { src: '/assets/flash/din.swf' };
		sIFR.activate(din);
		sIFR.replace(din, {transparent:'wmode',
		  selector: '.home .cc h3',
		  css: '.sIFR-root {color:#000000;text-transform:uppercase;}'
		});
	}
	sifrConfig();
	
	
	//set up auto-rotate

	var featuredInt = setInterval(function() {
		if (!int_interrupt) {
			var next = $('#featured .tabNavigation li.hi').next().length == 1 ? $('#featured .tabNavigation li.hi').next() : $('#featured .tabNavigation li:first-child');
			$('#featured .tabNavigation li.hi .featuredContentDarkMask').animate({width: 182}, 'slow');
			colourBar(next);
			next.children('.tabNavigation .featuredContentDarkMask').animate({left: 182, width: 0}, 'slow', null, function() {
				$(this).css('left', 0);
				sifrConfig();
			});
			setOnTab(next);
		}
	}, autoRotateSpeed);
	
	
	//utility for handling bar colour transition, hiding arrow, changing it then making it reappear
	
	function colourBar(basedOnThisLI, noFading) {
		var thisLIIDNum = parseInt(basedOnThisLI.attr('id').match(/\d+$/));
		if (!noFading)
			$('#featuredBar').animate({backgroundColor: '#'+barColours[thisLIIDNum-1]});
		else
			$('#featuredBar').css('backgroundColor', '#'+barColours[thisLIIDNum-1]);
		var arrow = $('#featuredBar').children('div');
		arrow.animate({opacity: 0, left: (thisLIIDNum-1) * (182 + 7)}, !noFading ? 'normal' : 0, null, function() {
			arrow.css({backgroundPosition: -((thisLIIDNum-1) * 23)+'px 0'});
			$(this).animate({opacity: 1}, !noFading ? 'normal' : 0);
		});
	}
	
	
	//</FEATURED CONTENT> =======
	
	//IE curvycorners
	if ($.browser.msie){
	    	$('.cc').corner({
	    		tl: { radius: 10 }, tr: { radius: 10 }, bl: { radius: 10 }, br: { radius: 10 }, antiAlias: true 
	    	});
	    	$('#popup .btn').corner({
	    		tl: { radius: 3 }, tr: { radius: 3 }, bl: { radius: 3 }, br: { radius: 3 }, antiAlias: true
	    	});	    	
	    	$(".cc").css("margin","0 0 40px 0");
	    	$("#page_tools").css("margin-top","0");
	    	$('.tabNavigation li:last-child').css('margin-right','0');	
		};
		//Select Internet Explorer 7 and below
		if (jQuery.browser.msie && jQuery.browser.version <= 7) {
			 $("#whats_new li").css("height","250px");
		};
});