﻿function silverlightFnc() {
    return $('#appWallOfSilver')[0].Content.Settings;
}
function keypress(sender, e, key) {
    var keycode;
    if (window.event)
        keycode = window.event.keyCode;
    else if (e)
        keycode = e.which;
    else
        return true;

    if (keycode == 13) {
        settingchanged(key, sender.value);
        return false;
    }
    else
        return true;
}
function settingchanged(key, value) {
    silverlightFnc().SetConfig(key, value);
}
jQuery.fn.ForceNumericOnly =
        function () {
            return this.each(function () {
                $(this).keydown(function (e) {
                    var key = e.charCode || e.keyCode || 0;
                    // allow backspace, tab, delete, arrows, numbers and keypad numbers ONLY
                    return (
                        key == 8 ||
                        key == 9 ||
                        key == 46 ||
                        (key >= 37 && key <= 40) ||
                        (key >= 48 && key <= 57) ||
                        (key >= 96 && key <= 105));
                })
            })
        };
var r;
function showTrending(json) {
    $('#trending').slideDown();
    json = $.parseJSON(json);
    $.each(json.trends, function (i, item) {
        $('#trending .list').append('<a href="#" onclick="">' + item.name + '</a> ');
    });
    $('#trending .list a').click(function () { $('#search').val($(this).text()); settingchanged('search', $('#search').val()); return false; });
}
function updateUI(json) {
    json = $.parseJSON(json);

    $('#search').val(json.search);
    $('#zoom').val(json.zoom);
    $('#speed').val(json.speed);
    $('#noreprise').attr('checked', json.noreprise);
    $('#nostats').attr('checked', json.nostats);
    $('#realtime').attr('checked', json.realtime);
    $('#link').val(json.link);
    $('#embed').data('code', json.embed);
    updateEmbed();

    $('#shortlink').val($('#shortlink').data('string'));

    try {
        pageTracker._trackEvent('Keyword tracking', json.search);
    } catch (err) { }
}
function updateEmbed() {
    if ($('#embed').data('code') != null) {
        $('#embed').text($('#embed').data('code').replace('%%WIDTH%%', $('#embedw').val()).replace('%%HEIGHT%%', $('#embedh').val()));
    }
}
function updateShortLink(shortlink) {
    $('#shortlink').val(shortlink).select();
}
function openWindow(sender, linkid) {
    if ($('#' + linkid).val() != '') {
        window.open(sender.href + escape($('#' + linkid).val()));
    }
    return false;
}
$(document).ready(function () {
    $('.setch').bind('change', function () { settingchanged(this.id, this.value); });
    $('.setch').bind('keypress', function (event) { keypress(this, event, this.id); });
    $('.setch2').bind('change', function () { settingchanged(this.id, this.checked ? 1 : 0); });
    //$('.setch3').bind('change', function () { settingchanged('api', $('#twitter').is(':checked') ? '' : $('#shoutemhost').val()); });

    $('input.text.small').ForceNumericOnly();
    $('#shortlink').data('string', $('#shortlink').val());
    $('#shortlink').click(function () {
        var shortlink = $(this);
        if ((shortlink.data('string') == shortlink.val()) && ($('#link').val() != '')) {
            shortlink.val('...');
            silverlightFnc().GenerateShortLink($('#link').val());
        }
    });

    $("input[type=text]").focus(function () { $(this).select(); });
    $("textarea").focus(function () { $(this).select(); });

    $('#embedw').change(updateEmbed);
    $('#embedh').change(updateEmbed);
});
