$(document).ready( function()
{
	$('a[rel="external"]').attr('target', '_blank');
	
	if ($('#id-15').length > 0) initSlideShow();
	if ($('.template-8').length > 0) initWorkSlideShow();
	
	
	$('.color_image').hide();
	
	if ($('#main_nav .active .active').length > 0)
	var activeID =  $('#main_nav .active .active').attr('id').substring(6);
	
	$('#services_mainnav a[rel="'+ activeID +'"]').children('.color_image').show();
	$('#services_mainnav a[rel!="'+ activeID +'"]').hover(
		function()
		{ $(this).children('.color_image').show(); }, 
		function()
		{ $(this).children('.color_image').hide(); });
	
	//$('#navid-64>a').attr('href', $('#navid-64 .first a').attr('href'));
	//$('#navid-63>a').attr('href', $('#navid-63 .first a').attr('href'));
	
	$('#main_container').css('padding-bottom', $('#footer').height() + 42);
	$('#footer').css('margin-top', -$('#footer').height() );
	
	if ($('.parent-64 #user_content').height() > $('#services_subnav').height())
		$('#services_subnav').height($('.parent-64 #user_content').height());
	else 
		$('.parent-64 #user_content').height($('#services_subnav').height());
		
	
	//--------------------search form------------------------
	$('#search').val('explore');
	$('#search').blur('explore', insertText);
	$('#search').focus('explore', clearText);
	
	$('#sname').blur('name', insertText);
	$('#sname').focus('name', clearText);
	
	$('#email').blur('email address', insertText);
	$('#email').focus('email address', clearText);
	$('#subscribe-submit').val('');
	
});



function validateFormOnSubmit(theForm)
{
	var reason = "";
	reason += validateEmpty(theForm.SNAME,"name");
	reason += validateEmail(theForm.EMAIL);
	if (reason != "")
	{
		alert("Some fields need correction:\n" + reason);
		return false;
	}
	$.post($('#emailform').attr('action'), $("#emailform").serialize());
	displayMessage
	(
		'<h3>Thank you for subscribing to our newsletter.</h3><p> An email is on its way to the address you used to register. Click the link within it and your subscription will be activated automatically.</p><p> If you can\'t find the email, try checking your \'junk\' or \'spam\' folders.</p>',
		'Okay', 
		500
	);
	return false;
}

function validateEmpty(fld,theText)
{
	var error = "";
	if (fld.value == theText || fld.value == "")
	{
		fld.style.background = 'Yellow';
		error = "Please fill in the \"" + theText + "\" field.\n"
	}
	else
	{
		fld.style.background = 'White';
	}
	return error;
}

function trim(s)
{
	return s.replace(/^\s+|\s+$/, '');
}

function validateEmail(fld)
{
	var error="";
	var tfld = trim(fld.value); // value of field with whitespace trimmed off
	var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
	var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
	if (fld.value == "")
	{
		fld.style.background = 'Yellow';
		error = "You didn't enter an email address.\n";
	}
	else
	if (!emailFilter.test(tfld))
	{
		//test email for illegal characters
		fld.style.background = 'Yellow';
		error = "Please enter a valid email address.\n";
	}
	else
	if (fld.value.match(illegalChars))
	{
		fld.style.background = 'Yellow';
		error = "The email address contains illegal characters.\n";
	}
	else
	{
		fld.style.background = 'White';
	}
	return error;
}




function displayMessage(message, buttonLabel, width, height )
{
	buttonLabel = buttonLabel || 'Okay';
	
	$('body').append
	(
		'<div id="popup">' +
			'<div id="popup-box">' +
				'<div id="popup-message">' +
					message +
				'</div>' +
				'<div id="popup-btn"><a>'+buttonLabel+'</a></div>' +
			'</div>' +
		'</div>'
	);
	$('#popup-btn').click(function()
	{
		$('#popup').remove()
		$(window).unbind('resize');
	});
	
	
	$(window).resize(onResize);
	
	function onResize()
	{
		$('#popup').css(
		{
			width	:	$(window).width(),
			height	: 	$(window).height() 
		});
		$('#popup-box').css('margin-top', ($(window).height() - $('#popup-box').height() )/ 2 );
	}
	
	if (width) $('#popup-box').css('width', width);
	if (height) $('#popup-box').css('height', height);
	
	onResize();
}

function clearText(e)
{
	if ( $(this).val() == e.data)
		$(this).val('');
}

function insertText(e)
{
	if ($(this).val() == '')
		$(this).val(e.data);
}

function setCookie( name, value, expires, path, domain, secure )
{
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );
	
	/*
	if the expires variable is set, make the correct
	expires time, the current script below will set
	it for x number of days, to make it for hours,
	delete * 24, for minutes, delete * 60 * 24
	*/
	if ( expires )
	{
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	
	document.cookie = name + "=" +escape( value ) +
	( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
	( ( path ) ? ";path=" + path : "" ) +
	( ( domain ) ? ";domain=" + domain : "" ) +
	( ( secure ) ? ";secure" : "" );
}

function getCookie(c_name)
{
	var i,x,y,ARRcookies=document.cookie.split(";");
	for (i=0;i<ARRcookies.length;i++)
	{
		x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
		y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
		x=x.replace(/^\s+|\s+$/g,"");
		if (x==c_name)
	    {
	    	return unescape(y);
	    }
	}
}
