$(function() {
	$("div.accordion").accordion();
	
	//Make list items collapsible
	$('div.collapsible h5').each(function(){
		if($(this).parent().next().is(':visible')){
			$(this).parent().addClass('collapsible-open');
		}
	}).live('click', function() {
		$(this).parent().toggleClass('collapsible-open').next().slideToggle();
	});

	$("a.dialog,a.dialog300,a.dialog450,a.dialog600,a.dialog750").each(function(){
		if($(this).hasClass('dialog300')){
			var options = {
				autoOpen: false,
				modal: true,
				height: 300,
				width: 300
			}
		} else if($(this).hasClass('dialog600')){
			var options = {
				autoOpen: false,
				modal: true,
				height: 600,
				width: 600
			}
		} else if($(this).hasClass('dialog750')){
			var options = {
				autoOpen: false,
				modal: true,
				height: 750,
				width: 750
			}
		} else {
			var options = {
				autoOpen: false,
				modal: true,
				height: 450,
				width: 450
			}
		}
		$(this).after('<div class="dialog"><iframe width="100%" height="100%" marginWidth="0" marginHeight="0" frameBorder="0" scrolling="auto">This content is not available in your browser, please upgrade.</iframe></div>');
		var dialog = $(this).next();
		dialog.attr("title",$(this).attr("title"));
		dialog.dialog(options);
		$(this).click(function(){
			dialog.dialog("open");
			dialog.find("iframe").attr("src",$(this).attr("href"));
			return false;
		})
	});
	$('div.ui-tabs').tabs();
	$('a[rel*="external"]').click(function(){
		$(this).attr('target','_blank');
	});


});
$.fn.moreItemsToggle = function(desc,max) {
	return this.filter('ul,ol').each(function() {
		var i=0;
		var ul = $(this);
		ul.find('li').each(function(){
			if(i >= max) {
				$(this).hide();
			}
			i++;
		});
		if(i > max) {
			$('<a href="#">More '+desc+'</a>').click(function(){
				ul.find('li').show();
				$(this).remove();
				return false;
			}).appendTo(ul);
		}
	});
}
$.fn.moreWordsToggle = function(desc,max,min) {
	return this.each(function() {
		var words = $.trim($(this).html()).split(' ');
		if(words.length > min){
			var snippit = words.slice(0,min).join(' ');
			var remainder = "";
			var brk = false;
			$.each(words.slice(min,max),function(i,word){
				if(brk == false) {
					snippit = snippit + " " + word;
				} else {
					remainder = remainder + " " + word;
				}
				if(word.substring(word.length-1,word.length) == '.') {
					brk = true;
				}
			});
			remainder = remainder + words.slice(max).join(' ');
			
			$(this).html(snippit.substring(0,snippit.length-1)+"<span>&hellip; </span>");
			$('<a href="#">'+desc+' more</a>').click(function(){
				$(this).parent().html('. ' +  remainder);
				return false;
			}).appendTo($(this).find('span:last'));
		}
	});
}

