var build_badge = {    

    badge: '',
    badge_header: '',
    links: '',
    footer: '',

    hex: '',

    buildIncludeCode: function() {
        var random_id = parseInt(Math.random() * 1000);
        var newCode = '&lt;div class="twitstatus_badge_container" id="twitstat_badge_' + random_id + '"&gt;&lt;/div&gt;\n';
        
        var near = $("#near").val(),
            within = $("#within").val();

        newCode += '&lt;script type="text/javascript" src="http://twitstat.us/twitstat.us-min.js"&gt;&lt;/script&gt;\n';
        newCode += '&lt;script type="text/javascript"&gt;\n';
        newCode += 'twitstat.badge.init({\n';
        newCode += '    badge_container: "twitstat_badge_' + random_id + '",\n';
        newCode += '    title: "' + $("#title").val().replace(/"/g, '&#092;"') + '",\n';
        newCode += '    keywords: "' + $("#keywords").val().replace(/"/g, '&#092;"') + '",\n';
        newCode += '    max: ' + $("#max").val() + ',\n';
        newCode += '    border_color: "' + $("#border_color").val() + '",\n';
        newCode += '    header_background: "' + $("#header_background").val() + '",\n';
        newCode += '    header_font_color: "' + $("#header_font_color").val() + '",\n';
        newCode += '    content_background_color: "' + $("#content_background_color").val() + '",\n';
        newCode += '    content_font_color: "' + $("#content_font_color").val() + '",\n';
        newCode += '    link_color: "' + $("#link_color").val() + '",\n';
       
        if(near) newCode += '    near: "' + near + '",\n';

        if(within) {
            var withinBase = within.split(""),
                within = "",
                units = "";

            for(var i = 0; i < withinBase.length; i++) {
                if(parseInt(withinBase[i])) within += withinBase[i];
                else units += withinBase[i];
            }

            newCode += '    within: "' + within + '",\n';
            newCode += '    units: "' + units + '",\n';
        }

        newCode += '    width: ' + $("#badge_width").val() + '\n';
        newCode += '});\n'     
        newCode += '&lt;/script&gt;';
        
        $("#previewCode").html(newCode);
        build_badge.rebuildPreview(); 
    },

    rebuildPreview: function() {
        $('.twitstatus_badge_container').empty();

        var max = $('#max').val(),
            keywords = $('#keywords').val(),
            newProps = {
                badge_container: 'twitstat_badge',
                title: $('#title').val(),
                keywords: keywords,
                max: max,
                border_color: $('#border_color').val(), 
                header_background: $('#header_background').val(), 
                header_font_color: $('#header_font_color').val(), 
                content_background_color: $('#content_background_color').val(), 
                content_font_color: $('#content_font_color').val(), 
                link_color: $('#link_color').val() 
            };

        twitstat.badge.props = newProps;
        
        $.ajax({
            url: "http://search.twitter.com/search.json",
            data: "rpp=" + max + "&q=" + encodeURIComponent(keywords),
            dataType: 'jsonp',
            jsonp: 'callback',
            success: function(data) { twitstat.construct(data); }
        });
    }
}

$(function () {
    $("#show_location_params").click(function() {
        if($(this)[0].checked) {
            $("#location_params").fadeIn("slow");
        } else {
            $("#location_params").hide();
        }
    });
    
    $("#tos_link").click(function() {
        $("#terms_of_service").fadeIn("slow");
        $('html, body').animate({ scrollTop: $("#terms_of_service").offset().top }, 800);
        return false;
    });

    $("#faq_link").click(function() {
        $("#questions").fadeIn("slow");
        $('html, body').animate({ scrollTop: $("#questions").offset().top }, 800);
        return false;
    });

    $("#about_link").click(function() {
        $("#about").fadeIn("slow");
        $('html, body').animate({ scrollTop: $("#about").offset().top }, 800);
        return false;
    });

    $.each($('.color_picker'), function(i) {
        var lol = $(this)[0];
        $(this).ColorPicker({
            onShow: function (colpkr) {
                build_badge.badge = document.getElementById("twitstatus_badge");
                build_badge.badge_header = document.getElementById("twitstatus_badge_header");
                build_badge.links = $("#twitstatus_badge li a");
                build_badge.footer = document.getElementById("twitstatus_badge_footer");
                $(colpkr).fadeIn(500);
                return false;
            },
            onHide: function(colpkr) { 
                $(lol).css({'backgroundColor': '#' + build_badge.hex, 'color': '#' + build_badge.hex}).val("#" + build_badge.hex);
                $(colpkr).fadeOut(500);
                return false;
            },
            onChange: function(hsb, hex, rgb) {
                build_badge.hex = hex;
                // This is horribly inefficient, but this plugin sucks, and I'm somewhat tired of working around it.
                if(lol.id == "border_color") 
                    build_badge.badge.style.borderColor = "#" + hex;
                if(lol.id == "header_background") {
                    build_badge.badge_header.style.backgroundColor = "#" + hex; 
                    build_badge.footer.style.backgroundColor = "#" + hex;
                }
                if(lol.id == "header_font_color")
                    build_badge.badge_header.style.color = "#" + hex;
                if(lol.id == "content_background_color")
                    build_badge.badge.style.backgroundColor = "#" + hex;
                if(lol.id == "content_font_color")
                    $("#twitstatus_badge li").css({"color": "#" + hex});
                if(lol.id == "link_color")
                    $("#twitstatus_badge li a").css({"color": "#" + hex});
            }
        });
    });

    $(".close_button").click(function() {
        $('html, body').animate({ scrollTop: 0 }, 800);
        var lol = $(this); 
        setTimeout(function() { lol.parent().hide(); }, 1000);
    });

    $("#generateBadgeButton").click(function() {
        build_badge.buildIncludeCode();
        $("#pasteCode").fadeIn("slow");
        $('html, body').animate({ scrollTop: $("#pasteCode").offset().top }, 800);
        return false;
    });

    $("#cssTemplateLink").click(function() {
        $("#cssTemplate").fadeIn("normal");
        return false;
    });
});

