(function($) {

    $.fn.parseTemplate = function(data)
    {
        var str = (this).html();
        var _tmplCache = {};
        var err = "";
        try
        {
            //console.log("in function ="+str);
            var func = _tmplCache[str];
            if (!func)
            {
                var strFunc = "var p=[],print=function() {p.push.apply(p,arguments);}; " +
                              "with(obj){p.push('" +
                              str.replace(/[\r\t\n]/g, " ")
                                      .replace(/'(?=[^#]*#>)/g, "\t")
                                      .split("'").join("\\'")
                                      .split("\t").join("'")
                                      .replace(/<#=(.+?)#>/g, "',$1,'")
                                      .split("<#").join("');")
                                      .split("#>").join("p.push('")
                        + "');} return p.join('');";
                //console.log(strFunc);
                func = new Function("obj", strFunc);
                _tmplCache[str] = func;
            }
            return func(data);
        }
        catch (e)
        {
            err = e.message;
        }
        return "< # ERROR: " + err.toString() + " # >";
    };

})(jQuery);


(function($) {
    $.fn.resize = function(options) {

        var settings = $.extend({
            scale: 1,
            maxWidth: null,
			maxHeight: null
        }, options);

        return this.each(function() {

			if(this.tagName.toLowerCase() != "img") {
				// Only images can be resized
				return $(this);
			}

			var width = this.naturalWidth;
			var height = this.naturalHeight;
			if(!width || !height) {
				// Ooops you are an IE user, let's fix it.
				var img = document.createElement('img');
				img.src = this.src;

				width = img.width;
				height = img.height;
			}

			if(settings.scale != 1) {
				width = width*settings.scale;
				height = height*settings.scale;
			}

			var pWidth = 1;
			if(settings.maxWidth != null) {
				pWidth = width/settings.maxWidth;
			}
			var pHeight = 1;
			if(settings.maxHeight != null) {
				pHeight = height/settings.maxHeight;
			}
			var reduce = 1;

			if(pWidth < pHeight) {
				reduce = pHeight;
			} else {
				reduce = pWidth;
			}

			if(reduce < 1) {
				reduce = 1;
			}

			var newWidth = width/reduce;
			var newHeight = height/reduce;

			return $(this)
				.attr("width", newWidth)
				.attr("height", newHeight);

        });
    }
})(jQuery);



$.extend({URLEncode:function(c)
{
    return encodeURIComponent(c);
},
    URLDecode:function(s)
    {
        return decodeURIComponent(s);
    }
});


// number formatting function
// copyright Stephen Chapman 24th March 2006, 22nd August 2008
// permission to use this function is granted provided
// that this copyright notice is retained intact
function formatNumber(num)
{
    var dec = 2;
    thou = ' ';
    pnt = ',';
    curr1 = '';
    curr2 = '';
    n1 = '-';
    n2 = '';

    var x = Math.round(num * Math.pow(10, dec));
    if (x >= 0) n1 = n2 = '';
    var y = ('' + Math.abs(x)).split('');
    var z = y.length - dec;
    if (z < 0) z--;
    for (var i = z; i < 0; i++) y.unshift('0');
    if (z < 0) z = 1;
    y.splice(z, 0, pnt);
    if (y[0] == pnt) y.unshift('0');
    while (z > 3)
    {
        z -= 3;
        y.splice(z, 0, thou);
    }
    var r = curr1 + n1 + y.join('') + n2 + curr2;
    return r;
}


//zakacha harakteristikite
function initHaracteristic()
{
    $(".product_bottom h3").each(function()
    {
        var tip_id = this.id.substring(5);
        var cnt = $('#ttp_' + tip_id).html();

        var tt = $(this).wTooltip({
            content: cnt,
            follow: true,
            fadeIn: 600,
            delay: 400,
            style: {
                border:0,
                padding:0,
                background: "none",
                zIndex: "100"
            },
            className: "tooltip",
            id: "tt_"+tip_id
        });

    });
}

// zakacha BG cenite
function initBGPrices()
{
    var display_timeout = 0;
    var hide_timeout = 0;

    $(".more_show_hide").each(function(th)
    {
        $(this).mouseover(function()
        {
            var that = this;
            var article_id = that.id.substring(2);
            hide_timeout = setTimeout(
                    function()
                    {
                        hide_timeout = 0;
                        $('#pt-' + article_id).slideUp("normal", function()
                        {
                            $('#pt-' + article_id).hide();
                        });
                    }, 6000
                    );

            $('#pt-' + article_id).slideDown("normal");
        });
        $(this).mouseout(function()
        {
            display_timeout = 0;
            hide_timeout = 0;
            var that = this;
            var article_id = that.id.substring(2);
            $('#pt-' + article_id).slideUp("normal", function()
            {
                $('#pt-' + article_id).hide();
            });

        });
    });
}



//zakacha linkovete za basketa
function initBasketLinks()
{
    var page = ($('#in_detail_order').is('div')?1:0);
    
    $('a.order').each(
            function()
            {
                var tmr;
                $(this).unbind('click').click(function()
                {
                    var id = this.id.substring(2);

                    $("#productAddLoader").show();

                    $.ajax({type: "POST",url: '${base}/basket/' + id, data:{count:'1',page:page, _method:'PUT'},
                        complete: function()
                        {
                            //$('preload_bonus').hide();
                        },
                        success:function(data)
                        {
                            $('#order_list_products').html(data);
                        } // succes
                    }); // ajax POST
                    tmr = setTimeout(
                            function()
                            {
                                $("#productAddLoader").hide();
                            }, 2000
                            );
                    return false;
                }); // click
            });
}


function initCompareLinks()
{
    // закача линковете за сравняването
    $('a.compare').each(
            function()
            {
                var tmr;
                $(this).unbind('click').click(function()
                {
                    var id = this.id.substring(2);

                    $.ajax({type: "POST",url: '${base}/compare/' + id, data:{ _method:'PUT'},
                        complete: function()
                        {
                        },
                        success:function(data)
                        {
                            $('#compare_products_list').html(data);
                        } // succes
                    }); // ajax POST
                    return false;
                }); // click
            });
}

function initPreferedLinks()
{
    // закача линковете за сравняването
    $('a.favorite').each(
            function()
            {
                var tmr;
                $(this).unbind('click').click(function()
                {
                    var id = this.id.substring(3);
                    $.ajax({type: "POST",url: '${base}/prefer/' + id, data:{ _method:'PUT'},
                        complete: function()
                        {
                        },
                        success:function(data)
                        {
                            $('#prefer_products_list').html(data);
                        } // succes
                    }); // ajax POST
                    return false;
                }); // click
            });
}


