function isset( variable ){
	return( typeof( variable ) != 'undefined' );
}

function replace(f, r, s){
	var ra = r instanceof Array, sa = s instanceof Array, l = (f = [].concat(f)).length, r = [].concat(r), i = (s = [].concat(s)).length;
	while(j = 0, i--)
		while(s[i] = s[i].split(f[j]).join(ra ? r[j] || "" : r[0]), ++j < l);
	return sa ? s : s[0];
}
	
function utf8_decode ( str_data ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Webtoolkit.info (http://www.webtoolkit.info/)
    // +      input by: Aman Gupta
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Norman "zEh" Fuchs
    // +   bugfixed by: hitwork
    // +   bugfixed by: Onno Marsman
    // +      input by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // *     example 1: utf8_decode('Kevin van Zonneveld');
    // *     returns 1: 'Kevin van Zonneveld'
 
    var tmp_arr = [], i = 0, ac = 0, c1 = 0, c2 = 0, c3 = 0;
    
    str_data += '';
    
    while ( i < str_data.length ) {
        c1 = str_data.charCodeAt(i);
        if (c1 < 128) {
            tmp_arr[ac++] = String.fromCharCode(c1);
            i++;
        } else if ((c1 > 191) && (c1 < 224)) {
            c2 = str_data.charCodeAt(i+1);
            tmp_arr[ac++] = String.fromCharCode(((c1 & 31) << 6) | (c2 & 63));
            i += 2;
        } else {
            c2 = str_data.charCodeAt(i+1);
            c3 = str_data.charCodeAt(i+2);
            tmp_arr[ac++] = String.fromCharCode(((c1 & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
            i += 3;
        }
    }
 
    return tmp_arr.join('');
}

function base64_decode (data) {
    // http://kevin.vanzonneveld.net
    // +   original by: Tyler Akins (http://rumkin.com)
    // +   improved by: Thunder.m
    // +      input by: Aman Gupta
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   bugfixed by: Onno Marsman
    // +   bugfixed by: Pellentesque Malesuada
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // -    depends on: utf8_decode
    // *     example 1: base64_decode('S2V2aW4gdmFuIFpvbm5ldmVsZA==');
    // *     returns 1: 'Kevin van Zonneveld'
 
    // mozilla has this native
    // - but breaks in 2.0.0.12!
    //if (typeof this.window['btoa'] == 'function') {
    //    return btoa(data);
    //}
 
    var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
    var o1, o2, o3, h1, h2, h3, h4, bits, i = 0, ac = 0, dec = "", tmp_arr = [];
 
    if (!data) {
        return data;
    }
 
    data += '';
 
    do {  // unpack four hexets into three octets using index points in b64
        h1 = b64.indexOf(data.charAt(i++));
        h2 = b64.indexOf(data.charAt(i++));
        h3 = b64.indexOf(data.charAt(i++));
        h4 = b64.indexOf(data.charAt(i++));
 
        bits = h1<<18 | h2<<12 | h3<<6 | h4;
 
        o1 = bits>>16 & 0xff;
        o2 = bits>>8 & 0xff;
        o3 = bits & 0xff;
 
        if (h3 == 64) {
            tmp_arr[ac++] = String.fromCharCode(o1);
        } else if (h4 == 64) {
            tmp_arr[ac++] = String.fromCharCode(o1, o2);
        } else {
            tmp_arr[ac++] = String.fromCharCode(o1, o2, o3);
        }
    } while (i < data.length);
 
    dec = tmp_arr.join('');
    dec = utf8_decode(dec);
 
    return dec;
}

function print_r(theObj){
  if(theObj.constructor == Array ||
     theObj.constructor == Object){
    document.write("<ul>")
    for(var p in theObj){
      if(theObj[p].constructor == Array||
         theObj[p].constructor == Object){
document.write("<li>["+p+"] => "+typeof(theObj)+"</li>");
        document.write("<ul>")
        print_r(theObj[p]);
        document.write("</ul>")
      } else {
document.write("<li>["+p+"] => "+theObj[p]+"</li>");
      }
    }
    document.write("</ul>")
  }
}
function doRound(x, places) {
  return Math.round(x * Math.pow(10, places)) / Math.pow(10, places);
}
function IncludeJavaScript(jsFile)
{
  $("head").append('<script type="text/javascript" src="js/' + jsFile + '.js"></scr' + 'ipt>');
}
/**
 * Converts the given data structure to a JSON string.
 * Argument: arr - The data structure that must be converted to JSON
 * Example: var json_string = array2json(['e', {pluribus: 'unum'}]);
 * 			var json = array2json({"success":"Sweet","failure":false,"empty_array":[],"numbers":[1,2,3],"info":{"name":"Binny","site":"http:\/\/www.openjs.com\/"}});
 * http://www.openjs.com/scripts/data/json_encode.php
 */
function array2json(arr) {
    var parts = [];
    var is_list = (Object.prototype.toString.apply(arr) === '[object Array]');

    for(var key in arr) {
    	var value = arr[key];
        if(typeof value == "object") { //Custom handling for arrays
            if(is_list) parts.push(array2json(value)); /* :RECURSION: */
            else parts[key] = array2json(value); /* :RECURSION: */
        } else {
            var str = "";
            if(!is_list) str = '"' + key + '":';

            //Custom handling for multiple data types
            if(typeof value == "number") str += value; //Numbers
            else if(value === false) str += 'false'; //The booleans
            else if(value === true) str += 'true';
            else str += '"' + value + '"'; //All other things
            // :TODO: Is there any more datatype we should be in the lookout for? (Functions?)

            parts.push(str);
        }
    }
    var json = parts.join(",");
    
    if(is_list) return '[' + json + ']';//Return numerical JSON
    return '{' + json + '}';//Return associative JSON
}

/**



 */
 
	//INPUTS 
	$("input.text").focus(function () {
		$(this).addClass('focus');  	
	});
	$("input.text").blur(function () {
		$(this).removeClass('focus');  	
	});
	$("textarea.text").focus(function () {
		$(this).addClass('focus');  	
	});
	$("textarea.text").blur(function () {
		$(this).removeClass('focus');  	
	});
	$("input.text_small").focus(function () {
		$(this).addClass('focus');  	
	});
	$("input.text_small").blur(function () {
		$(this).removeClass('focus');  	
	});
	
	//SUBMIT		
	$('.onChangeSubmit').change(function () {
		$(this).parents("form:first").submit(); 
	});
	
	//CLEAR
	var old;
	$('.clear').click(function(){ 
		old = this.value;
		if( $(this).attr('title') != '1')
			this.value = '';
	}).blur(function () {
		if(this.value == '')
			this.value = old;
		else
			$(this).attr('title', '1');	
	});
	
	//PROPAGE CENTER
	$('.jCenter').each(function() {
		$(this).css({display:'none', position:'absolute'});
		$(this).parent().css({position:'relative'});
		var thisWidth  = parseInt($(this).attr('width'));
		var thisHeight = parseInt($(this).attr('height'));
		$(this).css({left: '50%', marginLeft: -thisWidth/2});
		$(this).css({top: '50%',  marginTop : -thisHeight/2}); 
		$(this).fadeIn(2000);
	}); 
		
	//ACTIVE 
	$(".listing li").hover(
      function () {
			$(this).addClass('active');
      }, function () {
	  		$(this).removeClass("active");
      }
    );
	
	$('.activetd').click(function() {
		$(this).parents('table').find('td').removeClass('active');
		$(this).parent().addClass('active');	
	});
	
	$('.activli').click(function() {
		$(this).parents('ul').find('li').removeClass('active');
		$(this).parents('li').addClass('active');	
	});
	
	//TRACE
 	$('.trace').click(function() {
	$.ajax({
		type: "GET",
		dataType: "html",
		url: jQuery.root+"/ajax/trace.php",
		data: { name: 'Kliknięcie', desc: $(this).attr("title") }
	});});
	
	$('a[target="_blank"]').click(function() {
	$.ajax({
        type: "GET",
		dataType: "html",
        url: jQuery.root+"/ajax/trace.php",
		data: { name: 'Zewnętrzny link' , desc: $(this).attr("href") }
    });});
	
	//AJAX
	$(".ajax_submit").live("click", function(){
		var href = $(this).attr('href');
		var form = $(this).parents('form:first');
		Boxy.load(form.attr('action'), {title: $(this).attr('title'), closeable: true, dataType: 'json', type: 'GET'},  form.serialize() , false);
		return false;
	});
	 
	$(".ajax_get").live("click", function(){
		var href = $(this).attr('href'); 
			href = href.split("?");
	 	
		if(href[0].lastIndexOf(jQuery.root)==0)
		{
			href[0] = href[0].substring(jQuery.root.length+1,href[0].length);	
		}
		var jgn = false; 	
		if($(this).hasClass('jgn')) 
			jgn = true; 
			
		Boxy.load(href[0], {title: $(this).attr('title'), dataType: 'json', type:'GET', jgn:jgn}, href[1]);
		return false;
	});

	
	$(".ajax_get_html").live("click", function(){
		var href = $(this).attr('href');
			href = href.split("?");
		Boxy.load(href[0], {title: $(this).attr('title'), dataType: 'html', type:'GET'}, href[1]);
		return false;
	});
	

