function generate_star_size() {
        var s = Math.round(Math.random()*100+1);
        if (s < 60)
            return 1;
        if (s < 85)
            return 2;
        
        return 3;
    }
    
    // Determine polygon intersection
    function pointInPoly(polyCords, pointX, pointY)
    {
        var i, j, c = 0;
        for (i = 0, j = polyCords.length - 1; i < polyCords.length; j = i++)
        {
            if (((polyCords[i][1] > pointY) != (polyCords[j][1] > pointY)) && (pointX < (polyCords[j][0] - polyCords[i][0]) * (pointY - polyCords[i][1]) / (polyCords[j][1] - polyCords[i][1]) + polyCords[i][0]))
            {
                c = !c;
            }
        }
        return c;
    }
    
    function validPos(pos_x, pos_y) {
        var p = $('#shoutmap-bg').position();
        var w = $('#shoutmap-bg').width();
        var h = $('#shoutmap-bg').height();
	var s = $(window).scrollTop();

        //var map = [[363,553],[1542,553],[1542,1177],[363,1177]];
        var map = [[p.left+5,p.top+5],[p.left+w-10,p.top+5],[p.left+w-10,p.top+h-10],[p.left+5,p.top+h-10]];
        
        if (pointInPoly(map, pos_x, pos_y+s)) {
            return true;
        } else {
            return false;
        }
    }
    
    $(document).ready(function($) {
    if ($('#shoutmap-bg').length > 0) {
       
        var newest = new Array();
        $.getJSON("/ajax.php", { mode: "loadsb" }, function(json){
            var parent = $('#shoutmap-ct');
            $.each(json, function(idx) {
                //XXX DEBUG
                //return true;
                
                
                if (idx <= 9) {
                    var bg = 'background-image: url(/images/shoutmapstars3.png)';
                } else {
                    var bg = '';
                }
                this.post_date = this.post_date.replace(' ', '&nbsp;');
                this.post_text = this.post_text.replace(/\\'/, '&apos;').replace(/\\&quot;/g, '&quot;');
                
                var ct = $('#star-'+this.star_type+'-ct');
                var pos_c = (this.star_x > 500) ? '-170px;' : '20px';
                var pos_d = (this.star_y > 300) ? '-30px;' : '20px';
                
                var tt = '<div class="tooltip" style="display:none; top: '+pos_d+'px;left: '+pos_c+'"><div class="tt-header">'+this.poster_name+'<br /><span class="small">posted&nbsp;on&nbsp;'+this.post_date+'</span></div><div class="tt-body">'+this.post_text+'</div></div>';
                ct.append('<div id="star-'+this.entry_id+'" class="star star-type-'+this.star_type+'" style="top: '+(this.star_y*ct.height()/parent.height())+'px; left: '+(this.star_x*ct.width()/parent.width())+'px; '+bg+'; width: 10px; height: 10px;"><img src="/images/shoutmapstarskreuz.gif" alt="a" style="display:none" />'+tt+'</div>');
                if (idx <= 9) {
                    var ch = ct.children();
                    newest.push(ch[ch.length-1]);
                }
            });
            
            $('.star').hover(function() {
                if (window.showTooltipTimeout) {
                    window.clearTimeout(window.showTooltipTimeout);
                }
                var self = this;
                window.showTooltipTimeout = window.setTimeout(function() {
                    $(self).find('img').css('display', 'none');
                    $(self).find('.tooltip').css('display', 'none');
                    $(self).find('img').show();
                    $(self).find('.tooltip').show().css({
                        top: (parseInt($(self).css('top')) > 300 ? $(self).find('.tooltip').height()*-1 - 10 : 10),
                        left: (parseInt($(self).css('left')) > 600 ? $(self).find('.tooltip').width()*-1 -10 : 10)
                    });
                }, 50);
                
            },
            function() {
                var self = this;
                window.setTimeout(function() {
                    $(self).find('img').fadeOut();
                    $(self).find('.tooltip').fadeOut();
                }, 100)
            });
            
            $('#shoutmap-ct').jparallax({xtravel: 1, ytravel: 1, mouseActiveOutside: true});
            
            /*
            if (user_logged_in === true) {
                $('.star').dblclick(function() {
                    $.ajax({
                        url: "<?= $basePath; ?>/ajax.php", 
                        data: {
                            mode: 'deletestar', 
                            entry_id: $(this).attr('id').substr(5)
                        }, 
                        success: function(msg){
                            msg = msg.split('|');
                            if (msg[0] == 'ok')
                                $('#star-'+msg[1]).remove();
                        }
                    });
                });
            }
            */
        });
        
        $('#facebox .post_text').live('keydown', function() {
            $(this).val($(this).val().substr(0,255));
        });
        
        $('#shoutmap-bg').click(function(ev) {
            if (validPos(ev.clientX, ev.clientY) && $(ev.target).attr('id') == 'star-3-ct') {
                //set new star
                var p = $('#shoutmap-bg').position();
                var s = $(window).scrollTop();
                jQuery.facebox($('.add-star-ct').html());
                $('#facebox .star_x').val(ev.clientX-p.left);
                $('#facebox .star_y').val(ev.clientY-p.top+s);
                $('#facebox .star_size').val(generate_star_size());
            }
        });
        
        $('input[type=submit]').live('click', function(ev) {
            if ($('#facebox .poster_name').val().length == 0 
                || $('#facebox .post_text').val().length == 0
                || $('#facebox .star_x').val() == 0
                || $('#facebox .star_y').val() == 0
        )
                return false;
            
            $.ajax({
                url: "/ajax.php", 
                data: {
                    mode: 'savestar', 
                    post_text: $('#facebox .post_text').val(), 
                    poster_name: $('#facebox .poster_name').val(),
                    star_x: $('#facebox .star_x').val(),
                    star_y: $('#facebox .star_y').val(),
                    star_size: $('#facebox .star_size').val()
                }, 
                success: function(msg){
                    jQuery(document).trigger('close.facebox');
                    window.location.reload();
                }
            });
            
            return false;
        });
        
        $('.infobox').fadeTo(1, 0, function() { $(this).hide();});
        $('.infolink').hover(function() {
            $(this).addClass('infolink-hover');
            $('.infobox').show();
            $('.infobox').fadeTo(200, 0.8);
        },
        function () {
            $(this).removeClass('infolink-hover');
            $('.infobox').fadeTo(200, 0, function() { $(this).hide();});
        });
        }
    });
