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;
    }
};

$(document).ready(function(){
	$(".list4 a,.list2 a").click(function () {
		var ax = $.cookie('a_ustawienia');
		if(ax !== null && ax == "Y"){
			returnPage(this);
                        $(this).blur();
			return false;
		}
    });

	$(".ajax a").click(function () {
		var ax = $.cookie('a_ustawienia');
		if(ax !== null && ax == "Y"){
			returnPage(this);
			$(".list li").removeClass();
			$(this).parent().addClass('active');
                        $(this).blur();
			return false;
		}
    });
	
	$("#a_ustawienia").click(function () {
		var title=$(this).html();
		var text=$("#a_ustawienia").attr('title');
		var save=$("#a_ustawienia").attr('save');
		if($.cookie('a_ustawienia')=="Y") var checked="checked"; else var checked="";
		$(this).before("<div id='a_form'><p class='title'>"+title+" <a href='#'><img src='/gfx/close2.gif' /></a></p><p><input type='checkbox' class='checkbox' id='a_ajax' name='a_ajax' "+checked+" /> "+text+"</p><p class='right'><button>"+save+"</button></p></div>");
		
		$("#a_form a").click(function () {
			
			$("#a_form").remove();
			return false;
		});

		$("#a_form button").click(function () {
            var date = new Date();
			if($('#a_form input').attr('checked')) var ajax="Y"; else var ajax="N";
            date.setTime(date.getTime() + (365 * 24 * 60 * 60 * 1000));
            $.cookie('a_ustawienia', ajax, { path: '/', expires: date });
			$("#a_form").remove();
			return false;
		});
		
		
		return false;
    });
	
});

function returnPage(that){
		var href=$(that).attr("href").replace("http://", "").replace(location.host+"/", "");
		$("#text").html("<p align='center'><img src='gfx/load_result.gif' /></p>");
		
		//JESLI PROMOCJA
		if ($(that).parent().css('color') == 'red') {
			location.href = $(that).attr("href");
			return false;
		}
		
		$.get("/index/getProductView/"+href,function(data){
			$("#text").replaceWith(data);
			$('table.dane tbody').each(function() {
				if (this.childNodes.length == 0) $(this).parent('table').hide();
			});
			$('#lang').proffDropdown('changeLang');
			$('a.imgFlow').lightBox({
                            imageLoading:  '/gfx/lightbox/loading.gif',
                            imageBtnPrev:  '/gfx/lightbox/prev.gif',
                            imageBtnNext:  '/gfx/lightbox/lb-next.gif',
                            imageBtnClose: '/gfx/lightbox/lb-close.gif',
                            imageBlank: '/gfx/lightbox/blank.gif',
                            txtOf: t('z'),
                            txtImage: t('Obrazek')
                        });
                        Filter.init();
			isLoaded = false;
			$(".list4 a,.list2 a").click(function () {
				returnPage(this);
                                $(this).blur();
				return false;
			});			
			$(document).ready(function() {
				if ($('#daneTechniczne tr').size() == 0) $('#daneTechniczne').remove();
			});		
		});
}
