// sets width of support block
function setSupport(){
	if($('body').width() < $('#wrapper').width()){
		$('#support').width($('#wrapper').width() - $('#page').width() + 150);
	}
	else{
		$('#support').width($('body').width() - $('#page').width() + 148);
	}
}

// sets height of content area
function setHeight(){
	$('#page').height('auto');

	if($('#page').height() < $(document).height()){
		$('#page').height($(document).height());
	}
}

function setExamples(){
	$('form dl.examples dt').hide();
	$('form dl.examples dd input[type="text"], form dl.examples dd input[type="password"], form dl.examples dd textarea, #aside form p input[type="text"]').each(function(){
		if($(this).val().length <= 0){
			$(this).example(function(){
				
				var label = $('label', $(this).parents('dd').prev('dt')).text().replace(':', '');
				
				if (label != '')
				{
					return label;
				}
				else{
					return $(this).attr('title');
				}
			});
		}
	});
}

function is_email(s){
	var reg = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	return reg.test(s);
}

function is_url(s){
	var reg = /(http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;
	
	return reg.test(s);
}

function validate(formData, jqForm, options){
	$(jqForm).addClass('active');
	$('body').addClass('wait');

	var regemail = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	var errors = '';

	for (var i=0; i < formData.length; i++){
		if(!formData[i].value){
			if(formData[i].name == 'name'){
				errors = errors + '<li>The Name field is required</li>';
			}

			if(formData[i].name == 'email'){
				errors = errors + '<li>The Email field is required</li>';
			}

			if(formData[i].name == 'comment'){
				errors = errors + '<li>The Comment field is required</li>';
			}
		}
		else if(formData[i].name == 'email' && !is_email(formData[i].value)){
			errors = errors + '<li>A valid Email is required</li>';
		}
		else if(formData[i].name == 'url' && !is_url(formData[i].value)){
			errors = errors + '<li>The URL field must be in the form of "http://domain.com"</li>';
		}
	}

	if(errors.length > 0){
		$('form.active').removeClass('active').prev('.messages').css('display', 'block').hide().html('<ul class="errors">' + errors + '</ul>').fadeIn(300);
		setExamples();
		setHeight();
		$(jqForm).removeClass('active');
		$('body').removeClass('wait');

		return false;
	}
	else{
		return true;
	}
}

function commentResponse(responseText, statusText){
	if(responseText.search('<div id="content">') != -1){
		errors = responseText.substring(responseText.indexOf('<div id="content">') + '<div id="content">'.length);
		errors = errors.substring(0, errors.indexOf('</div>'));
		errors = errors.substring(errors.indexOf('<ul>') + '<ul>'.length);
		errors = errors.substring(0, errors.indexOf('</ul>'));

		if(errors != null){
			if(errors.length > 0){
				$('form.active').removeClass('active').prev('.messages').css('display', 'block').hide().html('<ul class="errors">' + errors + '</ul>').fadeIn(300);
				setExamples();
				setHeight();
			}
			else{
				window.location.reload();
			}
		}
		else{
			window.location.reload();
		}
	}
	else{
		window.location.reload();
	}

	$('body').removeClass('wait');
}

function rgb2hex(rgb){
	var rgbvals = /rgb\((.+),(.+),(.+)\)/i.exec(rgb);
	var rval = parseInt(rgbvals[1]);
	var gval = parseInt(rgbvals[2]);
	var bval = parseInt(rgbvals[3]);

	return (rval.toString(16) + gval.toString(16) + bval.toString(16)).toUpperCase();
}

$(document).ready(function(){
	FLIR.init({path:'/_assets/facelift/'});
	
	$('#blog .post .date span.container .month, #blog .post .date span.container .day, #blog .post h4, #aside h2, #aside form p button').each(function(){
		FLIR.replace(this);
	});
	
	$('#blog .post .content h3').each(function(){
		FLIR.replace(this, new FLIRStyle({
			mode:"wrap"
		}));
	});

	$('body').bind('emchange', setHeight);

	setExamples();

	$('.blurb_content form, form#comment_form').ajaxForm({
		beforeSubmit: validate,
		success: commentResponse
	});
	
	$('form#search_form').submit(function(e){
		$('body').addClass('wait');
		$(this).addClass('active');

		var keywordsVal = $('input[name="keywords"]', this).val();
		
		if(keywordsVal == '' || keywordsVal.length < 3){
			$('form.active').removeClass('active').prev('.messages').css('display', 'block').hide().html('<ul class="errors"><li>Search term must be at least 3 characters in length</li></ul>').fadeIn(300);
			setExamples();
			setHeight();
			$('body').removeClass('wait');

			e.preventDefault();
		}
	});

	$('#blog .post .content p').each(function(){
		var length = $.trim($(this).html()).length;

		if(length <= 0){
			$(this).remove();
		};
	});
	
	$('#blog .post .content a[href$=".mp3"]').each(function(){
		var mp3 = $(this).attr('href');

		$(this).wrap('<div class="player"></div>');

		$(this).parents('div.player').flash({
			swf: '/_assets/swf/player.swf',
			width: '100%',
			height: 24,
			params: {
				wmode: 'transparent'
			},
			flashvars: {
				soundFile: mp3,
				remaining: 'yes',
				transparentpagebg: 'yes',
				bg: '000000',
				leftbg: '014F66',
				lefticon: 'FFFFFF',
				voltrack: 'FFFFFF',
				volslider: '98005D',
				rightbg: '98005D',
				righticon: 'FFFFFF',
				rightbghover: '03D8FF',
				border: 'FFFFFF',
				loader: '014F66',
				track: 'FFFFFF',
				tracker: 'AAAAAA',
				skip: 'FFFFFF',
				text: '000000'
			}
		});
	});

	$('#blog .post .content p img').each(function(){
		if ($(this).width() <= 200 && $(this).width() > 50 && $(this).height() > 25)
		{
			$(this).wrap('<span class="inline"></span>');
		}
		else if ($(this).height() > 25)
		{
			$(this).wrap('<span class="block"></span>');
		}
	});

	setSupport();
	setHeight();

	$(window).resize(function(){
		setSupport();
		setHeight();
	});
});
