// Palffyho kuria web interface
var PK = {

    /**
     * Init functions
     */         
    init: function () {
        if($.browser.msie){
            PK.fixIE6flicker(true);
            PK.emulateHover($('.flats-list tr'));    
        }
        PK.flatsListLinks();                            
        PK.print();
        PK.visualizations();
        PK.enlargeImage();
        PK.location();
    },
    
    /**
     * Google map
     */         
    location: function () {
        if (!$('#map').length) {
            return;
        }

        if (GBrowserIsCompatible()) {
            var map = new GMap2(document.getElementById("map"));
            map.setCenter(new GLatLng(48.210850, 17.146600), 15);
            map.addControl(new GSmallMapControl());
            map.addControl(new GMapTypeControl());
            var point = new GLatLng(48.210850, 17.146600);
            map.addOverlay(new GMarker(point));
        }
    },
   
    /**
     * Emulate hover
     */
    emulateHover: function (items) {
        items.hover(
            function () {
                $(this).addClass('hover');
            },
            function () {
                $(this).removeClass('hover');
            }
        )
    },
    
    /**
     * Open link in new window
     */         
    visualizations: function () {
        $('.visualizations a').click(function(){
            var dimensions = 'width=1024,height=649';
            if ($(this).hasClass('bigger')) {
                dimensions = 'width=1024,height=775';
            }
            window.open(this.href, 'name', dimensions);
            return false;
        });    
    },     
    
    /**
     * Enlarge image
     */         
    enlargeImage: function () {
        $('.enlarge').click(function(){
            window.open(this.href, 'name', 'width=1024,height=649');
            return false;
        });    
    },    
    
    /**
     * Print page
     */         
    print: function () {
        $("#print").bind("click", function () {
            window.print();
            return false;
        });
    },     

    /**
     * Table rows work as links
     */
    flatsListLinks: function () {
        $('.flats-list tr').bind("click", function () {
            var link = $(this).find('a');
            if (link.length > 0) {
                location.href = link.attr('href');
            }
        });
    },             
     
    /**
     * Fix background image flicker in IE6
     */         
    fixIE6flicker: function (fix) {
        try {
            document.execCommand("BackgroundImageCache", false, fix);
        } catch(err) { }
    },   
    
    /**
     * Preload images
     * @param {Array} images array with names of images   
     */               
    preloadImages: function (images) {
        for (var i = 0; i < images.length; i++) {
            var image = new Image();
            image.src = images[i];
        }
    }         
}

$(document).ready(function () {
    PK.init();
});

