var wpurl = window.location.protocol + '//' + window.location.hostname
var template_directory = wpurl + '/wp-content/themes/staffpro'

$(function() {
	labelMaker()
	homepage_anim()
	ie6_is_lame()
	tableSort()

	$('input.date').datepicker()
	
	$('body.recruiting-philosophy #sidebar ul.nav_sub').prepend('<li class="recruiting-philosophy"><a href="'+wpurl+'/recruiting-philosophy/">Recruiting Philosophy</a></li>')
	if ($('body').attr("class") == "recruiting-philosophy") {
		$('body.recruiting-philosophy #sidebar ul.nav_sub li.recruiting-philosophy').addClass('current_page_item')
	}

	Cufon.replace($('h2, h3, #nav li a'))
	$(window).load(function() {	
		$('span.cufon').css({ 'padding-bottom': '4px'})
	})
	
	$('body.join-our-mailing-list form').submit(function() {
		var message = ''
		
		$(this).find('input:text').each(function() {
			if ($(this).val() == '') message += "Please enter your " + $(this).attr('title') + "\n"
		})
		
		if (message != '') {
			window.alert(message)
			return false
		}
	})
})

function tableSort() {
	// Bounce if we're not on an experience page
	if ($('body').hasClass('experience-services') !== true) return false

	// For each table...
	$('table:not(.venues)').each(function() {
		var contents = []
		var output = ''
		
		// Iterate through all tds and if they have content, add them to our collection
		$(this).find('td').each(function() {
			var text = $.trim($(this).text())
			if (text !== '' && text !== '&nbsp;' && text !== " ") contents.push($(this).html())
		})

		contents.sort(
			function(a,b) {
				a = new String(a).toUpperCase()
				b = new String(b).toUpperCase()
				
				if (a > b) return 1 
				if (a < b) return -1 
				return 0; 
			}
		)

		if (contents[0] == '&nbsp;') contents = contents.slice(1) // Fuck you, IE

		// Wrap TDs and TRs back around our sorted content
		for (i=0; i < contents.length; i++) {
			if ((i % 2) === 0) output += '<tr>'
			output += '<td width="50%">' + contents[i] + '</td>'
			if ((i % 2) !== 0) output += '</tr>'
		}

		//Strip the old data
		$(this).find('td, tr').remove().end()
		$(this).append(output) // Append the new
	})
}

function labelMaker() {
	$('input').each(function() {
		if ($(this).is(':text'))	$(this).addClass('text')
		if ($(this).is(':submit'))	$(this).addClass('submit').addClass('button')
		if ($(this).is(':button'))	$(this).addClass('button')
	})
}

function homepage_anim() {
	$('body.home #page_content').prepend('<div class="flash">')
	$('body.home #page_content div.flash').flash({
		src:	template_directory + '/flash/index.swf',
		width:	864,
		height:	234
	})
}


function ie6_is_lame() {
	// Not IE6? Good.
	if ($.browser != 'msie' && parseInt($.browser.version) != 6) return false;
	var img

	// Capture source, toss image, apply filter to parent container.
	if ($('div.testimonial img').length > 0) {
		img = {
			source: $('div.testimonial img'),
			target: $('div.testimonial'),
			width: '417px',
			height: '269px'
		}
	} else if ($('div.fun_fact img').length > 0) {
		img = {
			source: $('div.fun_fact img'),
			target: $('div.fun_fact'),
			width: '405px',
			height: '200px'
		}
	} else {
		return false
	}

	$('<div class="png_replacement">')
	.appendTo(img.target)
	.css({ 
		width: img.width,
		height: img.height,
		filter: "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + img.source.attr('src') + "', sizingMethod='crop')"
	})
	
	img.source.remove()
}
