// spe_web.js
// non jQuery first
function emailArticleLink(title,url) {
    var m = $('mail');
    var sub = 'Recommended article from The Way Ahead';
    sub = sub.replace(/&amp;/g, " and ");
	title = title.replace(/$amp;/g, " and ");
    var tempMail = 'mailto:?subject=' + sub + '&body=' + title + ' (' + url + ')';
    m.action = tempMail;
    m.submit();
}

(function($) { // simulate block scope to use $ for jQuery here only

// Plugin for looping scroller with controls
$.fn.ScrollControl = function(show, settings) { 
	if (!show) show = 1;
	settings = jQuery.extend({
		selector: ".item",
		height: 170,
		time: 5000,
		speed: 2000,
		startdir: 1, // 1 = scroll up, -1 = scroll down
		forward: "#forward",
		reverse: "#reverse",
		pause: "#pause"
	}, settings);

	// Set properties
	var items = $(this).children(settings.selector);
	var ptr = new Array(); // pointer for stack
	var timer;
	var busy = false;
	
	//	Set up animation targets for moving up or down - see initialization
	var lo = new Array();
	var hi = new Array();
	
	// Methods
	// Increment pointer array according to direction
	function pointer(dir) {	
		if (dir==1) {
			ptr.push(ptr[0]);
			ptr.shift();
		}
		else {
			ptr.unshift(ptr.pop());
		}
	};
	// Scroll up one
	function up() {	
		move(hi);
		pointer(1); // Increment after move
		items.eq(ptr[show]).css('top', lo[show]); // Stage next item
	};
	//Scroll down one
	function down() {
		pointer(-1); // Increment first when going down
		items.eq(ptr[0]).css('top', hi[0]);	// Stage next item above
		move(lo);
	};	
	// Common move functionality
	function move(trgt) {
		for (var i=0; i<show; i++) {
			items.eq(ptr[i]).animate({top: trgt[i]}, settings.speed);
		}
		// Do last one separate so "busy" won't turn off until animation completed
		items.eq(ptr[show]).animate({top: trgt[show]}, settings.speed, function() {
			busy = false;
		});				
	}		
	// Do one iteration of the loop
	function doLoop(dir) {
		busy = true;
		if (dir==1) {
			up();
		}
		else {
			down();
		}
	};
	// Start timed loops
	function loopTimer(dir) {
		timer = setInterval(function(){doLoop(dir);},settings.time);
	};	
	// Rotate when clicked according to direction; no response if busy (already moving)
	function changeDir(dir) {
		if (busy==true) {
			return;
		}
		clearInterval(timer);		
		doLoop(dir); // Rotate once immediately
		loopTimer(dir); // Then start timer
	};		
	// Pause button
	 function pause() {
		clearInterval(timer);
	};
	
	// Initialize ===========================================================
	
	for (var i=0; i<=show; i++) {
		hi[i] = (i-1) * settings.height; // array holds positions for going up
		lo[i] = i * settings.height; // array holds postions for going down
	}
	for (var i=0; i<items.size(); i++) {
		ptr[i] = i;
	}
	
	// Place visible items
	items.lt(show).each( function(i){
		items.eq(i).css('top', lo[i])
	}).end().gt(show-1).css('top', lo[show]);
	
	// Set height of the visible container
	$(this).css('height',show * settings.height);
	
	// Add clickable controls
	$(settings.forward).click(function(){ changeDir(1); });
	$(settings.reverse).click(function(){ changeDir(-1); });
	$(settings.pause).click(function(){ pause(); });
	
	// Crank it up
	loopTimer(settings.startdir);
};


// Use this on home page for MySPE portlets
// e.g., jQuery("#div_portlets").SlidePanels()
// Defaults are already set

$.fn.SlidePanels = function(settings) { 
	settings = jQuery.extend({
		content: ".panelContent",
		header: ".panelHeader"
	}, settings);

	$(this).find(settings.content).hide().end().find(settings.header).click(function() {
		var panel = $(this).next();
		if (panel.is(':visible')) {
			panel.slideUp();
		} else {
			panel.slideDown();
      }
	});
};


// jQuery plugin functions for in-page sliders
// Not generalized - #main_content, classes hard coded; need options	
// Need to simplify by requiring ("#main_content h2"), etc. for the object, not just H2
$.fn.nextTag = function(tag) {
	var idx = $(this).siblings().index($(this).prev()[0]);
	return $(this).siblings().gt(idx).filter(tag).eq(0);
};
			 
$.fn.clickToSlide = function(tag, settings) {
	settings = jQuery.extend({
		widget: "widget",
		open: "open",
		closed: "closed",
		outdent: "outdent"
	}, settings);
	var proto = document.createElement("span");
	$(this).each( function() {
		var target = $(this).nextTag(tag);
		target.hide();
		$(proto).clone().addClass(settings.widget).addClass(settings.closed).prependTo($(this));
		$(this).addClass(settings.outdent).click(function() {
			$(target).slideToggle();
			$(this).children("span").toggleClass(settings.closed).toggleClass(settings.open);				
		});
	});		
};	

/* Used in topicSlide below */
$.aname = function() {
	var anker = window.location.hash;
	var pattern = /#(.*)/;
	var arr = pattern.exec(anker);
	if (arr != null) {
		result = arr[1];
	} else {
		result = false;
	}
	return result;
};

/* Like clickToSlide but customized for DL topic pages. Consolidate someday... */
$.fn.topicSlide = function(tag, settings) {
	settings = jQuery.extend({
		widget: "widget",
		open: "open",
		closed: "closed",
		outdent: "outdent"
	}, settings);
	var proto = document.createElement("span");
	var anchorname = $.aname();
//	alert('anchorname from aname() is ' + anchorname);
	$(this).each( function() {
		var nameattr = $("a:first", this).attr("name");
//		alert('Name attr is ' + nameattr );
		var target = $(this).nextTag(tag);
		target.hide();
		$(proto).clone().addClass(settings.widget).addClass(settings.closed).prependTo($(this));
		$(this).addClass(settings.outdent).click(function() {
			$(target).slideToggle();
			$(this).children("span").toggleClass(settings.closed).toggleClass(settings.open);
			$(this).next("h3").toggleClass(settings.open);
		});
//		alert('at if...');
		if (nameattr == anchorname ) {
//		alert('Name and anchorname match!');
		$(this).click();	
		}
	});
};	

// Like clickToSlide, but for bullet lists. Currently collapses all levels.
$.fn.clickList = function(settings) {
	// Every li with a child ul gets a span
	// Will size plain ones by default
	settings = jQuery.extend({
		widget: "widget",
		open: "open",
		closed: "closed"
	}, settings);
	$(this).css("list-style", "none").find("ul").css("list-style", "none");
	var proto = document.createElement("span");
	
	$("li", $(this)).each( function() {
		el = $(proto).clone().addClass(settings.widget).prependTo($(this));
		if ($(this).children("ul").size() > 0) {
			$(this).children("ul").hide();
			var target = $(this).find("ul:first");
			el.click( function() {
				target.toggle();
				$(this).toggleClass(settings.open);
				$(this).toggleClass(settings.closed);
			}).addClass(settings.closed);
		}
	});
};	
// Doc ready function
// Putting this here to start scroller until home page can be changed
// Adding buttons and link from here for now - cool
$(function(){ 			  
	var controls = '<span><img src="/web/images/ctrl_back.gif" border="0" alt="Go Back" id="reverse"><img src="/web/images/ctrl_pause.gif" border="0" alt="Pause" id="pause"><img src="/web/images/ctrl_fwd.gif" border="0" alt="Go Forward" id="forward"></span>';
	$(controls).prependTo("#mtgs_box_btm");
	$("#meeting_list").ScrollControl(1, {height: 170, speed: 2000, time: 5000, selector: ".item" } ); 
});


// end block scope
})(jQuery);