// (c) 2006 Chris M., Vergouwen Overduin

$(document).ready(function() {
    $('.qf_option_h').click(function(event) {
    	if ($(event.target).is('td.qf_option_h') || 
    		    $(event.target).is('div.qf_elb')) {
            $(':radio', this).trigger('click');
    	}
//        return false;
    });
    $('.qf_vgroup .qf_gel').click(function(event) {
    	if ($(event.target).is('input.qf_other')) {
            if (!$(':checkbox', this).attr('checked')) {
                $(':checkbox', this).trigger('click');
                $(':text', this).removeClass('qf_inactive');
                $('input.qf_other', this).focus();
            }
            if (!$(':radio', this).attr('checked')) {
                $(':radio', this).trigger('click');
                $(':text', this).removeClass('qf_inactive');
                $('input.qf_other', this).focus();
            }
            return false;
        } else if ($(event.target).is('div.qf_gel')) {
    		$(':checkbox', this).trigger('click');
    		$(':radio', this).trigger('click');
            if ($(':checkbox', this).attr('checked')) {
                $(':text', this).removeClass('qf_inactive');
            } else {
                $(':text', this).addClass('qf_inactive');
            }
    	}
//    	return false;
    });
    $('#searchWords').focus(function() {
    	$(this).val('');
    	$(this).removeClass('searchInit');
    	$(this).addClass('searchActive');
    });
});

// To get new forum post for realtime forum.
function displayNewForumPosts(msgs) {
    if (msgs) {
        var l = msgs.length;
        for (var i = 0; i < l; i++) {
            var msgTemp = $('#forumItemDummy').html().
                          replace('{TITLE}', msgs[i]['title']).
                          replace('{AUTHOR}', msgs[i]['author']).
                          replace('{DATE}', msgs[i]['date']).
                          replace('{CONTENT}', msgs[i]['content']);
            $(msgTemp).insertAfter('#forumItemDummy').hide().fadeIn(1200);
        }
    }
}

function getNewForumPosts(id, last) {
    $.ajax({
        type: "GET",
        url: "ajax.php",
        data: "act=getForumPosts&id=" + id + "&last=" + last,
        dataType: "json",
        async: true,
        timeout: 15000,
        success: function(dat) {
            var l = last;
            if (dat) {
                displayNewForumPosts(dat[1]);
                l = dat[0];
            }
            setTimeout(function() {
                    getNewForumPosts(id, l);
                }, 15000);
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            setTimeout(function() {
                getNewForumPosts(id, last);
            }, 15000);
        }
    });
}

