/**
 * Created by JetBrains PhpStorm.
 * User: Klaas
 * Date: 25-1-12
 * Time: 11:31
 * To change this template use File | Settings | File Templates.
 */
    var vars = window.location.search.substring(1).split("&");
    var loadinganim = $('<img style="margin: 0 22px;">').attr('src', 'img/loading.gif');

    function Param(name)
    {
      name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
      var regexS = "[\\?&]" + name + "=([^&#]*)";
      var regex = new RegExp(regexS);
      var results = regex.exec(window.location.href);
      if(results == null)
        return false;
      else
        return decodeURIComponent(results[1].replace(/\+/g, " "));
    }

    (function($) {
        $.fn.countTo = function(options) {
            if (options.to % 1 == 0) {
                options.decimals = 0;
            }
            if (parseInt(this.html()) != 0) {
                options.speed = 100;
                options.from = parseInt(this.html());
            }
            // merge the default plugin settings with the custom options
            options = $.extend({}, $.fn.countTo.defaults, options || {});

            // how many times to update the value, and how much to increment the value on each update
            var loops = Math.ceil(options.speed / options.refreshInterval),
                increment = (options.to - options.from) / loops;

            return $(this).each(function() {
                var _this = this,
                    loopCount = 0,
                    value = options.from,
                    interval = setInterval(updateTimer, options.refreshInterval);

                function updateTimer() {
                    value += increment;
                    loopCount++;
                    $(_this).html(value.toFixed(options.decimals)+"%");

                    if (typeof(options.onUpdate) == 'function') {
                        options.onUpdate.call(_this, value);
                    }

                    if (loopCount >= loops) {
                        clearInterval(interval);
                        value = options.to;

                        if (typeof(options.onComplete) == 'function') {
                            options.onComplete.call(_this, value);
                        }
                    }
                }
            });
        };

        $.fn.countTo.defaults = {
            from: 0,  // the number the element should start at
            to: 100,  // the number the element should end at
            speed: 500,  // how long it should take to count between the target numbers
            refreshInterval: 30,  // how often the element should be updated
            decimals: 1,  // the number of decimal places to show
            onUpdate: null,  // callback method for every time the element is updated,
            onComplete: null,  // callback method for when the element finishes updating
        };
    })(jQuery);

    function Kthis(s){
        var $ext = '';
        if (s > 1000) {s = s.substring(0, s.length - 3); $ext = 'k+'};

        s += '';
        var x = s.split('.');
        var x1 = x[0];
        var x2 = x.length > 1 ? '.' + x[1] : '';
        var rgx = /(\d+)(\d{3})/;
        while (rgx.test(x1)) {
            x1 = x1.replace(rgx, '$1' + '.' + '$2');
        }
        return x1 + x2+$ext;

    }



$('#disclaimer').click(function(){ $('#Popup_disclaimer').jqmShow().show(); });
$('#howitworks').click(function(){ $('#Popup_howitworks').jqmShow().show(); });


$(document).ready(function () {

    var startActions=   function(hash){         hash.w.hide(); };
    var endActions=     function(hash) {        hash.w.hide(); hash.o.remove()};
    $('#Popup_disclaimer, #Popup_howitworks').jqm({
       modal: false,
       onShow: startActions,
       onHide: endActions});

    $('#slider-pos, #slider-neg, #slider-ind').slider({
        range:"min",
        min:0,
        max:100,
        step:0.1,
        animate:1000,
        disabled: true,
        value: 0,
        change:function (event, ui) {
            $(this).prev('.amount').countTo({ to:$(this).slider("value"),onComplete: function(value) {
                //$(this).effect("highlight", { color:"" }, 3000);
            }
            });
            //$(this).prev('.amount').val($(this).slider("value")+"%");
        },
        create:function () {
            //$(this).prev('.amount').val($(this).slider("value")+"%");
        }
    });

    //if (!$.browser.msie) {
        //Corners
        $('.rounded').corner('5px round');
        $('.roundedsm').corner('2px round');
        $('.rounded_bottom').corner('5px round bottom');
        $('.rounded_top').corner('5px round top');
    //}

});
