/*
 * Initialises the javascript dependancies.
 */
$(document).ready( function() {
    // hide and show the search options
    $("#searchInput").focus(function(){$("#searchOptionsFrame").show(200);if($(this).val() == searchValue){$(this).val("");}});
    $("#searchClose").click(function(){$("#searchOptionsFrame").hide(200);});
    
    // Click anywhere on page actions
    $(document).click(function(e) { 
        var target = e.target;
        if (!$(target).is('#searchInput') && !$(target).is('#searchOptionsFrame') && !$(target).parents().is('#searchOptionsFrame')) {
            $('#searchOptionsFrame').hide();
        }
    });

    focusFirstInput();
});

/*
* Search form submits.
*/
function submitSearch(fromForm)
{	
	var iframeContents = $("#searchOptionsFrame").contents();
	
	var title = iframeContents.find("input[ id='searchInTitle' ]").attr('checked');
	var description = iframeContents.find("input[ id='searchInDescription' ]").attr('checked');
	var content = iframeContents.find("input[ id='searchInContent' ]").attr('checked');
	
	$("#searchInTitle").attr("checked", title);
	$("#searchInDescription").attr("checked", description);
	$("#searchInContent").attr("checked", content);
	
	if (!fromForm)
	{
		$("#search").submit();
	}
	else
	{
		return true;
	}
}

/*
 * Select first input field for focus.
 */
function focusFirstInput()
{ 
    var form = $('form:first');
    if(form.length && form.attr('name') != 'search')
    {
        var elem = $('input:visible', form).get(0);
        var select = $('select:visible', form).get(0);
        if (select && elem)
        {
            if (select.offsetTop < elem.offsetTop)
            {
                elem = select;
            }
        }
    
        var textarea = $('textarea:visible', form).get(0);
        if (textarea && elem && textarea.offsetTop < elem.offsetTop)
        {
            elem = textarea;
        }
    
        if (elem)
        {
            elem.focus();
        }
    }
}


/*
 * Sets up the terms dialog which can be viewed from any page.
 */
function showTermsDialog()
{
	// dynamically load the dialog and terms CSS   
    $("head").append("<link>");
    css = $("head").children(":last");
    css.attr({rel: "stylesheet", type: "text/css", href: contextPath+"/css/ui/smoothness-1.0.css"});
    $("head").append("<link>");
    css = $("head").children(":last");
    css.attr({rel: "stylesheet", type: "text/css", href: contextPath+"/css/terms-1.0.css"});


    // dynamically load the dialog JS and show the dialog.  
	$.getScript(contextPath+"/js/ui/dialog/jquery-ui-1.7.2.bgiframe.js", function() {
		$.getScript(contextPath+"/js/ui/dialog/jquery-ui-1.7.2.dialog.js", function() {
			$('#viewContent').css('width', '0%');
			$('#termsDialog').dialog({
				autoOpen:false, modal:true, bgiframe:true, width:600, height:460,
				close: function(){
					$('#viewContent').css('width', '100%');
				}
			});
			$('#termsDialog').dialog('open');
			$('#termsPlaceHolder').html('<p style="text-align:center;padding-top:200px;"><img src="'+ contextPath +'/images/loading.gif"/></p>');
			$('#termsPlaceHolder').load(contextPath+'/terms?uuid=' + new Date().getTime() + ' #termsContent');
		});
	});
}
