/* introduces elegant exists() function to jquery */

jQuery.fn.exists = function() {
    return jQuery(this).length>0;
};
jQuery.fn.missing = function() {
    return jQuery(this).length==0;
};
(function($) {
    $.fn.extend({
        center: function (options) {
            var options =  $.extend({ // Default values
                inside:window, // element, center into window
                transition: 0, // millisecond, transition time
                minX:0, // pixel, minimum left element value
                minY:0, // pixel, minimum top element value
                vertical:true, // booleen, center vertical
                withScrolling:true, // booleen, take care of element inside scrollTop when minX < 0 and window is small or when window is big
                horizontal:true // booleen, center horizontal
            }, options);
            return this.each( function() {
                var props = {
                    position:'absolute'
                };
                if (options.vertical) {
                    var top = ($(options.inside).height() - $(this).outerHeight()) / 2;
                    if (options.withScrolling)
                        top += $(options.inside).scrollTop() || 0;
                    top = (top > options.minY ? top : options.minY);
                    $.extend(props, {
                        top: top+'px'
                        });
                }
                if (options.horizontal) {
                    //debug.log('lightbox element outter width:')
                    //debug.log($(this).outerWidth())
                    var left = ($(options.inside).width() - $(this).outerWidth()) / 2;
                    if (options.withScrolling)
                        left += $(options.inside).scrollLeft() || 0;
                    left = (left > options.minX ? left : options.minX);
                    $.extend(props, {
                        left: left+'px'
                        });
                }
                if (options.transition > 0)
                    $(this).animate(props, options.transition);
                else
                    $(this).css(props);
                return $(this);
            });
        }
    });
})(jQuery);
(function($) { // extends original "show" function of jquery to look for 'visibility' attribute and for class 'hidden'
    var originalShowMethod = jQuery.fn.show;
    $.fn.show = function (options) {
        this.each( function() {
            $(this).css({
                'visibility': 'visible'
            })
            $(this).removeClass('hidden')
        });
        originalShowMethod.apply( this, arguments );
    };
})(jQuery);

(function($) { // make elements invisible positionable (dimensions must be accessible)
    $.fn.extend({
        makePositionable: function (options) {
            return this.each( function() {
                // display must be block, so that the dimnsions of the element are accsessible
                // visibility must be 'hidden' for the element still being not visible
                // it has to be 'absolute' positioned, so that the element is out of the rendering flow
                $(this).css({
                    'visibility': 'hidden',
                    'display': 'block',
                    'position': 'absolute'
                });
                return $(this);
            });
        }
    });
})(jQuery);

// detects support for placeholder attribute on input fields */
function supports_input_placeholder() {
    var i = document.createElement('input');
    return 'placeholder' in i;
}

function IsNumeric(sText) {
    var ValidChars = "0123456789.";
    var IsNumber = true;
    var Char;

    for (i = 0; i < sText.length && IsNumber === true; i++) {
        Char = sText.charAt(i);
        if (ValidChars.indexOf(Char) == -1) {
            IsNumber = false;
        }
    }
    return IsNumber;

}
//jquery simple Ajax function
//load url and replace divId with response data
function loadContentFromInto(url , divId) {
    $.get(url,
        '',
        function(data){
            $("#faqstats_" + divId).html(data);
            $("#faqstats_info_text_" +divId).hide();
        }
        );
}
/**
 * Javascript for eprimoaffiliate Extension
 *
 */

function loadCalcConf(url) {
    $.get(url,
        "",
        function(data){
            try{
                openCalculator(data)
            }catch(e){
                debug.log('invalid json');
            }
        }
        );
}
//
function openCalculator (data) {
    if (data.length) triggerAction(JSON.parse(data),null);
}
//
$(document).ready(function() {
    $(document).bind('calculatorInitialized', function(){
        if (getURLParameter('opencalc') == 1) {
            loadCalcConf ("/index.php?eID=calculatorconf");
        }
    });
});
//
function getURLParameter(name) {
    return decodeURI(
        (RegExp(name + '=' + '(.+?)(&|$)').exec(location.search)||[,null])[1]
    );
}


