﻿
function substitudeDivImage(SiteURL, img, url) {
    if (url.indexOf("blogger") != -1 || url.indexOf("blogspot") != -1) {
        img.src = 'http://static.socialapproach.com/TivotHome/images/BlogSpotAnon.png';
    }
    else if (url.indexOf("typepad") != -1) {
        img.src = 'http://static.socialapproach.com/TivotHome/images/Typepad.jpg';
    }
    else if (url.indexOf("youtube") != -1) {
        img.src = 'http://static.socialapproach.com/TivotHome/images/youtube.png';
    }
    else {
        img.src = 'http://static.socialapproach.com/TivotHome/images/Information_normal.png';
    }
}

function setFancyLoadingCenter() {
    var divloading = $("div#fancy_loading").css("display");
    if (divloading == 'block') {
        var containerHeight = getContainerHeight();
        var imageHeight = 62;
        var imageWidth = 108;
        var containerWidth = getContainerWidth();
        $("div#fancy_loading").css("position", "fixed");
        $("div#fancy_loading").css("top", (containerHeight / 2) - (imageHeight / 2));
        $("div#fancy_loading").css("left", (containerWidth / 2) - (imageWidth / 2));
    }
}

function substitudeImage(img, url) {
    if (url.indexOf("blogger") != -1 || url.indexOf("blogspot") != -1) {
        img.src = 'http://static.socialapproach.com/TivotHome/images/BlogSpotAnon.png';
    }
    else if (url.indexOf("typepad") != -1) {
        img.src = 'http://static.socialapproach.com/TivotHome/images/Typepad.jpg';
    }
    else if (url.indexOf("youtube") != -1) {
        img.src = 'http://static.socialapproach.com/TivotHome/images/youtube.png';
    }
    else {
        img.src = 'http://static.socialapproach.com/TivotHome/images/Information_normal.png';
    }
}

function checkSession() {
    $.ajax({
        type: "POST",
        url: "CheckSession.aspx",
        data: { Action: 'CheckSession' },
        success: handleCheckSessionResponse,
        error: handleCheckSessionError
    });
}


function handleCheckSessionResponse(result) {
    if (result == "Timeout") {
        parent.$.fn.fancybox.close();
        parent.window.location.reload(false);
        return;
    }
}

function handleCheckSessionError(error) {
    return true;
}

function getContainerHeight() {
    var divHeight = 0;

    if (typeof (window.innerHeight) == 'number') {
        divHeight = window.innerHeight;
    }
    else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
        divHeight = document.documentElement.clientHeight;
    }
    else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
        divHeight = document.body.clientHeight;
    }

    return divHeight;
}


function underProgress() {
    alert('not yet implemented');
}

function setTimeAgo() {
    jQuery('abbr[class*=timeago]').timeago();
    $('abbr').css("border-bottom", "none");
}


function handleError() {
    return true;
}

function processSessionTimeOut() {
    window.location.reload(false);
    return;
}

function getContainerWidth() {
    var divWidth = 0;
    if (typeof (window.innerWidth) == 'number') {
        divWidth = window.innerWidth;
    } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
        divWidth = document.documentElement.clientWidth;
    } else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
        divWidth = document.body.clientWidth;
    }
    return divWidth;
}

function setFancyLoading() {
    $("div#fancy_loading").css("visibility", "visible");
    $("div#fancy_loading").css("display", "block");
    var containerHeight = getContainerHeight();
    var imageHeight = 62;
    var imageWidth = 108;
    var containerWidth = getContainerWidth();
    $("div#fancy_loading").css("position", "fixed");
    $("div#fancy_loading").css("top", (containerHeight / 2) - (imageHeight / 2));
    $("div#fancy_loading").css("left", (containerWidth / 2) - (imageWidth / 2));
}

function hideFancyNavigation() {
    $("a#fancy_left").css("visibility", "hidden");
    $("a#fancy_right").css("visibility", "hidden");
}

function isEncHTML(str) {
    if (str.search(/&amp;/g) != -1 || str.search(/&lt;/g) != -1 || str.search(/&gt;/g) != -1 || str.search(/&quot;/g) != -1 || str.search(/&#39;/g) != -1)
        return true;
    else
        return false;
};

function decHTMLifEnc(str) {
    if (isEncHTML(str))
        return str.replace(/&amp;/g, '&').replace(/&lt;/g, '<').replace(/&gt;/g, '>').replace(/&quot;/g, '"').replace(/&#39;/g, "'");
    return str;
}

// Removes leading whitespaces
function LTrim(value) {

    var re = /\s*((\S+\s*)*)/;
    return value.replace(re, "$1");

}

// Removes ending whitespaces
function RTrim(value) {

    var re = /((\s*\S+)*)\s*/;
    return value.replace(re, "$1");

}

// Removes leading and ending whitespaces
function trim(value) {

    return LTrim(RTrim(value));

}

function substitutePersonaImage(img, personaUserName, personaURI, size) {

    var defaultImageNormal = 'http://static.socialapproach.com/TivotHome/images/no_photo_normal.png';
    var defaultImageBigger = 'http://static.socialapproach.com/TivotHome/images/no_photo_bigger.png';

    if (isPublic != undefined && isPublic == 'true') {
        defaultImageNormal = "http://static.socialapproach.com/TivotHome/images/no_photo_normal.png";
        defaultImageBigger = "http://static.socialapproach.com/TivotHome/images/no_photo_bigger.png"
    }
    if (personaURI.indexOf("twitter.com/") > 0) {
        var imageUrl = "";
        var imgURL = $("#" + hdnAppSpotURL_ClientID);
        if (size != "bigger") {

            imageUrl = imgURL[0].value + personaUserName;
        }
        else {
            imageUrl = imgURL[0].value + personaUserName + "/bigger";
        }

        if (img.src != imageUrl) {
            img.src = imageUrl;
        }
        else {
            if (size == "bigger") {
                img.src = defaultImageBigger;
            }
            else {
                img.src = defaultImageNormal;
            }
        }
    }
    else {
        if (size == "bigger") {
            img.src = defaultImageBigger;
        }
        else {
            substitudeImage(img, personaURI);
        }
    }
}