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+4; x++)
    {
        $('#thumb'+x).show();
    }
    
    if (currentleft==(items-4) && dir==2) return false;
    if (((currentleft-1)<0) && dir==1) return false;
    
    $('#leftbutton').show();
    $('#rightbutton').show();
    
    if (dir==1)
    {
        $('#thumb'+(currentleft+3)).hide();
        $('#thumb'+(currentleft-1)).show();
        currentleft = currentleft - 1;
        
    } else if (dir==2)
    {
        $('#thumb'+(currentleft)).hide();
        $('#thumb'+(currentleft+4)).show();
        currentleft++;  
    }
    
    if ((currentleft+5)>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 = 217;
    var maxw = 325;
    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)
    {
        image.style.marginBottom = (maxh-h) + 'px';
    }
    
    if (w>(maxw-5)) w = maxw;
    //if (w<maxw) image.style.marginLeft = (maxw-w)/2 + 'px';
    
    if (h>30)
    {
        image.className = 'checked';
        image.style.height = h + 'px';
        image.style.width = w + 'px';
    } else
    {
        image.className = 'checkfailed';
    }
    resizing = 0;
    return false;    
}

function showInfo()
{   
    if (document.getElementById('locationinfo').style.height=='119px')
    {
        document.getElementById('locationinfo').style.height = 'auto';
        document.getElementById('moreinfolink').innerHTML = 'lees minder';  
    } else
    {
        document.getElementById('locationinfo').style.height = '119px';
        document.getElementById('moreinfolink').innerHTML = 'lees meer';
    }
    $.scrollTo($('#wrapper'));
    
    return false;    
}

function checkInfo()
{
    if (document.getElementById('locationinfo').offsetHeight<=119)
    {
        $('#moreinfolink').hide();
        $('#supplierbutton').css('marginTop', 148 - document.getElementById('locationinfo').offsetHeight);
    }
    else
        showInfo();
}

function googlemapinit()
{
    initialize();    
}

var map;
function initializeMap(locations) 
{
    //locations = locations.reverse();
    map = new google.maps.Map2(document.getElementById("map"));
    
    map.addControl(new GSmallMapControl());
    map.addControl(new GMenuMapTypeControl());
    
    var points = [];
    var markers = [];
    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);
    }
    
    fitMap(map, points, locations);
    
    $('#map img').each(function() { if ($(this).attr('id') && $(this).attr('id').substring(0,4) == "mtgt") $(this).addClass('overlay-img'); });
    $('#map .overlay-img').bind('mouseover',function() { $('#map .overlay-img').css('zIndex',-1); $(this).css('zIndex',1); });
    $('#map .overlay-img').bind('mouseout',function() { $('#map .overlay-img').css('zIndex',-1); });    
}

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

function setMarker(location, type, url, price, ismain)
{
    var Icon = new GIcon();
    Icon.image = "http://www.lastminutebungalow.net/img/mapicons/icon.php?price="+price+"&ismain="+ismain+"&type="+type;
    
    var size = new GSize(29,37);
    if (type=="p" && ismain==0) size = new GSize(24,31);
    if (type=="h" && ismain==1) size = new GSize(94,50);
    if (type=="h" && ismain==0) size = new GSize(72,39);
            
    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;
            });
        }
        
    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);
}
