;(function($){
	$.fn.superfish = function(op){

		var sf = $.fn.superfish,
			c = sf.c,
			$arrow = $(['<span class="',c.arrowClass,'"> &#187;</span>'].join('')),
			over = function(){
				var $$ = $(this), menu = getMenu($$);
				clearTimeout(menu.sfTimer);
				$$.showSuperfishUl().siblings().hideSuperfishUl();
			},
			out = function(){
				var $$ = $(this), menu = getMenu($$), o = sf.op;
				clearTimeout(menu.sfTimer);
				menu.sfTimer=setTimeout(function(){
					o.retainPath=($.inArray($$[0],o.$path)>-1);
					$$.hideSuperfishUl();
					if (o.$path.length && $$.parents(['li.',o.hoverClass].join('')).length<1){over.call(o.$path);}
				},o.delay);	
			},
			getMenu = function($menu){
				var menu = $menu.parents(['ul.',c.menuClass,':first'].join(''))[0];
				sf.op = sf.o[menu.serial];
				return menu;
			},
			addArrow = function($a){ $a.addClass(c.anchorClass).append($arrow.clone()); };
			
		return this.each(function() {
			var s = this.serial = sf.o.length;
			var o = $.extend({},sf.defaults,op);
			o.$path = $('li.'+o.pathClass,this).slice(0,o.pathLevels).each(function(){
				$(this).addClass([o.hoverClass,c.bcClass].join(' '))
					.filter('li:has(ul)').removeClass(o.pathClass);
			});
			sf.o[s] = sf.op = o;
			
			$('li:has(ul)',this)[($.fn.hoverIntent && !o.disableHI) ? 'hoverIntent' : 'hover'](over,out).each(function() {
				if (o.autoArrows) addArrow( $('>a:first-child',this) );
			})
			.not('.'+c.bcClass)
				.hideSuperfishUl();
			
			var $a = $('a',this);
			$a.each(function(i){
				var $li = $a.eq(i).parents('li');
				$a.eq(i).focus(function(){over.call($li);}).blur(function(){out.call($li);});
			});
			o.onInit.call(this);
			
		}).each(function() {
			var menuClasses = [c.menuClass];
			if (sf.op.dropShadows  && !($.browser.msie && $.browser.version < 7)) menuClasses.push(c.shadowClass);
			$(this).addClass(menuClasses.join(' '));
		});
	};

	var sf = $.fn.superfish;
	sf.o = [];
	sf.op = {};
	sf.IE7fix = function(){
		var o = sf.op;
		if ($.browser.msie && $.browser.version > 6 && o.dropShadows && o.animation.opacity!=undefined)
			this.toggleClass(sf.c.shadowClass+'-off');
		};
	sf.c = {
		bcClass     : 'sf-breadcrumb',
		menuClass   : 'sf-js-enabled',
		anchorClass : 'sf-with-ul',
		arrowClass  : 'sf-sub-indicator',
		shadowClass : 'sf-shadow'
	};
	sf.defaults = {
		hoverClass	: 'sfHover',
		pathClass	: 'overideThisToUse',
		pathLevels	: 1,
		delay		: 800,
		animation	: {opacity:'show'},
		speed		: 'normal',
		autoArrows	: true,
		dropShadows : true,
		disableHI	: false,		// true disables hoverIntent detection
		onInit		: function(){}, // callback functions
		onBeforeShow: function(){},
		onShow		: function(){},
		onHide		: function(){}
	};
	$.fn.extend({
		hideSuperfishUl : function(){
			var o = sf.op,
				not = (o.retainPath===true) ? o.$path : '';
			o.retainPath = false;
			var $ul = $(['li.',o.hoverClass].join(''),this).add(this).not(not).removeClass(o.hoverClass)
					.find('>ul').hide().css('visibility','hidden');
			o.onHide.call($ul);
			return this;
		},
		showSuperfishUl : function(){
			var o = sf.op,
				sh = sf.c.shadowClass+'-off',
				$ul = this.addClass(o.hoverClass)
					.find('>ul:hidden').css('visibility','visible');
			sf.IE7fix.call($ul);
			o.onBeforeShow.call($ul);
			$ul.animate(o.animation,o.speed,function(){ sf.IE7fix.call($ul); o.onShow.call($ul); });
			return this;
		}
	});

})(jQuery);

/* END SUPERFISH */


jQuery(function(){jQuery('div#top_level_nav ul#nav').superfish();});

$(document).ready(function () {$("a.widget_icon").hover(function () {$(this).next('a').addClass("selected");}, function () {$(this).next('a').removeClass("selected");});});




/*  Collapsible Widgets   */
$(document).ready(function() {
  //Start the modalpanel
  //$(document).modalPanel();
  
  //Give functionality to the live widget
  $("div.live_module_set > > div.live_module > div.toggle > a").click(function(e){
    e.preventDefault();
	if ($(this).hasClass("deselected")) {
	  $(this).removeClass("deselected");
	  $(this).addClass("selected");
	  $(this).parent().siblings("div.content").slideToggle(30);
	}
	else if ($(this).hasClass("selected")) {
	  $(this).removeClass("selected");
	  $(this).addClass("deselected");
	  $(this).parent().siblings("div.content").slideToggle(30);
	}
  });
});
/****************************/





/* get twitter feed and insert it into our widget */
$(document).ready( function() {
  if ( $(".get_twitter").length > 0) {
  var twitter_params = $(".get_twitter").attr("title").split('|'); 
  var url = $(".get_twitter").attr("title");
  if (twitter_params[1]) { var howmany = twitter_params[1] - 1; }
  else { howmany = 2; }
  $(".get_twitter").html('<ul></ul>');
  $.getJSON(url, function(data){
	    $.each(data['results'], function(i, item) {
        $(".get_twitter ul").append('<li>' + makelink(item.text) + '<div class="info"><a href="http://twitter.com/' + item.from_user + '/statuses/' + item.id + '" target="_blank">' + relative_time(item.created_at) + '</a> from <strong>' + item.from_user + '</strong></div></li>');
	    $(".get_twitter li a").attr("target","_blank");
	    if ( i == 2 ) { return false; }
	  });
    });
}
});

function relative_time(time_value) {
  var values = time_value.split(" ");
  time_value = values[1] + " " + values[2] + ", " + values[4] + " " + values[3];
  var parsed_date = Date.parse(time_value);
  var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
  var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
  delta = delta + (relative_to.getTimezoneOffset() * 60);
  
  var r = '';
  if (delta < 60) {  r = 'a minute ago'; } 
  else if(delta < 120) { r = 'couple of minutes ago'; } 
  else if(delta < (45*60)) { r = (parseInt(delta / 60)).toString() + ' minutes ago'; } 
  else if(delta < (90*60)) { r = 'an hour ago'; }
  else if(delta < (24*60*60)) { r = '' + (parseInt(delta / 3600)).toString() + ' hours ago'; }
  else if(delta < (48*60*60)) { r = '1 day ago'; } 
  else { r = (parseInt(delta / 86400)).toString() + ' days ago'; }
  return r;
}

function makelink(content) {
  return content.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/, function(m) {return m.link(m); });
}
/****************************/





//Random Images and Quotes

function randomImg(n, prefix) {
	//Generate a random number from 1 to n
	var ranNum = Math.floor(Math.random() * n) + 1;
	
	//Create the random id by concatenating the prefix and the random number
	var id = prefix + ranNum;
	
	if (document.getElementById(id)) {
		document.getElementById(id).style.display = 'inline';
	}
}

function randomQuote(n, prefix) {
  var ranNum1 = Math.floor(Math.random() * n) + 1;
  var id1 = prefix + ranNum1;
  if ($('div#widgets p.' + id1)) { 
    $('div#widgets p.' + id1).css({ "display":"block" }); 
  }
}


function randomQuote2(n, prefix) {
  var ranNum1 = Math.floor(Math.random() * n) + 1;
  var ranNum2 = Math.floor(Math.random() * n) + 1;
  
  if (ranNum2 == ranNum1) { ranNum2 = ranNum1 - 1; }
  if ( ranNum2 == 0 ) { ranNum2 = n; }
  
  var id1 = prefix + ranNum1;
  var id2 = prefix + ranNum2;
  
  if ($('div#widgets p.' + id1)) {
    $('div#widgets p.' + id1).css({ "display":"block" });
  }
  if ($('div#group_quote p.' + id2)) {
    $('div#group_quote p.' + id2).css({ "display":"block" });
  }
}
/****************************/






/****** flickr widget ******/
$(document).ready( function() {
  if ( $(".get_flickr").length > 0) {
    var photoURL = $(".get_flickr").attr('title');
    $.getJSON(photoURL, function(data){ 
      var data_random = data.items.sort(function() {return 0.5 - Math.random()})
      $.each(data_random, function(i,item){
        var image_small = item.media.m.replace("_m.","_s.");
        $(".get_flickr").append('<div class="flickr_group"><a href="' + item.link + '" target="_blank" title="' + item.title + '" ><img src="' + image_small + '" alt="' + item.title + '" /></a></div>');
        if ( i == 5 ) return false;
      });
    });
  }
});
/****************************/