var goodWork;
var nosey;
var jup = 0;
var jcount;
var imCheck;
var imUp;
var imCount;
var imInt;
var last = 0;
var count = 0;

$(window).load(function(){
    $(window).resize(function() {
        stickFooter();     
    });       

    //this is obviously quality stuff
    goodWork = setInterval("stickFooter()", 500);
    setTimeout("clearInterval(goodWork)", 15000);
    if ($("#adminarea").val() == undefined) {
        nosey = setInterval("parker()", 20000);
        parker();
    }
    if ($("#jimc").val() == 0) {
        imCheckInt = setInterval("checkIm()", 5000);
        checkIm();
    }
    
    $("#pmText").live("keypress", function(e) {
        if(e.which == 13) {
            $("#aimPostButton, #imPostButton").click();   
        }
    });    
    
    $(".imClose").live("click", function() {
        $(this).hide();
        $(".im_box").hide(750);
        $.ajax({
            type: "POST",
            url: "../style/default/ajax/question_ajax.php",
            data: {
                "func": "imShut",
                "jid": $("#jid").val(),
                "gid": $("#group_id").val()
            },
            async: true,
            success: function(ret) {
                // er...
            }
        });          
    });
    
    $("#imPostButton").live("click", function() {
        var text = $("#pmText").val();
        $("#pmText").val('');
        if (text.length < 1) {
            return;   
        }
        $.ajax({
            type: "POST",
            url: "../style/default/ajax/question_ajax.php",
            data: {
                "func": "imPost",
                "jid": $("#jid").val(),
                "gid": $("#group_id").val(),
                "last": last,
                "text": text
            },
            async: true,
            success: function(ret) {
                // er...
            }
        });        
    });    
});

function checkIm() {
    var gid = $("#jgroup_id").val();
    if (!gid || gid == 0) {
        return;
    }
    imCount ++;
    if (imCount > 1000) {
        clearInterval(imCheckInt);
    }
    $.ajax({
        type: "POST",
        url: "../style/default/ajax/question_ajax.php",
        data: {
            "func": "checkForIm"
        },
        async: true,
        success: function(ret) {
            if (ret > 0) {
                clearInterval(imCheckInt);
                
                var addHtml = '<div class="im_box im_box_user">' +
                '<div class="imInner" style="height: 265px;"><div class="imClose"></div>' +
                '</div>' +
                '<input type="text" id="pmText" style="margin: 4px 5px 0px; font-size: 12px; width: 130px;" />' +
                '<input type="button" id="imPostButton" value="Post" style="font-size: 12px;" />' +
                '</div>' ;
                $("body").append(addHtml);
                checkNewIm();
                imInt = setInterval('checkNewIm()', 3000);
                $(".im_box").show(750, function() {
                    $(".imClose").fadeIn(1000);   
                });
            }
        }
    });    
}

function checkNewIm() {
    $.ajax({
        type: "POST",
        url: "../style/default/ajax/question_ajax.php",
        data: {
            "func": "imUpdate",
            "jid": $("#jid").val(),
            "gid": $("#group_id").val(),
            "last": last
        },
        async: true,
        success: function(ret) {
            $(".imInner").append(ret);
            last = $(".imInner .lastId").text();
            if ($(".imInner .lastId").hasClass('newIm')) {
                $(".imInner").animate({scrollTop: ($(".imInner").height() + 500)}, 250);
            }            
            $(".imInner .lastId").remove();
            count ++;
            if (count > 1000) {
                clearInerval(imInt);   
            }
        }
    });       
}


function parker() {
    jcount ++;
    if (jcount > 360) {
        clearInterval(nosey);
    }
    $.ajax({
        type: "POST",
        url: "../style/default/ajax/question_ajax.php",
        data: {
            "func": "journey_update",
            "page": $("input#jpage").val(),
            "user_id": $("input#juser_id").val(),
            "group_id": $("input#jgroup_id").val(),
            "question_id": $("input#jquestion_id").val(),
            "referrer": $("input#jref").val(),
            "ip": $("input#jip").val(),
            "browser": $("input#jbrowser").val(),
            "platform": $("input#jplatform").val(),
            "version": $("input#jversion").val(),
            "upid": jup
        },
        async: false,
        success: function(ret) {
            jup = ret;
        }
    });   
}

function stickFooter() {
    $(".footer").css({"bottom": "0px"});
    var winheight = $(window).height() - $(document).height();
    $(".footer").css({"bottom": winheight+"px"});    
}
