var life_drop_obj = {
	active:0,
	to:null,
	re:/[^a-z0-9]+/gi
};
	
var petlife_config = {
	lbl:["", "Home", "1-7<br/>Weeks", "8-14<br/>Weeks", "15-20<br/>Weeks", "6-12<br/>Months", "1-2<br/>Years", "Adulthood"],
	age:{
		2:[0, 55], // through 7 weeks (in days)
		3:[55, 104], // through 14 weeks
		4:[104, 146], // through 20 weeks
		5:[146, 355], // through 52 weeks
		6:[355, 729], // through 104 weeks
		7:[729] // after 104 weeks
	},
	your:{
		dog:["your pup", "your pups"],
		cat:["your cat", "your cats"]
	}
};

function life_nav_init(t)
{
	var arr = eval(t);

	$("div#"+ t +" > ul.top")
		.empty()
	.end();
	
	$.each(arr, function(i, o){
		
		var has_sub = (typeof(o) === "object");
		
		// top link
		$("<a></a>")
			.attr("href", (!has_sub ? o : "#"))
			.append("<span>"+ petlife_config.lbl[i] +"</span>")
			.bind("mouseover", function(){
				$("div#"+ t +" > ul.top > li > a")
					.removeClass("over")
				.end();
				
				$("div#"+ t +" > ul.drop")
					.hide()
				.end();
				
				if (has_sub) {
					$("ul#"+ t +"_drop_"+ i).trigger("mouseover").end();
				} else {
					clearTimeout(life_drop_obj.to);
				};
			})
			.bind("mouseout", function(){
				if (has_sub) {
					$("ul#"+ t +"_drop_"+ i).trigger("mouseout").end();
				} else {
					life_nav_active(t);
				};
			})
			.appendTo("div#"+ t +" > ul.top")
			.wrap("<li class='"+ t +"_"+ i +"'></li>")
		.end();
		
		// drop navs
		if (has_sub) {	
			var ul = $("<ul></ul>").hide().attr("id", t +"_drop_"+ i).addClass("drop");
			
			$.each(o, function(n, l){
				var li = $("<li></li>").addClass(n + 1 === o.length ? "end" : "");
				$("<a>"+ l +"</a>")
					.attr("href", life_dept + life_prefix + l.replace(/\//, "").replace(life_drop_obj.re, life_delim) + life_suffix)
					.appendTo(li)
				.end();
				
				ul.append(li);
			});
			
			$("div#"+t).append(ul);
		}
	});
	
	$("div#"+ t +" > ul.drop")
		.bind("mouseover", function(){
			$("div#"+ t +" > ul.top > li")
				.children("a")
					.removeClass("over")
				.end()
				.filter("."+ $(this).attr("id").replace("_drop", ""))
					.children("a")
						.addClass("over")
					.end()
				.end()
			.end();
			
			$(this).show().end();
			
			clearTimeout(life_drop_obj.to);
		})
		.bind("mouseout", function(){
			var ul = this;
			
			life_drop_obj.to = setTimeout(function(){
				$(ul).hide();
				life_nav_active(t);
			}, 250); 
		})
	.end();
	
	// active page
	var p = window.location.pathname.split("/").pop();
	var q = p.replace(life_prefix, "").replace(life_suffix, "").replace(life_drop_obj.re, " ");
	
	for(var i in arr) {
		if(typeof(arr[i]) === "object"){
			for(j in arr[i]){
				arr[i][j] = arr[i][j].replace(/\/+/, "");
			};
		};
		
		if ((typeof(arr[i]) === "object" && $.inArray(q, arr[i]) !== -1) || (typeof(arr[i]) === "string" && arr[i] === p)) {
			$("div#"+ t + " > ul.top > li."+ t +"_"+ i +" > a")
				.addClass("over")
			.end();
			
			life_drop_obj.active = parseInt(i);
			
			if (typeof(arr[i]) === "object") {
				$("ul#"+ t +"_drop_"+ i)
					.show()
					.find("li:eq("+ $.inArray(q, arr[i]) +") > a")
						.addClass("over")
					.end()
				.end();
			}
		}
	};
	
	// my pets
	if (petlife_my_pets !== null) {
		for (var x in petlife_my_pets)
		{
			var y = petlife_my_pets[x];
			var pet = (new Date(y.birth_year, y.birth_month - 1, y.birth_day)).getTime();
			var age = (((new Date()).getTime() - pet) / 1000 / 86400);
			
			for (var z in petlife_config.age) {
				if (age > petlife_config.age[z][0] && age <= petlife_config.age[z][1])
					break
			};
			
			if ($("ul.top li[class$='_"+ z +"'] > a > strong").length) {
				// multiple
				$("ul.top li[class$='_"+ z +"'] > a > strong")
					.text("("+ petlife_config.your[petlife_type][1] +")")
				.end();
			} else {
				// single
				$("ul.top li[class$='_"+ z +"'] > a")
					.prepend("<strong>("+ petlife_config.your[petlife_type][0] +")</strong>")
					.children("span")
						.each(function(){
							$(this).css("top", (4 + parseInt($(this).css("top"))) + "px");
						})
					.end()
				.end();				
			}
		}
	};
	
	// registration window
	$(window).load(function(){
		if (p.length > 0 && p.search(life_prefix) !== -1 && $.cookie("petlife_prereg") === null) {
			//life_register_modal();
		}
	});
}


function life_register_modal()
{
	$("div.petlife_prereg").modal({
		position: [100,],
		closeHTML: "<a id='prereg_close'>Close</a>",
		closeClass: "close",
		
		onOpen: function(modal){
			modal.overlay.fadeIn("fast", function(){
				modal.container.show();
				modal.data.show();
			});
			$("a#prereg_close").bind("click", function(){
				$.post(life_prereg_action, {action:"close"});
			});
		}
	});
}

function life_register_modal_thankyou(src)
{
	$("div#petlife_prereg_thankyou").modal({
		position: [100,0],
		closeHTML: "<a id='prereg_close'>Close</a>",
		closeClass: "close",
		onOpen: function(modal){
			modal.overlay.show();
			modal.container.show();
			$(modal.data)
				.show()
				.find("iframe")
					.remove()
				.end()
			.end();
			
			setTimeout("$.modal.close();",5000);
		}
	});
	$("<iframe></iframe>")
		.attr("src", "/"+ (/kitten/.test(src) ? "kitten" : "puppy") + "life/conversion.html")
		.appendTo("div#petlife_prereg_thankyou")
	.end();
}


function life_nav_active(t)
{
	$("div#"+ t +" > ul.top > li > a")
		.removeClass("over")
	.end()
		
	if (life_drop_obj.active >= 1) {
		$("div#"+ t +" > ul.top > li:nth-child("+ life_drop_obj.active +") > a")
			.addClass("over")
		.end();
		
		$("ul#"+ t +"_drop_"+ life_drop_obj.active)
			.show()
		.end();
	}
}


function life_nav_prevnext(t, l)
{
	var u = eval(t);
	var p = window.location.pathname.split("/").pop();
	
	var a = [];
	for(var x in u) {
		if (typeof(u[x]) === "string") {
			a.push(u[x]);
		} else {
			for (var y in u[x]) {
				a.push(life_prefix + u[x][y].replace(/\/+/, '').replace(life_drop_obj.re, life_delim) + life_suffix);
			}
		}
	};
	
	var b = "";
	for(var z in a)
	{
		if (a[z] === p || a[z].search(p) > 0) {
			z = parseInt(z);
			if (typeof(a[z-1]) !== "undefined" && /index.php$/.test(a[z-1]) === false && a[z-1] !== "#") {
				if(/^\//.test(a[z-1]) === true){
					b += "<a href='"+ a[z-1] +"'><< Younger "+ l +"</a>";
				}else{
					b += "<a href='"+ life_dept + a[z-1] +"'><< Younger "+ l +"</a>";
				};
				if (b.length > 0) {
					b += "&nbsp;&nbsp;|&nbsp;&nbsp;";
				};
			};
			
			if (typeof(a[z+1]) !== "undefined") {	
				if(/^\//.test(a[z+1]) === true){
					b += "<a href='"+ a[z+1] +"'>Older "+ l +" >></a>";
				}else{
					b += "<a href='"+ life_dept + a[z+1] +"'>Older "+ l +" >></a>";
				}
			}
		}
	};
	
	document.write( b );
}

function life_register_submit(frm)
{
	try {
		var valid = true;
		var params = {};
		
		$("div#form_error").empty();
		
		$.each(frm.elements, function(i, e){
			if ($.inArray($(e).attr("type"), ["submit"]) === -1) {	
				var n = $(e).attr("name");
				if (/\[[0-9]+\]$/.test(n) === true) {
					n = n.replace(/\[[0-9]+\]/, "[]");
					if (typeof(params[n]) === "undefined") {
						params[n] = [];
					};
					switch ($(e).attr("type"))
					{
						case "radio":
							if ($(e).attr("checked") === true) {
								params[n].push($(e).val().trim());
							};
							break;
							
						case "select-one":
							params[n].push(parseInt($(e).val()));
							break;
							
						default:
							params[n].push($(e).val().trim());
					};
				} else {
					params[n] = $(e).val().trim();
				};
			};
		});
		
		// entered first name & last name
		if (params.first_name.trim().length === 0 || params.last_name.trim().length === 0) {
			valid = false;
			$("div#form_error")
				.append("Please enter your first and last names.<br/>")
			.end();
		};
		
		// verify email
		if (/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(params.email_address) === false) {
			valid = false;
			$("div#form_error")
				.append("Please enter a valid email address.<br/>")
			.end();
		};
		
		// verify all entered pets
		for (var i in params["pet_name[]"])
		{
			if (params["pet_name[]"][i].trim().length === 0 || params["bd_day[]"][i] === 0 || params["bd_month[]"][i] === 0 || params["bd_year[]"][i] === 0) {
				valid = false;
				$("div#form_error")
					.append("Name and birthdate are required for all pets.<br/>")
				.end();				
				break;
			}
		};
		
		if (valid === true) {
			// saving...
			$(frm).find("input[type='submit']").val("Saving...").attr("disabled", true);
			
			var src = life_dept.replace(/[^a-z]+/i, '');
			
			setTimeout(function(){
				params.source = src;
				params.action = "register";
				$.post(life_prereg_action, params, function(o, r){
					$.modal.close();
					life_register_modal_thankyou(src);
				});
			}, 750);
		}
	} catch (e) {};
	
	return false;
}

var life_prereg_action = "/puppylife/petlife_prereg.php";

function life_register_append(a)
{
	var pi = $("form[name='petlife_prereg']").find("input[name='pet_index']");
	pi.val(parseInt(pi.val()) + 1);
	pi = pi.val();

	$("div#multi")
		.clone()
		.find("label")
			.each(function(i, lbl){
				$(lbl)
					.attr("for", $(lbl).attr("for").replace("[0]", "["+ pi +"]"))
				.end();
			})
		.end()
		.find("input,select")
			.each(function(i, inp){
				$(inp)
					.attr("name", $(inp).attr("name").replace("[0]", "["+ pi +"]"))
				.end();
			})
		.end()
		.find("input")
			.each(function(i, inp){
				$(inp)
					.val($("div#multi").find("input:eq("+ i +")").attr("defaultValue"))
				.end();
			})
		.end()
		.find("select")
			.each(function(i, sel){
				$(this)
					.attr("selectedIndex", 0)
				.end();
			})
		.end()
		.append( $("<a>Remove</a>").bind("click", function(){ $(this).parent().remove(); }) )
		.insertBefore( $(a).parent() )
	.end();
}

if (window.$) {
	$.extend(String.prototype, {
		trim: function(){
			var str = this.replace('&nbsp;', " ");
			return str.replace(/^\s+|\s+$/g, "");
		}
	});
}