String.prototype.trim = function () {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}

$(document).ready(function(){
	initHover();
	$("fieldset#login input#email,fieldset#register input#email, fieldset#forgot-password input#email, input#q, input#admin-manual-host").focus();
	$("div#quick-find a.now").click(function(){$(this).parent().parent().submit(); return false;});
	$("table.host-listing tr:odd").addClass("alt");
	$("div#host-details tr:odd").addClass("alt");
	$("a#delete-review").click(function(){if(confirm('Are you sure you want to delete this review? (It will be lost forever in the firey pits of hell)')){return true;} else {return false;};});
	$('a[href^="http://"]').filter(function(){return this.hostname && this.hostname !== location.hostname;}).attr('target', '_blank');
	$("a#contact-us").attr("href","mailto:info@whathost.com").text("info@whathost.com");
	validateForms();
	enableDropdowns();
	enableTopThreeDropdown();
	manualHost();
	hostList();
	if($("div.add-review").length>0){addSliders();}
});

function initHover(){
	$(".selector").hover(
		function(){$(this).addClass("hover");},
		function(){$(this).removeClass("hover");}
	);
}

function validateForms(){
	validateRegistrationForm();
	validateAddHostForm();
	validateReviewForm();
	validateLoginForm();
	validateForgotPasswordForm();
	validateSearchForm();
}

function manualHost(){
	$("a#host-not-listed").click(function(){
		$("ol#host-list").fadeOut(1000);
		$("ol#manual-host-entry").fadeIn(1000,function(){
			$("input#manual-host").focus();
		});
		return false;
	});
}

function hostList(){
	$("a#host-list").click(function(){
		$("ol#host-list").fadeIn(1000);
		$("ol#manual-host-entry").fadeOut(1000);
		return false;
	});
}

function enableDropdowns(){
	$("li.selector ul li a").click(function(){
		$(this).parent().parent().parent().children("span").text($(this).text());
		$(this).parent().parent().parent().children("input").val($(this).attr("href"));
		$(this).parent().parent().parent().removeClass("hover");
		return false;
	});
}

function enableTopThreeDropdown(){
	$("div#top3-selector li.selector ul li a").click(function(){
		$(this).parent().parent().parent().children("span").text($(this).text());
		$(this).parent().parent().parent().children("input").val($(this).attr("href"));
		$(this).parent().parent().parent().removeClass("hover");
		$("div#top3").load("index.php/hosts/top-three/" + $(this).attr("href"), function(){
			
		});
		return false;
	});
}

function addSliders(){
	 addSlider("reliability");
	 addSlider("support");
	 addSlider("features");
	 addSlider("pricing");
}

function addSlider(name){
	$("div#" + name + "-slider").slider({
		value:10,
		min: 1,
		max: 10,
		step: 1,
		value:5,
		slide: function(event, ui) {
			$("input#" + name).val(ui.value);
		}
	});
	$("input#" + name).val($("div#" + name + "-slider").slider("value"));
}

function validateRegistrationForm(){
	$("div.register form").validate({
		ignore: ".ignore",
		rules: {
			email: {
				required: true,
				email: true,
				remote: {
					url: "index.php/accounts/validate_email",
					type: "post"
				}
			},
			password: {required: true},
			confirm_password: {required: true, equalTo: "#password"},
			name: {required: true},
			website_url: {url: true}
		},
		messages: {
			email: {
				required: "Please enter your email address",
				email: "Please enter a valid email",
				remote: "This email address is already in use"
			},
			password: {
				required: "Please enter your password"
			},
			confirm_password: {
				required: "Please confirm your password",
				equalTo: "Must match password"
			},
			name: "Please enter your name",
			website_url: {
				url: "Please enter a valid url"
			}
		}
	});
}

function validateAddHostForm(){
	$("div.add-host form").validate({
		ignore: ".ignore",
		rules: {
			manual_host: "required",
			host_website_url: {url: true},
			host_affiliate_url: {url: true},
			whathost_host_email: {
				email: true
			},
			host_image: {accept: "jpg|jpeg|gif|png"}
		},
		messages: {
			manual_host: {
				required: "Please enter the Host Name"
			},
			host_website_url: {
				url: "Please enter a valid host url"
			},
			host_affiliate_url: {
				url: "Please enter a valid affiliate url"
			},
			whathost_host_email: {
				email: "Please enter a valid email"
			},
			host_image: {
				accept: "Please choose an image with one of the following extensions (jpg, jpeg, gif, png)"
			}
		}
	});
}

function validateReviewForm(){
	$("div.add-review form").validate({
		ignore: ".ignore",
		rules: {
			website_url: {url: true},
			email: {
				required: true,
				email: true
			},
			comments: {required:true}
		},
		messages: {
			website_url: {
				url: "Please enter a valid url"
			},
			email: {
				required: "Please enter your email address",
				email: "Please enter a valid email"
			},
			comments: {
				required: "Please enter your comments about the host"
			}
		}
	});
}

function validateLoginForm(){
	$("div.login form").validate({
		ignore: ".ignore",
		rules: {
			email: {
				required: true,
				email: true
			},
			password: {required: true}
		},
		messages: {
			email: {
				required: "Please enter your email address",
				email: "Please enter a valid email"
			},
			password: {
				required: "Please enter your password"
			}
		}
	});
}

function validateForgotPasswordForm(){
	$("div.forgot-password form").validate({
		ignore: ".ignore",
		rules: {
			email: {
				required: true,
				email: true
			}
		},
		messages: {
			email: {
				required: "Please enter your email address",
				email: "Please enter a valid email"
			}
		}
	});
}

function validateSearchForm(){
	$("button#search-button").click(function(){
		if(""==$("input#q").val()){
			attention("input#q",200);
			return false;
		}
	});
}

function attention(selector,speed){$(selector).fadeOut(speed).fadeIn(speed).fadeOut(speed).fadeIn(speed).fadeOut(speed).fadeIn(speed, function(){$(this).val("").focus();});}
