// TOP MENU //

$(document).ready(function(){

	// May be changed to OneCall prduction server link
	// var xmlStream = "https://www.onecall.no/minesider/tmp_erj.php?GENERAL_SEARCH=";
	var xmlStream = "http://www.onecall.no/inc/search_proxy.php?GENERAL_SEARCH=";
	
	var $searchField = $("#top_header input");
	var origSearchText = $searchField.val();

	function constrain(str,n){
		if(str.length > n){
			var s = str.substr(0, n);
			var words = s.split(' ');
			words[words.length-1] = '';
			str = words.join(' ') + '&hellip;'
		}
		return str;
	}
	
	function clearList() {
		$('#search_dropdown').empty();
	}	

	$searchField.focus(function() {
		$(this).val('');
	});

	$searchField.bind('blur', function() {
		if ($(this).val() == '') {
			$(this).val(origSearchText);
		}
		setTimeout(clearList, 500); // Must be delayed in order to wait for link
	});

	$searchField.keyup(function(event) {

		var searchTerm = $(this).val();

		$.get(xmlStream + searchTerm, function (data) {
			$('#search_dropdown').empty();
			var lastCategory = '';
			var html = '<ul>';
			var urlArray = [];

			// Demo code, replace with max amount of matches later (8)
			var maxAmount = 8;
			// alert($(data).text());
			
			$(data).find('match').each(function(i) {

				if (i < maxAmount) {
					var $match = $(this);
					
					// Add separator if needed
					
					if ($match.find('category').text() != lastCategory) {
						lastCategory = $match.find('category').text();
						html += '<li class="separator">' + lastCategory + '</li>';
						html += '<a href="' + $match.find('url').text() + '">';
						html += '<li class="first match' + i + '">';
					} else {
						html += '<a href="' + $match.find('url').text() + '">';
						html += '<li class="match' + i + '">';
					}

					// Add search match
					var imagepath = $match.find('imagepath').text();
					imagepath = (imagepath != '') ? imagepath : 'img/top_header/default_icon.png';
					html += '<img src="' + imagepath + '">';
					html += '<h4>' + constrain($match.find('title').text(), 30)  + '</h4>';
					html += '<p>' + constrain($match.find('description').text(), 65) + '</p>';
					html += '</li>';

					html += '</a>';
										
					// Remember url
					urlArray[i] = $match.find('url').text();
					// alert($match.find('url').text());

				}
				
			});
			html += '<!-- <li class="separator last"><a href="http://www.onecall.no/kundeservice">Vis alle s&oslash;keresultater &gt;&gt; </a></li> -->';
			html += '</ul>';
			// alert(html);
			$('#search_dropdown').append($(html));

		});
	});
});

/*******
* MENU *
*******/

$(document).ready( function() {
	

	var origUsernameText= $('#menu .username_input').val();
	var origPasswordText = $('#menu .password_input').val();
	
	$('#menu .username_input').focus(function() {
		$(this).val('');
	});
		
	$('#menu .username_input').blur( function() {
		if ($(this).val() == '') {
			$(this).val(origUsernameText);
		}
	});
	
	$('#menu .password_input').focus(function() {
		$(this).val('');
	});

	
});
