var imageFolder = '../images/';
var linkTextLength = 55;
var style = getRandomStyle();
var screenWidth = getScreenWidth();

//
// Set screen width
//
function getScreenWidth() {
	if (screen.availHeight > screen.availWidth) {
		width = '240';
	} else {
		width = '400';		
	}
	
	return width;
}

//
// Set stylesheet
//
function getStyleSheet() {
	document.write('<link rel="stylesheet" type="text/css" href="../css/style-'+ style +'.css" />');
}

//
// Set image folder
// 
function setImageFolder() {
	imageFolder = imageFolder + style + '/';
}

//
// Toggle entertainment
//
function toogleDropdown(object) {
	var entertainment = document.getElementById('entertainment');
	var history = document.getElementById('history');
	var entertainmentIcon = document.getElementById('entertainmenticon');
	var historyIcon = document.getElementById('historyicon');
	var historyLine = document.getElementById('historyline');
	
	if (object == 'entertainment') {
		if (screenWidth == '240') {
			entertainment.className = 'entertainmentVertical';
		} else {
			entertainment.className = 'entertainmentHorizontal';
		}
		
		if (entertainment.style.display == 'block') {
			entertainment.style.display = 'none';
			entertainmentIcon.src = imageFolder + 'Button_Plus.png';
		} else {
			entertainment.style.display = 'block';
			entertainmentIcon.src = imageFolder + 'Button_Minus.png';
		}
		
		if (history.style.display == 'block') {
			history.style.display = 'none';
			historyIcon.src = imageFolder + 'Button_Plus.png';
			historyLine.className = 'line';
		}
	} else if (object == 'history') {
		if (history.style.display == 'block') {
			history.style.display = 'none';
			historyIcon.src = imageFolder + 'Button_Plus.png';
			historyLine.className = 'line';
		} else {
			history.style.display = 'block';
			historyIcon.src = imageFolder + 'Button_Minus.png';
			historyLine.className = 'linedot';
		}
		
		if (entertainment.style.display == 'block') {
			entertainment.style.display = 'none';
			entertainmentIcon.src = imageFolder + 'Button_Plus.png';
		}	
	}
}

//
// Search
//
function focusInputSearch(o) {
	o.style.color = "#000000";
	o.value = "";
}

function blurInputSearch(o) {
	o.style.color = "#ababab";
	if (o.value == "")
		o.value = "Internetsök";
}

//
// Address
//
function focusInputAddress(o) {
	o.style.color = "#000000";
	o.value = "www.";
}

function blurInputAddress(o) {
	o.style.color = "#ababab";
	if (o.value == "www.")
		o.value = "Skriv in adress";
}

//
// Randomize style
//
function getRandomStyle() {
	var s;
	var randomNumber = Math.random();
	randomNumber = randomNumber * 5;
	randomNumber = Math.ceil(randomNumber);
	
	switch(randomNumber) {
		case 1:
			s = 'green';
			break;
		case 2:
			s = 'turquoise';
			break;
		case 3:
			s = 'red';
			break;
		case 4:
			s = 'purple';
			break;
		case 5:
			s = 'orange';
			break;			
		default:
			s = 'green';
			break;
	}		
	
	return s;
}

//
// History list
//
function getHistory() {
	var t = 0;
	
	if (myUsedUrls.length > 1 ) {
		for (var i = 0 ; i < myUsedUrls.length ; i+=3 ) {
			if (i >= 3) {
				document.write('<tr><td colspan="3"><div class="linedot"></div></td></tr>');
			}
			document.write('<tr><td></td><td><table class="listHistory"><tr>');
			document.write('<td class="iconhist"><img src="'+myUsedUrls[i+2]+'"/></td><td>');
			if (screenWidth == '240') {
				document.write('<div class="listHistoryVertical">');				
			} else {
				document.write('<div class="listHistoryHorizontal">');				
			}
			document.write('<a id="historylink'+t+'" href="'+myUsedUrls[i+1]+'" class="subcat">'+ getLinkText(myUsedUrls[i]) +'</a></div></td>');
			document.write('</tr></table></td></tr>');
			t++;
		}
	}
}

//
// Return the link text used in history
//
function getLinkText(text) {
	if (text.length > linkTextLength) {
		text = text.substring(0, linkTextLength) + "...";	
	}
	
	return text;
}

//
// Set focus on element
//
function setFocus(id) {
	document.getElementById(id).focus();
}
