$(document).ready(function() {
	
	$('a.popupLink').bind('click', function(e) { popupWindow($(this).attr('href'), $(this).attr('title'),'width=704, height=500, scrollbars'); e.preventDefault(); });
	
	bVersion = getInternetExplorerVersion();
	
	$('.partners').innerfade({ speed: 'slow', timeout: 4000, type: 'sequence', containerheight: '100px' }); 
	
	$("#subscribeName").focus(function(){
    	this.select();
	});
	
	$("#subscribeEmail").focus(function(){
    	this.select();
	});
	
	$('.secondarySelected').parent().siblings().addClass('selectedChild');
	
	$('.tertiarySelected').parent().prev().addClass('selectedChild');
	
	$('#subscribeForm').submit(function() {
		var formUrl = $(this).attr('action');
		var formData = $(this).serialize();
		$('.subscribeFormMessages').hide();
		$.get(formUrl, formData, function(data) {
			if ($('response', data).attr('status') == 'ok') {
				$('#subscribeFormSuccess').show();
			} else {
				$('#subscribeFormErrors').show().html($('error-message', data).text());
			}
		});
		return false;
	});
	
	$('input.defaultSwapText').focus(defaultSwapTextClearCheck).blur(defaultSwapTextReplaceCheck).blur();
	
});

function popupWindow(url, name, attributes){
	// Append popup to the URL if it does not already exist
	var queryStringExists = false;
	if(url.match(/\?/)) {
		queryStringExists = true;
	}
	
	if(queryStringExists) {
		// Only append if we haven't already got the parameter popup defined
		if(!url.match(/(\?|&)popup=/)) {
			url = url + '&popup=true';
		}
	} else {
		url = url + '?popup=true';
	}
	
	var newWindow = window.open(url, name.replace(/[^A-z]/ig, ''), attributes);
	newWindow.focus();
}

function getInternetExplorerVersion() {
	var rv = -1;
	if (navigator.appName == 'Microsoft Internet Explorer') {
		var ua = navigator.userAgent;
		var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
		if (re.exec(ua) != null)
		rv = parseFloat( RegExp.$1 );
	}
	return rv;
}


function defaultSwapTextClearCheck() {
    if ($(this).val() == $(this).attr('title')) {
        $(this).val('');
    }
}
function defaultSwapTextReplaceCheck() {
    if ($(this).val() == '') {
        $(this).val($(this).attr('title'));
    }
}