/*<![CDATA[*/

/* $Id: global.js,v 1.0 2010/05/10 14:00:00 twig Exp $ */
/*
	Include global and utility functions here
*/

var tallestColumn, tallestColumnHeight; // Holders for tallest column jQ object & its height

/*
	Intellectual property of Twig Interactive LLC
	Do not reuse or alter without express permission
	http://www.twiginteractive.com
*/

/* Twig Class */
var TWIG = {};

// Statics
TWIG.defaultTuringTestValue = 'Enter code...';
TWIG.defaultSearchValue = 'Enter Search Topic...';

// Toggles between default value and empty string
TWIG.toggleTextInputValue = (function() {
	function T(o,evt,defVal,len) {
		if (typeof(len) == 'undefined') {
			len = 60; // Default max length
		};
		if (evt == 'focus') {
			if ($.trim(o.value) == defVal) {
				o.maxLength = len;
				o.value = '';
			};
		};
		if (evt == 'blur') {
			if ($.trim(o.value) == '') {
				o.maxLength = defVal.length;
				o.value = defVal;
			};
		};
	}

	return T;
})();


// Checks email address against RFC rules
// Based on original PHP code by Cal Henderson (http://iamcal.com/publish/articles/php/parsing_email)
// Twig converted to JS regex
TWIG.isValidEmailAddress = (function() {
	function T(email) {
		var qtext = '[^\\x0d\\x22\\x5c\\x80-\\xff]';
		var dtext = '[^\\x0d\\x5b-\\x5d\\x80-\\xff]';
		var atom = '[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c'+'\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+';
		var quoted_pair = '\\x5c\\x00-\\x7f';
		var domain_literal = '\\x5b('+dtext+'|'+quoted_pair+')*\\x5d';
		var quoted_string = '\\x22('+qtext+'|'+quoted_pair+')*\\x22';
		var domain_ref = atom;
		var sub_domain = '('+domain_ref+'|'+domain_literal+')';
		var word = '('+atom+'|'+quoted_string+')';
		var domain = sub_domain+'(\\x2e'+sub_domain+')*';
		var local_part = word+'(\\x2e'+word+')*';
		var addr_spec = local_part+'\\x40'+domain;
		var filter = eval('/^' + addr_spec + '$/');
		
		if (filter.test(email)) { // Passes RFC RegEx
			// Sanity check for period '.' following @
			if (email.indexOf('.',email.indexOf('@')) >= 0) {
				return true;
			}
		}
		return false;
	}
	
	return T;
})();

// Checks input against RegEx for valid website URL
TWIG.isValidURL = (function() {
	function T(URL) {
		var urlMatch = /^https?:\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?$/;
		if (urlMatch.test(URL)) { // Passes RegEx
			// Sanity check for period '.' following //
			if (URL.indexOf('.',URL.indexOf('//')) >= 0) {
				return true;	
			}
		}
		return false;
	}
	
	return T;
})();

// Open a pop-up window with set parameters
TWIG.openWindow = (function() {
	function T(URL,Name,W,H,L,T,Scrolls,Resize) {
		// Used to control params of pop-ups
		var defProps = 'copyhistory=no,directories=no,fullscreen=no,location=no,menubar=no,status=no,titlebar=yes,toolbar=no';
		var poppedProps = '';
	
		if (W != null) {
			if (Scrolls == true) { W += 16; } // Allow for chrome in IE
			poppedProps += ('width='+W+',');
		}
		if (H != null) { poppedProps += ('height='+H+','); }
		if (L != null) { poppedProps += ('left='+L+','); }
		if (T != null) { poppedProps += ('top='+T+','); }
		poppedProps += 'scrollbars=' + ((Scrolls != false) ? 'yes' : 'no') + ',' ; // Default 1
		poppedProps += 'resizable=' + ((Resize != false) ? 'yes' : 'no') + ',' ; // Default 1
		poppedProps += defProps;

//		alert(poppedProps);		
		poppedUp = window.open(URL,Name,poppedProps);
		if (poppedUp) { setTimeout("poppedUp.window.focus();",100); };
		return poppedUp;
	}
	return T;
})();

/*
	Used to match the heights of content, sidebar-left and sidebar-right for decor
	Assuming that these columns have no padding or margin per CSS layout and that contents are in *-inner divs
	*** DOM ELEMENTS ALTERED FROM DRUPAL FOR WORDPRESS! ***
*/
TWIG.matchColumnHeights = (function() {
	function T() {
		var els = $('#container').add('#primary');
		var maxH = 0;
		els.each(function(n){
			$(this).css({minHeight: maxH}); // Reset
			if ((h = $(this).height()) > maxH) {
				maxH = h; // Update max height
				tallestColumn = $(':first-child', this); // Update tallest column (global)
			};
		});
		els.each(function(n){
			$(this).css({minHeight: maxH+'px'}); // Apply minimum height
		});
		tallestColumnHeight = tallestColumn.height(); // Update tallest column height (global)
	}
	
	return T;
})();
// Companion poller function to check whether the total height has changed & columns need to be re-matched
TWIG.checkColumnHeights = (function() {
	function T() {
		if (tallestColumn.height() != tallestColumnHeight) {
			TWIG.matchColumnHeights();
		};
	}
	
	return T;
})();


/* onReady */
$(document).ready(function(){

	// PNG fix for IE6
	$(document).pngFix({blankgif:'/wp-content/themes/twentyten/graphics/spacer.gif'});
	
	// Match column heights & check for change every second
	TWIG.matchColumnHeights(); var checkColumnHeights = setInterval(function(){ TWIG.checkColumnHeights(); }, 1000);
	
	// Insert padder on tables to be striped, set td heights & stripe
	var stripeyTables = $('table.striped');
	$('th', stripeyTables).add('td', stripeyTables).each(function(n){
		var h = $(this).html();
		$(this).html('<div class="pad">'+h+'</div>');
		delete h;
	});
	$('tr', stripeyTables).each(function(n){ // Set heights of TDs
		var els = $('td', this);
		var pdg = parseInt($('div.pad', els).css('padding-top'), 10) + parseInt($('div.pad', els).css('padding-bottom'), 10);
		var h, maxH = 0;
		els.each(function(o){
			if ((h = $(this).height()) > maxH) {
				maxH = h;
				maxH = (typeof(pdg) == 'number') ? (maxH - pdg) : maxH; // Adjust for padding if present
			};
		});
		els.each(function(o){
			$(this).css({height: maxH+'px'}); // Apply minimum height
		});
		delete els, pdg, h, maxH;
	});
	stripeyTables.stripeMe();
	delete stripeyTables;
		
});

/*]]>*/

