﻿var PhotographerSearch =
{
	IsValid: true,
	ShowHideLabel: function(textbox, e)
	{
		var label = $("label[for=" + textbox.attr("id") + "]");
		var forceHide = e && (e.which == 40 || e.which == 38); // 40 = up arrow, 38 = down arrow

		// if it's not a "ghost" label leave it alone
		if (label.length > 0 && typeof $.data(label.get(0), "data") == "undefined" || !$.data(label.get(0), "data").ghost)
			return;

		if (forceHide || textbox.val().length > 0)
			label.hide();
		else
			label.show();
	},
	OnTextBoxClick: function()
	{
		var textbox = $(this);
		PhotographerSearch.ShowHideLabel(textbox);
	},
	OnTextBoxKeydown: function(e)
	{
		var textbox = $(this);
		PhotographerSearch.ShowHideLabel(textbox, e);
	},
	OnTextBoxKeyup: function(e)
	{
		var textbox = $(this);
		PhotographerSearch.ShowHideLabel(textbox, e);
	},
	OnTextBoxFocus: function()
	{
		var textbox = $(this);
		textbox.select();
	},
	OnTextBoxBlur: function()
	{
		var textbox = $(this);
		PhotographerSearch.ShowHideLabel(textbox);
	},
	OnDropDownListCountryChange: function()
	{
		var country = $("select[id$=DropDownListCountry]");
		var state = $("select[id$=DropDownListState]");

		if (country.val() == "United States" || country.val() == "Canada")
			state.removeAttr("disabled");
		else
		{
			var option = $(state.find("option:first"));
			state.val(option.val());
			state.attr("disabled", "disabled");
		}
	},
	GenerateGhostLabel: function(target, text)
	{
		var label = $("<label/>");
		label.attr("for", target.attr("id"))
			.text(text)
			.css("position", "absolute")
			.css("cursor", "text")
			.css("color", "#999")
			.css("padding", "0 0 0 5px")
			.css("z-index", "100")
			.css("display", "inline")
			.css("text-align", "left")
			.css("width", "");

		target.parent().append(label);
		$.data
		(
			label.get(0),
			"data",
			{
				ghost: true
			}
		);

		return label;
	},
	RenderFormLabels: function()
	{
		var zipCodeTb = $("input[id$=TextBoxZipCode]");
		var zipCodeLabel = $("label[for=" + zipCodeTb.attr("id") + "]");
		if (zipCodeLabel.length === 0)
			zipCodeLabel = PhotographerSearch.GenerateGhostLabel(zipCodeTb, "zip code");

		zipCodeLabel.css("left", zipCodeTb.position().left + "px")
			.css("top", zipCodeTb.position().top + 2 + "px");

		PhotographerSearch.ShowHideLabel(zipCodeTb);
	},
	OnButtonPhotographerSearchClick: function()
	{
		if (PhotographerSearch.IsValid)
		{
			$(this).val("Searching...")
				.addClass("box-button-searching");
		}

		PhotographerSearch.IsValid = true;
	},
	OnUlSidenavLiHoverOver: function()
	{
    	$(this).find("div.popup").stop().show();
    	$(this).find("div.popup2").stop().show();
	},
	OnUlSidenavLiHoverOut: function()
	{
    	$(this).find("div.popup").stop().hide();
    	$(this).find("div.popup2").stop().hide();
	},
	OnDocumentReady: function()
	{
		PhotographerSearch.RenderFormLabels();

		$("#photographer-login").click(PhotographerSearch.RenderFormLabels);
		$(":text, :password").focus(PhotographerSearch.OnTextBoxFocus)
			.blur(PhotographerSearch.OnTextBoxBlur)
			.click(PhotographerSearch.OnTextBoxClick)
			.keydown(PhotographerSearch.OnTextBoxKeydown)
			.keyup(PhotographerSearch.OnTextBoxKeyup);
		$("input[id$=ButtonPhotographerSearch]").click(PhotographerSearch.OnButtonPhotographerSearchClick);
		$("select[id$=DropDownListCountry]").change(PhotographerSearch.OnDropDownListCountryChange);
		$("ul.sidenav li").hover(PhotographerSearch.OnUlSidenavLiHoverOver, PhotographerSearch.OnUlSidenavLiHoverOut);
	},
	CustomValidatorPhotographerSearch: function(sender, args)
	{
		var valid = false;
		var zip = $("input[id$=TextBoxZipCode]");
		var country = $("select[id$=DropDownListCountry]");
		var state = $("select[id$=DropDownListState]");

		if (state.find("option").length > 1 && (state.get(0).selectedIndex > 0 || zip.val().match(/^[0-9]{5,5}$/)))
			valid = true;
		else if (state.find("option").length == 1 || state.attr("disabled") == "disabled")
			valid = true;
		else if (zip.val().length > 0)
			valid = true;

		if (!valid)
		{
			PhotographerSearch.IsValid = false;
			args.IsValid = false;
		}
	}
};

$(document).ready(PhotographerSearch.OnDocumentReady);
