function performSearch(place, from, to) {
    debugger
    var mapsearch = true;
    if (readCookie("ms"))
    {
        mapsearch = (readCookie("ms") == "true") ? true : false;
    }
    if (mapsearch)
    {
        if (place == "")
        {
            // bug-fix for firefox... window.location.href = ... doesn't work
            self.setTimeout("self.location.href = \"search.aspx#lat=53.345&lng=-36.27&from="+from+"&to="+to+"&p=1\"", 1);
        }
        var geocoder = new GClientGeocoder();
        var zoomLevel = getZoomLevel(11, place);

        if (geocoder && place != "") {
            geocoder.getLatLng(
            place,
            function(point) {
                if (!point) {
                    self.setTimeout("self.location.href = \"search.aspx#place="+place+"&from="+from+"&to="+to+"&p=1\"", 1);
                } else {
                    self.setTimeout("self.location.href = \"search.aspx#place="+place+"&lat="+point.lat()+"&lng="+point.lng()+"&z="+zoomLevel+"&from="+from+"&to="+to+"&p=1\"", 1);
                }
            });
        }
    }
    else
    {
        self.setTimeout("self.location.href = \"search.aspx#place="+place+"&from="+from+"&to="+to+"&p=1\"", 1);
    }
}

function search() {
    debugger
    var place = document.getElementById("txtSearch").value.replace(' ', '+');
    var from = document.getElementById("ddFrom").value;
    var to = document.getElementById("ddTo").value;
    performSearch(place, from, to);
}



// ajax postback
function postBackHiddenField(hiddenFieldID) {
    var hiddenField = $get(hiddenFieldID);
    if (hiddenField) {
        hiddenField.value = (new Date()).getTime();
        __doPostBack(hiddenFieldID,'');
    }
}

function defaultButton(e, again) {
    var key = (window.event)? event.keyCode : e.which;
    // process only the Enter key
    if (key == 13) {        
        // cancel the default submit
        if (window.event) {
            event.returnValue=false;
            event.cancel = true;
        }
        else {
            //Firefox
            e.preventDefault();
        }
        if (again) { searchAgain(); } else { search();}
    }
}

// not animated collapse/expand
function togglePanelStatus(cont, hidemap, showmap) {
    var expand = (cont.style.display=="none");
    cont.style.display = (expand ? "block" : "none");
    toggleChevronIcon(cont, hidemap, showmap);
}

// current animated collapsible panel content
var currentC = null;

function togglePanelAnimatedStatus(control, interval, step, hide, show) {
    var cont = document.getElementById(control);
    // wait for another animated expand/collapse action to end
    if (currentC==null)
    {
        currentC = cont;
        var expand = (cont.style.display=="none");
        if (expand) {
            eraseCookie("ms");
            createCookie("ms", "true", 30);
            cont.style.display = "block";
        }
        else
        {
            eraseCookie("ms");
            createCookie("ms", "false", 30);
        }
        var max_height = 251;//cont.offsetHeight;

        var step_height = step + (expand ? 0 : -max_height);
        toggleChevronIcon(cont, hide, show);
        
        var caption = document.getElementById("mapboxbotdesc");
        caption.style.visibility = (expand ? "visible" : "hidden");
        caption.style.height = (expand ? "15px" : "0px");            
        caption.style.display = (expand ? "block" : "none");
        var searchbot = document.getElementById("searchbot");
        searchbot.style.top = (expand ? "0px" : "-5px");                           
        // schedule first animated collapse/expand event
        cont.style.height = Math.abs(step_height) + "px";
        setTimeout("togglePanelAnimatingStatus("
            + interval + "," + step
            + "," + max_height + "," + step_height + ")", interval);
    }
}

function togglePanelAnimatingStatus(interval, step, max_height, step_height) {
    var step_height_abs = Math.abs(step_height);

    // schedule next animated collapse/expand event
    if (step_height_abs>=step && step_height_abs<=(max_height-step))
    {
        step_height += step;
        var height = Math.abs(step_height);
        currentC.style.height = height + "px";
        document.getElementById("pictureMap").height = height + "px";
        document.getElementById("pictureMap").width = "910px";
        setTimeout("togglePanelAnimatingStatus("
            + interval + "," + step
            + "," + max_height + "," + step_height + ")", interval);
    }
    // animated expand/collapse done
    else
    {
        if (step_height_abs<step)
            currentC.style.display = "none";
        currentC.style.height = "";
        currentC = null;
    }
}

// change chevron icon into either collapse or expand
function toggleChevronIcon(cont, hidemap, showmap) {
    var chevron = cont.parentNode.childNodes[0].childNodes[1];
    var expand = (chevron.src.indexOf("Plus.gif")>0);
    chevron.src = (expand ? "img/Minus.gif" : "img/Plus.gif")
    cont.parentNode.childNodes[0].firstChild.firstChild.nodeValue = (expand ? hidemap : showmap)
}

// cookie handling
function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function getZoomLevel(zoomLevel, searchTerm)
{
    var z = zoomLevel;
    switch (searchTerm.toLowerCase())
    {
        case 'netherlands': case 'niederlande': case 'nederland':
            z = 7;
            break;
        case 'northern ireland': case 'slovakia': case 'slovenia': case 'latvia': case 'lithuania': case 'estonia':
        case 'switzerland': case 'schweiz':
        case 'belgium':
            z = 6;
            break;
        case 'ireland': case 'irland': case 'scotland, uk': case 'england': case 'wales': case 'austria': 
        case 'denmark': case 'spain': case 'poland': case 'polska': case 'polen': case 'japan': 
        case 'czech republic': case 'czechy': case 'czecha': case 'czechie': case 'tschechei': case 'tschechien':
        case 'portugal':
            z = 5;
            break;   
        case 'germany': case 'deutschland': case 'uk': case 'united kingdom': case 'großbritannien': case 'france': 
        case 'frankreich': case 'italy': case 'italia': case 'italien': case 'ukraine': case 'new zealand': 
        case 'south africa': case 'turkey':
            z = 4;
            break;
        case 'mexico': case 'brazil': case 'brasil': case 'argentina': case 'chile':  case 'finland': case 'india':
        case 'sweden': case 'svenska': case 'norway': case 'norge': case 'australia':
            z = 3;     
            break;
        case 'china': case 'canada': case 'united states': case 'us': case 'usa':
            z = 2;
            break;
        case 'russia':
            z = 1;
            break;
    }
    return z;
}

function redirect() {
    switch (document.getElementById("ddLanguage").value)
    {
        case "de-DE": window.location = "http://www.histografica.de"; break;
        case "pl-PL": window.location = "http://www.histografica.pl"; break;
        default: window.location = "http://www.histografica.com"; break;
    }
}