function assignPhoto(response){
    $("#images").after("<input type='hidden' value='" + response + "' name='images[]' />");
	
	if (!$("#assigned")[0]) {
	    $("#photo-label").before('<dt id="assigned-label"><label>Zdjęcie wgrane na serwer</label></dt><dd id="assigned"><img src="/public/content-images/S/' + response + '" /> </dd><p><small>Aby dodać opisy wpierw zapisz zmiany.</small></p>');
	} else {
	    $("#assigned").append("<img src='/public/content-images/S/" + response + "' /> ");
	}
}

$(document).ready(function() {
    $("#photos-element .photo img").tooltip({
        opacity: '0.8'
    });
    $("#photo-elements").append('<input type="button" class="button" id="assign-images" value="Przypisz pliki" rel="#overlay" />');
    
    if ($('#assign-images')[0]) {
        $("body").append('<div class="overlay" id="overlay"><div class="contentWrap"></div></div>');
    }
    
    $("input[rel]").overlay({ 
 
        expose: {
            color: '#fff', 
            loadSpeed: 200, 
            opacity: 0.9 
        },
        
        onBeforeLoad: function() { 
 
            // grab wrapper element inside content 
            var wrap = this.getContent().find(".contentWrap"); 
 
            // load the page specified in the trigger 
            wrap.load('/admin/photos-assign'); 
        },
        
        onLoad: function() {
            var wrap = this.getContent().find(".contentWrap");
            
            wrap.find('li img').click(function(){
                var response = $(this).attr('src').replace('/public/content-images/S/', '');
                assignPhoto(response);
                $(this).parent('li').fadeOut('slow');
            });
        }
 
    });
    
    // confirm-delete
    var trigger;
    var triggers = $("a.confirm-delete").overlay({ 
     
        // some expose tweaks suitable for modal dialogs 
        expose: { 
            color: '#fff', 
            loadSpeed: 200, 
            opacity: 0.9 
        }, 
        
        onLoad: function(){
            trigger = this.getTrigger();
        },
        closeOnClick: false 
    });
    
    var buttons = $("#yesno button").click(function(e) { 
        // get user input 
        var yes = buttons.index(this) === 0; 
     
        // do something with the answer 
        if (yes) {
            window.location = trigger.attr("href"); 
        }
    });
    
    
    $("#photo-elements input[type=file]").uploadify({
		'uploader': '/public/js/uploadify/uploadify.swf',
		'cancelImg': '/public/js/uploadify/cancel.png',
		'script': '/polygon',
		'folder': '/tmp',
		'auto': true,
		'displayData': 'percentage',
		'fileExt': '*.jpg;*. gif;*.png;*.jpeg',
		'fileDesc': 'Pliki graficzne',
		'multi': true,
		'buttonImg': "/public/images/wgrajPliki.png",
		'width': 102,
		'height': 25,
		'wmode' : 'transparent',
		'rollover': true,
		//'buttonText': 'Przegladaj...',
		onComplete: function (evt, queueID, fileObj, response, data) {
			assignPhoto(response);
		},
		onError: function(evt, queueID, fileObj, errorObj){
		    console.log("fileObj: " +fileObj.name);
		    console.log("error: " +errorObj.type + " -> " + errorObj.info);
		}
		
	});
	
	$("ul.tabs").tabs("div.panes > div").history(); 
});
