﻿// This used for the popup

$(document).ready(function () {

    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(AfterPostback);

});

var cachecounter = 0;
var idcache = new Array();
var resultscache = new Array();
var callsmade = 0;
var updatetimer = null;
var lastid = 0;
var postbacktimer;
  
function AfterPostback(sender, args)
{
    if (postbacktimer != null) clearInterval(postbacktimer);
    postbacktimer = setTimeout("ClearMessages()", 15000);
}
function ClearMessages() {
    if (postbacktimer != null) clearInterval(postbacktimer);
    postbacktimer = null;
    $(".MessageFade").stop().fadeOut(2000);
    $(".MessageFadeStay").stop().fadeTo(2000, 0.01);
    return true;
}

function ShowWebResult(id, type) {
    var div = document.getElementById("sticky1");

    //        BuildDebug()
    // Load straight away if in the cache
    for (i = 0; i < cachecounter; i++) {
        if (idcache[i] == id) {
            div.innerHTML = resultscache[i];
            return;
        }
    }

    // If id not in the cache then fire straight away
    if (i == cachecounter) {
        idcache[cachecounter] = id;
        resultscache[cachecounter] = "<br /><br /><br /><br /><img src='/images/popupload.gif' />";
        div.innerHTML = "<br /><br /><br /><br /><img src='/images/popupload.gif' />";
        callsmade = callsmade + 1;
        ret = PopupService.GetDetails(id, type, cachecounter, OnComplete, OnTimeout, OnError);
        lastid = id;
        cachecounter = cachecounter + 1;
        if (updatetimer != null) clearTimeout(updatetimer);
        updatetimer = setTimeout("CheckResult()", 5000);
        return;
    }

}

// the three outcomes from the call
function OnComplete(results) {

    var callnum = parseInt(results.substring(0, 2));
    var clean = results.substring(2, results.length);

    resultscache[callnum] = clean;
    //        BuildDebug()

    // only load into the div if it was the last one
    if (idcache[callnum] == lastid) {
        var div = document.getElementById("sticky1");
        div.innerHTML = clean;
    }
}

function CheckResult() {
    for (i = 0; i < cachecounter; i++) {
        if (resultscache[i] == "<br /><br /><br /><br /><img src='/images/popupload.gif' />") {
            resultscache[i] = "<br /><br /><br /><br />Sorry, we couldn't get this user's details";
        }
    }
}

function OnTimeout() {

}
function OnError() {

}

function ShowMessage(message) {
    var div = document.getElementById("sticky1");
    lastid = 0;
    
    // If id not in the cache then fire straight away
    if (message==1) {
        div.innerHTML = "<div class='InfoBox'>When you follow another member you get the credits they are offering. " +
                        "Sometimes you will receive less credits than are on offer. There are a couple of reasons why this might happen:<br/><br/>" +
                        "1) They may have run out of credits just after this page loaded.<br/>"+
                        "2) You may be receiving less credits if you have a high risk level. </div>";
        
        return;
    }
    if (message == 2) {
        div.innerHTML = "<div class='InfoBox'>When you follow another member twiends checks to see if you followed them before through the site. " +
                        "You will not recieve credits for repeat follows through the site. This helps prevent system abuse.</div>";
        return;
    }
    if (message == 3) {
        div.innerHTML = "<div class='InfoBox'>Sometimes when you try to follow someone it will not work. There are a couple of reasons why this might happen:<br/><br/>" +
                        "1) Twitter may be over-capacity and unable to process the request.<br/>" +
                        "2) There may be a problem with the other user or they may have been suspended.<br/>" +
                        "3) You may have reached a following limit imposed by twitter. Over 2000 friends twitter will only allow you to follow 10% more people than you have followers.</div>";

        return;
    }
    if (message == 4) {
        div.innerHTML = "<div class='InfoBox'>If you are following everyone on this page and it fails too many times we will stop trying to follow everyone. " +
                        "This might be a temporary problem with twitter or you may have hit a follow limit imposed by them. " +
                        "When you havesss 2000 friends twitter will only allow you to follow 10% more people than you have followers. "+
                        "If the problem persists please <a href='/Authenticate.aspx'>relink your twitter</a> profile.</div>";


        return;
    }

}

// facebook stuff

//function FacebookLoginCheck() {
//    FB.Connect.get_status().waitUntilReady(function(status) {
//        switch (status) {
//            case FB.ConnectState.userNotLoggedIn:
//                window.location.reload();
//                break;
//        }
//    });
//}


// this used by follow.aspx

function CheckImages() {
    for (var i = 1; i < 51; i++) {
        
        var pic = document.getElementById("ctl00_maincontent_Image"+i);
        if (pic == null) continue;
        if (pic.complete==true) continue;
        else pic.src = 'Images/blank.png';
        pic.width = 64;
        pic.height = 64;   
    }
}

// This is the sticktooltip code


function ShowDiv(divname, descdiv, margin, divset) {

    if (divset == 1) {
        document.getElementById("DescCountry").className = "Hidden";
        document.getElementById("DescEmails").className = "Hidden";
        document.getElementById("DescNotify").className = "Hidden";
//        document.getElementById("DescNew").className = "Hidden";
    }

    if (divset == 2) {
        document.getElementById("DescMain").className = "Hidden";
        document.getElementById("DescOffer").className = "Hidden";
        document.getElementById("DescListed").className = "Hidden";
        document.getElementById("DescDeleteAcc").className = "Hidden";
        document.getElementById("DescApproved").className = "Hidden";
        document.getElementById("DescExclude").className = "Hidden";
    }

    if (divset == 3) {
        document.getElementById("DescPub").className = "Hidden";
        document.getElementById("DescPass").className = "Hidden";
        document.getElementById("DescDelete").className = "Hidden";
    }

    if (divset == 4) {
        document.getElementById("DescMain").className = "Hidden";
        document.getElementById("DescCredits").className = "Hidden";
        document.getElementById("DescDelete").className = "Hidden";
        document.getElementById("DescListed").className = "Hidden";
        document.getElementById("DescDeleteAcc").className = "Hidden";
    }
    if (divset == 5) {
        document.getElementById("DescCheck").className = "Hidden";
        document.getElementById("DescCredits").className = "Hidden";
        document.getElementById("DescDelete").className = "Hidden";
        document.getElementById("DescListed").className = "Hidden";
        document.getElementById("DescPictop").className = "Hidden";
        document.getElementById("DescPic").className = "Hidden";
        document.getElementById("DescName").className = "Hidden";
        document.getElementById("DescLink").className = "Hidden";
        document.getElementById("DescDesc").className = "Hidden";
        document.getElementById("DescVisits").className = "Hidden";
    }

    var outerdiv = document.getElementById(descdiv);
    outerdiv.className = "Visible";
    outerdiv.style.marginTop = margin;

    var mydiv = document.getElementById(divname);
    mydiv.className = "Visible";


}

var modaltimer = null;

function StartModalCountdown(duration) {
    ModalTicksLeft = duration;
    if (modaltimer != null) clearInterval(modaltimer);
    modaltimer = setInterval("ModalTick()", 1000);
    return true;
}

function ModalTick() {
    if (ModalTicksLeft>0) ModalTicksLeft = ModalTicksLeft - 1;

    $("#ModalCountdownMessage").empty().append( ModalTicksLeft.toString());

    if (ModalTicksLeft == 0) {
        if (modaltimer != null) clearInterval(modaltimer);
        $find("MPE1").hide(); 
    }
}