$(function(){
    $('#menu a img').hover(
		function() {
		src = $(this).attr('src');
        src = src.replace('_n.png', '_a.png');
        $(this).attr('src', src);
	},
	function() {
        if(!$(this).hasClass('current')){
            src = $(this).attr('src');
            src = src.replace('_a.png', '_n.png');
            $(this).attr('src', src);
        }
	}
	);
        
    $('#kat_thumbs img').hover(
		function() {
		clas = $(this).attr('class');
		src = $(this).attr('src');
        src = src.replace('_n.png', '_a.png');
        $(this).attr('src', src);
        
        src = $('#submenu img.'+clas).attr('src');
        src = src.replace('_n.png', '_a.png');
        $('#submenu img.'+clas).attr('src', src);
	},
	function() {
        clas = $(this).attr('class');
        src = $(this).attr('src');
        src = src.replace('_a.png', '_n.png');
        $(this).attr('src', src);
        
        src = $('#submenu img.'+clas).attr('src');
        src = src.replace('_a.png', '_n.png');
        $('#submenu img.'+clas).attr('src', src);
	}
	);
    
    $('#submenu img').hover(
		function() {
		clas = $(this).attr('class');
		src = $(this).attr('src');
        src = src.replace('_n.png', '_a.png');
        $(this).attr('src', src);
        
        src = $('#kat_thumbs img.'+clas).attr('src');
        src = src.replace('_n.png', '_a.png');
        $('#kat_thumbs img.'+clas).attr('src', src);
	},
	function() {
        clas = $(this).attr('class');
        src = $(this).attr('src');
        src = src.replace('_a.png', '_n.png');
        $(this).attr('src', src);
        
        src = $('#kat_thumbs img.'+clas).attr('src');
        src = src.replace('_a.png', '_n.png');
        $('#kat_thumbs img.'+clas).attr('src', src);
	}
	);
});

function paGalleryMakeFLink(value){
	
	value = value.replace(/[!@#$^()\[\]{}:?*"'<>&|^%=~`;]/g, '');
	value = value.replace(/ /g, '_');
	value = value.replace(/\./g, ',');
	value = value.replace(/\\/g, '');
	value = value.replace(/\//g, '');
	value = value.toLowerCase();
	value = escape(value);
	value = value.replace(/%u0119/g, 'e');
	value = value.replace(/%F3/g, 'o');
	value = value.replace(/%u0105/g, 'a');
	value = value.replace(/%u015B/g, 's');
	value = value.replace(/%u0142/g, 'l');
	value = value.replace(/%u017C/g, 'z');
	value = value.replace(/%u017A/g, 'z');
	value = value.replace(/%u0107/g, 'c');
	value = value.replace(/%u0144/g, 'n');
	value = value.replace(/%2C/g, ',');
	
	link = 'gallery/'+value+'.html';
	$('#admin-gallery-cats-editor').find('input[name="link"]').attr('value', link);
}

function paGalleryCatsCheckEditor(form){
	
	var send = true;
	var error = "";
	var value = $('#admin-gallery-cats-editor input[name="name"]').attr('value');
	
	if(value == "" || value == null){
		send = false;
		error += $('#admin-gallery-errors-editor-name-null').html();
	}

	if(send)form.action = 'index.php?m=gallery';
	else $('#admin-gallery-cats-editor-info').html(parseHTMLTemp("admin-gallery-error-temp", new Array("errorTitle", "errorText"), new Array($('#admin-gallery-errors-title').html(), error))).show('slide');
}


var xmlHttpPAWCSI = null;
function paGalleryCatsShowInfo(id){
	$('#admin-gallery-cats-load').show();
	$('#admin-gallery-cats-info').hide('slide');
	
	if(window.XMLHttpRequest)
		xmlHttpPAWCSI = new XMLHttpRequest();
	else
		if(window.ActiveXObject)
			xmlHttpPAWCSI = new ActiveXObject("Microsoft.XMLHTTP");

	if (xmlHttpPAWCSI == null){
		alert("Error: HTTP Request");
		return false;
	}

	var url = "../ajax.php";
	var params = "m=gallery&f=aGetCatsInfo&id=" + id + "&sid=" + Math.random()

	xmlHttpPAWCSI.onreadystatechange = callbackpaGalleryCatsShowInfo;
	xmlHttpPAWCSI.open("POST", url, true);
	xmlHttpPAWCSI.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttpPAWCSI.setRequestHeader("Content-length", params.length);
	xmlHttpPAWCSI.setRequestHeader("Connection", "close");
	xmlHttpPAWCSI.send(params);
}
function callbackpaGalleryCatsShowInfo(){
	if (xmlHttpPAWCSI.readyState == 4 && xmlHttpPAWCSI.status == 200){
		eval("var dane = ("+xmlHttpPAWCSI.responseText+")");
		
		$('#admin-gallery-cats-load').hide();
		
		if(!dane.errors){
			if(dane.sbox){
				$('#admin-gallery-cats-info').html(parseHTMLTemp("admin-gallery-correct-temp", new Array("cTitle", "cText"), dane.replacement)).show('slide');
				setTimeout(function(){$('#info').hide('slide').html('');}, 10000);
			}
			
			$('#admin-gallery-cats-info-tr').css('display', 'table-row')
				.find('table')
				.find('.name').html(dane.info.name).end()
				.find('.link').html(dane.info.link).end()
				.find('.imagess').html(dane.info.images).end();
				//.find('.images').html(dane.info.images).css('width', dane.info.iwidth+'px').end();
                //.find('.scroll-pane').css('height', (parseInt(dane.info.iheight)+30)+'px');
			
			$('#admin-gallery-cats-info-id').attr('value', dane.info.id);
			$('#admin-gallery-cats-info-to-id').attr('value', dane.info.to_id);
            
            $( "#imagess" ).sortable({
                placeholder: "ui-state-highlight-il",
                start: function(event, ui){
                    $(".ui-state-highlight-il")
                    .css('width', dane.info.iheight+'px')
                    .css('height', dane.info.iheight+'px');
                },
                update: function(event, ui){                    
                    var order = $(this).sortable('toArray').toString();
                    var xmlHttpPAGUO = null;
                    
                    if(window.XMLHttpRequest)
                        xmlHttpPAGUO = new XMLHttpRequest();
                    else
                        if(window.ActiveXObject)
                            xmlHttpPAGUO = new ActiveXObject("Microsoft.XMLHTTP");

                    if (xmlHttpPAGUO == null){
                        alert("Error: HTTP Request");
                        return false;
                    }

                    var url = "../ajax.php";
                    var params = "m=gallery&f=aUpdateOrderInCat&order=" + order + "&sid=" + Math.random()

                    xmlHttpPAGUO.open("POST", url, true);
                    xmlHttpPAGUO.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
                    xmlHttpPAGUO.setRequestHeader("Content-length", params.length);
                    xmlHttpPAGUO.setRequestHeader("Connection", "close");
                    xmlHttpPAGUO.send(params);
                }
            });
            $( "#imagess" ).disableSelection();
            
            $('.images a.lightbox').lightBox({
                imageLoading: '../images/lightbox-ico-loading.gif',
                imageBtnClose: '../images/lightbox-btn-close.gif',
                imageBtnPrev: '../images/lightbox-btn-prev.gif',
                imageBtnNext: '../images/lightbox-btn-next.gif',
                txtImage: 'Zdjęcie',
                txtOf: 'z'
            });
            
            if(dane.info.subcats){
                $('#admin-gallery-subcats-tr').find('td').html(dane.info.subcats).end().css('display', 'table-row');
                
                $( "#sortable" ).sortable({
                        stop: function(event, ui){
                            paGalleryUpdateSubcatsOrder($( "#sortable" ).sortable('toArray'));
                        }
                    });
                $( "#sortable" ).disableSelection();
            }
            else
                $('#admin-gallery-subcats-tr').find('td').html('').end().css('display', 'none');
		}
		else
			$('#admin-gallery-cats-info').html(parseHTMLTemp("admin-gallery-error-temp", new Array("errorTitle", "errorText"), dane.replacement)).show('slide');
	}
}

function paGalleryEditCat(){
	
	$('#admin-gallery-cats-id').attr('value', $('#admin-gallery-cats-info-id').attr('value'));
	$('#admin-gallery-cats-editor')
		.find('input[name="name"]').attr('value', $('#admin-gallery-cats-info-tr').find('table').find('.name').text()).end()
		.find('input[name="link"]').attr('value', $('#admin-gallery-cats-info-tr').find('table').find('.link').text()).end()
        .find('select option[value="'+$('#admin-gallery-cats-info-to-id').attr('value')+'"]').attr('selected', true).end();
	
	$('#admin-gallery-cats-editor-form').show();
}

function paGalleryDeleteCat(){
	var text = $('#admin-gallery-dialog-content-delete-cat').html().split('%content');
	
	$('#admin-gallery-dialog-content-delete-cat input[name="id"]').attr('value', $('#admin-gallery-cats-info-id').attr('value'));
	
	var title = text[0];
	var content = text[1];
	
	$('#admin-gallery-dialog').html(content);
	$('#admin-gallery-dialog').dialog("option", "title", title);
	$('#admin-gallery-dialog').dialog("option", "buttons", { 
		'Tak': function(){
			$('#admin-gallery-dialog-content-delete-cat-form').attr('action', 'index.php?m=gallery').submit();
		},
		'Nie': function(){ 
			$(this).dialog("close");
		}
	});
	$('#admin-gallery-dialog').dialog('open');
}

function paGalleryUploadEnd(file, error, etext, cdir, width, height){
	$('#img-editor-load').hide();

    if(!error){
        $('#img-editor').css('height', height+'px').css('width', width+'px')
                    .find('img').attr('src', cdir+'images/gallery/temp/'+file).css('height', height+'px').css('width', width+'px').end().show();
        $('#gallery-tabs-3').find('input[name="photo"]').val(file);
	}
	else
		$('#admin-gallery-editor-info').html(parseHTMLTemp("admin-gallery-error-temp", new Array("errorTitle", "errorText"), new Array($('#admin-gallery-errors-title').html(), etext))).show('slide');
}

function galleryPALoadImageFromTemp(data){
    data = data.split('|');
    
    $('#img-editor').css('height', data[2]+'px').css('width', data[1]+'px')
                    .find('img').attr('src', data[3]+'images/gallery/temp/'+data[0]).css('height', data[2]+'px').css('width', data[1]+'px').end().show();
    $('#gallery-tabs-3').find('input[name="photo"]').val(data[0]);
}

function paGalleryCheckEditor(){

	var form = document.getElementById('admin-gallery-editor-form');
	var send = true;
	var error = "";
	
    if(!$('#admin-gallery-editor-form input[name="photo"]').val()){
        send = false;
        error += $('#admin-gallery-errors-editor-image-null').html();
    }
	
	if(send){
		form.action = 'index.php?m=gallery';
		form.send();
	}
	else $('#admin-gallery-editor-info').html(parseHTMLTemp("admin-gallery-error-temp", new Array("errorTitle", "errorText"), new Array($('#admin-gallery-errors-title').html(), error))).show('slide');
}

function paGalleryDelete(id){
	var text = $('#admin-gallery-dialog-content-delete-work').html().split('%content');
	
	$('#admin-gallery-dialog-content-delete-work input[name="id"]').val(id);
	
	var title = text[0];
	var content = text[1];
	
    //bo niestety ff nie potrafi dobrze obsłużyć tego co 2 linie wyżej
    content = content.replace('%id', id);
    
	$('#admin-gallery-dialog').html(content);
	$('#admin-gallery-dialog').dialog("option", "title", title);
	$('#admin-gallery-dialog').dialog("option", "buttons", { 
		'Tak': function(){
			$('#admin-gallery-dialog-content-delete-work-form').attr('action', 'index.php?m=gallery').submit();
		},
		'Nie': function(){ 
			$(this).dialog("close");
		}
	});
	$('#admin-gallery-dialog').dialog('open');
}

var xmlHttpPAWCE = null;
function paGalleryCancelEditor(form){
	form.reset(); 
	$('#admin-gallery-editor-info').hide();  
	$('#admin-gallery-image-form input[type=file]').attr('value', ''); 
	$('#img-editor').hide();
	
	if(window.XMLHttpRequest)
		xmlHttpPAWCE = new XMLHttpRequest();
	else
		if(window.ActiveXObject)
			xmlHttpPAWCE = new ActiveXObject("Microsoft.XMLHTTP");

	if (xmlHttpPAWCE == null){
		alert("Error: HTTP Request");
		return false;
	}
    
    var file = $('#gallery-tabs-3').find('input[name="photo"]').val();
    
	var url = "../ajax.php";
	var params = "m=gallery&f=aClearTemp&file=" + file + "&sid=" + Math.random()
	
	xmlHttpPAWCE.onreadystatechange = function(){}
	xmlHttpPAWCE.open("POST", url, true);
	xmlHttpPAWCE.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttpPAWCE.setRequestHeader("Content-length", params.length);
	xmlHttpPAWCE.setRequestHeader("Connection", "close");
	xmlHttpPAWCE.send(params);
}

var xmlHttpPAGUSO = null;
function paGalleryUpdateSubcatsOrder(order){
	$('#admin-gallery-cats-order-save').show();
	
	if(window.XMLHttpRequest)
		xmlHttpPAGUSO = new XMLHttpRequest();
	else
		if(window.ActiveXObject)
			xmlHttpPAGUSO = new ActiveXObject("Microsoft.XMLHTTP");

	if (xmlHttpPAGUSO == null){
		alert("Error: HTTP Request");
		return false;
	}
    
    var file = $('#gallery-tabs-3').find('input[name="photo"]').val();
    
	var url = "../ajax.php";
	var params = "m=gallery&f=aSaveSubCatsOrder&order=" + order + "&sid=" + Math.random()
	
	xmlHttpPAGUSO.onreadystatechange = function(){
        if (xmlHttpPAGUSO.readyState == 4 && xmlHttpPAGUSO.status == 200){
            $('#admin-gallery-cats-order-save').hide();
        }
    }
	xmlHttpPAGUSO.open("POST", url, true);
	xmlHttpPAGUSO.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttpPAGUSO.setRequestHeader("Content-length", params.length);
	xmlHttpPAGUSO.setRequestHeader("Connection", "close");
	xmlHttpPAGUSO.send(params);
}

function paGalleryEdit(cdir){
	
	//przerzucenie danych
	$('#admin-gallery-editor-form')
	.find('input[name="edit"]').attr('value', $('#admin-gallery-info-id').attr('value')).end()
	.find('input[name="name"]').attr('value', $('#admin-gallery-info-tr .name').text()).end()
	.find('select[name="category"]').attr('value', $('#admin-gallery-info-tr .category').text()).end()
	.find('input[name="type"]').attr('value', $('#admin-gallery-info-tr .type').text()).end()
	.find('textarea[name="describe"]').attr('value', $('#admin-gallery-info-tr .describe').html()).end();
	
	//wstawienie obrazków
	var images = $('#admin-gallery-info-tr .images').html().split('|');
	var mimage = $('#admin-gallery-info-tr .mimage').html().split('|');
		
	$('#admin-gallery-up-images-show').html('');
	
	for(i = 0; i < images.length; i++){
		if(images[i])
			$('#admin-gallery-up-images-show').html($('#admin-gallery-up-images-show').html()+'<p style="text-align: center; float: left; margin: 0 10px 10px 0;"><label><img src="'+cdir+'images/gallery/small/'+images[i]+'" alt="" style="height: 150px; width: 200px;" /><br /><input type="checkbox" name="images[]" value="'+images[i]+'" checked="checked" /> Załącz</label></p>');
	}
	
	if(!images.length)
		$('#admin-gallery-up-images-show').html($('#admin-gallery-editor-images').html());
	
	//wstawianie obrazka głównego
	$('#admin-gallery-up-mimage-show').html('<p style="text-align: center; float: left; margin: 0 10px 10px 0;"><label><img src="'+cdir+'images/gallery/small/'+mimage+'" alt="" style="height: 150px; width: 200px;" /><input type="hidden" name="mimage" value="'+mimage+'"/></p>');
	
	$('#admin-gallery-editor-form-div').show(); 
}

