/*jslint browser: true */
/*global jQuery, google, window */

(function ($) {
    $(function () {
        var initDonatorsMap = function () {
            $('#donators_map').each(function () {
                var latitude = parseFloat($(this).attr('data-latitude'), 10) || 51,
                    longitude = parseFloat($(this).attr('data-longitude'), 10) || 10,
                    zoom = parseInt($(this).attr('data-zoom'), 10) || 6,
                    icon = $(this).attr('data-icon') ? 'upload/' + $(this).attr('data-icon') : null,
                    latlng = new google.maps.LatLng(latitude, longitude),
                    options = {
                        zoom: zoom,
                        center: latlng,
                        mapTypeId: google.maps.MapTypeId.ROADMAP
                    },
                    map = new google.maps.Map($(this).get(0), options),
                    openInfoWindow;
                $('.location').each(function () {
                    var marker = new google.maps.Marker({
                            position: new google.maps.LatLng(
                                parseFloat($(this).attr('data-latitude')),
                                parseFloat($(this).attr('data-longitude'))
                            ),
                            icon: icon
                        }),
                        infoWindow = new google.maps.InfoWindow({
                            content: $(this).get(0).cloneNode(true)
                        });
                    google.maps.event.addListener(marker, 'mouseover', function () {
                        if (openInfoWindow) {
                            openInfoWindow.close();
                        }
                        infoWindow.open(map, marker);
                        openInfoWindow = infoWindow;
                    });
                    marker.setMap(map);
                });
            });
        },
        initHelperMap = function (prefix) {
            $('#' + prefix + '_wish_map').each(function () {
                var latitude = $('#' + prefix + '_wish_latitude').val() || 51,
                    longitude = $('#' + prefix + '_wish_longitude').val() || 10,
                    icon = $('#donators_map').attr('data-icon') ? 'upload/' + $('#donators_map').attr('data-icon') : null,
                    latlng = new google.maps.LatLng(latitude, longitude),
                    options = {
                        zoom: 8,
                        center: latlng,
                        mapTypeId: google.maps.MapTypeId.ROADMAP
                    },
                    map = new google.maps.Map($(this).get(0), options),
                    marker = new google.maps.Marker({
                        position: map.getCenter(),
                        draggable: true,
                        icon: icon
                    });
                google.maps.event.addListener(marker, 'dragend', function () {
                    $('#' + prefix + '_wish_latitude').val(marker.getPosition().lat());
                    $('#' + prefix + '_wish_longitude').val(marker.getPosition().lng());
                });
                marker.setMap(map);
                $('#' + prefix + '_wish_map_button').click(function () {
                    var geocoder = new google.maps.Geocoder();
                    geocoder.geocode({
                        address:    $('#' + prefix + '_wish_street').val()     + ' ' +
                                    $('#' + prefix + '_wish_postalcode').val() + ' ' +
                                    $('#' + prefix + '_wish_city').val()       + ' ' +
                                    $('#' + prefix + '_wish_country').val()
                    },
                    function (results, status) {
                        if (status === google.maps.GeocoderStatus.OK) {
                            map.setCenter(results[0].geometry.location);
                            marker.setPosition(results[0].geometry.location);
                            $('#' + prefix + '_wish_latitude').val(results[0].geometry.location.lat());
                            $('#' + prefix + '_wish_longitude').val(results[0].geometry.location.lng());
                        }
                    });
                    return false;
                });
            });
        },
        showDonationForms = function (instant) {
            var duration = instant ? 0 : 'fast';
            $('#DonationContent').fadeOut(duration, function () {
                $('#DonationDebit').attr('action', $('#DonationDebit').attr('action') + '#DonationDebit');
                $('#DonationCredit').attr('action', $('#DonationCredit').attr('action') + '#DonationCredit');
                $('#DonationDebit, #DonationCredit').hide();
                $('#donation_forms').show().get(0).scrollIntoView();
                if (window.location.hash === '#DonationCredit' || window.location.search.indexOf('DonationCredit') !== -1) {
                    $('#DonationCredit').fadeIn('fast', function () {
                        initHelperMap('credit');
                    });
                } else {
                    $('#DonationDebit').fadeIn('fast', function () {
                        initHelperMap('debit');
                    });
                }
            });
            return false;
        },
        formLinker = {};
        $('#DonationDebit').link(formLinker, {
            wish_description: 'DonationDebit:wish_description',
            wish_street : 'DonationDebit:wish_street',
            wish_postalcode : 'DonationDebit:wish_postalcode',
            wish_city : 'DonationDebit:wish_city',
            wish_country : 'DonationDebit:wish_country',
            wish_latitude : 'DonationDebit:wish_latitude',
            wish_longitude : 'DonationDebit:wish_longitude'
        });
        $('#DonationCredit').link(formLinker, {
            wish_description: 'DonationCredit:wish_description',
            wish_street : 'DonationCredit:wish_street',
            wish_postalcode : 'DonationCredit:wish_postalcode',
            wish_city : 'DonationCredit:wish_city',
            wish_country : 'DonationCredit:wish_country',
            wish_latitude : 'DonationCredit:wish_latitude',
            wish_longitude : 'DonationCredit:wish_longitude'
        });
        //$('#show_donation_forms').click(showDonationForms);
        $('#showDonationCredit').click(function () {
            $('#DonationDebit').fadeOut('fast', function () {
                $('#DonationCredit').fadeIn('fast', function () {
                    initHelperMap('credit');
                });
            });
            return false;
        });
        $('#showDonationDebit').click(function () {
            $('#DonationCredit').fadeOut('fast', function () {
                $('#DonationDebit').fadeIn('fast', function () {
                    initHelperMap('debit');
                });
            });
            return false;
        });
        $('#donators_map').each(function () {
            $('#debit_wish_title, #credit_wish_title').html($(this).attr('data-title'));
            $('#debit_wish_description_label, #credit_wish_description_label').html($(this).attr('data-description_label'));
            $('#debit_wish_address_label, #credit_wish_address_label').html($(this).attr('data-address_label'));
        });
        if (window.location.hash === '#DonationDebit' || window.location.hash === '#DonationCredit' || window.location.search.indexOf('DonationDebit') !== -1 || window.location.search.indexOf('DonationCredit') !== -1) {
            showDonationForms(true);
            if (window.location.search.indexOf('step=3') !== -1) {
                $('#donation_forms').append($('#show_donation_map').show());
            }
        } else {
            initDonatorsMap();
        }
    });
}(jQuery));

