﻿var help_timer = -1;
var agents_status = false;



function trim(str) {
    return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}

function isSecure() {
    return window.location.protocol == 'https:';
}

// IF AGENTS ARE AVAILABLE:
function agents_available() {
    if (!agents_status) {
        agents_status = true;
        $('#live_chat').html('<a href="" onClick="window.open(\'https://www.rocks.tv/livechat.aspx\',\'custclient\',\'width=500,height=320,scrollbars=0\');return false;"><img src="/Images/sidebar/smiley.png" alt="Help" align="left"/><p>Chat Live with our Help Team</p></a>');
    }
    return true;
}
// IF AGENTS ARE NOT AVAILABLE:
function agents_not_available() {
    if (agents_status) {
        agents_status = false;
        $('#live_chat').html('<a href="mailto:help@rocks.tv?subject=Rocks.TV Auction Enquiry"><img src="/Images/sidebar/smiley.png" alt="Help" align="left"/><p>Email our Help Team</p></a>');
    }
    return true;
}

function showprocess(ctl, img) {
    document.getElementById(ctl).style.display = "none";
    document.getElementById(img).style.display = "block";
    return true;
}
function refresh_live_chat() {
    $(function() {
        var protocol = isSecure() ? 'https' : 'http';
        var img = new Image();
        $(img).load(function() {
            $(this).hide();
            $('#live_chat_test').append(this);
            agents_available();
        }).error(function() {
            agents_not_available();
        }).attr('src', protocol + '://admin.instantservice.com/resources/smartbutton/6243/23506/available.gif?6911');
    });
}
$(document).ready(function() {
    $('#toggle_chat').click(function() {
        if (!chat_on) {
            $('#toggle_chat').html('Turn chat off');
        }
        else {
            $('#toggle_chat').html('Turn chat on');
        }
        toggle_chat();
    });
    $("#login").dialog({
        bgiframe: true,
        height: 200,
        modal: true,
        draggable: false,
        autoOpen: false,
        resizable: false,
        title: "Login to Rocks.TV"
    });
    $('#login_link').click(function() {
        $("#login").dialog("open");
    });
    refresh_live_chat();
    help_timer = setInterval(refresh_live_chat, 3000);
    $('#popup').dialog({
        autoOpen: false,
        modal: true
    });
});
function loadPopup(width, height, page) {
    $('#popup').dialog("option", "title", "Loading...");
    $('#popup').html('<img src="/images/ajax.gif" alt="Loading" />');
    $('#popup').load('/Popups/' + page + '.html', function () {
        $('#popup').dialog("option", "title", $('#popup h1').html());
        $('#popup h1').remove();
    });
    $('#popup').dialog("option", "height", height);
    $('#popup').dialog("option", "width", width);
    $('#popup').dialog("open");
    if (page == "LiveHelp") {
        liveHelp = setInterval(refresh_live_chat, 3000);
    }
}

function closePopup() {
    $('#popup').dialog('close');
    clearInterval(liveHelp);
    liveHelp = null;
}