
function basename(path) {
    return path.replace(/\\/g,'/').replace( /.*\//, '' );
}
 
function dirname(path) {
    return path.replace(/\\/g,'/').replace(/\/[^\/]*$/, '');;
}

function urlEncode(s) {
     return encodeURIComponent( s ).replace( /\%20/g, '+' ).replace( /!/g, '%21' ).replace( /'/g, '%27' ).replace( /\(/g, '%28' ).replace( /\)/g, '%29' ).replace( /\*/g, '%2A' ).replace( /\~/g, '%7E' );
}

function urlDecode(s) {
     return decodeURIComponent( s.replace( /\+/g, '%20' ).replace( /\%21/g, '!' ).replace( /\%27/g, "'" ).replace( /\%28/g, '(' ).replace( /\%29/g, ')' ).replace( /\%2A/g, '*' ).replace( /\%7E/g, '~' ) );
}

//+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com/array/shuffle [v1.0]

function shuffle (o) {
	for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
	return o;
};

$(document).ready( function() {

	// Section headings
	$('.heading').click( function() {
		var $this = $(this);
	 	var $next = $this.next();
		$next.toggle();
		// Arrow image
		var source = $this.find('img').attr('src');
		var filename = ($next.css('display') == 'none') ? 'right_arrow.png' : 'down_arrow.png';
		$this.find('img').attr('src', dirname(source)+'/'+filename);
	});
	
	// Screen
	var $screen = $('.screen');
	if ($screen.length > 0) {
		$screen.screen();
		$('#screens').height(screen_registry[screen_index].height()+'px');
		$('#Screen_Background_Image').height(screen_registry[screen_index].height()+'px');
		if (window.location.hash.length > 1) {
			var hash = window.location.hash.substr(1);
			changeScreenToTitle(hash);
		}
	}
	
	// Vectors nav
	$('#vectors_explore').mouseover(function(){ $(".pulldown-content",this).show(); });
	$('#vectors_explore').mouseout(function(){ $(".pulldown-content",this).hide(); });
	$('.pulldown-content li').each(function() {
		$this = $(this);
		// Rolleover effect
		$this.mouseover(function() {
			$(this).addClass('sel');
		});
		$this.mouseout(function() {
			$(this).removeClass('sel');
		});		
		// Make LI clickable
		var url = $this.find('a').attr('href');
		$this.click(function(event) {
			document.location.href = url;
			event.stopPropagation();
		});
	});
	
	// Project nav
	// TODO: move this to a jQuery plugin
	if ('undefined'!=typeof(projects) && 'undefined'!=typeof(ProjectNav) && 'undefined'!=typeof(project_id)) {
		//projects = shuffle(projects);
		
	  	var project_nav = new ProjectNav(464, 18);
	  	for (var j = 0; j < projects.length; j++ ) {
			if ('undefined'==typeof(projects[j])) continue;
	  		project_nav.addElement(projects[j].title, projects[j].url);
	  		if (projects[j].project_id == project_id) project_nav.currentElement = j;
	  	}
	  	project_nav.titleText('Memory');
	  	project_nav.titleUrl('/issues/index.php?issue=6');
	  	project_nav.titleWidth(44);
	  	project_nav.insertInto('project_nav');
	}

});

// http://engineeredweb.com/blog/09/12/preloading-images-jquery-and-javascript
(function($) {
  var cache = [];
  $.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
})(jQuery);

(function($) {

	$.fn.screen = function() {

	   	return this.each(function() {  
  
			var $this = $(this);          
			if ('undefined'==typeof(screen_registry)) {
				screen_registry = new Array;
				screen_index = 0;
			}
			screen_registry.push($this);

			init();
		
			function init() {
				var my_li = document.createElement('li');
				my_li.innerHTML = '<a class="hl'+(($this.hasClass('initial_screen'))?' sel':'')+'" href="#'+urlEncode($this.attr('title'))+'" onclick="changeScreenTo('+(screen_registry.length-1)+');">'+$this.attr('title')+'</a>';
				$('#Screen_Nav').append(my_li);
				if ($this.hasClass('initial_screen')) screen_index = (screen_registry.length-1);
			}	
		
		});

	} // $.fn.texteo

})(jQuery);

function changeScreenToTitle(myTitle) {

	var myTitle = urlDecode(myTitle);
	
	for (var j = 0; j < screen_registry.length; j++) {
		if (screen_registry[j].attr('title') == myTitle) {
			if (screen_registry[j].attr('alt') && screen_registry[j].attr('alt').length > 0) return;
			changeScreenTo(j);
			return;
		}
	}

}

// TODO: place this into the jQuery plugin
function changeScreenTo(myIndex) {

	if (myIndex == screen_index) return;

	// Grab to and from screens
	
	var $from = screen_registry[screen_index];
	var $to = screen_registry[myIndex];
	var to_height = $to.height();

	// Alternatively, screen link can point to another URL using alt=""

	if ($to.attr('alt') && $to.attr('alt').length > 0) {
		document.location.href = $to.attr('alt');
		return;
	}
	
	// Change the height of the screen
	
	$('#screens').animate({
	    height: to_height+'px'
	  }, 2000);	

	// Change the height of the background image, but only if one of the other requires it

	if (!$to.hasClass('screen_background') || !$from.hasClass('screen_background')) {
		$('#Screen_Background_Image').height($from.height());
		$('#Screen_Background_Image').animate({
		    height: to_height+'px'
		}, 2000, function() {
			if (screen_registry[screen_index].hasClass('screen_background')) {
				$('#Screen_Background_Image').hide();
			}
		});	
	}
	
	// Element must be display'd in order to get height, so using visibility
	// TODO: this might not be needed afterall
	
	$to.css('display', 'none');
	$to.css('visibility', 'visible');
	
	// Fade screens
	
	$from.fadeOut(1500);
	$to.fadeIn(2000);
	
	// Set index and links
	
	screen_index = myIndex;
	
	$('#Screen_Nav').find('a').removeClass('sel');
	$('#Screen_Nav').find("a:contains("+$to.attr('title')+")").addClass('sel').blur();
	
}  // see http://addictedtonew.com/archives/414/creating-a-jquery-plugin-from-scratch/

function JournalNav (myWidth, myHeight, myDefaultText) {

  this.instance_index = null;
  this.width = 0;
  this.height = 0;
  this.defaultText = '';
  this.options = new Array;
  this.container_div = null;
  this.options_are_on = false;
  this.mouseover_state = false;

  this.constructor   = constructor;
  this.addOption     = addOption;
  this.insertInto    = insertInto;
  this.getHeader     = getHeader;
  this.getOption     = getOption;
  this.drawOptions   = drawOptions;
  this.autoCheckMouseoverState = autoCheckMouseoverState;
  this.removeOptions = removeOptions;

  this.constructor(myWidth, myHeight);

  function constructor(myWidth, myHeight) {

    this.width = parseInt(myWidth);
    this.height = parseInt(myHeight);

    this.instance_index = parseInt(JournalNav.instances.length);
    JournalNav.instances[this.instance_index] = this;

  } // constructor

  function addOption(myText, myUrl) {

    var myOption = new Object;
    myOption.text = myText;
    myOption.url = myUrl;

    this.options[this.options.length] = myOption;

  }

  function insertInto(myDiv) {

    var divObj = document.getElementById(myDiv);
    if (!divObj) return false;

    var myHeader = this.getHeader();

    divObj.appendChild(myHeader);

  }

  function getHeader() {

    // container div

    var headerBgColor = '#eeeeee';
    var headerMoColor = '#e5e5e5';

    var myDiv = document.createElement("DIV");
    myDiv.id = 'JournalNav_'+this.instance_index;
    myDiv.style.backgroundColor = headerBgColor;
    myDiv.style.height = this.height;
    myDiv.style.width = this.width;
    myDiv.style.padding = '0px 0px 0px 0px';
    myDiv.style.position = 'relative';
    myDiv.style.top = '0px';
    myDiv.style.left = '0px';
    myDiv.style.zIndex = '0';
    myDiv.pass_instance_index = this.instance_index;
    myDiv.onmouseover = function() {JournalNav.instances[this.pass_instance_index].drawOptions(); this.style.backgroundColor=headerMoColor; JournalNav.instances[this.pass_instance_index].mouseover_state=true;}
    myDiv.onmouseout = function() {this.style.backgroundColor=headerBgColor; JournalNav.instances[this.pass_instance_index].mouseover_state=false;}
    myDiv.style.cursor = 'pointer';

    // text div (positioned absolutely so that the user increasing font size doesn't break the box)

    var myDiv2 = document.createElement("DIV");
    myDiv2.style.zIndex = '2';
    myDiv2.style.position = 'absolute';
    myDiv2.style.top = '2px';
    myDiv2.style.left = '6px';
    myDiv.style.fontFamily = 'Verdana';
    myDiv2.style.fontSize = '11px';
    myDiv2.innerHTML = 'Explore the Vectors Journal';
    myDiv2.style.letterSpacing = '0';
    myDiv2.pass_instance_index = this.instance_index;
    myDiv2.onmouseover = function() {this.parentNode.style.backgroundColor=headerMoColor;JournalNav.instances[this.pass_instance_index].mouseover_state=true;}
    myDiv2.onmouseout = function() {this.parentNode.style.backgroundColor=headerBgColor;JournalNav.instances[this.pass_instance_index].mouseover_state=true;}
	myDiv.appendChild(myDiv2);

	// vertical line
	myLine = document.createElement("DIV");
	myLine.style.zIndex = '3';
	myLine.style.width = '1px';
	myLine.style.height = this.height;
	myLine.margin = '0px 0px 0px 0px';
	myLine.padding = '0px 0px 0px 0px';
	myLine.overflow = 'hidden';
	myLine.style.backgroundColor = '#cccccc';
	myLine.style.position = 'absolute';
	myLine.style.right = '21px';
	myLine.style.top = '0px';
	myDiv2.onmouseover = function() {this.parentNode.style.backgroundColor=headerMoColor;}
    myDiv2.onmouseout = function() {this.parentNode.style.backgroundColor=headerBgColor;}

	myDiv.appendChild(myLine);

	// down arrow
	myArrow = new Image();
	myArrow.src = '/common/js/JournalNavDownArrow.png';
	myArrow.style.width = '9px';
	myArrow.style.height = '5px';
    myArrow.style.position = 'absolute';
    myArrow.style.top = (this.height / 2) - 2;
    myArrow.style.right = '6px';
    myDiv2.onmouseover = function() {this.parentNode.style.backgroundColor=headerMoColor;}
    myDiv2.onmouseout = function() {this.parentNode.style.backgroundColor=headerBgColor;}

	myDiv.appendChild(myArrow);

    this.container_div = myDiv;

    return myDiv;

  }

  function getOption(optionsIndex) {

    var optionBgColor = '#f3f3f3';
    var optionMoColor = '#e5e5e5';

    var myDiv = document.createElement("DIV");
    myDiv.id = 'JournalNavOption_'+this.instance_index+'_'+optionsIndex;
    myDiv.style.height = this.height;
    myDiv.style.width = this.width - 2;
    myDiv.style.borderWidth = '1px';
    myDiv.style.borderColor = '#dddddd';
    myDiv.style.borderStyle = 'solid';
    myDiv.style.backgroundColor = optionBgColor;
    myDiv.pass_href = this.options[optionsIndex].url;
    myDiv.onmousedown = function() {document.location.href = myDiv.pass_href;}
    myDiv.pass_id = myDiv.id;
    // not sure why this isn't working:
    // myDiv.onmouseover = function() {this.style.fontFamily='Arial';}
    // myDiv.pass_instance_index = this.instance_index;
    myDiv.style.zIndex = '21';
    myDiv.style.position = 'absolute';
    myDiv.style.top = '0px';
    myDiv.style.left = '0px';
    myDiv.style.padding = '0px 0px 0px 0px';
    myDiv.style.textAlign = 'left';

    var myDiv2 = document.createElement("DIV");
    myDiv2.style.margin = '2px 1px 2px 6px';
    myDiv2.style.padding = '0px 0px 0px 0px';
    myDiv2.style.fontFamily = 'Verdana';
    myDiv2.style.fontSize = '11px';
    myDiv2.style.textAlign = 'left';
    myDiv2.style.zIndex = '22';
    myDiv2.innerHTML = this.options[optionsIndex].text;

    myDiv.onmouseover = function() {this.style.backgroundColor=optionMoColor;}
    myDiv.onmouseout  = function() {this.style.backgroundColor=optionBgColor;}

    myDiv.appendChild(myDiv2);

    return myDiv;

  }

  function drawOptions() {

    if (this.options_are_on) return;

    this.options_are_on = true;

    for (var a = 0; a < this.options.length; a++) {
      var myOption = this.getOption(a);
      myOption.style.top = parseInt(this.height) * (a + 1);
      this.container_div.appendChild(myOption);
    }

    setTimeout("JournalNav.instances["+this.instance_index+"].autoCheckMouseoverState();", 1000);

  }

  function autoCheckMouseoverState() {

    if (!this.options_are_on) return;

    if (!this.mouseover_state) {
      this.removeOptions();
    } else {
      setTimeout("JournalNav.instances["+this.instance_index+"].autoCheckMouseoverState();", 1000);
    }

  }

  function removeOptions() {

    if (!this.options_are_on) return;

    this.options_are_on = false;

    for (var a = 0; a < this.options.length; a++) {
		var optionId = 'JournalNavOption_'+this.instance_index+'_'+a;
		var optionObj = document.getElementById(optionId);
		if (optionObj) {
		  optionObj.parentNode.removeChild(optionObj);
		}
    }

  }

} // JournalNav

JournalNav.instances = [];

// TODO: Legacy function...

function popUpWin() {
  var tools = '';
  if (browser_is_custom == '1') {
    var myChrome = (browser_disp_chrome == 1) ? 'yes' : 'no';
	var tools="toolbar="+myChrome+",statusbar="+myChrome+",location="+myChrome+",menubar="+myChrome+",";
    if (browser_width == '0' && browser_height == '0') {
		var strWidth = screen.availWidth - 10;    // add a very small amount of wiggle room
		var strHeight = screen.availHeight - 10;  // add a very small amount of wiggle room
		tools += "scrollbars=yes,resizable=yes,width="+strWidth+",height="+strHeight+",top=0,left=0";
    } else {
	  var x = (screen.availWidth - browser_width) /2;
	  var y = (screen.availHeight - browser_height)/3;
	  tools += "scrollbars=";
	  tools += ('1' == browser_disp_scrollbars) ? 'yes' : 'no';
	  tools += ",resizable=yes,width="+browser_width+",height="+browser_height+",left="+x+",top="+y+"";
    }
  }
  newWindow = window.open(project_url, '_blank',tools);
  newWindow.focus();
}

