function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}
function submitBandJumpMenu()
{
	var el = document.getElementById("bandJumpMenu");
	if(el != undefined)
		if(el.selectedIndex > 0)
			window.location.href = el.options[el.selectedIndex].value;
	return false;
}
function submitPhotographerJumpMenu()
{
	var el = document.getElementById("photographerJumpMenu");
	if(el != undefined)
		if(el.selectedIndex > 0)
			window.location.href = el.options[el.selectedIndex].value;
	return false;
}
function submitGenreJumpMenu()
{
	var el = document.getElementById("genreJumpMenu");
	if(el != undefined)
		if(el.selectedIndex > 0)
			window.location.href = el.options[el.selectedIndex].value;
	return false;
}

var message = "Copyright rockarchive.com";

/////////////

function switchLearnMoreDiv(divName)
{
	document.getElementById(divName + "short").style.display = 'none';
	document.getElementById(divName + "learn").style.display = 'none';
	document.getElementById(divName + "long").style.display = 'block';
	return false;
}

function switchBackLearnMoreDiv(divName)
{
	document.getElementById(divName + "short").style.display = 'block';
	document.getElementById(divName + "learn").style.display = 'block';
	document.getElementById(divName + "long").style.display = 'none';
	return false;
}
//////////////////////

var firstShowPrices = true;

function showPrices(activeCode)
{
	if(firstShowPrices) {
		window.alert("Prices in currencies other than GBP are a guide only");
		firstShowPrices = false;
	}
	var codes = Array("gbp", "usd", "jpy", "eur");
	for(var i = 0; i < codes.length; i++) {
		var code = codes[i];
		var el, j = 0;
		while((el = document.getElementById("price_"+code+"_"+j+"_ex")) != undefined) {
			el.style.display = (activeCode == code) ? '' : 'none';
			j++;
		}
		j = 0;
		while((el = document.getElementById("price_"+code+"_"+j+"_inc")) != undefined) {
			el.style.display = (activeCode == code) ? '' : 'none';
			j++;
		}
//		document.getElementById("show_" + code).style.fontWeight = (activeCode == code) ? "bold" : "normal";
	}
	return false;
}

function showAllFacets(id, linkObj, mode){
   var id = '#' + id + 'hiddenFacetList';
   if(mode == 'show'){
        $(id).slideDown();    
   } else {
        $(id).slideUp();   
   }
   
   $('#' + linkObj.id).toggle();
   $('#' + linkObj.id + 'HideMore').toggle();
}




// Basic function to remove default text in search 
function checkSearchBoxStatus(mode, defaultText, searchBox){
    // The ID of the search box
    var searchBoxRef = '#' + searchBox;
    
    // Default text for the search box
    var defaultText = defaultText;
    
    // Define the styles when a user is typing in a query
    var activeTextClass = 'activeSearchText';
    
    // Define the styles when the search box is inactive with the default text
    var inActiveTextClass = 'inActiveSearchText';
     
    if(mode == "remove"){
        if($(searchBoxRef).val() == defaultText){
           $(searchBoxRef).val('');
           $(searchBoxRef).toggleClass(activeTextClass);
           $(searchBoxRef).toggleClass(inActiveTextClass);
        }
    } else {
        if($(searchBoxRef).val() == ""){
           $(searchBoxRef).val(defaultText);
           $(searchBoxRef).toggleClass(inActiveTextClass);
           $(searchBoxRef).toggleClass(activeTextClass);
        }
    }
}



/**
 * @author Rik
 */
var searchRequest=null;


var NetaphorConfig = {
        autoComplete:true,
        autoCompleteUrl:"http://www.netaphorsearch.com/search/modules/autoComplete/autoComplete.php?q=",
        resultOpener:"<li class='result'>",
        titleOpener:"<div class='title'>",
        titleCloser:"</div>",
        abstractOpener:"<div class='description'>",
        abstractCloser:"</div>",
        resultCloser:"</li>",
        closeDiv:"</div>",
        resultCountOpener:'<div id="resultCount">',
        resultCountCloser:'</div>',
        baseResultsUrl:"",
        baseResultsUrlAppender:"",
        baseResultsUrlAppender:"",
        baseServerURI:"",
        searchServer:function(){
            var serverURI = this.baseServerURI;
            return serverURI;
        },
        searchPage:null,
        start:0,
        resultCount:0,
        resultsPerPage:10,
        renderBackClass:"paginationLink",
        renderForwardClass:"paginationLink",
        paginationControls:"<div class='pagination'>",
        jsonSearchPage:"/netaphorSearchPrin20.php",
        appendSearchTo:"",
        abstractTrimLength:400,
        resultsImagePath:"",
        currentSuggestion:null,
        autoCompleteTrimLength:38

};


 
var NetaphorData = {
    suggestions: null, 
    suggestionsFullTexts:null,
    allowSuggestionHiding:true,
    q: "",
    fq: [],
    start: 0,
    ajr:null // a pointer to the suggestion auto complete ajax request
};


//NetaphorData.suggestions = {"responseHeader":{"status":0,"QTime":15,"params":{"fl":"title,id","q":"6000","hl.fl":"title","qt":"auto","wt":"json","hl":"true","rows":"5"}},"response":{"numFound":69,"start":0,"docs":[{"id":"399959","title":"HP Officejet 6000 Printer"},{"id":"618578","title":"Xerox Phaser 6000V (A4) USB Colour Laser Printer (Base Model) 600 x 600 12ppm"},{"id":"618579","title":"Xerox Phaser 6000V (A4) USB Colour Laser Printer (Base Model) 600 x 600 12ppm"},{"id":"585177","title":"HP Bundle: Compaq 6000 Pro MT Desktop PC Core 2 Duo (E8500) 3.16GHz 4GB 500GB DVD+-RW SM (LS) Windows 7 32-bit (Intel GMA 4500) + LE1711 17 inch LCD Monitor"},{"id":"583844","title":"HP Compaq 6000 Pro MT Desktop PC Core 2 Duo (E8400) 3.0GHz 3072MB 320GB DVD+-RW SuperMulti  (LS) Windows 7 Pro 32-Bit (Intel GMA 4500) ENERGY STAR"}]},"highlighting":{"399959":{"title":["HP Officejet <em>6000</em> Printer"]},"618578":{"title":["Xerox Phaser <em>6000</em>V (A4) USB Colour Laser Printer (Base Model) 600 x 600 12ppm"]},"618579":{"title":["Xerox Phaser <em>6000</em>V (A4) USB Colour Laser Printer (Base Model) 600 x 600 12ppm"]},"585177":{"title":["HP Bundle: Compaq <em>6000</em> Pro MT Desktop PC Core 2 Duo (E8500) 3.16GHz 4GB 500GB DVD+-RW SM (LS"]},"583844":{"title":["HP Compaq <em>6000</em> Pro MT Desktop PC Core 2 Duo (E8400) 3.0GHz 3072MB 320GB DVD+-RW SuperMulti  (LS"]}}}

var NetaphorTools = {
    autoComplete:function (theKey){
        var q = $('#searchBox').val();
        var searchPage = '/searchInterface.php';
        //we dont always want to do a search so iterate over the exlcude list and return false if we dont want to perform the search
        function isValidKeyEntry (){
            var excludeList = ["40","38","13","37","39"];
            var isValid = true;
            for (var i = 0; i< excludeList.length; i++){
                if (theKey == excludeList[i]){
                    isValid = false;
                    break;
                }
            }
            return isValid;
        }
        
        if(theKey == "38" || theKey == "40"){
            // if there is a suggestion already highlighted then un-hihglight it
            if($("#suggestion"+NetaphorConfig.currentSuggestion)){
                $("#suggestion"+NetaphorConfig.currentSuggestion).css("background-color","white");
            } 
            // if there is not a current selection or we are at the end of the list reset to 0
            if(NetaphorConfig.currentSuggestion == null || (NetaphorConfig.currentSuggestion == 4 && theKey == "40")){
                NetaphorConfig.currentSuggestion = 0;
                $("#suggestion"+NetaphorConfig.currentSuggestion).css("background-color","#f0f0f0");
            } 
            // if we at the start of the list and going backwards the set to the end of thelist 
            else if (NetaphorConfig.currentSuggestion == 0 && theKey == "38"){
                NetaphorConfig.currentSuggestion = 4;
                $("#suggestion"+NetaphorConfig.currentSuggestion).css("background-color","#f0f0f0");
            } 
            // if we are in the middle of the list the increment if moving fowards and decrement if moving backwards
            else{
                theKey == "40" ? NetaphorConfig.currentSuggestion++ : NetaphorConfig.currentSuggestion--;
                $("#suggestion"+NetaphorConfig.currentSuggestion).css("background-color","#f0f0f0");
            }           

        }
        
        
        if(q.length >= 3 && isValidKeyEntry()){
            // cancel the previous request so that we don't end up with loads of 'em
            if(NetaphorData.ajr && NetaphorData.ajr !== null) {NetaphorData.ajr.abort();}
            var areThereResults = true;
            
            NetaphorData.ajr = $.getScript(NetaphorConfig.autoCompleteUrl + q, function(){
                if(NetaphorData.suggestions.response && NetaphorData.suggestions.response.numFound > 0){
                    $('#suggestions').css("display", "block");
                    var suggestionList = '';
                    var suggestionCount = 0;
                    NetaphorData.suggestionsFullTexts = [];
                    for(var property in NetaphorData.suggestions.response.docs){
                        
                        if (NetaphorData.suggestions.response.docs[property].title){
                            //console.log(NetaphorData.suggestions.response.docs[property].title);
                            var titleString = NetaphorData.suggestions.response.docs[property].title;
                            
                            //the full text string, used if a user selects the item to search
                            NetaphorData.suggestionsFullTexts[suggestionCount] = titleString;
                            
                            // see if we have a result with some highlighting
                            //var titleHighlight = SearchRenderer.getHighlighting("title", NetaphorData.suggestions.response.docs[suggestionCount].id, NetaphorData.suggestions);
                            
                            //if we do have highlighting the use that
                            //if(titleHighlight !== false){
                            //    titleString = titleHighlight;
                            //} 
                            
                            // if the length of the title text is too long for the tect box then we should trim it down a bit
                            //if(titleString.length > NetaphorConfig.autoCompleteTrimLength){
                            //  titleString = titleString.substring(0,NetaphorConfig.autoCompleteTrimLength) + "...";
                            //}
                            
                            //build up the html displayed to the user
                            suggestionList += "<div class='suggestionContainer' id='suggestion" + suggestionCount + "' onmouseover='NetaphorTools.highlightAutoCompleteResult(this,true)'  onmouseout='NetaphorTools.highlightAutoCompleteResult(this,false)'><span class='suggestionLink' onclick='NetaphorTools.searchWithSuggestion(" + suggestionCount + ", \"" + searchPage + "\")' class='suggestItem'>" + titleString + "</span></div>";
                            suggestionCount++;
                        }
                        
                    }
                    $('#suggestions').html(suggestionList);
                }
            });
            
        } else if (q.length < 3){
            $('#suggestions').html("");
            $('#suggestions').css("display", "none");
        }
    },
    highlightAutoCompleteResult: function(obj,highlight){
        // un-highlight the current one if necessary:
        NetaphorConfig.currentSuggestion !== null ? $("#suggestion"+NetaphorConfig.currentSuggestion).css("background-color","white") : "";
        highlight ? $("#"+obj.id).css("background-color","#f0f0f0") : $("#"+obj.id).css("background-color","#fff");
        NetaphorConfig.currentSuggestion = null;
    },
    searchWithSuggestion: function (selectedSuggestion, searchPage){
        var query = NetaphorData.suggestionsFullTexts[selectedSuggestion];
        window.location.href = searchPage + '?q=' + query;
    }
    
};

var SearchRenderer = {
    bindAutoComplete: function (){
        this.setupNetaphorSearchForm();
        var searchBox = $('#searchBox');
        if(NetaphorConfig.autoComplete){
            searchBox.keyup(function(e){
                NetaphorTools.autoComplete(e.keyCode);
            });
            searchBox.blur(function(){
                // hide the suggestions if they are 
                SearchRenderer.hideSuggestions();
            });
            
            // We do not want the hideSuggestions method to complete if the blur event has been triggered over the suggestions box
            $('#suggestions').hover(function(){NetaphorData.allowSuggestionHiding = false;},function(){NetaphorData.allowSuggestionHiding = true;});

        }
    },
    setupNetaphorSearchForm: function (){
        $("#netaphorSearchForm").submit(function(){
            if(NetaphorConfig.currentSuggestion != null){
                var suggestionValue = NetaphorData.suggestionsFullTexts[NetaphorConfig.currentSuggestion];
                $("#searchBox").val(suggestionValue);
            }
        });
    },
    hideSuggestions:function(){
        NetaphorData.allowSuggestionHiding ? $('#suggestions').css("display", "none") : "";
    },
    
    getHighlighting:function(type, id, requestObj){
        var highlightingText = false;
        if(requestObj.highlighting[id][type]){
            highlightingText = requestObj.highlighting[id][type][0];
        }
        return highlightingText;
    }
};

var NetaphorQueryTools = {

}

$(document).ready(function(){
    SearchRenderer.bindAutoComplete();
});





