/* Function to addEventListener to onload
 * @param func - a function which should be executed once the page has loaded
 * http://simon.incutio.com/archive/2004/05/26/addLoadEvent
 * it will work even if something has previously been assigned to window.onload
 * without using addLoadEvent itself. 
 */

/*
 * Function to clear a text field
 * @param thefield
 *     the field to clear
 */

/*
 * rClearText
 * Clears an input box if text is equal to default text onfocus. Adds default back on blur if input box is empty
 */
function rClearText(el) {
	this.initialize(el);
}
rClearText.prototype = {
	initialize: function(el) {
		$(el).focus(function() {
			if (el[0].value == el[0].defaultValue) { // Text hasn't been changed yet
				el[0].value = '';
			}
		});
		$(el).blur(function() {
			if (el[0].value == '') { // Text hasn't been changed yet
				el[0].value = el[0].defaultValue;
			}
		});
	}
}
function rClearTextFocus(el) {
	if (el.value == el.defaultValue) { // Text hasn't been changed yet
		el.value = '';
	}
}

function rClearTextBlur(el) {
	if (el.value == '') { // Text hasn't been changed yet
		el.value = el.defaultValue;
	}
}
 
/*
 * rDropDown
 * Adds class of 'hover' to LI onmouseover. Removes class onmouseout. Adds iFrame fix for IE
 */
function rDropDown() {
	this.initialize();
}
rDropDown.prototype = {
	open: false,
	timeout: false,
	openLi: null,
	initialize: function() {
	    var userAgent = navigator.userAgent.toLowerCase()
        if (userAgent.indexOf('mac') != -1 && userAgent.indexOf('firefox')!=-1)
            $("#TopNav > ul").addClass('macFF');
            
		var lis = $("#TopNav > ul > li");
		for (var i=0; i<lis.length; i++) {
			$(lis[i]).bind('mouseover', {parentThis: this, li:lis[i]}, function(params) {
				params.data.parentThis.show(params.data.li);
			});
			$(lis[i]).bind('mouseout', {parentThis: this, li:lis[i]}, function(params) {
				params.data2 = params.data;
				params.data.parentThis.timeout = setTimeout(function() {
					params.data = params.data2;
					params.data.parentThis.hide(params.data.li);
				}, 1);
			});
			$(lis[i]).bind('click', {parentThis: this, li:lis[i]}, function(params) {
			    $(params.data.li).find("a").blur();
			    params.data.parentThis.hide(params.data.li);
			});
		}
	},
	show: function(li) {
		if (this.openLi && this.openLi != li) {
			this.hide(this.openLi);
		}
		if(this.timeout){
			clearTimeout(this.timeout);
			this.timeout = false;
		}
		if(this.open){
			return;
		}
		if ($(li).hasClass('hasSub')) {
			$(li).addClass('hoverSub');
		}
		else {
			$(li).addClass('hover');
		}
		this.openLi = li;
		this.open = true;
		this.iframeFix(li);
	},
	hide: function(li) {
		if(!this.open){
			return;
		}
		$(li).removeClass('hover');
		$(li).removeClass('hoverSub');
		this.open = false;
		if (this.iframe) {
			this.iframe.style.display = "none";
		}
	},
	iframeFix: function(li) {
		if(!document.all) {
			//return;
		}
		
		var subnav = $('div.subnav', li)[0];
		if (subnav) {
			if (!this.iframe) {
				this.iframe = document.createElement('iframe');
				this.iframe.style.position = 'absolute';
				this.iframe.frameBorder = 0;
				this.iframe.style.filter = 'alpha(opacity=0)';
				this.iframe.style.zIndex = -1;
				document.body.appendChild(this.iframe);
			}
			this.iframe.style.display = "block";
			this.iframe.style.top = $(subnav).offset().top + 'px';
			this.iframe.style.left = $(subnav).offset().left + 'px';
			this.iframe.style.width = $(subnav).width() + 'px';
			this.iframe.style.height = $(subnav).height() + 'px';
		}
	}
}

/*
 * onLoad functions
 * Initializes our functions on page load
 */
$(document).ready(function() {
    new rClearText($("#Search input"));
    new rDropDown();
    // add .last class to last LI item within Breadcrumb
    $("div#Breadcrumb ul li:last-child").addClass("last");

    // add enter key event to search text box in search container
    $("#Search input[@id$=search]").keydown(function(e) {
        if (e.keyCode == 10 || e.keyCode == 13) {
            $("#Search input:image").click();
            return false;
        }
    });

    // add enter key event to search text box in content container
    $("#Content input[@id$=SearchTerms]").keydown(function(e) {
        if (e.keyCode == 10 || e.keyCode == 13) {
            $("#Content input[@id$=Search]").click();
            return false;
        }
    });


    // send search queries directly to search page
    $("#Search input:image").click(function() {
        var searchVal = $("#Search input[@id$=search]").val();
        var appPath = (location.pathname.indexOf("/ChevronWebSite") >= 0) ? "/ChevronWebSite" : window.location.protocol + "//" + window.location.host;
        searchVal = escape(searchVal);
        window.location.href = appPath + "/search/?k=" + searchVal + "&text=" + searchVal.toLowerCase() + "&Header=FromHeader&ct=All%20Types";
        return false;
    }).keypress(function(e) {
        if (e.keyCode != 9) {
            $(this).click();
        }
    });

    // global search page functionality
    if ($("#Content select[@id$=ContentType], #Content select[@id$=Month], #Content select[@id$=Year]").length >= 5) {
        // disable month/year
        $("#Content select[@id$=ContentType]").bind("change", function() {
            if (this.selectedIndex != 0) {
                $("#Content fieldset>div:eq(2), #Content fieldset>div:eq(3)").show();
            } else {
                $("#Content fieldset>div:eq(2), #Content fieldset>div:eq(3)").hide();
            }
        }).trigger("change");
    }
});

