    var miniBasketTimeout;

    function getId(str){
            var str = str.split("_");

            return str[1].toUpperCase();
    }

    function dimScreenCheckout(div_id, opac){
        var wW = $(window).width();
        var wH = $(document).height();

        if(div_id != "" && $("#"+div_id)){
            wH = Math.max($(document).height(),pY+$("#"+div_id).height());
        }

        if(opac == "")opac = "0.7";

        if(!$("#dim-bg").is(":visible")){
            $('body').prepend("<div id='dim-bg'></div>");
            $('#dim-bg').css({display:'block',opacity: opac, 'width':wW+'px','height':wH+'px'});
        }
    }

    function dimScreen(div_id, opac){
        var wW = $(window).width();
        var wH = $(document).height();

        if(div_id != "" && $("#"+div_id)){
            wH = Math.max($(document).height(),pY+$("#"+div_id).height());
        }

        if(opac== "")opac = "0.7";

        if(!$("#dim-bg").is(":visible")){
            $('body').prepend("<div id='dim-bg'></div>");
            $('#dim-bg').css({display:'block',opacity: opac, 'width':wW+'px','height':wH+'px','position':'absolute','z-index':9998,'background':'#000'});
            $("#dim-bg").click(function(){
                hideMiniBasket();
            })
        }
    }

    function removeDim(){

        if($("#cvc_popup") && $("#cvc_popup").is(":visible")){
            $("#cvc_popup").slideUp();
        }

        if($("#what_is_this_cardtype") && $("#what_is_this_cardtype").is(":visible")){
            $("#what_is_this_cardtype").slideUp();
        }

        if($("#newsletter_what_popup") && $("#newsletter_what_popup").is(":visible")){
            $("#newsletter_what_popup").slideUp();
        }

        $("#dim-bg").fadeOut("fast", function(){
            $("#dim-bg").remove();
        });

    }

    function backToTop() {
        var x1 = x2 = x3 = 0;
        var y1 = y2 = y3 = 0;

        if (document.documentElement) {
            x1 = document.documentElement.scrollLeft || 0;
            y1 = document.documentElement.scrollTop || 0;
        }

        if (document.body) {
            x2 = document.body.scrollLeft || 0;
            y2 = document.body.scrollTop || 0;
        }

        x3 = window.scrollX || 0;
        y3 = window.scrollY || 0;

        var x = Math.max(x1, Math.max(x2, x3));
        var y = Math.max(y1, Math.max(y2, y3));

        window.scrollTo(Math.floor(x / 2), Math.floor(y / 2));

        if (x > 0 || y > 0) {
            window.setTimeout("backToTop()", 25);
        }
    }


    function removeItemMini(input_id){
        item_id = getId(input_id);

        updateItemActionP(item_id, 0, "addTo");

    }

    function updateItemMini(input_id){
        item_id = getId(input_id);
        quantity = $("#inputmini_"+item_id).val();

        updateItemActionP(item_id, quantity, "addTo");

    }

    function updateMainBasket(){ // use if change quantity in mini basket @ basket page - temp
        var url = site_url + "basket/basket_ajax";

           $.ajax({
            type:"POST",
            url:url,
            dataType:"text",
            success: function(html){
                $("#basket_content").html(html)
            }
        })
    }

    function addItemToBasketAll(input_id, quantity){
        item_id = getId(input_id);

        updateItemActionP(item_id, quantity, "addMore");

    }

    function update_minisum(){
        total_item = $("#total_item_p").val();
        total_price = $("#total_price_p").val();
        $("#header_items").html(total_item);
        $("#header_price").html(total_price);
    }
    function showMiniBasket(autohide){

        dimScreen("", 0.7);
        $("#mini_basket").slideDown("slow");

        if(autohide == true){
            miniBasketTimeout=setTimeout(hideMiniBasket,10000);

            $("#mini_basket").bind("mouseenter",function(){
                clearTimeout(miniBasketTimeout);
            }).bind("mouseleave",function(){
                miniBasketTimeout=setTimeout(hideMiniBasket,10000);
            });
        }

    }

    function hideMiniBasket(){

        clearTimeout(miniBasketTimeout);

        removeDim();

        $("#mini_basket").slideUp("fast");

        if(miniBasketTimeout){
            clearTimeout(miniBasketTimeout);
        }
    }

    function updateItemActionP(item_id, quantity, action){
        var url = site_url + "basket/add_from_product"+"/"+item_id + "/" + quantity + "/" + action;
        var total_item = 0;
        var total_price = 0;
        var mini_basket_sum = "";
        var item = msg_item;
           $.ajax({
            type:"POST",
            url:url,
            dataType:"text",
            success: function(html){
                $("#mini_basket").html(html);
                bind_event_update_minibasket();
                update_minisum();
                if(total_item > 1)item = msg_items;
                showMiniBasket(true);
                if($("#basket_content")){
                    updateMainBasket();
                }
            }
        })
    }

    function show_popup(title,text,popupY){
    	if($("#popup-box").css('display') != 'none') return false;
        if($("#c_subject")){
            $("#c_subject").hide();
        }
        $('html, body').animate({scrollTop:0}, 'fast');//scroll to top
        var wW = $(window).width();
        var wH = $(window).height();
        var pW = $("#newsletter-box").width();        //Width of the popup
        var pX = (wW-pW)/2;    //X coodinate of the popup
        var pY = 117;

        //var pY = ((popupY!=null)&&(parseInt(popupY)+''!='NaN'))?parseInt(popupY):250;    //Y coodinate of the popup
        var wH = Math.max($(document).height(),pY+$("#popup-box").height());



        $("#popup-title").html(title);
        $("#popup-text").html(text);
        $('body').prepend("<div id='popup-bg'></div>");
        $('#popup-bg').css({display:'block',opacity: 0.7, 'width':wW+'px','height':wH+'px'});
        //$("#popup-box").css({display:'block',top:pY+'px',left:pX+'px',width:pW+'px'});
        $("#popup-box").css({display:'block'});
        set_popup_at_center("#popup-box",9999);
        $("#close-link, #close-image, #popup-bg").click(function(){
            hide_popup();
        })
    }

    function hide_popup() {
        if($("#c_subject")){
            $("#c_subject").show();
        }

        $("#popup-box").css('display','none');
        $("#popup-bg").css('display','none');
        $("#popup-bg").remove();
    }

    /*
    Set an element HTML at the center of the screen
    Params: string of class or id; OR an jquery object of element
    */
    function set_popup_at_center(elementPopup, zindex){
        var wW = $(window).width();
        var wH = $(window).height();
        var pW = $(elementPopup).width();
        var pH = $(elementPopup).height();
        var pX = (pW >= wW) ? 0 : (wW-pW)/2;    //X coodinate of the popup
        var pY = (pH >= wH) ? 0 : (wH-pH)/2;    //Y coodinate of the popup
        $(elementPopup).css({'left':pX,'top':pY,'z-index':zindex});
    }

    function format_price(price,currency){
        html = '';
        price = isNaN(price) ? 0 : price;
        currency_decimal_point = (typeof currency.currency_decimal_point != 'undefined') ? currency.currency_decimal_point : 0;
        currency_sign_before = (typeof currency.currency_sign_before != 'undefined' && currency.currency_sign_before !='') ? currency.currency_sign_before+'&nbsp;' : '';
        currency_sign_after = (typeof currency.currency_sign_after != 'undefined' && currency.currency_sign_after !='') ? '&nbsp;'+currency.currency_sign_after : '';
        currency_float_point = typeof currency.currency_float_point != 'undefined' ? currency.currency_float_point : '';
        new_price = number_format(price, currency_decimal_point);
        int_part = Math.floor(new_price);
        float_part = number_format(new_price - int_part,currency_decimal_point);
        float_part = float_part.substring(2);
        float_part = float_part.length>0 ? '<sup>'+float_part+'</sup>' : '';
        new_value = currency_sign_before + int_part + currency_float_point + float_part + currency_sign_after;
        return new_value;
    }

    function price_helper(price,sale_price,is_active,currency,sale_price_tag,sale_price_class,price_tag,price_class){
        html = '';
        if(is_active == 1 && sale_price > 0){
            html += '<' + price_tag + ' class="'+price_class+'">';
            html += currency.currency_sign_before != '' ? currency.currency_sign_before + '&nbsp;' : '';
            html += get_currency_format(price,currency);
            html += '</' + price_tag + '>';
            html += '<' + sale_price_tag + ' class="'+sale_price_class+'">';
            html += get_currency_format(sale_price,currency);
            html += currency.currency_sign_after != '' ? '&nbsp;' + currency.currency_sign_after : '';
            html += '</'+ sale_price_tag + '>';
        } else {
            html += '<' + price_tag + '>';
            html += currency.currency_sign_before != '' ? currency.currency_sign_before + '&nbsp;' : '';
            html += get_currency_format(price,currency);
            html += currency.currency_sign_after != '' ? '&nbsp;' + currency.currency_sign_after : '';
            html += '</' + price_tag + '>';
        }
        return html;
    }

     function format_price_only(price,currency){
        html = '';
        price = isNaN(price) ? 0 : price;
        currency_decimal_point = (typeof currency.currency_decimal_point != 'undefined') ? currency.currency_decimal_point : 0;
        currency_sign_before = (typeof currency.currency_sign_before != 'undefined' && currency.currency_sign_before !='') ? currency.currency_sign_before+'&nbsp;' : '';
        currency_sign_after = (typeof currency.currency_sign_after != 'undefined' && currency.currency_sign_after !='') ? '&nbsp;'+currency.currency_sign_after : '';
        //currency_float_point = typeof currency.currency_float_point != 'undefined' ? currency.currency_float_point : '';
        new_price = number_format(price, currency_decimal_point);
        int_part = Math.floor(new_price);
        float_part = number_format(new_price - int_part,currency_decimal_point);
        float_part = float_part.substring(2);
        float_part = float_part.length>0 ? '<sup>'+float_part+'</sup>' : '';
        return int_part + float_part;
    }

    function price_helper_only(price,sale_price,is_active,currency,sale_price_tag,sale_price_class,price_tag,price_class){
        html = '';
        if(is_active == 1 && sale_price > 0){
            html += '<' + price_tag + ' class="'+price_class+'">';
            html += currency.currency_sign_before != '' ? currency.currency_sign_before + '&nbsp;' : '';
            html += format_price_only(price,currency);
            html += '</' + price_tag + '>';
            html += '<' + sale_price_tag + ' class="'+sale_price_class+'">';
            html += format_price_only(sale_price,currency);
            html += currency.currency_sign_after != '' ? '&nbsp;' + currency.currency_sign_after : '';
            html += '</'+ sale_price_tag + '>';
        } else {
            html += '<' + price_tag + '>';
            html += currency.currency_sign_before != '' ? currency.currency_sign_before + '&nbsp;' : '';
            html += format_price_only(price,currency);
            html += currency.currency_sign_after != '' ? '&nbsp;' + currency.currency_sign_after : '';
            html += '</' + price_tag + '>';
        }
        return html;
    }

    function get_currency_format(amount,obj_currency){
        currency_sign_before = obj_currency.currency_sign_before;
        currency_sign_after = obj_currency.currency_sign_after;
        currency_decimal_point = obj_currency.currency_decimal_point;
        formated_price = '';
        //formated_price = currency_sign_before;
        //formated_price += (currency_sign_before != '') ? " " : "";
        formated_price +=  number_format(amount,currency_decimal_point);
        //formated_price +=  (currency_sign_after != '') ? " " : "";
        //formated_price +=  currency_sign_after;
        return formated_price;

    }

function number_format (number, decimals, dec_point, thousands_sep) {
    // Formats a number with grouped thousands
    //
    // version: 906.1806
    // discuss at: http://phpjs.org/functions/number_format    // +   original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +     bugfix by: Michael White (http://getsprink.com)
    // +     bugfix by: Benjamin Lupton
    // +     bugfix by: Allan Jensen (http://www.winternet.no)    // +    revised by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +     bugfix by: Howard Yeend
    // +    revised by: Luke Smith (http://lucassmith.name)
    // +     bugfix by: Diogo Resende
    // +     bugfix by: Rival    // +     input by: Kheang Hok Chin (http://www.distantia.ca/)
    // +     improved by: davook
    // +     improved by: Brett Zamir (http://brett-zamir.me)
    // +     input by: Jay Klehr
    // +     improved by: Brett Zamir (http://brett-zamir.me)    // +     input by: Amir Habibi (http://www.residence-mixte.com/)
    // +     bugfix by: Brett Zamir (http://brett-zamir.me)
    // *     example 1: number_format(1234.56);
    // *     returns 1: '1,235'
    // *     example 2: number_format(1234.56, 2, ',', ' ');    // *     returns 2: '1 234,56'
    // *     example 3: number_format(1234.5678, 2, '.', '');
    // *     returns 3: '1234.57'
    // *     example 4: number_format(67, 2, ',', '.');
    // *     returns 4: '67,00'    // *     example 5: number_format(1000);
    // *     returns 5: '1,000'
    // *     example 6: number_format(67.311, 2);
    // *     returns 6: '67.31'
    // *     example 7: number_format(1000.55, 1);    // *     returns 7: '1,000.6'
    // *     example 8: number_format(67000, 5, ',', '.');
    // *     returns 8: '67.000,00000'
    // *     example 9: number_format(0.9, 0);
    // *     returns 9: '1'    // *     example 10: number_format('1.20', 2);
    // *     returns 10: '1.20'
    // *     example 11: number_format('1.20', 4);
    // *     returns 11: '1.2000'
    // *     example 12: number_format('1.2000', 3);    // *     returns 12: '1.200'
    var n = number, prec = decimals;

    var toFixedFix = function (n,prec) {
        var k = Math.pow(10,prec);        return (Math.round(n*k)/k).toString();
    };

    n = !isFinite(+n) ? 0 : +n;
    prec = !isFinite(+prec) ? 0 : Math.abs(prec);    var sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep;
    var dec = (typeof dec_point === 'undefined') ? '.' : dec_point;

    var s = (prec > 0) ? toFixedFix(n, prec) : toFixedFix(Math.round(n), prec); //fix for IE parseFloat(0.55).toFixed(0) = 0;
     var abs = toFixedFix(Math.abs(n), prec);
    var _, i;

    if (abs >= 1000) {
        _ = abs.split(/\D/);        i = _[0].length % 3 || 3;

        _[0] = s.slice(0,i + (n < 0)) +
              _[0].slice(i).replace(/(\d{3})/g, sep+'$1');
        s = _.join(dec);    } else {
        s = s.replace('.', dec);
    }

    var decPos = s.indexOf(dec);    if (prec >= 1 && decPos !== -1 && (s.length-decPos-1) < prec) {
        s += new Array(prec-(s.length-decPos-1)).join(0)+'0';
    }
    else if (prec >= 1 && decPos === -1) {
        s += dec+new Array(prec).join(0)+'0';    }
    return s;
}

function trim(str, chars) {
    return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function ucfirst(str) {
	str += '';
    var f = str.charAt(0).toUpperCase();
    return f + str.substr(1).toLowerCase();
}

    function show_newsletter(){
    	if($("#newsletter-box").css('display') != 'none') return false;
        var wW = $(window).width();
        var wH = $(window).height();
        var pW = $("#newsletter-box").width();
        var pY = 117;        //Y coodinate of the popup
        var pX = (wW-pW)/2;    //X coodinate of the popup


        //var pY = ((popupY!=null)&&(parseInt(popupY)+''!='NaN'))?parseInt(popupY):250;    //Y coodinate of the popup
        var wH = Math.max($(document).height(),pY+$("#newsletter-box").height());

        $('body').prepend("<div id='newsletter-bg' class='close-newsletter'></div>");
        $('#newsletter-bg').css({display:'block',opacity: 0.7, 'width':wW+'px','height':wH+'px'});
        $("#newsletter-box").css({display:'block',top:pY+'px',left:pX+'px'});
        $("#strFirstName").val('');
        $("#strLastName").val('');
        $("#strCity").val('');
        $("#strZip").val('');
        $("#bornday").val('');
        $("#bornmonth").val('');
        $("#bornyear").val('');
        $("#radio_m").removeAttr('checked');
        $("#radio_f").attr('checked','checked');
        if(!$('#newsletter-box form').hasClass('jNice')) $('#newsletter-box form').addClass('jNice');
        $('#newsletter-box form').jNice();

        $(".close-newsletter").click(function(){
            hide_newsletter();
        })
    }

    function hide_newsletter() {
        $("#newsletter-box").css('display','none');
        $("#newsletter-bg").css('display','none');
        $("#newsletter-bg").remove();
        if($('#newsletter-box form').hasClass('jNice')) $('#newsletter-box form').removeClass('jNice');
        $("#nMsg").html('');
    }