function setFieldFocus()
{
    try
    {
        var foundfield = false;
        
        for (f=0; f < document.forms.length; f++)
        {
            for(i=0; i < document.forms[f].length; i++)
            {
                if (document.forms[f][i].type != "hidden" && document.forms[f][i].disabled != true && document.forms[f][i].readOnly != true)
                {                    
                    if (document.forms[f][i].type == 'text' || document.forms[f][i].type == 'password')
                    {
                        document.forms[f][i].select();
                    } document.forms[f][i].focus();
                    
                    var foundfield = true;                  
                }
                
                if (foundfield == true)
                {
                    break;
                }
            }
                
            if (foundfield == true)
            {
                break;
            }
        }
    } catch(e)
    {
        // do nothing
    }
}

var currentleft = 0;
var current = 0;

function showImage(id)
{
    if ($('#main'+id).attr('class')!="checked") return false;
    $('#main'+current).hide();
    current = id;
    $('#main'+current).show();
            
    return false;               
}

function scrollImages(dir)
{    
    if (resizing) return false;
    
    var items = 0;
    $('#fotos img.checked').each(function() {
            items++;
    });
    
    var x;
    for (x=currentleft; x<currentleft+3; x++)
    {
        $('#thumb'+x).show();
    }
    
    if (currentleft==(items-3) && dir==2) return false;
    if (((currentleft-1)<0) && dir==1) return false;
    
    $('#leftbutton').show();
    $('#rightbutton').show();
    
    if (dir==1)
    {
        $('#thumb'+(currentleft+2)).hide();
        $('#thumb'+(currentleft-1)).show();
        currentleft = currentleft - 1;
        
    } else if (dir==2)
    {
        $('#thumb'+(currentleft)).hide();
        $('#thumb'+(currentleft+3)).show();
        currentleft++;  
    }
    
    if ((currentleft+4)>items)
        $('#rightbutton').hide();
        
    if (currentleft==0)
        $('#leftbutton').hide();
    
    return false;
}

var resizing = 0;
var checks = 0;
var tries = 0;

function resizeImages()
{
    tries++;
    if (resizing==1) return false;
    
    if (tries>100)
    {
        clearInterval(imagechecker);
        
        $('.fotoGroot img').each(function () { 
            if ($(this).attr('id')!='loadericon')
            {
                if ($(this).attr('class')!="checked")
                {
                    var thumbid = this.id.replace('main','thumb');
                    $(this).attr('class',"failed");
                    $(this).attr('id',$(this).attr('id')+'failed');
                    //$(this).hide();
                    $('#'+thumbid).attr('class',"failed");
                    $('#'+thumbid).attr('id',thumbid+'failed');
                    //$('#'+thumbid).hide();
                }
            }
        });
        
        //hernummeren...
        var checknr = 0;
        $('.fotoGroot img.checked').each(function () {
            var thumbid = this.id.replace('main','thumb');
            $(this).attr('id','temp' + $(this).attr('id'));
            $('#'+thumbid).attr('id','temp' + thumbid);
            //$('#'+thumbid).hide();
            //$(this).hide();
        });
        
        $('.fotoGroot img.checked').each(function () {
            var thumbid = this.id.replace('main','thumb');
            $(this).attr('id','main' + checknr);
            $('#'+thumbid).attr('id','thumb' + checknr);
            checknr++;
        });
        
        resizingDone();
        
        return false;
    }
    
    var image = $('.fotoGroot img.unchecked:first');
    if (image.attr('id'))
    {
        checks++;        
        resizing = 1;
        resizeImage(image.attr('id'));
            
    } else
    {
        //check tried before, but failed
        var recheck = 0;
        var images = $('.fotoGroot img.checkfailed');
        images.each(function() { if ($(this).attr('id')) recheck++; });
        if (recheck>0)
        {
            images = $('.fotoGroot img.checkfailed');            
            images.each(function() { if ($(this).attr('id')) { $(this).attr('class',"unchecked"); }});
            return false;            
        }
        
        clearInterval(imagechecker);
        
        resizingDone();
    }
    
    return false;
}

function resizingDone()
{
    $('.thumbs a').each(function () 
        {
            $(this).bind("mouseover", function() { showImage(this.id.replace('thumb', '')); });
            $(this).bind("click", function() { showImage(this.id.replace('thumb', '')); });
        });
    
    $('#loadericon').hide();
    showImage(0);
    scrollImages(0);
}

function resizeImage(id)
{
    var image = document.getElementById(id);
    if (!image) return false;
    if (image.className=='checked') return false;
    var maxh = 216;
    var maxw = 287;
    image.style.display = '';
    var h = image.offsetHeight;
    var w = image.offsetWidth;
    image.style.display = 'none';
    
    var r = 1;
    if (w>0) r = h/w;
    
    if (w>maxw)
    {
        w = maxw;
        h = Math.ceil(w*r);    
    }

    if (h>maxh)
    {
        h = maxh;
        w = Math.ceil(h*1/r);
            
    } else if (h>30 && w>30)
    {
        image.style.marginBottom = (maxh-h) + 'px';
    }
    
    if (w>(maxw-5)) w = maxw;
    
    if (h>30 && w>30)
    {
        image.className = 'checked';
        image.style.height = h + 'px';
        image.style.width = w + 'px';
        if (w<maxw) image.style.marginLeft = (maxw-w)/2 + 'px';
        
    } else
    {
        image.className = 'checkfailed';
    }
    resizing = 0;
    return false;
}

function checkInfo()
{
    if (document.getElementById('locationinfo').offsetHeight<=119)
        $('#moreinfolink').hide();
    else
        showInfo();
}

function googlemapinit()
{
    initialize();    
}

var map;
var points = [];
var markers = [];
var contents = [];
function initializeMap(locations, divid) 
{
    //locations = locations.reverse();
    map = new google.maps.Map2(document.getElementById(divid));
    
    map.addControl(new GSmallMapControl());
    map.addControl(new GMenuMapTypeControl());
    
    for (var i = 0; i < locations.length; i++) 
    {
        //alert(locations[i]["ismain"]);
        points[i] = new GLatLng(locations[i]["lat"], locations[i]["lng"]);            
        marker = setMarker(points[i], locations[i]["type"], locations[i]["url"], locations[i]["price"], locations[i]["ismain"]);
        map.addOverlay(marker);
        markers[locations[i]["id"]] = marker;
        contents[locations[i]["id"]] = locations[i]["content"];
    }
    
    fitMap(map, points, locations);
    
    $('#'+divid+' img').each(function() { if ($(this).attr('id') && $(this).attr('id').substring(0,4) == "mtgt") $(this).addClass('overlay-img'); });
    $('#'+divid+' .overlay-img').bind('mouseover',function() { $('#'+divid+' .overlay-img').css('zIndex',-1); $(this).css('zIndex',1); });
    $('#'+divid+' .overlay-img').bind('mouseout',function() { $('#'+divid+' .overlay-img').css('zIndex',-1); });    
}

function zOrder(marker,b)
{
    return -1;
}    

function setMarker(location, type, url, price, ismain)
{
    var Icon = new GIcon();
    Icon.image = ismain==1 ? "img/icons/mapiconselected.png" : "img/icons/mapiconunselected.png";
    
    var size = ismain==1 ? new GSize(46,54) : new GSize(27,32);
            
    Icon.iconSize = size;
    Icon.iconAnchor = new GPoint(size.width/2, size.height);
    Icon.infoWindowAnchor = new GPoint(0, 0);
    Icon.shadowSize = new GSize(0, 0);

    markerOptions = { 'icon':Icon,'zIndexProcess':zOrder };
    
    var marker = new GMarker(location, markerOptions);
    marker.importance = ismain;
    
    if (ismain!=1)
    {
        GEvent.addListener(marker, "click", function()
        {
            document.location.href = url;
        });
    }
    
    /*
    if (content!='')
    {
        GEvent.addListener(marker, "mouseover", function()
        {
            marker.openInfoWindowHtml(content, { });
        });
        
        GEvent.addListener(marker, "mouseout", function()
        {
            //marker.closeInfoWindow();
        });
    }
    */
        
    return marker;
}

function fitMap(map, points, locations) 
{
    var mapbounds = new google.maps.LatLngBounds();
    for(var i = 0; i < points.length; i++){
        mapbounds.extend(points[i]);
    }
    
    /*if (points.length>0)
    {
        if (locations[locations.length-1]['ismain']==1)        
            map.setCenter(points[locations.length-1]);
        else 
            map.setCenter(points[0]);
    } else
    {*/
        map.setCenter(mapbounds.getCenter());
    //}
    
    map.setZoom(map.getBoundsZoomLevel(mapbounds));
    var zoom = map.getZoom();
    //zoom = zoom+1;
    
    if(/*(zoom<10) || */(locations.length==1))
    {
        zoom = 10;
    }
    
    map.setZoom(zoom);
}

function loadCampings(regionlink)
{
    if (regionlink==0) return;
    var data = { 'link': regionlink };
    $('#campingselect option:first').html('Laden...');                     
    $('#campingselect').load('ajax/campings', data, function() {
            
            //alert('Load was performed.');
    });
}

function gotoCamping(siteurl, campinglink)
{
    if (campinglink==0) return;
    window.location = siteurl + 'camping-' + campinglink;
}
