var total_thumbs
var total_rows
var available_rows_down
var available_rows_up
var geocoder;
var map;
var map_address;
function initialize() {
    geocoder = new google.maps.Geocoder();
    var latlng = new google.maps.LatLng( - 34.397, 150.644);
    var myOptions = {
        zoom: 15,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    map = new google.maps.Map(document.getElementById("mapCanvas"), myOptions);
    codeAddress()
}

$(document).ready(function() {
    map_address = $("#map_address").val()
    initialize()

    total_thumbs = $(".thumbs img").size()
    total_rows = Math.round(total_thumbs / 3)
    $(".thumbs").css("marginTop", 0)
    $(".thumbs img").mouseover(function() {
        $(this).addClass("hover")
    })
    $(".thumbs img").mouseout(function() {
        if (!$(this).hasClass("selected")) {
            $(this).removeClass("hover")
        }
    })
    $('.photo_album .loading').slideUp("fast");

    $(".property2").load(function() {
        $('.photo_album .loading').slideUp("fast");
    });
    $(".thumbs img").click(function() {
        expand_album()
        $(".photo_album .loading").slideDown("fast");

        $('.thumbs img.selected').removeClass("selected").removeClass("hover")
        $(this).addClass("selected")

        $(".property2").attr("src", $(this).attr("data-value"));
//$(".property2").attr("src", $(this).attr("src").replace('/82/','/444/'));

        available_rows_down = total_rows - ($(".thumbs").css("marginTop").replace("px", "").replace("-", "") / 69)
        available_rows_up = $(".thumbs").css("marginTop").replace("px", "").replace("-", "") / 69

        if ($(this).hasClass('row4')) {
            if (available_rows_down - 4 > 1) {
                $(".thumbs").animate({
                    marginTop: "-=69px"
                },
                function() {
                    organise_rows()
                })
            }
        }
        if ($(this).hasClass('row5')) {
            if (available_rows_down - 5 > 1) {
                $(".thumbs").animate({
                    marginTop: "-=138px"
                },
                function() {
                    organise_rows()
                })
            } else if (available_rows_down - 5 == 1) {
                $(".thumbs").animate({
                    marginTop: "-=69px"
                },
                function() {
                    organise_rows()
                })
            }
        }

        if ($(this).hasClass('row2')) {

            if (available_rows_up >= 1) {
                $(".thumbs").animate({
                    marginTop: "+=69px"
                },
                function() {
                    organise_rows()
                })
            }
        }
        if ($(this).hasClass('row1')) {
            if (available_rows_up > 1) {
                $(".thumbs").animate({
                    marginTop: "+=138px"
                },
                function() {
                    organise_rows()
                })
            } else if (available_rows_up == 1) {
                $(".thumbs").animate({
                    marginTop: "+=69px"
                },
                function() {
                    organise_rows()
                })
            }
        }

    })
    $(".property:not(.sold)").click(function() {
        expand_album()
    })
});
var expanded = false;
function organise_rows() {
    var count = 0;
    var row_count = 1;
    var first_count = 1;
    var top = $(".thumbs").css("marginTop").replace("px", "").replace("-", "") / 69;

    $(".row1").removeClass("row1")
    $('.row2').removeClass("row2")
    $('.row3').removeClass("row3")
    $('.row4').removeClass("row4")
    $('.row5').removeClass("row5")

    $('.thumbs img').each(function(el) {

        if (first_count > (top) * 3 && row_count < 6) {
            count++;
            $(this).addClass("row" + row_count)
            if (count == 3) {
                count = 0;
                row_count++;
            }

        }
        first_count++
    })

}
function expand_album() {
    if (!expanded) {
        expanded = true;
        $(".thumbs").fadeOut("fast");
        $(".photo_album").animate({
            width: "755px",
            paddingLeft: 0
        },
        800,
        function() {
            $(".thumbs").css("width", "275px")
            $(".thumbs").css("paddingLeft", "0")
            $(".thumbs").fadeIn("fast")
            $(".thumbs img").animate({
                width: "82px",
                height: "61px"
            },
            100);
            $(".thumbs").animate({
                height: "5000px"
            },
            100)
            $(".photo_album .property").animate({
                width: "444px",
                opacity: 0
            },
            800);
            $(".property2").fadeIn()
        })
    }
}
function codeAddress() {
    var address = map_address;
    geocoder.geocode({
        'address': address
    },
    function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            map.setCenter(results[0].geometry.location);
            var marker = new google.maps.Marker({
                map: map,
                position: results[0].geometry.location
            });
        }
    });
}
