window.addEvent('domready', function(){

	$$('.clicker').each( function(el){
		new DivClicker(el);
	});
	$$('#breadcrumbs .bookmarks').each( function(el){ insertBookmarks(el); });

	$$('.imageviewer').each( function(el){
		new ImageViewer(el);
	});

	checkFlashMovies();

	// Google Analytics
	if(typeof urchinTracker == "function"){
		_uacct = "UA-356557-4";
		urchinTracker();
	}

});

function checkFlashMovies(){

	if ($('speakingoutvideo') && $('speakingouturl')) {
		var speakingouturl = $('speakingouturl').value;
		var speakingout = new SWFObject(speakingouturl, 'speakingout_movie', '210', '183', '9');
		speakingout.addParam('quality', 'high');
		speakingout.addParam('salign', '');
		speakingout.addParam('wmode', 'transparent');		
		speakingout.addParam('allowFullScreen', 'true');
		speakingout.addParam('allowScriptAccess', 'always');
		speakingout.write('speakingoutvideo');
	}

}

function insertBookmarks(el){
	var url = escape(window.location.href);
	var title = escape(document.title);
	new Ajax('/bookmarkme.html', {
		method: 'get',
		update: el,
		onComplete: function(){
			el.getElements('a').each( function(link){
				link.href = link.href.replace('[[[url]]]', url).replace('[[[title]]]', title);
			});
		}
	}).request();
}

var DivClicker = new Class({

	initialize: function(el){
		this.element = el;
		el.removeEvents();
		el.addEvent('click', this.goThere.bindWithEvent(this))
		.addEvent('mouseover', this.addHover.bind(this))
		.addEvent('mouseout', this.removeHover.bind(this));
	},

	goThere: function(e){
		var dis = $(e.target);
		if (this.element.hasClass('jeffoverride')) {
			this.jeffoverride(dis);
		} else if (dis.getTag() != 'a') {
			var url = this.element.getElements('a')[0].getProperty('href');
			window.location.href = url;
		}
	},
	
	addHover: function(){
		this.element.addClass('hover');
	},
	
	removeHover: function(){
		if (this.element.hasClass('hover')) {
			this.element.removeClass('hover');
		}
	},
	
	jeffoverride: function(el) {
		var url = this.element.getElements('a')[0].getProperty('href');
		if(daWindow == null || daWindow.closed){
			daWindow = window.open(url, null, "height=420,width=440,menubar=no,toolbar=no,location=no,status=yes,resizable=yes,scrollbars=yes");
		}else{
			daWindow.location.href = url;
			daWindow.focus();
		}
	}

});
