//Globals
var GAvars = false;
var GMAPKEY = 'ABQIAAAAnxBMm1G-0bE02Riiiqi0BxTRIQPIJifZx1tKk0RXZ5uss04NcBS2bG5lyCI3VTPGsLElv8j_jVDQTA';

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}


// -- GA Tools -- //
function GA_setVar( v ) {
	if (!GAvars) { 
		GAvars = [];
		addLoadEvent( GA_setVars );
	}
	GAvars.push( v );	
}
function GA_setVars() {
	for( var i in GAvars) {
		pageTracker._setVar( GAvars[i] );	
	}
}




// -- Sortable Tables -- //

function sortableTable2(id, o) {
	this.id = id;
	this.table = $i(id);
	this.o = o;
	this.thead = this.table.getElementsByTagName('thead')[0];
	this.tbody = this.table.getElementsByTagName('tbody')[0];
	
	this.sortDirDefault = 'DESC';
	this.sortDirIndex = {DESC:-1, ASC:1}
	this.sortDir = this.o.startDir ? this.o.startDir * -1 : this.sortDirIndex[ this.sortDirDefault ];

	this.headerLinks = {};
	
	this._init();
}
sortableTable2.prototype = {
	
	_init : function() {
		var self = this;
		var sortLink, firstSortLink, index;
		var As = this.thead.getElementsByTagName('a');
		for(var i=0; i < As.length; i++) {
			if (As[i] && As[i].parentNode.tagName == 'TH') {
				As[i].onclick = function() { self.sort(this); }
				index = As[i].parentNode.cellIndex + ( As[i].getAttribute('sortIndexOffset') ? As[i].getAttribute('sortIndexOffset') * 1 : 0 );
				this.headerLinks[index] = As[i];
				
				if (As[i].getAttribute('sortDefault') || this.o.defaultSortIndex == index) {					
					sortLink = As[i];
				}
				if (!firstSortLink) { firstSortLink = As[i]; }
			}
		}
		if (sortLink) { this.sort(sortLink, true); } else {
		this.sort(firstSortLink, null, true); }
	},
	
	sort : function(sortLink, index, skipSorting) { //sortDirDefault
		if (!sortLink) { 
			sortLink = this.headerLinks[ index ];
			alert(index + ' | ' + sortLink);
		}
		
		var sortArray = [];
		var value, valueSecond, i;
		
		var index = sortLink.parentNode.cellIndex + ( sortLink.getAttribute('sortIndexOffset') ? sortLink.getAttribute('sortIndexOffset') * 1 : 0 );
		var indexSecond = sortLink.getAttribute('sortSecondary');
		var numeric = sortLink.getAttribute('sortNumeric');
		
		//remove header styles
		if (this.currentTH) {
			Removeclass( this.currentTH , 'tr_sort_' + this.sortDir );
			img = this.currentTH.getElementsByTagName('img')[0];
			if (img) { 
				this.currentTH.removeChild(img);
			}	
		}
		
		
		//Get Data		
		var TRs = this.tbody.getElementsByTagName('tr');
		for(i=0; i < TRs.length; i++) {
			//set up new classes - remove old classes
			if (this.currentColumn != index) {
				if (this.currentColumn >= 0) {
					Removeclass( TRs[i].cells[this.currentColumn] , 'tr_sorted' );
				}
				Addclass( TRs[i].cells[index] , 'tr_sorted' );
			}
						
			//prepare for sorting
			value = TRs[i].cells[index].getAttribute('sortValue');
			if ( indexSecond ) {
				valueSecond = TRs[i].cells[indexSecond].getAttribute('sortValue');
			}
			if (numeric) { value *= 1; }
			
			sortArray[ i ] = { 
				value		:	(value || value == 0) ? value : TRs[i].cells[index].innerHTML ,
				valueSecond : 	indexSecond ? valueSecond : null,
				tr			:	TRs[i],
				self		:	this
			};
		}
		
		//Sort Data
		if (!skipSorting) {
			if (this.currentColumn == index) {
				this.sortDir *= -1;
			} else {
				this.sortDir = this.sortDirIndex[ sortLink.getAttribute('sortDirDefault') ? sortLink.getAttribute('sortDirDefault') : this.sortDirDefault ];
			}
			
			if (sortLink.getAttribute('sortSecondary')) {
				this.sortSecondary = sortLink.getAttribute('sortSecondary');
				sortArray.sort( this.sort_funcWithDepth );				
			} else {
				sortArray.sort( this.sort_func );
			}
			
			//Rearrange Rows
			for(i=0; i < sortArray.length; i++) {
				this.tbody.appendChild( sortArray[i].tr );	
			}
		
			//Update Table Coloring
			this.reZebra();
		}
		
		//Update header
		Addclass( sortLink.parentNode , 'tr_sort_' + this.sortDir );
		img = new Image();
		img.src = '/images/v2/sort_'+(this.sortDir<0?'up':'down')+'.gif';
		if (!sortLink.parentNode.className.match('tr_ar'))
		{
			sortLink.parentNode.appendChild(img);
		} else {
			sortLink.parentNode.insertBefore(img, sortLink.parentNode.firstChild); 
		}
		
		//Store Currents
		this.currentColumn = index;
		this.currentTH = sortLink.parentNode;
		
		//Event
		if (this.o && this.o.eventSorted) { this.o.eventSorted(this); }
	},
	
	sort_func : function(a,b) {
		return ((a.value < b.value) ? -1 : ((a.value > b.value) ? 1 : 0)) * a.self.sortDir;
	},	
	
	sort_funcWithDepth : function(a,b) { //could use some optimization all over
		var r = ((a.value < b.value) ? -1 : ((a.value > b.value) ? 1 : 0)) * a.self.sortDir;
		if (r == 0) {
			((a.valueSecond < b.valueSecond) ? -1 : ((a.valueSecond > b.valueSecond) ? 1 : 0)) * a.self.sortDir;
		}
		return r;
	},	
	
	reZebra : function() {	
	
		if (this.o.skipAltShading) return;
	
		// do alt shading
		var TRs = this.tbody.getElementsByTagName('tr');
		var n = 0;
		for(var i in TRs) {
			if (TRs[i].tagName == 'TR') {
				if ( n%2 != 0) {
					Addclass( TRs[i] , 'tr_alt' );
				} else {
					Removeclass( TRs[i] , 'tr_alt' );
				}
				n++;
			}
		}
	}
	
}