/* Copyright (c) 2010 Brandon Aaron (http://brandonaaron.net)
 * Dual licensed under the MIT (MIT_LICENSE.txt)
 * and GPL Version 2 (GPL_LICENSE.txt) licenses.
 */
(function(a){a.extend(a.fn,{livequery:function(e,d,c){var b=this,f;if(a.isFunction(e)){c=d,d=e,e=undefined}a.each(a.livequery.queries,function(g,h){if(b.selector==h.selector&&b.context==h.context&&e==h.type&&(!d||d.$lqguid==h.fn.$lqguid)&&(!c||c.$lqguid==h.fn2.$lqguid)){return(f=h)&&false}});f=f||new a.livequery(this.selector,this.context,e,d,c);f.stopped=false;f.run();return this},expire:function(e,d,c){var b=this;if(a.isFunction(e)){c=d,d=e,e=undefined}a.each(a.livequery.queries,function(f,g){if(b.selector==g.selector&&b.context==g.context&&(!e||e==g.type)&&(!d||d.$lqguid==g.fn.$lqguid)&&(!c||c.$lqguid==g.fn2.$lqguid)&&!this.stopped){a.livequery.stop(g.id)}});return this}});a.livequery=function(b,d,f,e,c){this.selector=b;this.context=d;this.type=f;this.fn=e;this.fn2=c;this.elements=[];this.stopped=false;this.id=a.livequery.queries.push(this)-1;e.$lqguid=e.$lqguid||a.livequery.guid++;if(c){c.$lqguid=c.$lqguid||a.livequery.guid++}return this};a.livequery.prototype={stop:function(){var b=this;if(this.type){this.elements.unbind(this.type,this.fn)}else{if(this.fn2){this.elements.each(function(c,d){b.fn2.apply(d)})}}this.elements=[];this.stopped=true},run:function(){if(this.stopped){return}var d=this;var e=this.elements,c=a(this.selector,this.context),b=c.not(e);this.elements=c;if(this.type){b.bind(this.type,this.fn);if(e.length>0){a.each(e,function(f,g){if(a.inArray(g,c)<0){a.event.remove(g,d.type,d.fn)}})}}else{b.each(function(){d.fn.apply(this)});if(this.fn2&&e.length>0){a.each(e,function(f,g){if(a.inArray(g,c)<0){d.fn2.apply(g)}})}}}};a.extend(a.livequery,{guid:0,queries:[],queue:[],running:false,timeout:null,checkQueue:function(){if(a.livequery.running&&a.livequery.queue.length){var b=a.livequery.queue.length;while(b--){a.livequery.queries[a.livequery.queue.shift()].run()}}},pause:function(){a.livequery.running=false},play:function(){a.livequery.running=true;a.livequery.run()},registerPlugin:function(){a.each(arguments,function(c,d){if(!a.fn[d]){return}var b=a.fn[d];a.fn[d]=function(){var e=b.apply(this,arguments);a.livequery.run();return e}})},run:function(b){if(b!=undefined){if(a.inArray(b,a.livequery.queue)<0){a.livequery.queue.push(b)}}else{a.each(a.livequery.queries,function(c){if(a.inArray(c,a.livequery.queue)<0){a.livequery.queue.push(c)}})}if(a.livequery.timeout){clearTimeout(a.livequery.timeout)}a.livequery.timeout=setTimeout(a.livequery.checkQueue,20)},stop:function(b){if(b!=undefined){a.livequery.queries[b].stop()}else{a.each(a.livequery.queries,function(c){a.livequery.queries[c].stop()})}}});a.livequery.registerPlugin("append","prepend","after","before","wrap","attr","removeAttr","addClass","removeClass","toggleClass","empty","remove","html");a(function(){a.livequery.play()})})(jQuery);

/////////////// WICKEDWEB INLINE FLASH REPLACEMENT (WWIFR) v1.6 ///////////////

function setCSS(css) {
	try{
		document.getElementsByTagName("head")[0].appendChild(css);
	}catch(e){
		setTimeout(function(){setCSS(css)}, 100);
	}
}
var css = document.createElement("link");
css.setAttribute("href","/static/css/wwifr.css");
css.setAttribute("rel","stylesheet");
css.setAttribute("type","text/css");
setCSS(css);
css = null;

function setDimensions(height, width, id){
	$('#' + id).height(height).attr('height', height).width(width).attr('width', width);
}
function toHex(N) {
	if(N===null){
		return "00";
	}
	N=parseInt(N);
	if(N===0 || isNaN(N)){
		return "00";
	}
	N=Math.max(0,N); N=Math.min(N,255); N=Math.round(N);
	return "0123456789ABCDEF".charAt((N-N%16)/16)
	+ "0123456789ABCDEF".charAt(N%16);
}
function RGBtoHex(R,G,B){
	return '#'+toHex(R)+toHex(G)+toHex(B);
}
function RGB(c){
	var color = "";
	if(c.indexOf('#') <= -1){
		for(i=0; i<c.length; i++){
			if(parseInt(c.charAt(i))%1 === 0 || c.charAt(i) == ',') {
				color += c.charAt(i);
			}
		}
		nc = color.split(",");
		c = RGBtoHex(nc[0],nc[1],nc[2]);
	}
	return c;
}
function rgbHTML(theHTML){
	theHTML = theHTML.replace(/rgb\((.*?)\)/g, function(match,c){
		return RGB(c);
	});
	return theHTML;
}
function reformHTML(s){
	s= s.replace(/< *\/?(\w+)/g,function(c){
		return c.toLowerCase();
	});
	var Rx=/<\w+[^>]+>/g;
	var pat,tem='',temp= '',str= '',x= 0;
	while((pat= Rx.exec(s)) !== null){
		tem+= s.substring(x,pat.index);
		temp=pat[0];
		if(/; *charset *=/i.test(temp)===false){
			temp= temp.replace(/\= *([^"\s]+(?=[ >]))/g,'=\"$1\"');
		}
		temp=temp.replace(/[\w-]+ *\= */g,function(c){
			return c.toLowerCase();
		});
		temp=temp.replace(/style *\= *"[^"]+/g,function(c){
			if(c.charAt(c.length - 1) != ';'){
				c += ';';
			}
			c=c.replace(/: /gi, ':').replace(/:(.*?);/gi, '="$1"').replace(/font-size/gi, 'size');
			return c.toLowerCase();
		});
		tem+= temp;
		x= Rx.lastIndex;
	}
	tem+= s.substring(x);
	tem=tem.replace(/<span style="(.*?)">/g, "<font $1>").replace(/<\/span>/g, "</font>").replace(/&nbsp;/g, ' ').replace(/<em>(.*?)<\/em>/g, "<i>$1</i>").replace(/<strong>(.*?)<\/strong>/g, "<b>$1</b>").replace(/\+/g, '&#43;').replace(/%/g, '&#37;');
	return tem;
}

var wwifrCount = 0;
function replaceText(Class, Not, Font, Path, Weight, LinkColour, LinkHoverColour, LinkDecoration, LinkHoverDecoration, LineHeight, Spacing, Zoom, OffsetX, OffsetY){
	$(Class + ':visible').livequery(function(e){
		if($.flash.hasVersion(8)){
			$(this).find('object').remove();
			$(this).find('.wwifr').before($(this).find('.wwifr').html()).remove();
			$(this).not(Not).each(function(e){
				wwifrCount += 1;
				Class = Class.replace(/[^a-zA-Z 0-9]+/g, '');
				var ID = 'wwifr' + wwifrCount;
				var Text = $(this).html();
				var Original = Text;
				var Width = $(this).width();
				var Height = $(this).height();
				var Link = $(this).attr('href');
				var Align = $(this).css('text-align');
				var Size = $(this).css('font-size').replace(/[a-z]/g, '');
				var Colour = rgbHTML($(this).css('color'));
				if(Weight == '' || Weight === null){
					Weight = $(this).css('font-weight');
				}
				if(document.location.href.indexOf('file:///') > -1){
					var Live = false;
				}
				else{
					var Live = true;
				}
	
				Text = reformHTML(Text);
				Text = rgbHTML(Text);
				
				Flash = $.flash.create({swf:Path+Font+'.swf', width:Width, height:Height, expressInstaller:Path+'expressInstall.swf', flashvars:{ID:ID, Text:Text, Link:Link, Width:Width, Height:Height, Size:Size, Colour:Colour, LinkColour:LinkColour, LinkHoverColour:LinkHoverColour, LinkDecoration:LinkDecoration, LinkHoverDecoration:LinkHoverDecoration, Weight:Weight, LineHeight:LineHeight, Spacing:Spacing, Align:Align, Zoom:Zoom, OffsetX:OffsetX, OffsetY:OffsetY, Live:Live}, params:{wmode:'transparent'}});
				$(this).html(Flash);
				if(Live === true){
					$(this).find('object').attr('id', ID).attr('name', ID);
					$(this).mouseover(function(){
						$(this).find('object').focus();
					});
					$(this).mouseout(function(){
						$(this).find('object').flash(function(){
							this.SetVariable('resetvar', 0);
						});
						$(this).find('object').blur();
					});
				}
				$(this).append('<span class="wwifr">' + Original + '</span>');
				$(this).css('visibility', 'visible');
			});
		}
		else{
			$(Class).css('visibility', 'visible');
		}
	});
}

// selector*, not, font*, path*, fontWeight, linkColor, linkHoverColor, linkDecoration, linkHoverDecoration, lineHeight(0 = normal)*, letterSpacing(0 = normal), zoom(1 = 100%), xOffset, yOffset* //
replaceText('h1, h2', '', 'helvetica_1_6', '/static/flash/', 'bold', '#000000', '', '', '', '0', '0', '1', '-2', '-2');
replaceText('h3', '', 'helvetica_1_6', '/static/flash/', 'bold', '#A6A8AB', '', '', '', '0', '0', '1', '-2', '-2');
replaceText('h4, h5', '', 'helvetica_1_6', '/static/flash/', 'bold', '#999999', '', '', '', '0', '0', '1', '-2', '-2');
replaceText('.imagetext, .ourpartners', '', 'helvetica_1_6', '/static/flash/', 'bold', '', '', '', '', '0', '0', '1', '-2', '-2');
replaceText('#mainnav #leftmainnav li a.off, #mainnav #leftmainnav li a.on, #mainnav #leftmainnav li a.parent', '', 'helvetica_1_6', '/static/flash/', 'bold', '#FFFFFF', '#FFFFFF', '', '', '0', '0', '1', '-2', '-2');

function twitter(){
	$('#twitter_update_list').html('<span>Loading Twitter feed</span>');
	if($('#twitter_update_list').size() > 0){
		$.getScript("http://twitter.com/javascripts/blogger.js", function(){
			$.getScript("http://twitter.com/statuses/user_timeline/LFWEnd.json?callback=twitterCallback2&count=2");
		});
	}
}

$(document).ready(function(){
	twitter();
});