
(function($){
	$.resize = function(){
		$("#main").height('auto');
		if($("#strona").height() < $(window).height()){
		var reszta = $("#strona").height() - $("#main").height();
		var height = ($(window).height() - reszta);
		$("#main").height(height);
		}
	};
	
	$.encode_dialog = function(name, id_photo){
		var dest_field = $('<input style="width:90%;" type="text" readonly="readonly"/>');
		var input_field = $('<input type="text"/>');
		
		
		$(input_field).keyup(function(){
				var tekst = http_build_query({
					id: id_photo,
					tekst: $(this).val()
				}, '', '&');
				$(dest_field).val('{'+name+' '+tekst+'/'+name+'}');
			}
		);
		
		var container = $('<div></div>');
		$(container).append('Tekst:<br/>').append(input_field).append('<br/>Wynik:<br/>').append(dest_field);
		
		$(container).dialog();
		$(input_field).focus();
	}
	
	
})(jQuery);


function http_build_query( formdata, numeric_prefix, arg_separator ) {
    // Generate URL-encoded query string
    // 
    // +    discuss at: http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_http_build_query/
    // +       version: 809.2411
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Legaev Andrey
    // +   improved by: Michael White (http://getsprink.com)
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // -    depends on: urlencode
    // *     example 1: http_build_query({foo: 'bar', php: 'hypertext processor', baz: 'boom', cow: 'milk'}, '', '&amp;');
    // *     returns 1: 'foo=bar&amp;php=hypertext+processor&amp;baz=boom&amp;cow=milk'
    // *     example 2: http_build_query({'php': 'hypertext processor', 0: 'foo', 1: 'bar', 2: 'baz', 3: 'boom', 'cow': 'milk'}, 'myvar_');
    // *     returns 2: 'php=hypertext+processor&myvar_0=foo&myvar_1=bar&myvar_2=baz&myvar_3=boom&cow=milk'

    var key, use_val, use_key, i = 0, j=0, tmp_arr = [];

    if (!arg_separator) {
        arg_separator = '&';
    }

    for (key in formdata) {
        use_val = urlencode(formdata[key].toString());
        use_key = urlencode(key);

        if (numeric_prefix && !isNaN(key)) {
            use_key = numeric_prefix + j;
            j++;
        }
        tmp_arr[i++] = use_key + '=' + use_val;
    }

    return tmp_arr.join(arg_separator);
}// }}}

function urlencode( str ) {
    // URL-encodes string
    // 
    // +    discuss at: http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_urlencode/
    // +       version: 809.1713
    // +   original by: Philip Peterson
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: AJ
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // %          note: info on what encoding functions to use from: http://xkr.us/articles/javascript/encode-compare/
    // *     example 1: urlencode('Kevin van Zonneveld!');
    // *     returns 1: 'Kevin+van+Zonneveld%21'
    // *     example 2: urlencode('http://kevin.vanzonneveld.net/');
    // *     returns 2: 'http%3A%2F%2Fkevin.vanzonneveld.net%2F'
    // *     example 3: urlencode('http://www.google.nl/search?q=php.js&ie=utf-8&oe=utf-8&aq=t&rls=com.ubuntu:en-US:unofficial&client=firefox-a');
    // *     returns 3: 'http%3A%2F%2Fwww.google.nl%2Fsearch%3Fq%3Dphp.js%26ie%3Dutf-8%26oe%3Dutf-8%26aq%3Dt%26rls%3Dcom.ubuntu%3Aen-US%3Aunofficial%26client%3Dfirefox-a'
                                     
    var histogram = {}, histogram_r = {}, code = 0, tmp_arr = [];
    var ret = str.toString();
    
    var replacer = function(search, replace, str) {
        var tmp_arr = [];
        tmp_arr = str.split(search);
        return tmp_arr.join(replace);
    };
    
    // The histogram is identical to the one in urldecode.
    histogram['!']   = '%21';
    histogram['%20'] = '+';
    
    // Begin with encodeURIComponent, which most resembles PHP's encoding functions
    ret = encodeURIComponent(ret);
    
    for (search in histogram) {
        replace = histogram[search];
        ret = replacer(search, replace, ret) // Custom replace. No regexing
    }
    
    // Uppercase for full PHP compatibility
    return ret.replace(/(\%([a-z0-9]{2}))/g, function(full, m1, m2) {
        return "%"+m2.toUpperCase();
    });
    
    return ret;
}// }}}





$(document).ready(function() {

	$.resize();

	
    $('a.lightbox').lightBox({navigate: true});
	
	$('a.lightbox_single').lightBox({navigate: false});
	
	$('.admin_photos a.lightbox').unbind("click").click(function(){
		var id = $(this).attr('photo_id');
		$.encode_dialog('zdjecie', id);
		return false;
	});
	
	$('.admin_documents .doctitle').unbind("click").click(function(){
		var id = $(this).attr('doc_id');
		$.encode_dialog('dokument', id);
		return false;
	});
	
	$('.imgtooltip').tooltip({ 
	    delay: 0, 
	    showURL: false, 
		track: true, 
	    bodyHandler: function() { 
			var elem = $('<img/>');
			elem.attr('src', $(this).attr('href').replace('.jpg', '_t.jpg'));
	        return elem; 
	        return '<h1>' +  $(this).attr('href') + '</h1>'; 
	    } 
	});
	
	$('#menu ul ul li').css('opacity', 0.9);
	
	$('#menu ul').superfish();
});


