
// Pop a url into a new window
function popout (url, w, h)
{
	if (!w)
		w = 500;
	if (!h)
		h = 400;
	window.open(url, "", "width="+w+", height="+h+", location=no, resizable=yes, scrollbars=yes, toolbar=no");
	return false;
}
// Pop an element into a new window (overwrites)
function popouta (e, w, h, name)
{
	var width = 400;
	var height = 400;
	if (w) width = w;
	if (h) height = h;
	var url = window.location;
	if (e) {
		if (e.href)
			url = e.href;
		else if (e.src)
			url = e.src;
		else
			url = e;
	}
	if (!name) name = 'help';
	window.open(url, name, "width="+width+", height="+height+", location=no, resizable=yes, scrollbars=yes, toolbar=no");
	return false;
}

/** Tab interface **/
$(function () {	
	
	/** Set first page title **/
	var t = $('.page_collapsed .jcarousel-tabs a:first').attr('title');
	if (t) {
		$('#panel_title').text(t);
		$('#panel_title').siblings('.pipe').show();
	}
	
	/** Setup page tabs **/
	if ($.fn.tabs) {
		
		/** Setup left/right arrows **/
		var wins = $('.page_collapsed .window').addClass('window_clip');
		var arrows = $('<div class="goleft"><a href="#" title="go left">go left</a></div><div class="goright"><a href="#" title="go right">go right</a></div>');
		wins.after(arrows);
	
		var menu = $('.page_collapsed .jcarousel-tabs');
		var menulinks = $('a',menu);
		var goleft = $('.page_collapsed .window').siblings('.goleft');
		var goright = $('.page_collapsed .window').siblings('.goright');
		var panels = $('.page_collapsed .jcarousel-panels');
		// fix page load animations
		panels.css('left', -5);
		
		menu.tabs(1, {
			fxCarousel: true,
			//fxOffset: 10,
			fxSpeed: 900,
			fxEasing: 'easeOutQuart',
			bookmarkable: true,
			click: function (clicked, show, hide) {
				
				// Set the title
				var title = $(clicked).attr('title') || '';
				if (title) {
					$('#panel_title').siblings('.pipe').show();
					$('#panel_title').text(title);
					try {
						var t = document.title.replace(/ \/ .*/,'');
						document.title = t + ' / '+title;
					} catch (E) { }
				} else {
					$('#panel_title').siblings('.pipe').hide();
				}
				
				var self = $.ui.tabs.getInstance(menu.get(0));
				var newindex = self.$tabs.index(clicked);
				
				// hide right arrow
				if (self.$tabs.length <= newindex+1) {
					goright.hide();
				} else {
					goright.show();
					goright.unbind('click.ui-tabs')
						.bind('click.ui-tabs', 
							function(e){ menulinks.eq(newindex+1).trigger('click', [true]); this.blur(); return false; });
				}
				// hide left arrow
				if (newindex <= 0) {
					goleft.hide();
				} else {
					goleft.show();
					goleft.unbind('click.ui-tabs')
						.bind('click.ui-tabs', 
						function(e){ menulinks.eq(newindex-1).trigger('click', [true]); this.blur(); return false; });
				}
			},
			
			panelClass: 'content-panel',
			hideClass: 'content-panel-hidden',
			selectedClass: 'active'
		});
		
		$('#founders > ul.people-tabs').add('#people > ul.people-tabs').tabs(3, {
			fxFade: true,
			fxSpeed: 200,
			panelClass: 'people-tab',
			hideClass: 'people-tab-hidden',
			selectedClass: 'active',
			bookmarkable: false
		});
	}
	
	
	///# Setup page links to use javascript internal hash tabs
	
	var l = $('.page_collapsed .jcarousel-tabs a');
	var v = $('a');
	//console.log('Found '+v.length+' links on the page');
	v.not(l).each( function(){
		// check for hash mark, if found also check that pathname is the same
		if (this.href && this.href.indexOf('#') != -1 && 
				( this.pathname == location.pathname || '/'+this.pathname == location.pathname ) &&
				this.search == location.search) {
			var hash = this.hash.replace('#', '');
			var lnk = $('.page_collapsed .jcarousel-tabs a[href="'+this.hash+'"]');
			if (lnk.length) {
				//console.log('Changed click for link to:' +this.href);
				$(this).bind('click.tabbed-link', function(e) {
					lnk.trigger('click', [true]);
					this.blur();
					return false;
				});
			}
		}
	});
});


/** Header menu rollover setup **/
var header_menu_text_hider = 0;
var header_menu_text_shower = 0;
function header_menu_text_hide () {
	$('#header_menu_text').fadeOut();
}
function header_menu_text_show () {
	$('#header_menu_text').fadeIn();
	header_menu_text_endhide();
}
function header_menu_text_startshow () {
	header_menu_text_shower = setTimeout('header_menu_text_show()', 200);
}
function header_menu_text_starthide () {
	clearTimeout(header_menu_text_hider);
	clearTimeout(header_menu_text_shower);
	header_menu_text_hider = setTimeout('header_menu_text_hide()', 500);
}
function header_menu_text_endhide () {
	clearTimeout(header_menu_text_hider);
}
function header_menu_text_change (id) {
	$('#header_menu_text li').not('#header_menu_text_'+id).hide();
	$('#header_menu_text_'+id).show();
}
$(function () {
	// wipe the slate clean
	$('#header_menu_text li').hide();
	// setup actions
	$('#header_menu_text').hover(header_menu_text_endhide, header_menu_text_starthide);
	$('#header li').not('#header_client_login').children('a').removeAttr('title').hover(function () {
			header_menu_text_change($(this).parent().get(0).id);
			header_menu_text_startshow();
		}
		, header_menu_text_starthide);
});