if (!katani) var katani = {};

katani.newseventpost = function() {

	var youtuberegexp = /^youtube ([A-Za-z\d_-]{11})$/;

	function init() {

		var newseventpost = $('col3newsmiddle');
		if (!global.dom.isnodecomplete(newseventpost)) {
			// reset timeout to try again later
			window.setTimeout(init,20);
			return;
		}

		// get a list of all <div> tags, and look for youtube embed signatures
		// need to use embedlist, since divlist is a live node list - embedding while in the for() loop will update the live nodelist
		var divlist = newseventpost.getElementsByTagName('div'),
			embedlist = [];

		for (var i = 0,j = divlist.length;i < j;i++) {
			var item = divlist[i],
				match = youtuberegexp.exec(item.className);

			if (!match) continue;
			embedlist[embedlist.length] = [match[1],item.firstChild.id];
		}

		for (var i = 0,j = embedlist.length;i < j;i++) {
			// embed flash player for youtube video
			var item = embedlist[i];
			swfobject.embedSWF('http://www.youtube.com/v/' + item[0],item[1],'480','295','8');
		}
	}

	// detect required flash version for youtube embeds
	// if flash not found add marker to <html> class attribute, we will quit now and not run init()
	if (!swfobject.hasFlashPlayerVersion('8')) {
		global.addhtmlclass('noflash');
		return;
	}

	// init blogpost youtube embed routines
	init();

	// set class="js" on <html> tag
	global.addhtmlclass('js');
}();


katani.newseventsocial = function() {

	var obj = {},
		event = global.event,
		dom = global.dom,
		onloadfired,

		shortenurlinprogress,
		twitterstatusupdate,
		twitterwindowref,
		twitterfindurlregexp = /http%3A[^+]+/,
		twitterstatusurlprefix = 'http://twitter.com/home?status=',

		// http://bit.ly/ account details (login/API key)
		bitly = {
			login: 'katani',
			apikey: 'R_991eb96a1075b460a9dd740bbbf6d7a1'
		};

	function createwindow(targeturl,name,width,height) {

		return window.open(targeturl,'sharepost' + name,'width=' + width + ',height=' + height + ',toolbar=no,status=no');
	}

	function builddeliciousclick(targeturl) {

		return function(e) {

			event.preventdefault(e);

			// extract our URL from delicious save URL
			var match = /\?url=(.+)$/.exec(targeturl);
			if (!match) return;

			createwindow('http://delicious.com/save?v=5&noui&jump=close&url=' + match[1],'delicious',550,550);
		};
	}

	obj.shortenurlcallback = function(data) {

		shortenurlinprogress = false;
		if (data.status_txt != 'OK') return;

		// replace original twitter status text URL with short URL
		twitterstatusupdate = twitterstatusupdate.replace(twitterfindurlregexp,encodeURIComponent(data.data.url));

		// redirect the location href of the popup window to the twitter status update page
		twitterwindowref.location.href = twitterstatusurlprefix + twitterstatusupdate;
	};

	function shortenurl(siteurlencoded) {

		// note: don't need to URL encode the given URL, as it is already encoded server side
		var requesturl =
			'http://api.bit.ly/v3/shorten?' +
			'callback=katani.newseventsocial.shortenurlcallback&' +
			'format=json&' +
			'login=' + bitly.login + '&' +
			'apiKey=' + bitly.apikey + '&' +
			'longUrl=' + siteurlencoded;

		// embed javascript include to make the call to bitly
		var el = document.createElement('script');
		el.type = 'text/javascript';
		el.src = requesturl;
		document.body.appendChild(el);

		// raise flag to say URL shorten is in progress
		shortenurlinprogress = true;
	}

	function popuptwitterwindow(url) {

		return createwindow(url,'twitter',810,500);
	}

	function buildtwitterclick(targeturl) {

		return function(e) {

			event.preventdefault(e);

			// if currently in the process of fetching a short URL exit now to prevent multiple clicks
			if (shortenurlinprogress) return;

			if (twitterstatusupdate) {
				// we already have our short URL twitter update built - so just re popup the twitter status update page
				popuptwitterwindow(twitterstatusurlprefix + twitterstatusupdate);
				return;
			}

			// save original twitter status update text from URL
			var match = /\?status=(.+)$/.exec(targeturl);
			if (!match) return;
			twitterstatusupdate = match[1];

			// extract URL from twitter status update text - note URL has already been encoded server side
			// regular expression twitterfindurlregexp assumes URL starts with "http:" and ends with a space/end of string
			match = twitterfindurlregexp.exec(twitterstatusupdate);
			if (!match) return;

			// shorten siteurl using http://bit.ly/ and wait for return callback to public katani.newseventsocial.shortenurlcallback() method
			shortenurl(match[0]);

			// popup the twitter window as a blank window and then change to the twitter URL after http://bit.ly/ has returned a shorturl result
			// we do this, since attempting to open a popup window after http://bit.ly/ returns and calls the katani.newseventsocial.shortenurlcallback()
			// method will result in the browsers popup window blocker kicking in since we haven't fired window.open directly off of a button click
			twitterwindowref = popuptwitterwindow('');
		};
	}

	function buildfacebookclick(targeturl) {

		return function(e) {

			event.preventdefault(e);
			createwindow(targeturl,'facebook',626,436);
		};
	}

	function init() {

		var socialel = $('social');
		if (!dom.isnodecomplete(socialel)) {
			// if window onload has fired, then #social does not exist in page
			if (onloadfired) return;

			// reset timeout to try again later
			window.setTimeout(init,20);
			return;
		}

		// get all anchors in social networking list
		var anchorlist = socialel.getElementsByTagName('a');

		// cycle over anchor list and attach popup handlers to each social network type
		for (var i = 0,j = anchorlist.length;i < j;i++) {
			var anchoritem = anchorlist[i],
				anchorclass = anchoritem.className,
				anchorhref = anchoritem.href;

			if (anchorclass == 'facebook') event.add(anchoritem,'click',buildfacebookclick(anchorhref));
			if (anchorclass == 'twitter') event.add(anchoritem,'click',buildtwitterclick(anchorhref));
			if (anchorclass == 'delicious') event.add(anchoritem,'click',builddeliciousclick(anchorhref));
		}
	}

	init();

	// add event for window onload firing to stop #social node search
	event.add(window,'load',function() { onloadfired = true; });

	return obj;
}();
