﻿
// Helper functions
function $id(id)
{
	return document.getElementById(id);
}

// Rensa upp flashobject på sidan
if (window.attachEvent)
{
	window.attachEvent("onbeforeunload", cleanUpObjectTags);
}
function cleanUpObjectTags()
{
	var arr = document.all.tags("object");
	if (arr && arr.length) {
		for (var i=arr.length-1; 0<=i; i--) {
			arr[i].removeNode(true);
		}
	}
}

function hideSiteMap() {
    $('#SiteMapContainer').slideUp(); 
    $('#SiteMapActivationButton').fadeIn();
}
function showSiteMap() {
    $('#SiteMapContainer').slideDown(); 
    $('#SiteMapActivationButton').fadeOut();
}

function showSearchResults() {
    $('#SearchResultContainer').slideDown();
}
function hideSearchResults() {
    $('#SearchResultContainer').slideUp();
}

function performSearch() {
    hideSiteMap();

    $.ajax({
        dataType: "html",
        method: "get",
        url: "DefaultAjaxResponse.aspx",
        data: "search=" + encodeURIComponent($('#SearchFieldInput').attr('value')),
        beforeSend: function() {
            $("#loading").show();
        }, //show loading just when link is clicked
        complete: function() { $("#loading").fadeOut(); }, //stop showing loading when the process is complete
        success: function(html) { //so, if data is retrieved, store it in html
            $("#SearchResultContainer").html(html); //show the html inside div
            $('#SearchResultContainer a').click(dynamicLinkResponse);
            showSearchResults();
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            $("#SiteContent").html("<img src=\"../images/loading_error.png\" style=\"margin:30px 0 0 100px;\" />"); //'<div class="text"><h1>Timeout</h1>Det tog för lång tid att hämta sidan du begärde.<br />Var god försök igen.</div>'); //show the html inside div
            //							// typically only one of textStatus or errorThrown will have info
            //							this; // the options for this ajax request
            //							if (textStatus == 'timeout')
            //								$("#SiteContent").html('<div class="text"><h1>Timeout</h1>Det tog för lång tid att hämta sidan du begärde.<br />Var god försök igen.</div>'); //show the html inside div
            //							else if (textStatus == 'error')
            //								$("#SiteContent").html('<div class="text"><h1>Fel</h1>Något gick fel när sidan skulle laddas.<br />Var god försök igen.</div>'); //show the html inside div
            //							else
            //								alert(XMLHttpRequest + " or " + errorThrown);
        }
        //					, timeout: 1
    });
    //document.location.href = "?siteId=-1&search=" + $('#SearchFieldInput').attr('value');
}

function clearSearch() {
    hideSearchResults();
    $('#SearchFieldInput').get(0).value = '';
}

function handleSearchInput(e)
{
    var charCode;
    
    if(e && e.which){
        charCode = e.which;
    }else if(window.event){
        e = window.event;
        charCode = e.keyCode;
    }

    // Return: search
    if (charCode == 13) {
        performSearch();
    }
    // Esc: clear search
    else if (charCode == 27) {
        clearSearch();
    }
    // (Backspace OR Del) AND Empty input field: hide search results
    else if ((charCode == 8 || charCode == 46) && $('#SearchFieldInput').get(0).value == '') {
        hideSearchResults();
    }
}

function handleSearchFieldTitle() {
    if ($('#SearchFieldInput').get(0).value == lang_SearchTitle) {
        $('#SearchFieldInput').get(0).value = '';
        $('#SearchFieldInput').removeClass('search_field_title');
    }
}
