function transmit(f,send,place) {

	var url = curDir() + '/remote/' + f + '.php';

	$.ajax({
		type: "POST",
		url: url,
		dataType: "html",
		data: send + '&referrer=' + location.href,
		error: function(XMLHttpRequest, textStatus, errorThrown) {
			alert('Transmission error; please try again.');
		},
		success: function(data, textStatus){
			$(place).html(data).show("slow");
		}
	});

}

function collapseNavigation() {
	$('#navigation').children('ul').children('li').children('ul').fadeOut("fast", function() {
		$('#navigation').children('ul').children('li').removeClass('brightNav');
	});
}

function toggle(id) {
	if($('#' + id).is(':hidden'))	$('#' + id).slideDown();
	else	$('#' + id).slideUp();
}

function curDir() {
	var path = location.href;
	var dir = path.substring(path.indexOf('/', 1)+1, path.lastIndexOf('/'));
	
	return (location.protocol + '/' + dir);
}

function vote(num) {
	transmit('vote','ranking=' + num,'.ratingBlock');
}

function restoreRatings() {
	var average = $('#ratingAverage').val();

	$('.rating').each(function(i) {
		if(average >= i+1 - 0.05) $(this).children('a').children('img').attr('src','images/star.png');
		else if(average >= i+1 - 0.55) $(this).children('a').children('img').attr('src','images/star_half.png');
		else if(average < i+1) $(this).children('a').children('img').attr('src','images/star_dim.png');
	});
}



// Portfolio Settings
var portfolioUrl = new Array(
	'www.hamilton-marine-llc.com',
	'www.haylofticecream.com',
	'www.localmotiveband.com',
	'www.purpleheartmi.com',
	'www.roforever.com',
	'register.miraclecamp.com'
);
	
var portfolioTitle = new Array(
	'Hamilton Marine, LLC',
	'Hayloft Ice Cream',
	'Local Motive Band',
	'Purple Heart MI',
	'RoForever',
	'Miracle Camp Registration'
);

var portfolioYear = new Array(
	'2007',
	'2007',
	'2008',
	'2008',
	'2008',
	'2009'
);

var portfolioInfo = new Array(
	'Small business. Designed to specifications by a small business owner, selling an ingenious product to assist in lining up trailer hitches. (Especially useful when camping or fishing with little help.)',
	'Small business. For this relatively small website I was able to add a level of interactivity to the menu. Users can choose what category of food that they are hungry for and quickly see those items and prices on the same page.',
	'Entertainment. Local Motive is a Country band gaining popularity in the southwest Michigan area, with plenty of events and even some merchandise to check out.',
	'Government. While the Purple Heart remains unfinished, there is more than enough useful information available regarding several related news and events.',
	'MMORPG. RoForever is a Ragnarok Online (RO) private server that has been around for several years. This website is a true testament to my abilities as not only a web developer but as a web designer. There are several advanced features on the website including an intuitive registration and login process, user-submitted screenshots, and more.',
	'Youth Camp. For years, Miracle Camp had all of its young campers sign up for camp on paper or over the phone, taking up much of their time as all records had to be input by hand into their computer system. Since implementing this secure web form, the staff at Miracle Camp has greatly reduced their time spent on processing data. It has been used to process over 700 registrations since going live in March 2009.'
);

function initPortfolio() {
	var size = portfolioUrl.length - 1;
	var nav = ''; var img = '';
	
	for(var i=0; i <= size; i++) {
		nav += '<a href="javascript:updatePortfolio(' + i + ');" class="portfolio-link-' + i + '"><span class="year">' + portfolioYear[i] + '</span>' + portfolioTitle[i] + '</a>';
		img += '<img src="images/portfolio/t/' + portfolioUrl[i] + '.png" class="portfolio-image-' + i + '" alt="' + portfolioTitle[i] + '" />';
	}

	$('#portfolio .nav').html(nav);
	$('#portfolio .image').html(img);

}

function updatePortfolio(id) {

	var size = portfolioUrl.length - 1;
	
	if(id <= size) {
	
		$('#portfolio .nav a').removeClass('selected');
		$('.portfolio-link-' + id).addClass('selected');
	
		$('#portfolio .title').fadeOut("fast",function() {
			$(this).html('<a href="http://' + portfolioUrl[id] + '">Check out ' + portfolioTitle[id] + '</a>').fadeIn();
		});
		
		$('#portfolio .description').fadeOut("fast",function() {
			$(this).html(portfolioInfo[id]).fadeIn();
		});
		
		$('#portfolio .image img').not('.portfolio-image-' + id).animate({ height: "0px", width: "0px", opacity: 0 }, 250);
		$('.portfolio-image-' + id).animate({ height: "250px", width: "400px", opacity: 1 }, 500);

	}
}


$(document).ready(function(){

	initPortfolio();
	updatePortfolio(4);

	displayResult();
	var collapseTimer = '';
	var ratingTimer = '';

	$('#navigation ul li ul').hide();
	$('#navigation').mouseover(function(event) {
		clearTimeout(collapseTimer);
		var target = $(event.target);
		if($(event.target).is('a')) {
			target.parent('li').addClass('brightNav');		
		}
		if($(event.target).is('li')) {
			target.siblings('li').removeClass('brightNav');
			$(event.target + ' a:only-child').addClass('brightNav');
			target.siblings('li').children('ul').fadeOut("fast");
			target.children('ul').slideDown("fast");
		}
	});
	
	$('#navigation').mouseout(function(event) {
		 collapseTimer = setTimeout("collapseNavigation()",250);
	});
	
	$('.rating').mouseover(function(event) {
		clearTimeout(ratingTimer);
		var stop = this.id.substr(-1,1);
		$('.rating').each(function(i) {
			if(i < stop) $(this).children('a').children('img').attr('src','images/star.png');
			else $(this).children('a').children('img').attr('src','images/star_dim.png');
		});
	});
	
	$('.rating').mouseout(function(event) {
		ratingTimer = setTimeout("restoreRatings()",100);
	});

	$('form').submit(function() {

		var form = '#' + $(this).attr('id');
		var action = $(this).attr('action');
		var formClass = $(this).attr('class');

		if(formClass == 'formIgnore') { return true; }
		
		normalAlert('Preparing data...');
		
		var sendData,inputID,inputValue,skipInput;
			sendData = '';
		var i = 0;
		
		$(form + ' input[@type!=submit],select,textarea').each(function() {

			var inputID = $(this).attr('id');
				if(!inputID) { inputID = $(this).attr('class'); }

			var inputValue = '';
			var inputType = $(this).attr('type');
			
			skipInput = false; // Do not skip the current input.

			if(inputType == "checkbox") {
				if($(this).attr('checked')) {
					inputValue = true;
				} else {
					inputValue = false;
				}
			} else if(inputType == "radio") {
				if($(this).attr('checked')) {
					skipInput = false; // Do not skip the current radio button.
					inputValue = $(this).val();
					
					inputValue = inputValue.replace(/\+/g,'[plus]'); // replace all of the plus signs so data is properly sent to the server
					inputValue = inputValue.replace(/\&/g,'[amp]'); // replace all of the ampersands

				} else {
					skipInput = true; // The radio button is not checked, so skip it.
				}
			} else {
				inputValue = $(this).val();
				
				inputValue = inputValue.replace(/\+/g,'[plus]'); // replace all of the plus signs so data is properly sent to the server
				inputValue = inputValue.replace(/\&/g,'[amp]'); // replace all of the ampersands
			}
		
			if(skipInput == false) { sendData += '&' + inputID + '=' + inputValue; }

			i++;
		});
		
		ajax(action,sendData);
		
		return false;
		
	});

});