this.showTips = function(){
    xTOffset = 15;
    yTOffset = -30;
    $("#tooltip").hide();    
    $(".tooltip").hover(function(e){
        $(this).attr('oldtitle',this.title);
        if(this.title) {
            $("body").append("<p class='internalTip' id='tooltip'>"+ this.title +"</p>");
            $("#tooltip").fadeIn("fast");
            xOff = tooltipX(e);
            $("#tooltip").css("top",e.pageY + yTOffset  + "px").css("left",e.pageX + xOff  + "px");
            $(this).attr('title',null);
        }
    },
    function(){
        $("#tooltip").remove();
        $(this).attr('title',$(this).attr('oldtitle'));
    });
    $(".tooltip").mousemove(function(e){
        xOff = tooltipX(e);
        $("#tooltip")
        .css("top",e.pageY + yTOffset + "px")
        .css("left",e.pageX + xOff  + "px");
    });
    $(".tooltip").click(function(){
        $("#tooltip").remove();
        return true;
    });
    function tooltipX(e) {
        xOff = (xTOffset);
        if(e.pageX+$('#tooltip').width()>$(window).width()) {
            xOff*=-1;
        }
        return xOff;
    }
};

$(document).ready(function(){
    showTips();
});
function switchBlock(el,eff) {
    if(!el) {
        return;
    }
    var  name;
    var checked;
    if( el.id ) {
        name = el.id;
        checked = el.checked;
    }
    else {
        name = el.attr('id');
        checked = el.attr('checked');
    }

    if( typeof(eff)=="undefined" ) {
        eff = true;
    }

    if(checked) {
        if(!eff) {
            $('#'+name+'Data').show();
        }
        else {
            $('#'+name+'Data').slideDown();
        }
        if( name.substr(name.length-'allegro'.length,name.length)  == 'allegro' ) {
            if( $('#order_delivery_2_1').length) {
                $('#order_delivery_2_1').attr('disabled','disabled');
            }
            if( $('#order_delivery_2_2').length) {
                $('#order_delivery_2_2').attr('disabled','disabled');
            }
        }
    }
    else {
        if(!eff) {
            $('#'+name+'Data').hide();
        }
        else {
            $('#'+name+'Data').slideUp('fast');
        }
        if( name.substr(name.length-'allegro'.length,name.length)  == 'allegro' ) {
            if( $('#order_delivery_2_1').length) {
                $('#order_delivery_2_1').removeAttr('disabled');
            }
            if( $('#order_delivery_2_2').length) {
                $('#order_delivery_2_2').removeAttr('disabled');
            }
        }
    }
}

function initSearch() {
    var s = document.getElementById('searchVal');
    if( s.value == 'Szukaj' ) {
        s.value = '';
    }
}

function finishSearch() {
    var s = document.getElementById('searchVal');
    if( s.value == '' ) {
        s.value = 'Szukaj';
    }
}

function loadRecommendations(offset,elId,url) {
    path = url+((url.indexOf('?')!=-1)?'&':'?')+'tmp='+(new Date().getTime())+'&offset='+offset;
    if($('#'+elId).height()) {
        $('#'+elId).fadeOut('fast');
    }
    jQuery.ajax({
        type:'POST',
        dataType:'html',
        success: function(html) {
            $('#'+elId).html('');
            $('#'+elId).append(html);
            $('#'+elId).fadeIn('fast');
        },
        failure: function() {
            $('#'+elId).html('');
        },
        url: path
    });
}
function decOffset(offset,elId,url) {
    offset-=1;
    if( offset<0 ) {
        offset=0;
        return;
    }
    loadRecommendations(offset,elId,url);
}
function incOffset(offset,elId,url) {
    offset+=1;
    if( offset<0 ) {
        offset=0;
    }
    loadRecommendations(offset,elId,url);
}

var currentProductPreview;
this.productPreview = function(){
    /* CONFIG */
    xOffset = 160;
    yOffset = 30;

    /* END CONFIG */
    $("a.productPreview").hover(function(e){
    	
        if(($.cookie('productPreviewTooltip')==null) || $.cookie('productPreviewTooltip')=='On')
        {
            $("body").append("<div class=raised id=ppp style='display:none'>"+$("#productPreview"+$(this).attr('id')).html()+"</div>");
            this.t = this.title;
            this.title = "";
            var c = (this.t != "") ? "<br/>" + this.t : "";
            $('#ppp').click(function(){
                $(this).remove();
            });
            currentProductPreview = $('#ppp');
            $("#ppp").css("position", "absolute")
            .css("z-index", "9999")
            .css("top",previewY(e.pageY - xOffset) + "px")
            .css("left",previewX(e.pageX + yOffset) + "px")
            .delay(600)
            .fadeIn('fast');
        }
    },
    function(){
        this.title = this.t;
        $("#ppp").remove();
    });
    $("a.ProductPreview").mousemove(function(e){
        $("#ppp")
        .css("position", "absolute")
        .css("top",previewY(e.pageY - xOffset) + "px")
        .css("left",previewX(e.pageX + yOffset) + "px");
    });
   
};

function previewY(y) {
    if(y<$(window).scrollTop()) {
        y=$(window).scrollTop();
    }
    if(y>$(window).height()+$(window).scrollTop()-400) {
        y=$(window).height()+$(window).scrollTop()-400;
    }
    return y;
}
function previewX(x) {
    if(x+600>$(window).width()) {
        x=x-600;
    }
    if(x<1) {
        x=1;
    }
    return x;
}

function  changePreview()
{	
    if(($.cookie('productPreviewTooltip')==null) || $.cookie('productPreviewTooltip')=='On')
    {
        $.cookie('productPreviewTooltip', 'Off');
        $("#zoom").attr('title', 'Włącza powiększanie miniatur produktów');
        if($('#zoom').attr('unactive')) {
            $("#zoom").html("<img src='"+$("#zoom").attr('unactive')+"'>");
        }
        $("#zoom").removeClass('active');
        notify({
            style: 'jsSuccess',
            body: 'Wyłączono podgląd produktów',
            delay: 2000
        });
    }
    else
    {
        $.cookie('productPreviewTooltip', 'On');
        $("#zoom").attr('title', 'Wyłącza powiększanie miniatur produktów');
        if($('#zoom').attr('active')) {
            $("#zoom").html("<img src='"+$("#zoom").attr('active')+"'>");
        }
        $("#zoom").addClass('active');
        notify({
            style: 'jsSuccess',
            body: 'Włączono podgląd produktów',
            delay: 2000
        });
    }
}

jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

function numSuff(num,texts) {
    num = Math.abs(num);
    if(num==1) {
        return texts[0];
    }
    if(num>=10 && num<=20) {
        return texts[2];
    }
    if(num%10>1 && num%10<5) {
        return texts[1];
    }
    return texts[2];
}
