(function($) {
    $.toJSON = function(o)
    {
        if (typeof(JSON) == 'object' && JSON.stringify)
            return JSON.stringify(o);
        
        var type = typeof(o);
    
        if (o === null)
            return "null";
    
        if (type == "undefined")
            return undefined;
        
        if (type == "number" || type == "boolean")
            return o + "";
    
        if (type == "string")
            return $.quoteString(o);
    
        if (type == 'object')
        {
            if (typeof o.toJSON == "function") 
                return $.toJSON( o.toJSON() );
            
            if (o.constructor === Date)
            {
                var month = o.getUTCMonth() + 1;
                if (month < 10) month = '0' + month;

                var day = o.getUTCDate();
                if (day < 10) day = '0' + day;

                var year = o.getUTCFullYear();
                
                var hours = o.getUTCHours();
                if (hours < 10) hours = '0' + hours;
                
                var minutes = o.getUTCMinutes();
                if (minutes < 10) minutes = '0' + minutes;
                
                var seconds = o.getUTCSeconds();
                if (seconds < 10) seconds = '0' + seconds;
                
                var milli = o.getUTCMilliseconds();
                if (milli < 100) milli = '0' + milli;
                if (milli < 10) milli = '0' + milli;

                return '"' + year + '-' + month + '-' + day + 'T' +
                             hours + ':' + minutes + ':' + seconds + 
                             '.' + milli + 'Z"'; 
            }

            if (o.constructor === Array) 
            {
                var ret = [];
                for (var i = 0; i < o.length; i++)
                    ret.push( $.toJSON(o[i]) || "null" );

                return "[" + ret.join(",") + "]";
            }
        
            var pairs = [];
            for (var k in o) {
                var name;
                var type = typeof k;

                if (type == "number")
                    name = '"' + k + '"';
                else if (type == "string")
                    name = $.quoteString(k);
                else
                    continue;  //skip non-string or number keys
            
                if (typeof o[k] == "function") 
                    continue;  //skip pairs where the value is a function.
            
                var val = $.toJSON(o[k]);
            
                pairs.push(name + ":" + val);
            }

            return "{" + pairs.join(", ") + "}";
        }
    };
    $.evalJSON = function(src)
    {
        if (typeof(JSON) == 'object' && JSON.parse)
            return JSON.parse(src);
        return eval("(" + src + ")");
    };
    $.secureEvalJSON = function(src)
    {
        if (typeof(JSON) == 'object' && JSON.parse)
            return JSON.parse(src);
        
        var filtered = src;
        filtered = filtered.replace(/\\["\\\/bfnrtu]/g, '@');
        filtered = filtered.replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']');
        filtered = filtered.replace(/(?:^|:|,)(?:\s*\[)+/g, '');
        
        if (/^[\],:{}\s]*$/.test(filtered))
            return eval("(" + src + ")");
        else
            throw new SyntaxError("Error parsing JSON, source is not valid.");
    };
    $.quoteString = function(string)
    {
        if (string.match(_escapeable))
        {
            return '"' + string.replace(_escapeable, function (a) 
            {
                var c = _meta[a];
                if (typeof c === 'string') return c;
                c = a.charCodeAt();
                return '\\u00' + Math.floor(c / 16).toString(16) + (c % 16).toString(16);
            }) + '"';
        }
        return '"' + string + '"';
    };
    
    var _escapeable = /["\\\x00-\x1f\x7f-\x9f]/g;
    
    var _meta = {
        '\b': '\\b',
        '\t': '\\t',
        '\n': '\\n',
        '\f': '\\f',
        '\r': '\\r',
        '"' : '\\"',
        '\\': '\\\\'
    };
})(jQuery);

function pokaz_okno(ini)
{  
$.ajax({
		  url: "templates/ajax_okno.htm",
		  cache: false,
		  success: function(html)
		  {
		    $("body").append(html);
		    var id = $("#dialog"); 
		    if(ini.width) id.width(ini.width);
		    if(ini.url!='')
				{
		    	if(ini.DataType!='json') ini.DataType='text';
		    	
		    	$.ajax({
		  		  url: ini.url,
		  		  dataType:ini.DataType,
		  		  cache: false,
		  		  success: function(html2)
		  		  	{
		    		if(ini.DataType=='json')
		    			{
		    			$("#dialog .tresc_info").html(html2.opis);
		    			$("#dialog b").text(html2.tytul);
		    			}
		    		else
		    			{
		    			$("#dialog .tresc_info").html(html2);
		    			$("#dialog b").text(ini.tytul);
		  		  		}
		    		
		    		$('#dialog .close').click(function(e) 
		     				{  
		        	         	$('#boxes').remove();
		     				});
		    		}
		    		});
				}
		    else
		    {
		    	$("#dialog .tresc_info").html(ini.tresc);
    			$("#dialog b").text(ini.tytul);	
    			
	    		$('#dialog .close').click(function(e) 
	     				{  
	        	         	$('#boxes').remove();
	     				});
		    }
		    
    		var maskHeight = $(document).height();  
    		var maskWidth = $(window).width();  

    		$('#mask').css({'width':maskWidth,'height':maskHeight});         
    		$('#mask').fadeIn(200);      
    		$('#mask').fadeTo("fast",0.6);  
		    	 
    		var winH = $(window).height();  
    		var winW = $(window).width();
		   
    		if(!ini.top)
    			$(id).css('top',  winH/2-$(id).height()/2);  
    			else $(id).css('top',ini.top);
    		$(id).css('left', winW/2-$(id).width()/2);  
    		$(id).fadeIn(500);
    		$(id).show();

    		 $('#mask').click(function () 
    			{  
    	         $('#boxes').remove();   
    			});
		  },
		  complete: function() { 
		  } 
	});	
}  

function hide_select(ini)
{
$("select").attr('ukr',function()
	{var elm =''; 
	elm=$(this).attr('ukr');
	if(elm==undefined) {
		$(this).css({ visibility: "hidden" });
		return ini
		} 
	});
}

function show_sele(ini)
{
$("select").attr('ukr',function()
			{var elm =''; 
			elm=$(this).attr('ukr');
			if(elm==ini)
				$(this).removeAttr('ukr').css({ visibility: "visible" });
			});	
}

function pokaz_logowanie()
	{
	var winW=$(window).width();
	if(loged==1)
		{
		$("#menu li a:eq(4)").click(
				function()	{
				var ini = {
                                        url:"templates/ajax_wyloguj.htm",
                                        DataType:"text",
                                        tytul:"Wyloguj",
                                        top:"150px",
                                        width:"380px"};
					pokaz_okno(ini);
				});
		}
		else
		{
			$("#menu li a:eq(4)").click(
					function()
					{
						var ini = {
                                                        url:"templates/ajax_logowanie.htm",
                                                        DataType:"text",
                                                        tytul:"Logowanie",
                                                        top:"150px",
                                                        width:"380px"};
						pokaz_okno(ini);
					});
		}
	}

function validate_email(email)
	{
	var reg=/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	var address=email;
	if(reg.test(address)==false)
		{return false;}
		else
		{return true;}
	}

function select_typ(typx)
	{
	switch(typx)
		{
		case"o":
    		   $("#men_szuk li:eq(0)").attr("class","li_wybrany");
		break;
		case"m":
		   $("#men_szuk li:eq(2)").attr("class","li_wybrany");
		break;
		case"c":
		   $("#men_szuk li:eq(1)").attr("class","li_wybrany");
		break;
		}
	}
$(document).ready(function()
	{
	$(".menu_pasek a span").bind("mouseover",
			function()
				{$(this).animate({"marginTop":"9px","paddingBottom":"1px"},100,
						function()
						{$(this).animate({"marginTop":"17px"},100,
								function()
								{$(this).css("marginTop","13px");});
						});
				});

	$('.okienko .ramka').attr('name',
			function() {
				var ok=$(this);
				var nazwa = $(ok).attr('name');
				var img = new Image();
				img.src=nazwa;
				img.height="230";
				img.width="340";
				$(img).load(function ()	{
                                        $(ok).children('a').html(img);
                                        $(ok).children('img').fadeIn('100');
                                        return true;
                                        });
				
				});
	});
