var google_map;

$(document).bind('google_maps_loaded', function() {

    google_map = new GoogleMaps('map', gmap_data);

    google_map.drawMap();

    if ((typeof(pc_location) !== 'undefined') && (typeof(pc_hint) !== 'undefined')) {

        google_map.addPoint(pc_location.lat, pc_location.lon, pc_hint, '/images_site/google_marker_red.png');
        google_map.setCenter(pc_location.lat, pc_location.lon);
        google_map.setZoom(9);

    } else {

        if ((typeof(gmap_center) !== 'undefined')) {

            google_map.setCenter(gmap_center.lat, gmap_center.lon);
            google_map.setZoom(gmap_center.zoom);
        }
    }
});

$(document).ready(function() {

    $('input[type=radio].star').rating();

    var breed_gallery = function() {

        // Check the container div exists.
        if ($('.breeds_gallery_container').length == 0) return;

        // Get the breed type from the URL
        if (!(breed_type = (new RegExp('[?&]' + encodeURIComponent('breed_type') + '=([^&]*)')).exec(window.location.search))) {
            return;
        }

        // Get the breed from the URL
        if (!(breed = (new RegExp('[?&]' + encodeURIComponent('breed') + '=([^&]*)')).exec(window.location.search))) {
            return;
        }

        $('.breeds_gallery_container').contentSlider({

            onAppendContent : function() {

                console.log(this);

                $(this).find('a[rel=lightbox]:not(.hasLightBox)').lightBox({

                    imageLoading : '/images/static/misc/lightbox-ico-loading.gif',
                    imageBtnClose : '/images/static/misc/lightbox-btn-close.png',
                    imageBtnPrev : '/images/static/misc/lightbox-btn-prev.png',
                    imageBtnNext : '/images/static/misc/lightbox-btn-next.png',
                    imageBlank : '/images/static/misc/lightbox-blank.png',
                    txtImage : trn.image_text,
                    txtOf : trn.image_of_text

                }).addClass('hasLightbox');
            },

            request: {

                url:  '/api/api.php',

                type: 'SOAPjr',

                service_type : 'breeds',

                action_type : 'get_breed_images',

                data: {

                    breed_type: breed_type[1],
                    breed : breed[1],
                    limit: 3

                }
            }
        });
    }();

    $('a[rel=lightbox]').lightBox({

        imageLoading : '/images/static/misc/lightbox-ico-loading.gif',
        imageBtnClose : '/images/static/misc/lightbox-btn-close.png',
        imageBtnPrev : '/images/static/misc/lightbox-btn-prev.png',
        imageBtnNext : '/images/static/misc/lightbox-btn-next.png',
        imageBlank : '/images/static/misc/lightbox-blank.png',
        txtImage : trn.image_text,
        txtOf : trn.image_of_text

    }).addClass('hasLightbox');

    $('a[rel=items_lightbox]').each(function() {

        $(this).lightBox({

            imageLoading : '/images/static/misc/lightbox-ico-loading.gif',
            imageBtnClose : '/images/static/misc/lightbox-btn-close.png',
            imageBtnPrev : '/images/static/misc/lightbox-btn-prev.png',
            imageBtnNext : '/images/static/misc/lightbox-btn-next.png',
            imageBlank : '/images/static/misc/lightbox-blank.png',
            txtImage : trn.image_text,
            txtOf : trn.image_of_text

        }).addClass('hasLightbox');
    });

    $('a.marketplace_map_toggle').bind('click', function() {

        var $item_container = $(this).closest('div.marketplace_list_item');

        var $toggle_link = $item_container.find('a.marketplace_map_toggle');

        $item_container.find('div.marketplace_map').slideToggle();

        $toggle_link.text($toggle_link.text() == trn.hide_map ? trn.show_map : trn.hide_map);

        return false;
    });
    
    // Apply the commentator plugin to comments

    var comments = function() {

        // Check the container exists.
        if ($('.comment').length == 0) {
             return;
        }

        $('.comment').commentator({
            request: {
                url:  '/api/api.php'
            }
        });

    }();

});
