var input2Texts = new Object;
var input2ToggleClass = 'default';
var input2IgnoreClass = 'ignore';

function setupInput2Toggles(){
	$("input[type=text]:not(."+input2IgnoreClass+")").bind("focus", function(c){ input2Toggle($(this), 'focus'); });
	$("input[type=text]:not(."+input2IgnoreClass+")").bind("blur", function(c){ input2Toggle($(this), 'blur'); });
	$("textarea:not(."+input2IgnoreClass+")").bind("focus", function(c){ input2Toggle($(this), 'focus'); });
	$("textarea:not(."+input2IgnoreClass+")").bind("blur", function(c){ input2Toggle($(this), 'blur'); });
}


function input2Toggle(el,ev){
	var elId = el.attr("id");
	if(!input2Texts[elId]){
		input2Texts[elId] = el.val();
	}
	if(ev == "focus"){
		if(el.val() == input2Texts[elId]){
			el.val('');
		}
		if(el.hasClass(input2ToggleClass)){
			el.removeClass(input2ToggleClass);
		}
	} else { // blur action
		if(el.val() == ''){
			el.val(input2Texts[elId]);
			if(!el.hasClass(input2ToggleClass)){
				el.addClass(input2ToggleClass);
			}
		}
	}
}

function commaThis(number) {
	number = '' + number;
	if (number.length > 3) {
		var mod = number.length % 3;
		var output = (mod > 0 ? (number.substring(0,mod)) : '');
		for (i=0 ; i < Math.floor(number.length / 3); i++) {
			if ((mod == 0) && (i == 0))
				output += number.substring(mod+ 3 * i, mod + 3 * i + 3);
			else
				output+= ',' + number.substring(mod + 3 * i, mod + 3 * i + 3);
		}
		return (output);
	} else return number;
}
function updateTotal(elm,val){
	if($(elm).attr("type") == "radio"){
		busVal = val;
	}
	
	if($(elm).attr("type") == "checkbox"){
		otherVal = ($(elm).attr("checked") != "") ? otherVal + val : otherVal - val;
	}
	var total = busVal + otherVal;
	$("#total_investment").val(commaThis(total) + ".00");
}

function copyAddr(){
	var local_addr = $("#local_address").val();
	if(local_addr != ""){
		$("#billing_address").val(local_addr);
	}
	var local_city = $("#local_city").val();
	if(local_city != ""){
		$("#billing_city").val(local_city);
	}
	var local_state = $("#local_state").val();
	if(local_state != ""){
		$("#billing_state").val(local_state);
	}
	var local_zip = $("#local_zip").val();
	if(local_zip != ""){
		$("#billing_zip").val(local_zip);
	}
	return false;
}




function slideshow(imgs,cont,loop,rand){
	var delay = 2000; // 2 seconds
	var fadeTime = 800; // .8 seconds
	var current_index = 0;
	
	var rand = (rand) ? true : false;
	var loop = (loop === false) ? false : true;
	var images = new Array();
	var imgTotal = 0;

	$("#"+imgs).children("img").each(function(e){
			images[images.length] = $(this);
		});
	if(rand) images = randomize(images);
	imgTotal = images.length;
	transition(cont);
	
	function transition(cont){
		if(current_index+1 == imgTotal){
			current_index = 0;
			if(rand) images = randomize(images);
		} else {
			current_index++;
		}
		
		var current = images[current_index];
		
		var newObj = current.clone();
		newHTML = $("<div></div>").append( newObj ).html();
		newHTML = "<div class=\"slide current\" style=\"display:none;\">"+newHTML+"</div>\n";
		$("#"+cont+" div.last-one").removeClass("last-one");
		$("#"+cont+" div.current").addClass("last-one");
		$("#"+cont+" div.last-one").removeClass("current");
		$("#"+cont).prepend(newHTML);
		$("#"+cont).show();
		$("#"+cont+" div.current").fadeIn(fadeTime, function(e){
				if(loop){
					if($("#"+cont+" div").length > 2){
						$("#"+cont+" div:not(.current,.last-one)").remove();
					}
					setTimeout(function(t){ transition(cont); }, delay);
				}
			});
	}

}


$(document).bind("ready", function(r){
	setupInput2Toggles();
	$("#about_link").bind("click", function(e){
			var elHeight = $("#about_us").css("height").replace("px","");
			elHeight = parseInt(elHeight) + 30; // height of element plus existing top/bottom padding
			$("#about_us_wrap").animate({
					height: (($("#about_us_wrap").css("height") == "0px") ? elHeight+"px" : "0px")
				}, 700, "easeOutExpo");
			return false;
		});
	
	var tableHeight = null;
	$("#zip_toggler").bind("click", function(e){
			var zipObj = $(this);
			zipObj.blur();
			var action = (zipObj.html().toLowerCase().indexOf("show") == -1) ? "hide" : "show";
			tableHeight = (tableHeight) ? tableHeight : $("#zip_guide table").css("height").replace("px","");
			tableHeight = (tableHeight == "auto") ? 220 : tableHeight;
			$("#zip_guide").animate( {
					height: ((action == "show") ? (parseInt(tableHeight)+20)+"px" : "15px")
				}, 1000, "easeOutExpo");
			//$("#zip_guide").css("overflow",((action == "show") ? "visible" : "hidden"));
			if(action == "show"){
				$("#zip_guide table").show();
			}
			$("#zip_guide").removeClass("collapsed");
			zipObj.html(((action == "show") ? "Hide" : "Show"));
			setTimeout(function(t){
					if(action == "hide"){
						$("#zip_guide table").hide();
						$("#zip_guide").addClass("collapsed");
					}
				}, 500);
			return false;
		});
	$("input[name=by]").bind("click", function(e){
			if($(this).val() == "category"){
				$("#category_select").show();
				$("#name_list").hide();
				$("#category").focus();
			} else {
				$("#category_select").hide();
				$("#name_list").show();
				$("#company").focus();
			}
		});
	$("#member_search").bind("submit", function(e){
		if($("#category_select:visible").length > 0){
			if($("#category").val() == ""){
				var err = "Select a category before continuing.";
				$("#category").focus();
			}
		} else {
			if($("#company").val() == ""){
				var err = "Enter the name of a company to search for or select from list of letters below.";
				$("#company").focus();
			}
		}
		if(err){
			$("#validator").html(err);
			$("#validator").show();
			return false;
		} else {
			$("#validator").hide();
			return true;
		}
	});
	
	$("#copy_address").click(function(e){ return copyAddr(); });
	$("#copy_address").show();
	
	/*
	$("#second-nav-more a").click(function(e){
			$(this).blur();
			$("#second-nav-wrap").animate({
					marginLeft: ($("#second-nav-wrap").css("margin-left") == "0px") ? "-790px" : "0px"
				}, 600, "easeOutExpo");
			$("#second-nav-more a").html((($("#second-nav-more a").html().toLowerCase().indexOf("more") == -1) ? "More &raquo;" : "&laquo; Less"));
			return false;
		});
	
	var subNav = $("#second-nav-wrap ul.first").css("width").replace("px","");
	if(parseInt(subNav) > 880){
		$("#second-nav-more").show();
	}
	*/
	
	$("#search_form").bind("submit", function(e){
			if($("#s").val() == "enter search term or phrase"){
				$("#s").val('');
			}
			return true;
		});
	
	// slideshow(img_src_id, display_id, repeat, random)
	if($("#slideshow1").length > 0){
		slideshow("slideshow_images1","slideshow1", true, true);
	}
});

function shuffle(o){ //v1.0
	for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
	return o;
}

function randomize ( myArray ) {
	var i = myArray.length;
	if ( i == 0 ) return false;
	while ( --i ) {
		var j = Math.floor( Math.random() * ( i + 1 ) );
		var tempi = myArray[i];
		var tempj = myArray[j];
		myArray[i] = tempj;
		myArray[j] = tempi;
	}
	return myArray;
}