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.productLink,.list2 a.productLink").click(function () {
		if($.cookie('a_ustawienia')!="N"){
			returnPage(this);
			return false;
		}
    });

	$(".ajax a").click(function () {
		if($.cookie('a_ustawienia')!="N"){
			returnPage(this);
			$(".list li").removeClass();
			$(this).parent().addClass('active');
			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')=="N") var checked=""; else var checked="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>");
		$.get("/includes/ajax.pageLoad.php"+href,function(data){
			$("#text").replaceWith(data);
			$('#lang').proffDropdown('changeLang');
			$('a.imgFlow').lightBox();
			isLoaded = false;
			$(".list4 a.productLink,.list2 a.productLink").click(function () {
				returnPage(this);
				return false;
			});			
			
		});
}