 function LoadVideoSearch(tag) 
 {
      var defaultTags = [
        { query : tag }
      ];
	
      var options = {
        twoRowMode : true, 
        largeResultSet : false
      };
	
      new GSvideoSearchControl(document.getElementById("videosearch"),
	                               defaultTags, null, null, options);
}

function SearchMapPoint(title, zoom)
{
	if (GBrowserIsCompatible()) 
	{
		var karttaDiv = document.getElementById('kartta');
 		var localSearch = new GlocalSearch();
 		localSearch.setRestriction(GSearch.RESTRICT_TYPE, GlocalSearch.TYPE_LOCALONLY_RESULTS);

		localSearch.setSearchCompleteCallback(null, function() 
		{
			if (localSearch.results[0] && localSearch.results[0].lat && localSearch.results[0].lng && localSearch.results[0].lat.indexOf("37.") == -1 && localSearch.results[0].lng.indexOf("-121.") == -1)
            {
            	var mapOptions = {
	            googleBarOptions : {
	              style : "new",
	              adsOptions : {
	                client: "partner-pub-6951091062298729",
	                channel: "5891526272",
	                language: "fi"
	              }
	            }
	          	}
	          
            	karttaDiv.style.width='468px';
				karttaDiv.style.height='315px';	
				karttaDiv.style.margin='10px 20px 10px 20px';
            	document.getElementById('karttateksti').style.display = 'block';
            	document.getElementById('menu_map').style.display = 'block';
            	var map = new GMap2(document.getElementById('kartta'), mapOptions);
	        	map.addControl(new GMapTypeControl());
				map.addMapType(G_PHYSICAL_MAP);
				map.enableScrollWheelZoom();
                var resultLat = localSearch.results[0].lat;
                var resultLng = localSearch.results[0].lng;
                map.setCenter(new GLatLng(resultLat, resultLng), zoom);
                map.setUIToDefault();
        		map.enableGoogleBar();
            }
            else
            {
				var mapDiv = document.getElementById('mapdiv');
				while (mapDiv.childNodes[0])
				{
					mapDiv.removeChild(mapDiv.childNodes[0]);
				}
				mapDiv.parentNode.removeChild(mapDiv);
			}
		});
				
		localSearch.execute(title);
	}
}

function SetMapPoint(lat, lon, zoom)
{
	if (GBrowserIsCompatible()) 
	{
		var karttaDiv = document.getElementById('kartta');

		var mapOptions = {
	            googleBarOptions : {
	              style : "new",
	              adsOptions : {
	                client: "partner-pub-6951091062298729",
	                channel: "5891526272",
	                language: "fi"
	              }
	            }
	    }
      	karttaDiv.style.width='468px';
		karttaDiv.style.height='315px';	
       	var map = new GMap2(document.getElementById('kartta'), mapOptions);
       	map.addControl(new GMapTypeControl());
		map.addMapType(G_PHYSICAL_MAP);
		map.enableScrollWheelZoom();
		var point = new GLatLng(lat, lon);
        map.setCenter(point, zoom);
		map.setUIToDefault();
        map.enableGoogleBar();
        map.addOverlay(new GMarker(point));
    }
}

function odplink (link, dir) 
{
	var links = [ "http://dmoz.org/cgi-bin/add.cgi?where=", "http://dmoz.org/about.html", "http://dmoz.org/cgi-bin/apply.cgi" ];
	if (link == 0)
	{  
		odp = window.open(links[link]+dir, "ODP");
	}
	else
	{
		odp = window.open(links[link], "ODP");
	}
}

function open_link (link)
{
	window.open(link, "Site");
}

function open_link (id, type, title) {
  var a0 = 'http://www.gnu.org/copyleft/fdl.html';
  var a1 = 'http://**type**.wikipedia.org/wiki/**title**';
  if(id == 0){
    window.open(a0);
  }else{
    var url = a1;
    a1 = a1.replace(/\*\*type\*\*/, type);
    a1 = a1.replace(/\*\*title\*\*/, title);
    window.open(a1);
  }  
}
function UCFirst(element)
{
	if(element.value.length == 1)
		element.value = element.value.toUpperCase();
	if(element.value == " ")
		element.value = "";
}
function Toggle(elementId, orig, trans)
{
	var elementIdc = elementId + 'C';
	
	if(strpos(document.getElementById(elementIdc).innerHTML, 'alku', 0))
	{
		document.getElementById(elementId).innerHTML = orig;
		document.getElementById(elementIdc).innerHTML = "N&auml;yt&auml; k&auml;&auml;nn&ouml;s";
	}
	else if(strpos(document.getElementById(elementIdc).innerHTML, 'nn', 0))
	{
		document.getElementById(elementId).innerHTML= trans;
		document.getElementById(elementIdc).innerHTML = "N&auml;yt&auml; alkuper&auml;inen";
	}
}
function strpos (haystack, needle, offset)
{   
    var i = (haystack+'').indexOf(needle, (offset ? offset : 0));
    return i === -1 ? false : i;
}
function OpenUrl(url)
{
	window.open(url, '_blank');
}

/* Autocomplete */
function findValue(li) {

	if( li == null ) return alert("No match!");

	var sValue = li.selectValue;
	location.href='http://www.tietoportti.com/' + sValue + ".html";
}

function selectItem(li) {

	findValue(li);
}

function formatItem(row) {
	return row[0];
}

function lookupAjax(){
	var oSuggest = $("#searchinput")[0].autocompleter;

	oSuggest.findValue();

	return false;
}

$(document).ready(function() {
	$("#searchinput").autocomplete(
		"autocomplete.php",
		{
			delay:400,
			minChars:2,
			matchSubset:1,
			matchContains:4,
			cacheLength: 100,
			maxItemsToShow: 12,
			onItemSelect:selectItem,
			onFindValue:findValue,
			formatItem:formatItem,
			autoFill:true
		}
	);
});

