var current_use_type = '';
var current_category = '';
var current_fence_style = '';
var current_fence_top_type = '';
var fence_top_type = '';
var step = 1;

String.prototype.toCapitalCase = function() {
	var words = this.split('_');
	re = /(\S)(\S+)/;
	for (i = words.length - 1; i >= 0; i--) {
		if (isNaN(words[i])) {
			re.exec(words[i]);
			words[i] = RegExp.$1.toUpperCase() + RegExp.$2.toLowerCase();
		}
	}
	return words.join(' ');
} 

function activate(this_step) {
	$('a[rel=lightbox]').lightBox({fixedNavigation:true}); // Select all links in object with gallery ID
	
	step = this_step;
	$('#style_timeline a').click( function () {
		var id = $(this).parent().attr('id');
		
		var info = id.split("_");
		step = info[(info.length-1)];
		for (var i = step; i <= 5; i++) {
			$('#show_step_'+i).html('');
		}
		var left = ((step-1)*607)*-1;
		$("#new_style_content").animate({ 
   						left: left
					}, 1000 );
		if (step == 1) {
			$('#directions').text('Select your desired option below'); 
		}

	});
	
	
	
	$('#step_'+step+' a.selector').click( function () {
		if (step == 2) {
			var id = $(this).attr('id');
			var info = id.split("--");
			current_use_type = info[(info.length-1)];
			current_fence_style = '';
			fence_top_type = '';
			var image = $('#'+id+' img').attr('src');
			$('#show_step_'+step).html('<a href="#"><img src="'+image+'" alt="'+current_use_type+'" /><br />'+current_use_type.toCapitalCase()+'</a>');
		} else if (step == 3) {
			var id = $(this).attr('id');
			var info = id.split("--");
			current_fence_style = info[(info.length-1)];
			fence_top_type = '';
			var image = $('#'+id+' img').attr('src');
			$('#show_step_'+step).html('<a href="#"><img src="'+image+'" alt="'+current_fence_style+'" /><br />'+current_fence_style.toCapitalCase()+'</a>');
		} else if (step == 4) {
			var id = $(this).attr('id');
			var info = id.split("--");
			fence_top_type = info[(info.length-1)];
			var image = $('#'+id+' img').attr('src');
			$('#show_step_'+step).html('<a href="#"><img src="'+image+'" alt="'+fence_top_type+'" /><br />'+fence_top_type.toCapitalCase()+'</a>');
		}
		step++;
		$.ajax({
			type: "POST",
			async: false,
			url: "http://www.tru-link.com/new_site/wp-content/themes/tru-link-wp/get_style_selection.php",
			data: "use_type="+current_use_type+"&category="+current_category+"&fence_style="+current_fence_style+"&fence_top_type="+fence_top_type,
			success: function(html){
				$("#step_"+step+" .replace_me").html(html);
			}
		});
		var left = ((step-1)*607)*-1;
		$("#new_style_content").animate({ 
   						left: left
					}, 1000 );
		
		
		activate(step);
	});
}


$(document).ready(function() {

	$('#step_1 a.selector').click( function () {
		
		current_category = $(this).attr('id');
		current_use_type = '';
		current_fence_style = '';
		fence_top_type = '';
		var image = $('#'+current_category+' img').attr('src');
		
		$('#show_step_1').html('<a href="#"><img src="'+image+'" alt="'+current_category+'" /><br />'+current_category.toCapitalCase()+'</a>');
		
		$.ajax({
			type: "POST",
			async: false,
			url: "http://www.tru-link.com/new_site/wp-content/themes/tru-link-wp/get_style_selection.php",
			data: "use_type="+current_use_type+"&category="+current_category+"&fence_style="+current_fence_style+"&fence_top_type="+fence_top_type,
			success: function(html){
				var response = html;
				$("#step_2 .replace_me").html(html);
			}
		});
						
		$("#new_style_content").animate({ 
   						left: -607
					}, 1000 );
		
		$('#directions').text('Click a chosen option to reselect'); 
		
		activate(2);
		
		
	});
							
	
});
