// JavaScript Document
function init () { //put in onload of each page
//create thumbnail object
	thumbShot() ;

//the section below sets our columns to equal heights
	var agg = document.getElementById('aggregated_content');
	var con = document.getElementById('content');
	var per = document.getElementById('personalized_content');
	var maxHeight = 0;
	
	if (agg.offsetHeight >= con.offsetHeight && agg.offsetHeight >= per.offsetHeight) {
		var maxHeight = agg.offsetHeight;
	}else if(con.offsetHeight >= agg.offsetHeight && con.offsetHeight >= per.offsetHeight) {
		var maxHeight = con.offsetHeight;
	}else if(per.offsetHeight >= agg.offsetHeight && per.offsetHeight >= con.offsetHeight) {
		var maxHeight = per.offsetHeight;
	}
	
	agg.style.height = maxHeight + 'px';
	con.style.height = maxHeight + 'px';
	per.style.height = maxHeight + 'px';

//the section below sets our sub_nav widths equal
	var sub_nav = document.getElementById('sub_nav') ;
	if (sub_nav) {
		var areas = sub_nav.getElementsByTagName('ul')[0].li ;
		
		if (document.all || getElementsByClass('areas', areas, 'ul').length > 1) {
			var desiredWidth = sub_nav.offsetWidth - 12 + 'px' ;
		} else {
			var desiredWidth = sub_nav.offsetWidth - 13 + 'px' ;
		}
	
		for (var i = 0; i < getElementsByClass('areas', areas, 'ul').length; i++) {
			getElementsByClass('areas', areas, 'ul')[i].style.width = desiredWidth ;
		}
		
		sub_nav.style.paddingRight = 0 ;
	}
}

function thumbShot() {
	//create HTML elements - t stands for "thumb"
	t_div = document.createElement('div');
	t_a = document.createElement('a');
	t_img = document.createElement('img');

	//set some properties
	t_img.alt = '' ;
	t_img.title = '' ;

}

function thumbDisplay (wrapper, website) {
	//define variables
	var image = 'http://open.thumbshots.org/image.pxf?url=' + website ;
	var website = decodeURIComponent(website) ;
	t_wrapper = wrapper ;
		
	//set the attributes of the HTML elements
	t_a.setAttribute('href', website);
	t_img.src = image ;
	t_img.alt = '' ;
	t_img.title = '' ;
	t_img.border = '1' ;
	t_img.onload = function(z) {
		z = z || window.event;
		thumbImageCheck() ;
	}

	t_div.id = 'thumb_container' ;
		
	//handle mouse events
	wrapper.onmouseover = '' ; //this prevents the script from triggering over and over again
	t_div.onmouseout = function(z) {
		z = z || window.event;
		thumbDissolve(website) ;
	}

	//add elements to the page
	t_a.appendChild(t_img);
	t_div.appendChild(t_a);
	wrapper.appendChild(t_div);
}
	
function thumbDissolve(website) {
	t_wrapper.onmouseover = function(e) {
		e = e || window.event;
		website = encodeURIComponent(website) ;
		thumbDisplay(this, website) ; //restore mouseover event
	}

	t_a.removeChild(t_img) ;
	t_div.removeChild(t_a) ;
	t_wrapper.removeChild(t_div) ;
	
	t_a.setAttribute('href', '') ;
	t_img.src = '' ;
	t_img.border = '0' ;
}
	
function thumbImageCheck() {
	if (t_img.width < 20) {
		t_a.removeChild(t_img) ;
		t_img.src = 'http://files.cok.net/images/preview_not_available.png' ;
		t_a.appendChild(t_img) ;
	}
}	

/*function thumbShot (wrapper, website) {
	//variables
	var div = document.createElement('div');
	var a = document.createElement('a');
	var img = document.createElement('img');

	//setting the attributes of our new creations
	a.setAttribute('href', website);
	img.setAttribute((document.all ? 'className' : 'class'), 'thumbnail') ;
	img.src = 'http://open.thumbshots.org/image.pxf?url=' + website ;
	img.alt = '' ;
	img.title = '' ;
	img.border = '0' ;
	img.onload = function(x) {
		x = x || window.event ;
		if (this.width < 20) {
			this.src = 'http://files.cok.net/images/preview_not_available.png' ;
		}
	}
	//adding creations into the page
	a.appendChild(img);
	div.appendChild(a);
	wrapper.appendChild(div);
}*/

function getElementsByClass(searchClass,node,tag) { /* Created by: Dustin Diaz :: http://www.dustindiaz.com/ */
	var classElements = new Array();
	if (node == null)
		node = document;
	if (tag == null)
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if (pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

function showMap(a) {
	//variables
	var wrapper = a.parentNode;
	var map_wrapper = document.createElement('div');
	var br = document.createElement('br');
	
	//changing behavior of the link
	a.innerHTML = 'Click to close map';
	a.onclick = function(e) {
		e = e || window.event;
		closeMap(this);
	}

	//setting the attributes of our new creations
	map_wrapper.setAttribute((document.all ? 'className' : 'class'), 'map_wrap');
	map_wrapper.setAttribute('id', 'map_wrap');
	
	//adding creations into the page
	wrapper.appendChild(br);
	wrapper.appendChild(map_wrapper);

	//call function which actually maps address
	showAddress(a.name);
}

function closeMap(x) {
	var wrapper = x.parentNode;
	wrapper.removeChild(x.nextSibling);
	wrapper.removeChild(x.nextSibling);
	x.onclick = function(e) {
		e = e || window.event;
		showMap(this);
	}	
	x.innerHTML = 'Click to show map';
}


function showAddress(address) {
	var addy = new String(address) ;
	var underscore = new RegExp("_", "g") ;
	var tripleDash = new RegExp("---", "g") ;
	var pound = new RegExp("-pound-", "g") ;
	var amp = new RegExp("-amp-", "g") ;
	addy = addy.replace(underscore, " ") ;
	addy = addy.replace(tripleDash, ", ") ;
	addy = addy.replace(pound, "#") ;
	addy = addy.replace(amp, "&amp;") ;
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map_wrap"));
		geocoder = new GClientGeocoder();
		if (geocoder) {
			geocoder.getLatLng(
				addy,
				function(point) {
					if (!point) {
						alert("GoogleMaps could not find:\n" + addy);
					} else {
						map.addControl(new GSmallMapControl());
						map.addControl(new GMapTypeControl());
						map.setCenter(point, 13);
						var marker = new GMarker(point);
						map.addOverlay(marker);
					}
				}
			);
		}
	} else {
		alert("Your browser is not capable of using the GoogleMaps feature.\n\nWe suggest the FireFox browser:\nhttp://www.getfirefox.com");
	}
}

// Creates a marker at the given point with the given number label
/*function createMarker(point, number) {
	var marker = new GMarker(point);
	GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml("Marker #<b>" + number + "</b>");
	});
return marker;
}
*/