var o;

function setup() {
	o.baseUrl = location.protocol + '//';
	o.secureUrl = "https://";
	if (o.test) {
		o.baseUrl += 'test';
		o.secureUrl += "test";
	} else {
		o.baseUrl += 'www';
		o.secureUrl += "www";
	}
	o.baseUrl += ".buyandread.com";
	o.secureUrl += ".buyandread.com";
}

function getResults() {
	document.getElementById("barSearchResults").innerHTML = '';
	document.getElementById('barLoadingDiv').style.display = '';
	ss = document.getElementById('barSearchstring').value; 
	
	var url = o.baseUrl+'/mediaport/servlet/tools?action=searchJSON&ss='+escape(ss);
	if (o.pid) url += '&pid='+o.pid;
	
	var aObj = new JSONscriptRequest(url);
	
	aObj.buildScriptTag();
	aObj.addScriptTag();
	
	
	/*if (searching) {
		var div = document.getElementById("searchResults");	
		div.innerHTML = '<h2 style="color: red;">Søket kunne inn gjennomføres: Ingen kontakt med serveren.</h2>';
	}*/
}

function showResults(data) {
	
	var div = document.getElementById("barSearchResults");	
	var html = '<table id="barResTable" '+o.resTableCustom+'>';
	
	var queryTime = data.querytime;
	var hits = data.hits;
	
	if (hits == 0) {
		html += '<tr><td '+o.resNoResCustom+'>Ingen resultater ble funnet.</td></tr>';
	} else {
		var i;
		for (i = 0; i < data.results.length; i++) {
			var sr = data.results[i];
	
			var id = sr.id;
			var url = o.secureUrl + "/mediaport" + sr.url;
			var tnUrl = o.baseUrl + sr.tnurl;
			var summary = sr.summary;
			if (o.highlightCustom) {
				summary = summary.replace(/class="highlight"/,o.highlightCustom)
			}
			
			html += '<tr id="'+id+'">';
			html += '<td '+o.resTdCustom+'>';
			html += '<a href="javascript:toogleRes('+id+')" '+o.resHeaderCustom+'>' + sr.name + '</a>';
			html += '<div id="'+id+'tn" '+o.resDivCustom+'>';
			html += '<a href="' + url + '">';
			html += '<img src="' + tnUrl + '" '+o.resTnCustom+' />';
			html += '</a>';
			html += '<div '+o.resSummaryCustom+'>' + summary + '</div>';
			html += '<a '+o.resBuyLinkCustom+' href="' + url + '">Kj&oslash;p</a>'
			html += '</div>';
			html += '</td>';
			
		}
		var moreResultsUrl = o.baseUrl+'/mediaport/tools.htm?action=search&q='+escape(ss);
		if (o.pid) moreResultsUrl += '&pid='+o.pid;
		html += '<tr><td '+o.resMoreResCustom+'><a href="'+moreResultsUrl+'">&raquo; Flere resultater &raquo;</a></td></tr>';	
	}
	
	html += '<tr><td '+o.resInfoCustom+'>';
	html += 'Totalt antall treff: '+hits+' (' + (queryTime/1000.0) + ' sekunder).';
	if (data.pageCount) {
		html += '<br/>Det ble s&oslash;kt gjennom ' + data.editionCount + ' publikasjoner og ' + data.pageCount + ' sider';
	}
	if (data.fromDate) {
		html += ' siden ' + data.fromDate + '.';
	} else html += '.';
	
	html += '</td></tr>';
	html += '</table>';
	
	div.innerHTML = html;
	
	document.getElementById('barLoadingDiv').style.display = 'none';
	
}

function toogleRes(id) {
	var div = document.getElementById(id+'tn');
	
	if (div.style.display == 'none') div.style.display = '';
	else div.style.display = 'none';
}

function printSearchPage(opt) {
	if (opt) o = opt;
	else o = new Object();
	
	setup();
	
	if (o.preset) {
		if (o.preset == 'ringblad') {
			if (!o.containerDivCustom) o.containerDivCustom = 'style="padding: 0px;"';
			if (!o.textBoxCustom) o.textBoxCustom = 'class="apiSkattesokFreetext" size="40"';
			if (!o.searchButtonCustom) o.searchButtonCustom = 'class="apiSkattesokFormSubmit" style="float: none; margin: 0;"';
			if (!o.searchResDivCustom) o.searchResDivCustom = 'style="font-size: 11px;"';
			if (!o.logoCustom) o.logoCustom = 'style="border: none; "';
		} else if (o.preset == 'rbnett') {
			if (!o.containerDivCustom) o.containerDivCustom = 'style="width: 450px;"';
			if (!o.resTdCustom) o.resTdCustom = 'class="Font1 Color1"';
			if (!o.resHeaderCustom) o.resHeaderCustom = ' style="font-weight: bold;"';
			if (!o.resSummaryCustom) o.resSummaryCustom = '';
			if (!o.resBuyLinkCustom) o.resBuyLinkCustom = 'style="font-weight: bold;"';
			if (!o.resMoreResCustom) o.resMoreResCustom = 'class="Font1" style="font-weight: bold;"';
			if (!o.resInfoCustom) o.resInfoCustom = 'class="Font1" style="padding-top: 5px;"';
			o.doNotShowTitle = true;
		}
	}
	
	if (!o.resTableCustom) o.resTableCustom = 'style="width: 100%"';
	if (!o.resNoResCustom) o.resNoResCustom = '';
	if (!o.highlightCustom) o.highlightCustom = 'style="background-color: yellow;"';
	if (!o.resTdCustom) o.resTdCustom = '';
	if (!o.resHeaderCustom) o.resHeaderCustom = '';
	if (!o.resDivCustom) o.resDivCustom = 'style="display: none;"';
	if (!o.resTnCustom) o.resTnCustom = 'style="width: 100px; float: left; margin-right: 3px; border: none;"';
	if (!o.resSummaryCustom) o.resSummaryCustom = '';
	if (!o.resBuyLinkCustom) o.resBuyLinkCustom = '';
	if (!o.resMoreResCustom) o.resMoreResCustom = '';
	if (!o.resInfoCustom) o.resInfoCustom = 'style="font-size: 9px; padding-top: 10px;"';
	
	if (!o.containerDivCustom) o.containerDivCustom = '';
	if (!o.textBoxCustom) o.textBoxCustom = 'size="50"';
	if (!o.searchButtonCustom) o.searchButtonCustom = '';
	if (!o.searchResDivCustom) o.searchResDivCustom = '';
	if (!o.logoCustom) o.logoCustom = 'style="float: right; border: none; margin-top: 5px;"';
	if (!o.logoURL) o.logoURL = o.baseUrl+'/tools/logoPoweredBy150px.gif';
	
	var div = '<div '+o.containerDivCustom+'>';
	var text = '<input type="text" '+o.textBoxCustom+' onKeyPress="checkEnter(event)" name="barSearchstring" id="barSearchstring"/>';
	var button = '<input type="button" '+o.searchButtonCustom+' value="S&oslash;k" onclick="getResults()">';
	var loading = '<div id="barLoadingDiv" style="display: none;"><img src="'+o.baseUrl+'/tools/loadingAnimation.gif" alt="...henter resultat, vennligst vent..."/></div>'
	var searchRes = '<div id="barSearchResults" '+o.searchResDivCustom+'></div>';
	var hr = '<hr style="margin-top: 5px;"/>';
	var logo = '<a href="'+o.baseUrl+'"><img '+o.logoCustom+' src="'+o.logoURL+'" alt="Powered by www.BuyAndRead.com"/></a>'
	var clear = '<div style="clear: both; "></div>'

	var html = '';
	
	
	var defaultLayout = div + '<form onSubmit="return false;">' + text + button + '</form>' + loading + searchRes + hr + logo + clear + "</div>";
	
	if (o.preset) {
		if (o.preset == 'ringblad') {
			html += div;
			html += '<form onSubmit="return false;">';
			html += '<div class="apiSkattesokField">' + text + '</div>';
			html += '<div class="apiSkattesokExample">Eksempel: jobb</div>'
			html += '<div style="text-align: right; padding: 0; ">' + button + '</div>';
			html += '</form>';
			html += clear;
			html += loading;
			html += searchRes;
			html += hr;
			html += '<div style="text-align: center; margin-top: 5px;">' + logo + '</div>';
			html += clear;
			html += "</div>";
		} else if (o.preset == 'rbnett') {
			html += defaultLayout;
		} else {
			if (!o.doNotShowTitle) html += '<h1>S&oslash;k i papirutgaven</h1>';
			html += defaultLayout;
		}
	} else {
		if (!o.doNotShowTitle) html += '<h1>S&oslash;k i papirutgaven</h1>';
		html += defaultLayout;
	}
	
	document.write(html);
	
}

function printSearchBox(opt) {
	if (opt) o = opt;
	else o = new Object();
	setup();

	if (!o.width) o.width = 150;
	
	var html = '<div style="text-align: center;">';
	
	html += '<table style="border: none; width: '+o.width+'px; border-collapse: collapse;">';
	
	if (!o.doNotShowTitle) {
		html += '<tr>';
		html += '<td style="font-weight: bold;">S&oslash;k i papirutgaven:</td>';
		html += '</tr>';
	}
	html += '<tr>';
	html += '<td width="100%" style="text-align: center;">';
	html += '<form action="'+o.baseUrl+'/mediaport/tools.htm" method="GET">';
	html += '<input type="hidden" name="action" value="search"/>';
	if (o.pid) {
		html += '<input type="hidden" name="pid" value="'+o.pid+'"/>';
	}
	html += '<input type="text" value="" name="q" style="font-size:12px; width: '+(o.width-8)+'px; color:#000000; background-color:#eeeeee"/><br/>';
	//html += '<a href="'+baseUrl+'/mediaport/search.htm?searchType=advanced" style="font-size: 10px; color: black; font-weight: bold; text-decoration: none;">Avansert s&oslash;k &raquo;</a><br/>';
	html += '<input type="submit" value="S&oslash;k" name="search" style="width: 80px; font-size:10px; color:#000000; background-color:#FFFFFF; font-weight:bold"/>';
	html += '</form><div style="margin-top: 10px;"></div>';
	html += '</td>';
	html += '</tr>';
	html += ' <tr>';
	html += '  <td style="text-align: center; background-color: black;">';
	html += '   <a href="'+o.baseUrl+'">';
	html += '    <img style="border: none; width: '+(o.width-6)+'" width="'+(o.width-6)+'" src="'+o.baseUrl+'/tools/logoPoweredBy150px.gif">';
	html += '   </a>';
	html += '  </td>';
	html += '</tr>';
	html += '</table>';
	html += '</div>'; 
	
	document.write(html);
}

function checkEnter(e) {
	var characterCode
	if(e && e.which){
		e = e;
		characterCode = e.which;
	} else {
		e = event;
		characterCode = e.keyCode;
	}	 
	if(characterCode == 13) {
 		getResults();
 		return false;
	}
	return true
}

// GetUrlParameter
function gup(name) {
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp( regexS );
	var results = regex.exec( window.location.href );
	if(results == null) return "";
	else return results[1];
}

// JSONscriptRequest -- a simple class for accessing Yahoo! Web Services
// using dynamically generated script tags and JSON
//
// Author: Jason Levitt
// Date: December 7th, 2005
//
// A SECURITY WARNING FROM DOUGLAS CROCKFORD:
// "The dynamic <script> tag hack suffers from a problem. It allows a page 
// to access data from any server in the web, which is really useful. 
// Unfortunately, the data is returned in the form of a script. That script 
// can deliver the data, but it runs with the same authority as scripts on 
// the base page, so it is able to steal cookies or misuse the authorization 
// of the user with the server. A rogue script can do destructive things to 
// the relationship between the user and the base server."
//
// So, be extremely cautious in your use of this script.
//

// Constructor -- pass a REST request URL to the constructor
//
function JSONscriptRequest(fullUrl) {
    // REST request path
    this.fullUrl = fullUrl; 
    // Keep IE from caching requests
    this.noCacheIE = '&noCacheIE=' + (new Date()).getTime();
    // Get the DOM location to put the script tag
    this.headLoc = document.getElementsByTagName("head").item(0);
    // Generate a unique script tag id
    this.scriptId = 'YJscriptId' + JSONscriptRequest.scriptCounter++;
}

// Static script ID counter
JSONscriptRequest.scriptCounter = 1;

// buildScriptTag method
//
JSONscriptRequest.prototype.buildScriptTag = function () {

    // Create the script tag
    this.scriptObj = document.createElement("script");
    
    // Add script object attributes
    this.scriptObj.setAttribute("type", "text/javascript");
    this.scriptObj.setAttribute("src", this.fullUrl + this.noCacheIE);
    this.scriptObj.setAttribute("id", this.scriptId);
}
 
// removeScriptTag method
// 
JSONscriptRequest.prototype.removeScriptTag = function () {
    // Destroy the script tag
    this.headLoc.removeChild(this.scriptObj);  
}

// addScriptTag method
//
JSONscriptRequest.prototype.addScriptTag = function () {
    // Create the script tag
    this.headLoc.appendChild(this.scriptObj);
}

