var pr_countries = new Array();
$(document).ready( function() {
	
	//$('input#get_countries').val('');;
	
	$(document).click( function() { $('#autocomplete').remove();});
	
	// ODD COLUMNS
	$("tr.area").each( function() { $(this).children("td:odd").addClass( 'odd' ) } );
	
	
	// BUTTONS
	$('tr.area td').click(
		function() {
			//alert("clicked");
			if( !$(this).parents('tr.area').children('td').hasClass('hilighted') ) {
				hilight($(this).parents('tr.area') );
				hilight($(this).parents('tr').next('tr.countries') );
			} else {
				$(this).parents('tr.area').find('img').attr('src', '/PRODUCT_METADATA_0/Products/Services/Ovi_Maps/images/prices_and_coverage/btn-plus.gif' );
				$(this).parents('tr.area').children('td').removeClass('hilighted');
				$(this).parents('tr.area').next('tr.countries').hide();
			} 
		}
	);
	
	// SUBMIT SEARCH
	$('input#submit_countries').bind('click', function() { hilightLines(); } );
	
	// CLEAR SEARCH
	$('a.price-clear').click( function() { $('#get_countries').val('Enter country'); removeHilight(); $('#autocomplete').remove(); return false; } );
	
	// GET COUNTRIES IN AN ARRAY
	$('td.countries').each( function() {
		var tmp = $(this).text().split(', ');
		for( e in tmp ) {
			var country = tmp[e].replace( /(\s\([A-z+]+\))/g, "" );
			if( country != "" )
				pr_countries[country] = country;
		}
	});
	
	$('#countrycomplete').attr('autocomplete', 'off');
	$('input#get_countries').bind( 'keyup', function(e) {
		var KEY={UP:38,DOWN:40,DEL:46,TAB:9,RETURN:13,ESC:27,COMMA:188,PAGEUP:33,PAGEDOWN:34,BACKSPACE:8};
		var keyCode = e.keyCode || window.event.keyCode;
		$("#autocomplete").remove();
		autoCompleteList();
		
		if( keyCode == KEY.DOWN && (selected+1) < $('.pre').length ) {
			if( $('.pre').get(selected) ) {
				selected++;
				$('.pre').removeClass('selected');
				$('.pre:eq('+selected+')').addClass('selected');
			}
			return;
		} else if( keyCode == KEY.UP && selected > 0 ) {
			if( $(".pre").get(selected - 1) ) {
				selected--;
				$(".pre").removeClass("selected");
				$(".pre:eq("+selected+")").addClass("selected");
			}
			return;
		} else if( (keyCode == KEY.RETURN ) && selected > -1 ) {
			removeHilight();
			$(this).blur();
			$(this).val($(".pre:eq("+selected+")").text()); hilightLines();
			$('#autocomplete').remove();
			return;
		} else if( (keyCode == KEY.TAB ) ) {
			$('#autocomplete').remove();
			return;
		} else {
			
			if( $('.pre').length > 0 ) {
				selected = 0;
				$(".pre").removeClass("selected");
				$(".pre:eq("+selected+")").addClass("selected");
				return;
			} else {
				$("#autocomplete").remove();
				selected = -1;
				return;
			}
		}
		
	} );

	$('form#countrycomplete').submit( function() { return false; } );

});

function autoCompleteList() {
	$("#autocomplete").remove();
	var skey = $('input#get_countries').val().toLowerCase();
	if( skey != "" ) {
		$("body").append( "<div id=\"autocomplete\" />" );
		var offset = $("input#get_countries").offset();
		var offsetTop = $("input#get_countries").outerHeight();
		var w = $("input#get_countries").outerWidth() - 2;
		$("#autocomplete").css( { position: 'absolute', display: 'none', left: offset.left +'px', top: offset.top + offsetTop + 'px', width: w + 'px', textAlign: 'left' } );
		var i = 0;
	// First the ones which start the same way
		for( e in pr_countries ) {
			if( e.toLowerCase().indexOf( skey ) == 0 && i < 10 ) {
				i++;
				$('#autocomplete').append( "<div class='pre'>"+ e + "</div>" );
			}
		}
	// Then those wich have same string in them
		for( e in pr_countries ) {
			if( e.toLowerCase().indexOf( skey ) > 0 && i < 10 ) {
				i++;
				$('#autocomplete').append( "<div class='pre'>"+ e + "</div>" );
			}
		}
		$('#autocomplete').show();
		$('#autocomplete div.pre').bind( 'click', function() { $('#autocomplete').remove(); $('input#get_countries').val($(this).text()); hilightLines();});
	} else {
		$('#autocomplete').remove();
	}
}

function hilightLines() {
	$('#autocomplete').remove();
	var skey = $('input#get_countries').val().toLowerCase();
	//removeHilight();
	$('#results tbody').empty();
	if( skey != "" ) {
		$('.countries').each(
			function() {
				if( $(this).text().toLowerCase().indexOf(skey) > -1 ) {
					hilight($(this).parents('tr').prev('tr.area') );
					hilight($(this).parents('tr') );
				}
			}
		);
	}
}


function hilight(el) {
	$(el).find('img').attr('src', '/PRODUCT_METADATA_0/Products/Services/Ovi_Maps/images/prices_and_coverage/btn-miinus.gif' );
	$(el).children('td').addClass( 'hilighted' );
	$(el).show();
}

function removeHilight() {
	$('.hilighted img').attr('src', '/PRODUCT_METADATA_0/Products/Services/Ovi_Maps/images/prices_and_coverage/btn-plus.gif' );
	$('.hilighted').parents('tr.countries').hide();
	$('.hilighted').removeClass('hilighted');
}

