/* PLUGINS */

/* Flash (http://jquery.lukelutman.com/plugins/flash). V1.0. 2006/11/09. (c) 2006 Luke Lutman (http://www.lukelutman.com). Dual licensed under the MIT and GPL licenses. */
eval(function(p,a,c,k,e,r){e=function(c){return(c<a?'':e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--)r[e(c)]=k[c]||e(c);k=[function(e){return r[e]}];e=function(){return'\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p}(';(4(){5 g;g=l.1J.q=4(a,b,c,d){5 e=c||g.m;b=g.y(g.I,b);9(!g.j(b.A)){9(b.19&&g.j(6,0,10)){5 f={h:{1E:v,1A:\'1x\',1v:l(\'1t\').1p()}}}1k 9(b.t){e=d||g.t}1k{7 8}}a=g.y(g.1c,f,a);7 8.1W(4(){e.1T(8,g.y(a))})};g.y=4(){5 a={},h={};F(5 i=0;i<r.1Q;i++){5 b=r[i];9(b==12)Z;l.X(a,b);9(b.h==12)Z;l.X(h,b.h)}a.h=h;7 a};g.j=4(){9(/j\\=k/.U(v))7 k;9(/j\\=w/.U(v))7 w;5 a=g.j.T().o(/\\d+/g);5 b=E([r[0],r[1],r[2]]).o(/\\d+/g)||E(g.I.A).o(/\\d+/g);F(5 i=0;i<3;i++){a[i]=1m(a[i]||0);b[i]=1m(b[i]||0);9(a[i]<b[i])7 w;9(a[i]>b[i])7 k}7 k};g.j.T=4(){z{z{5 a=W M(\'C.C.6\');z{a.1s=\'1q\'}B(e){7\'6,0,0\'}}B(e){}7 W M(\'C.C\').1n(\'$A\').m(/\\D+/g,\',\').o(/^,?(.+),?$/)[1]}B(e){z{9(J.1Z["1i/x-1g-q"].1X){7(J.1e["1b n 2.0"]||J.1e["1b n"]).1V.m(/\\D+/g,",").o(/^,?(.+),?$/)[1]}}B(e){}}7\'0,0,0\'};g.1c={1U:1S,h:{},1R:\'18://1h.16.15/14/1j\',1P:\'#\',1O:\'1i/x-1g-q\',1N:1M};g.I={19:w,t:k,A:\'6.0.10\'};g.m=4(a){8.u=\'<Y 11="K">\'+8.u+\'</Y>\';l(8).V(\'q-1L\').L(g.O(a))};g.t=4(a){5 b=E(v).1H(\'?\');b.1G(1,0,\'?j=k&\');b=b.1F(\'\');5 c=\'<p><R>1D 1C 1B H Q n G.</R> <a S="18://1h.16.15/14/1j">1z H Q n G</a>. 1I 1y H n G? <a S="\'+b+\'">1K 1w.</a></p>\';8.u=\'<N 11="K">\'+8.u+\'</N>\';l(8).V(\'q-t\').L(c)};4 17(){5 s=\'\';F(5 a 13 8)9(P 8[a]!=\'4\')s+=a+\'="\'+8[a]+\'" \';7 s};4 1l(){5 s=\'\';F(5 a 13 8)9(P 8[a]!=\'4\')s+=a+\'=\'+1u(8[a])+\'&\';7 s.m(/&$/,\'\')};g.O=4(a){a.1a=17;9(a.h)a.h.1a=1l;7\'<1r \'+E(a)+\'/>\'};9(1f.1d){1f.1d("1o",4(){1Y=4(){};20=4(){}})}})();',62,125,'||||function|var||return|this|if||||||||flashvars||hasFlash|true|jQuery|replace|Flash|match||flash|arguments||update|innerHTML|location|false||copy|try|version|catch|ShockwaveFlash||String|for|Player|the|pluginOptions|navigator|alt|prepend|ActiveXObject|span|transform|typeof|latest|strong|href|playerVersion|test|addClass|new|extend|div|continue|65|class|undefined|in|go|com|adobe|toAttributeString|http|expressInstall|toString|Shockwave|htmlOptions|attachEvent|plugins|window|shockwave|www|application|getflashplayer|else|toFlashvarsString|parseInt|GetVariable|onbeforeunload|text|always|embed|AllowScriptAccess|title|encodeURIComponent|MMdoctitle|here|PlugIn|have|Download|MMplayerType|requires|content|This|MMredirectURL|join|splice|split|Already|fn|Click|replaced|320|width|type|src|length|pluginspage|240|call|height|description|each|enabledPlugin|__flash_unloadHandler|mimeTypes|__flash_savedUnloadHandler'.split('|'),0,{}))

/* jQMinMax v0.1 - Copyright (c) 2006 Dave Cardwell (http://davecardwell.co.uk/) Released under the MIT License (http://www.opensource.org/licenses/mit-license.php) */
eval(function(p,a,c,k,e,d){e=function(c){return(c<a?"":e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--){d[e(c)]=k[c]||e(c)}k=[(function(e){return d[e]})];e=(function(){return'\\w+'});c=1};while(c--){if(k[c]){p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c])}}return p}('u m(){$.5={D:y,v:y};$(G).12(m(){8 h=G.W(\'I\');$(h).e({\'3\':\'J\',\'6-3\':\'K\'});$(\'L\').M(h);$.5.v=(h.s&&h.s==2);$(h).N();b($.5.v)o;$.5.D=O;$.5.A();$(\':5\').5()});$.5.A=m(){8 p=u E(\'6-3\',\'6-4\',\'9-3\',\'9-4\');8 5=u E();Q(8 i=0;i<p.R;i++){8 n="$.e(a,\'"+p[i]+"\')!=\'S\'&&"+"$.e(a,\'"+p[i]+"\')!=\'z\'&&"+"$.e(a,\'"+p[i]+"\')!=f.g";b(p[i].U(2)==\'x\')n+="&&$.e(a,\'"+p[i]+"\')!=\'X\'";$.n[\':\'][p[i]]=n;5[i]=\'(\'+n+\')\'}$.n[\':\'][\'5\']=5.Y(\'||\')};$.Z.5=m(){o $(c).10(m(){8 7={\'6-3\':r(c,\'6-3\'),\'9-3\':r(c,\'9-3\'),\'6-4\':r(c,\'6-4\'),\'9-4\':r(c,\'9-4\')};8 3=c.s;8 4=c.w;8 k=3;8 l=4;b(7[\'9-3\']!=f.g&&k>7[\'9-3\'])k=7[\'9-3\'];b(7[\'6-3\']!=f.g&&k<7[\'6-3\'])k=7[\'6-3\'];b(7[\'9-4\']!=f.g&&l>7[\'9-4\'])l=7[\'9-4\'];b(7[\'6-4\']!=f.g&&l<7[\'6-4\'])l=7[\'6-4\'];b(k!=3)$(c).e(\'3\',k);b(l!=4)$(c).e(\'4\',l)})};m r(t,p){8 q=$(t).e(p);b(q==f.g||q==\'z\')o f.g;8 j;j=q.B(/^\\+?(\\d*(?:\\.\\d+)?)%$/);b(j){o T.V(C((/3$/.h(p)?$(t).F().H(0).s:$(t).F().H(0).w)*j[1]/P))}j=q.B(/^\\+?(\\d*(?:\\.\\d+)?)(?:11)?$/);b(j){o C(j[1])}o f.g}}();',62,65,'|||width|height|minmax|min|constraint|var|max||if|this||css|window|undefined|test||result|newWidth|newHeight|function|expr|return||raw|calculate|offsetWidth|obj|new|native|offsetHeight||false|auto|expressions|match|Number|active|Array|parent|document|get|div|1px|2px|body|append|remove|true|100|for|length|0px|Math|charAt|round|createElement|none|join|fn|each|px|ready'.split('|'),0,{}))


/* ------- */

// Set a Flash version universally
var flash_ver = 7;

$(document).ready(DocReady);

function DocReady()
{
	/* EVENTS JS CODE */
	$(':text').each( InitHint );
	// Initiate checkboxes not contained in label elements, differently to those that are
	$('body:not(.edit)').find(':checkbox').each( InitFancyCheckbox );
	$('body:not(.edit)').find('div.enter_edit').each(InitEditChoices);

	// Flash text replacement stuff
	DoFlashReplacements();

	// "Typography" elements
	// Add in the .additional element, to take the "closing quote" graphics, etc
	$('#content .col0_tint0').add('#content .col0_tint1').add('#content .col1_tint0').add('#content .col1_tint1').add('#content .quote').add('#content .donation').add('#content .donation_one_line').prepend('<span class="additional"></span>');
	// span.additional elements should have height: 100%, which isn't respected by IE6, so...
	$('#content span.additional').each( function ( el ) { $(this).height( $(this).parent().height() ); } );
}

function DoFlashReplacements()
{
	var text_col = rgbToHtml( $('body').css('color') );
	var link_col = rgbToHtml( $('body a').css('color') );
	
	var debug = 1;
	var no_debug = 0;

	var border = 1;
	var no_border = 0;

	var slides = true;
	var no_slides = false;
	
	HideFlashText();

	// SWTODO: Check display with Javascript turned off
	// SWTODO: Delay before text displays in IE6 to prevent Flash of Unstyled Content. Perhaps we should do some quick JS ops like changing a class to hide the text?
	flashReplace( 
		'event_info', '#event_info',
		[
			'h3 { font-family: "Serifa LT 65 Bold"; display: block; font-size: 14; color: '+text_col+'; font-weight: normal; leading: 2; }',
			'p { font-family: "Serifa LT 65 Bold"; font-weight: normal; color: '+text_col+'; leading: 2; font-size: 14; }',
			'a { font-family: "Serifa LT 65 Bold"; color: '+link_col+'; text-decoration: none; }',
			'a:hover { color: '+link_col+'; text-decoration: underline; }'
		].join(''),
		187, 91, '0,0,0,0', no_slides, no_border, no_debug
		);

	flashReplace( 
		'panel', '#panel:not(.map)',
		[
			'div { leading: 2; font-size: 5; }',
			'p { font-family: "Serifa LT 65 Bold"; font-weight: normal; color: #ffffff; leading: 2; margin: 10px; font-size: 16; }',
			'em { font-family: "Serifa LT 55 Roman"; }',
			'a { font-family: "Serifa LT 65 Bold"; color: '+link_col+'; text-decoration: none; }',
			'a:hover { text-decoration: underline; }'
		].join(''),
		610, 170, '9,100,10,100', 
		slides, no_border, no_debug
		);

	var default_css = [
			'h3 { font-family: "Serifa LT 65 Bold"; display: block; font-size: 14; color: '+text_col+'; font-weight: normal; leading: 2; }',
			'div { leading: 2; font-size: 5; }',
			'p { leading: 2; font-size: 14; }',
			'a { font-family: "Serifa LT 65 Bold"; color: '+text_col+'; text-decoration: none; }',
			'a:hover { color: '+link_col+'; text-decoration: underline; }',
			'.thin { font-family: "Serifa LT 55 Roman"; }',
			'.thin_current { font-family: "Serifa LT 55 Roman"; color: '+link_col+'; text-decoration: none; leading: 2; }',
			'.current { color: '+link_col+'; text-decoration: none; leading: 2; }'
		].join('');

	flashReplace( 
		'main_nav', '#main_nav', default_css, 
		165, 170, '10,10,10,10', no_slides, no_border, no_debug
		);

	flashReplace( 
		'secondary_nav', '#secondary_nav', default_css, 
		165, 170, '10,10,10,10', no_slides, no_border, no_debug
		);

	flashReplace( 
		'tertiary_nav', '#tertiary_nav', default_css, 
		165, 170, '10,10,10,10', no_slides, no_border, no_debug
		);

	flashReplace( 
		'events_nav', '#events_nav', default_css, 
		165, 460, '10,10,10,10', no_slides, no_border, no_debug
		);

	flashReplace( 
		'case_studies', '#case_studies',
		[
			'h3 { display: block; font-family: "Serifa LT 65 Bold"; font-weight: normal; font-size: 14; leading: 2; color: '+text_col+'; }',
			'p { font-family: "Serifa LT 65 Bold"; font-weight: normal; font-size: 14; leading: 2; color: '+text_col+'; }',
			'a { font-family: "Serifa LT 65 Bold"; color: '+link_col+'; text-decoration: none; }',
			'a:hover { color: '+link_col+'; text-decoration: underline; }'
		].join(''),
		187, 64, '0,0,0,0', no_slides, no_border, no_debug
		);

	// Reveal the text again
	$('#event_info').add('#panel').add('#events_nav').add('#main_nav').add('#secondary_nav').add('#case_studies').removeClass('temp_hide');
	
	// Lastly, important that it is last because it takes AGES
	// Init Google Maps (if needed)
	if ('function' == typeof(SetupGoogleMap)) {
		SetupGoogleMap();
	}
}

function HideFlashText()
{	
	// Hide the text while we load everything in, Flash can take a while to load on IE6
	$('#event_info').add('#panel').add('#main_nav').add('#events_nav').add('#secondary_nav').add('#case_studies').addClass('temp_hide');
}

function flashReplace( name, selector, css, width, height, offset, slides, border, debug )
{
	// SWTODO: Check the site looks OK without Flash. Remove multiple upgrade/install notices, and replace with one notice.
	$(selector).flash(
		{ 
			src: template_directory + '/images/panel.swf' 
		},
		{ version: flash_ver },
		function ( html_options ) {
			var content = eval(name);
			var bg_colour = $(this).css('background-color');
			// Move the old content into a child div classed "alt"
			this.innerHTML = '<div class="alt">'+this.innerHTML+'</div>';
			var $alt = $(this.firstChild);
			if ( slides ) {
				html_options.flashvars.imgs = slide_imgs;
			}
			html_options.flashvars.txt = content;
			html_options.flashvars.css = css + 'body { background-color: ' + bg_colour + '; }';
			html_options.flashvars.width = width;
			html_options.flashvars.height = height;
			html_options.flashvars.name = name;
			html_options.width = width;
			html_options.height = height;
			html_options.flashvars.offset = offset;
			html_options.flashvars.border = border;
			html_options.flashvars.debug = debug;
			var flash_code = $.fn.flash.transform(html_options);
			$(this).addClass('flash_replaced').prepend(flash_code);
		}
	);
}

function rgbToHtml( rgb_str )
{
	var rgb;
	var hex_string;
	var hex_value;

	if ( rgb_str.indexOf('#') !== -1 ) {
		// Actually of a decent form, e.g. #ff0000
		return rgb_str;
	}

	rgb_str = rgb_str.substr(4);
	rgb_str = rgb_str.substr(0, (rgb_str.length - 1) );
	rgb = rgb_str.split(', ');

	a = GiveHex( Math.floor(rgb[0] / 16) );
	b = GiveHex( rgb[0] % 16 );
	c = GiveHex( Math.floor(rgb[1] / 16) );
	d = GiveHex( rgb[1] % 16);
	e = GiveHex( Math.floor(rgb[2] / 16) );
	f = GiveHex( rgb[2] % 16 );

	return '#' + a + b + c + d + e + f;
}

function GiveHex( dec )
{
	if ( dec == 10 ) return "A";
	if ( dec == 11 ) return "B";
	if ( dec == 12 ) return "C";
	if ( dec == 13 ) return "D";
	if ( dec == 14 ) return "E";
	if ( dec == 15 ) return "F";
	return "" + dec;
}


// For the page before you submit an event, where you choose if it's a multiple 
// venue event or not.
function InitEditChoices()
{
	$(this).mouseover(HoverOn).mouseout(HoverOff).click(GoToContainedHref);
}

// This function assumes the referenced element contains one A element.
// It gets the href property, and sets the window location to it.
function GoToContainedHref()
{
	var href = $(this).find('a').attr('href');
	if ( ! href ) return false;
	window.location = href;
}

function HoverOn()
{
	$(this).addClass('hover');
}

function HoverOff()
{
	$(this).removeClass('hover');
}

function InitHint()
{
	// Can't seem to chain remove() onto the end of this line, weird.
	var hint = $(this).parent().parent().find('.hint').text();
	// We put the CSS property into the JS because it's intrinsically linked and should only be allowed if the JS is functional
	$(this).parent().parent().find('.hint').css('display', 'none');
	// If there is a hint...
	// ...and if the value of the text field has not changed from the value of the hint (i.e. no one has started editing)
	if ( hint && ( ! $(this).attr('value') || $(this).attr('value') == hint ) ) {
		// Set the value of the field to the hint, add a class of 'hinted' for styling, add actions onFocus and onBlur
		// Then find the parent form and attach a submit action to wipe hints before submitting
		$(this).focus(FocusHint).blur(BlurHint).addClass('hinted').attr('value', hint).attr('hint', hint).parents().filter('form').submit(ClearHints);
	}
}

// Clear the hint when the field is focussed
function FocusHint()
{
	// If the hint is unchanged (i.e. unedited)
	if ( $(this).attr('hint') == $(this).attr('value') ) {
		// Wipe the hint from the value in preparation for someone entering some text, toggle the 'hinted' class
		$(this).attr('value','').toggleClass('hinted');
	}
}

// Reinstate the hint when the field is focussed (if the user hasn't edited the field)
function BlurHint()
{
	// If the value is blank (i.e. unedited)
	if ( ! $(this).attr('value')  ) {
		// Reinstate the hint, toggle the 'hinted' class
		$(this).attr('value', $(this).attr('hint')).toggleClass('hinted');
	}
}

// Get rid of all hints before form submission
function ClearHints()
{
	// Find all the hinted elements and clear them
	$(this).find('.hinted').attr('value', '');
	// Safe to submit now
	$(this).submit();
	return false;
}

function InitFancyCheckbox()
{
	return;
	// SWTODO: Restore tab functionality to fancy checkboxen: WrapInner a link into the label, allowing for a tab event, then add a click event to capture mouse & keyboard input
	// Start on the checkbox, get the parent (assumed to be a label element), add a class and event, find the checked ones 
	// and add a class to their parents
	$(this).parent().addClass('fancy_checkbox').find(':checked').parent().addClass('fancy_checked');
	// Not ideal, as we depend on the label having a for attribute... but hell, we make the markup, so it's fine
	$('label[for='+$(this).attr('id')+']').mouseup(FancyCheckboxChange);
}

function FancyCheckboxChange()
{
	return;
	// 'this' is the container of the checkbox, possibly the label, possibly not
	// Find the checkbox, toggle the containing element class
	$(this).find(':checkbox').each(ToggleChecked).parent().toggleClass('fancy_checked');
}


// When the checkboxes are hidden, IE6 doesn't properly toggle their checked state.
// This function forces it to behave.
function ToggleChecked()
{
	return;
	// Will mess up browsers which do things properly
	if ( ! $.browser.msie ) return;
	var major_version = $.browser.version.split('.')[0];
	if ( major_version > 6 ) return;
	// Switch the checked state of a checkbox
	if ( $(this).attr('checked') ) {
		$(this).attr('checked', false);
	} else {
		$(this).attr('checked', true);
	}
}
