/*
--------------------------------------------------

UL
application.js

David Munger [david.munger@acquitygroup.com]
9/18/2008

Copyright © 2008 Acquity Group LLC

--------------------------------------------------
*/

$(document).ready(function() {

    // No fancy dropdowns for solution finder in IE6
    if (!(jQuery.browser.msie && (parseInt(jQuery.browser.version) == "6"))) {
        $(".chooseDropDown #industry, "
            + ".chooseDropDown #service, "
            + ".chooseDropDown #market").selectbox();

        $("#industry_container > ul li:first, "
            + "#service_container > ul li:first, "
            + "#market_container > ul li:first").addClass("firstOption");

        $("#industry_container, "
            + "#service_container, "
            + "#market_container").append('<div class="selectbox-wrapper-bottom"></div>');
    } else {
        $("#sf-form select#industry, #sf-form select#service, #sf-form select#market").addClass("styleNative");
    }

    // load custom select fields
	$("#documents, "
      + "#video, "
      // SF dropdowns were here
      + ".chooseDropDown #perspective, "
      + ".chooseDropDown #productgroup, "
      + ".chooseDropDown #producttype, "
      + "#questions, "
      + "#contactLocation, "
      + "#subject, "
      + "#filterBy, "
      + "#sortBy, "
      + "#resultsperpage, "
      + "fieldset.dropdown select").selectbox();

	$("#documents_container > ul li:first, "
      + "#video_container > ul li:first, "
      // SF dropdowns were here
      + "#perspective_container > ul li:first, "
      + "#questions_container > ul li:first, "
      + "fieldset.dropdown .selectbox-wrapper ul > li:first, "
      + "fieldset.dropdown #sortby_container.selectbox-wrapper ul > li:first, "
      + "fieldset.dropdown #location_container.selectbox-wrapper ul > li:first, "
      + "fieldset.dropdown #serviceline_container.selectbox-wrapper ul > li:first, "
      + "fieldset.dropdown #doctype_container.selectbox-wrapper ul > li:first, "
      + "fieldset.dropdown.small .selectbox-wrapper > ul li:first, "
      + "fieldset.dropdown.small #sortBy_container.selectbox-wrapper > ul li:first, "
      + "fieldset.dropdown.narrow .selectbox-wrapper > ul li:first, "
      + "fieldset.dropdown.xwide .selectbox-wrapper > ul li:first").addClass("firstOption");

	$("#documents_container, "
      + "#video_container, "
      // SF dropdowns were here
      + "#perspective_container, "
      + "#questions_container, "
      + ".dropdown .selectbox-wrapper, "
      + ".dropdown.small .selectbox-wrapper, "
      + "dropdown.narrow .selectbox-wrapper").append('<div class="selectbox-wrapper-bottom"></div>');


            $(function()
            {
				$('.turn-me-into-datepicker')
					.datePicker({inline:true})
					.bind(
						'dateSelected',
						function(e, selectedDate, $td)
						{
							console.log('You selected ' + selectedDate);
						}
					);
            });


	/* Handle the hiding and showing of the login controls */

	// We turn the collapsed, clickable login area on here,
	// so it degrades gracefully.. we don't want this to show
	// if we're showing the login stuff in <noscript>
	//$("#userLoginCollapsed").show();

	$("#userLoginCollapsed").click(function () {
		$(this).hide();
    	$("#userLoginExpanded").show();
	});

	$("#userLoginExpanded a").click(function () {
		$("#userLoginExpanded").hide();
		$("#userLoginCollapsed").show();
	});
	/* End hiding and showing login controls */


	/*
		Handle blanking and defaulting of prompt text in input fields.

		NOTE: the password prompt is a dummy, so that the prompt text
		can be readable. Otherwise having it in a password
		type field would render it unreadable. So, when
		the prompt field gains the focus, we hide it, reveal
		the real password field, and give it focus.

	*/

	$("#loginForm #loginUsername").focus(function() {
		if ($(this).val() == "User Name")
		{
			$(this).val("");
		}
	});

	$("#loginForm #loginUsername").blur(function() {
		if ($(this).val() == "")
		{
			$(this).val("User Name");
		}
	});

	$("#loginForm #passwordPrompt").focus(function() {
        /*
		if ($("#loginForm #password").val() == "Password")
		{
			$("#loginForm #password").val("");
		}
       */
		$("#loginForm #password").show().focus();
		$(this).hide();
	});

	$("#loginForm #password").blur(function() {
		if ($(this).val() == "")
		{
			$("#loginForm #passwordPrompt").show();
			$(this).hide();
			//$("#loginForm #password").val("Password");
		}
	});

	$("#mainContent #searchControl input").focus(function() {
		if ($(this).val() == "Search UL")
		{
			$(this).val("");
		}
	});

	$("#mainContent #searchControl input").blur(function() {
		if ($(this).val() == "")
		{
			$(this).val("Search UL");
		}
	});

	$("#fastQuoteForm #productDescription").focus(function() {
		if ($(this).val() == "Brief description of the product")
		{
			$(this).val("");
		}
	});

	$("#fastQuoteForm #productDescription").blur(function() {
		if ($(this).val() == "")
		{
			$(this).val("Brief description of the product");
		}
	});

	$("#fastQuoteForm #fileNumber").focus(function() {
		if ($(this).val() == "File Number")
		{
			$(this).val("");
		}
	});

	$("#fastQuoteForm #fileNumber").blur(function() {
		if ($(this).val() == "")
		{
			$(this).val("File Number");
		}
	});

	$("#fastQuoteForm #companyName").focus(function() {
		if ($(this).val() == "Company Name")
		{
			$(this).val("");
		}
	});

	$("#fastQuoteForm #companyName").blur(function() {
		if ($(this).val() == "")
		{
			$(this).val("Company Name");
		}
	});

	$("#fastQuoteForm #purchaseOrder").focus(function() {
		if ($(this).val() == "Purchase Order or Identifier")
		{
			$(this).val("");
		}
	});

	$("#fastQuoteForm #purchaseOrder").blur(function() {
		if ($(this).val() == "")
		{
			$(this).val("Purchase Order or Identifier");
		}
	});
	/* end blanking and defaulting of prompt text


	/* Flyover employee quotes */

	$("#careerSideBar .employeeProfileThumbnails li a").mouseover(function() {

		// First, clear whatever's showing
		$("#careerSideBar .quote").hide();
		$("#careerSideBar .quoteAttribution").hide();

		// Now clear away the selected style
		$("#careerSideBar .employeeProfileThumbnails li a").removeClass("toggledOn");

		// Show the info for the moused over employee
		thisId = $(this).attr("id");
		thisIdNum = thisId.substring(thisId.length -2, thisId.length);
		$("#quote-" + thisIdNum).show();
		$("#attrib-" + thisIdNum).show();

		// Apply the style to show the thumbnail as selected
		$(this).addClass("toggledOn");
	});

	// trigger the first one, to default first, when page loads */
	$("#emp-01").mouseover();

	/* end flyover employee quotes */


	/* Slide show! */
	$("#slideshow").cycle({
		fx: 'uncover',
		speed: 250,
		timeout: 5000,
		pause: 1,
		pager: '#slideControls'
	});

	/* popout form - quote request */
	$("#fastFormOnline").click(function () {
		$("#fastQuote").slideToggle();
	})
	$("#fastQuote .head a.close").click(function () {
		$("#fastQuote").slideToggle();
	})

	/*
		Custom list box stuff

		Fancy formatted ul replaces the standard fixed-size
		listbox implemented with select. This is combined with
		the use of jScrollPane to give a fancy scrollbar.
		The code below hides the select, and then reveals the
		ul and puts the fancy scrollbars on the wrapper around
		the ul. The hiding and showing is done for degrading to
		the standard select box for users w/o javascript or with
		js disabled.

		Within the UL, the actual value for the choice needs
		to go in a span tag with the class of "value." The code
		below will take the html contents of the "value" span
		and set them to be the value for the associated select.

		** The ul must have an id that is the same as the select
		field it is replacing.

		** This will only work in windows due to the ctrl-key multi-select
		deal.

		** This has also been

	*/

	if (BrowserDetect.browser == "Explorer")
	{
		if ($(".customListboxWrapper").length)
		{
			$("fieldset.listbox select").hide();
			$(".customListboxWrapper").show();
			$(".customListboxWrapper").jScrollPane();

			// disable text selection so that ctrl-click won't also select the text in the li
			$(".customListboxWrapper ul.customListboxSelect li").attr('unselectable', 'on');

			$("ul.customListboxSelect li").click(function (e) {

				// select associated option in the form
				listboxId = $(this).parent("ul").attr("id");
				selectId = listboxId.substring(8, listboxId.length); // strip out 'listbox' to get select ID
				selectedValue = $(this).children(".value").html();
				targetOptionTag = "option#" + selectId + "-" + selectedValue;

				if (! e.ctrlKey)
				{
					$("#" + selectId + " option").removeAttr("selected");
				}
				$(targetOptionTag).attr("selected","selected");

				// apply visual styling to selected li
				// (turn off current selection first)
				if (! e.ctrlKey)
				{
					$("ul#listbox-" + selectId + " li").removeClass("selected");
				}
				$(this).addClass("selected");
			});
		}
	}
	/* end Custom list box stuff */

	/* dropdown menus load URLs on select */
	$(".downloads select, #questions, .makeDropdownLinks select").change(function () {
		if ($(this).val() != 'none')
		{
			window.location = $(this).val();
		}
	});

	$(".selectbox-wrapper ul, .selectbox-wrapper-bottom").pngfix();
});

/* Suckerfish function for IE, to allow focus on non-a tags */
sfHover = function() {
	if (document.getElementById("topNav") != null) {
		var sfEls = document.getElementById("topNav").getElementsByTagName("LI");
		for (var i=0; i<sfEls.length; i++) {
			sfEls[i].onmouseover=function() {
				this.className+=" sfhover";
			}
			sfEls[i].onmouseout=function() {
				this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
			}
		}
	}
}

if (window.attachEvent) window.attachEvent("onload", sfHover);
