﻿var help_timer = -1;
var agents_status = false;
$(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);
});

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');
    });
}
// 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 trim(str) {
    return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}

function isSecure() {
    return window.location.protocol == 'https:';
}