$.fn.nextUntil = function(expr) {
	var match = [];
	this.each(function(){
		for (var i = this.nextSibling; i; i = i.nextSibling) {
			if (i.nodeType != 1) continue;
			if ($(i).is(expr)) break;
			match.push(i);
		}
	});
	return this.pushStack(match, arguments);
};

// attach a class to body tag if javascript is enabled
$(function(){
	$('body').addClass('jsEnabled');
});


$(function(){
	$('.datagrid_search SELECT').change(function(){
		this.form.submit();
	});
});

$(function(){
	$('a.video').click(function(evt){
			$('BODY').append('<div style="display:none" id="video"></div>').append('<a href="#video" id="videoLink" />');
			var videoDetails = $(this).metadata({type:'attr', name:'data'});
			videoDetails.url = $(this).attr('href');
			videoDetails.title = $(this).attr('title');

			var flashvars = {
				autostart:'true', repeat:'false',
				width:videoDetails.width,
				height:videoDetails.height+20,
				file:videoDetails.url
			};

			$('#videoLink').css({position:'absolute', top:evt.pageY, left:evt.pageX}).fancybox({
				onComplete:function(){
					console.debug(flashvars);
					$('#fancybox-inner').html('<div id="fancy_video"></div>');
					swfobject.embedSWF('/swf/player.swf', 'fancy_video', videoDetails.width, videoDetails.height+20, '9.0.115', null, flashvars, {wmode:'opaque', allowfullscreen:'true'});
				},
				'type':'swf',
				'width':videoDetails.width,
				'height':videoDetails.height+20,
			}).trigger('click');

			return false;

		});
	
});


$(function(){
	$('.expandable-content H3').click(function(){
		$(this).attr('className', ($(this).next().toggle().is(':visible') ? 'expanded' : 'contracted'));
	}).each(function(){
		$(this).nextUntil('H3').wrapAll('<div class="content-wrapper"></div>');
	}).addClass('contracted').next().hide();
});

// open external + pdf links in a new window
$(function(){
	$('A, AREA').filter(function(){
		return (!this.target && (this.href.indexOf(window.location.hostname) == -1 || this.href.match(/\.pdf$/i)));
	}).attr('target', 'blank');
});

// prevent flicker on menu images
try {
	document.execCommand("BackgroundImageCache", false, true);
} catch(err) {}

// preload images
jQuery.preloadImages = function() {
	jQuery(arguments[0]).each(function() {
		jQuery("<img />").attr("src", this).css({position:'absolute', top:'-500px'}).appendTo('BODY');
	});
};

// jCarousel callbacks
var media_itemList = []; // init

function media_getItemHTML(item) {
	return '<img src="' + item.url + '" alt="' + item.title + '" title="' + item.title + '" />';
};

function media_getItemCaption(item) {
	return item.caption;
};

function media_itemLoadCallback(carousel, state) {
	for (var i = carousel.first; i <= carousel.last; i++) {
		if (carousel.has(i)) {
			continue;
		}
		if (i > media_itemList.length) {
			break;
		}
		carousel.add(i, media_getItemHTML(media_itemList[i-1]));
	}
};

function media_itemFirstInCallback(carousel, item, idx, state) {
	$('#mediaCaption').html(media_getItemCaption(media_itemList[idx-1]));
};

function media_initCallback(carousel) {
	jQuery('#mediaNext').bind('click', function() {
		carousel.next();
		return false;
	});
	jQuery('#mediaPrev').bind('click', function() {
		carousel.prev();
		return false;
	});
};



