$(document).ready(function(){
$("#form_builder_form_change_knowledge").knowledge_select();
$("#form_builder_form_change_categories").categories_select();
$(".art_open div.text , .art_close div.text , #banners").each(function(){$(this).arrange();});
});

$.fn.arrange = function()
{
	$(this).attr('style','');
	var outerHeight = $(this).outerHeight();
	var height = $(this).height();
	var difference = (( outerHeight -1  )%14);
	var correction = 14 - difference;
	var padding = parseInt($(this).css('padding-bottom'));
	//alert(outerHeight + " > " + height + " :: " + correction + " ::: " + difference + "?" + padding);
	if( difference != 0 )
	{
		//$(this).height(height + correction);
		if((padding - difference) > 7)
			$(this).css('padding-bottom',parseInt($(this).css('padding-bottom'))-difference+"px");
		else
			$(this).css('padding-bottom',parseInt($(this).css('padding-bottom'))+correction+"px");
	}
}

function ajaxFileUpload(obj)
{

$("#loading")
		.ajaxStart(function(){
			$(this).show();
		})
		.ajaxComplete(function(){
			$(this).hide();
		});
		
var wrapperDiv = $("#changes_module #attached_files");
		
$.ajaxFileUpload(
	{
	url:"ajax.php?method=attached_files",
	//url:"ajaxUpload.php",
	secureuri:false,
	fileElementId:$(obj).attr('id'),
	dataType: 'html',
	success: function (data, status)
	{
		wrapperDiv.html(data);
	},
	error: function (data, status, e)
	{
		alert("Error: " + e);
	}
	}
)
}

function ajaxSubmit(obj) {
var params = {};
var form = $("#" + $(obj).attr('name'));
var i = 0;
form
.find("input[@checked], input[@type='text'], input[@type='hidden'], input[@type='password'], input[@type='submit'], option[@selected], textarea")
.filter(":enabled")
.each(function() {
	params[ this.name || this.id || this.parentNode.name+(i++) || this.parentNode.id+(i++) ] = this.value;
	});
$.ajax({
  	type: "POST",
  	url: "ajax.php?method=" + $(obj).attr('id'),
  	data: params,
  	beforeSend: function(msg){
  		form.parent().fadeOut('slow');
  		},
  	success: function(msg){
  		 
  		form.parent().fadeIn('slow'); 
    	form.parent().html(msg);
    	$("#form_builder_form_change_knowledge").knowledge_select();
  	}
});
}

jQuery.fn.knowledge_select = function()
{
	var form = $(this);
	$(this).find('tr').each(function()
	{
		var default_select = $(this); 
		var index = 1;
		var used = new Array();

		/*default_select.bind('change',function(){
		//test = test.substring(11,test.length);		//alert(test.substring(0,test.indexOf('_')));		//alert(test.substring(test.indexOf('_')+1));
		//alert($('option:selected()',this).val());
		$('option:selected()',this).attr('disabled','disabled');
		relock();
		});*/
		if($(this).attr('class') == "default")
			default_select.render(index);
	}
	);
}

jQuery.fn.categories_select = function()
{
	var form = $(this);
	var first_select = $(this).find('select#id_kategorie');
	var second_select = $(this).find('select#id_kategorie2');
	var option;
	first_select.find('option').bind('dblclick',
	function(){
		if($(this).parent().attr('id') == 'id_kategorie')
		{
			$(this).appendTo(second_select);
			first_select.remove($(this));
		}
		else
		{
			$(this).appendTo(first_select);
			second_select.remove($(this));
		}
	});
	second_select.find('option').bind('dblclick',
	function(){
		if($(this).parent().attr('id') == 'id_kategorie')
		{
			$(this).appendTo(second_select);
			first_select.remove($(this));
		}
		else
		{
			$(this).appendTo(first_select);
			second_select.remove($(this));
		}
	});
}

function relock()
{
	var selected = new Array();
	$("#form_builder_form_change_knowledge").find('select').filter('.selection_znalost').each(function()
	{
		selected[$('option:selected()',this).val()] = '1';
	});
	$("#form_builder_form_change_knowledge").find('select').filter('.selection_znalost').find('option').each(function()
	{
		if(selected[$(this).val()] != 1)
			$(this).removeAttr('disabled');			
	});
	
	
}

jQuery.fn.render = function(index)
{
var render_new = true;
var parent = $(this);
$(this).find('select').one('change',function(){
	render_new = true
	/*if($(this).attr('class') == 'selection_znalost')
		$('option:selected()',this)
		.attr('disabled','disabled');*/
	parent.find('select').each(function()
	{
		if(this.selectedIndex == 0)
			render_new = false;
	});
	if(render_new)
	{
		parent.select_render(index);
	}				
});
}

jQuery.fn.select_render = function(index)
{
	var new_tr = document.createElement('tr');
	var parent = $(this);
	$(new_tr).addClass('default');
	$(new_tr).html($(this).html());
	$(new_tr).find('select').each(function()
	{
		$(this).attr('id',$(this).attr('id').replace(/(NEW([0-9]*)?)/, "NEW"+(index)))
		.attr('name',$(this).attr('name').replace(/(NEW([0-9]*)?)/, "NEW"+(index)));
		$(this).find('option:first()').attr('selected','selected');
	});
	$(new_tr).render(index+1);
	parent.removeClass('default');
	parent.after($(new_tr));
	relock();
}

/*jQuery.fn.convert_selects = function()
{
    $(this).find('select').each(function()
    {
        var date = new Date();
        
        // Create a list wrapper
        var wrapper = document.createElement('div');
        wrapper.id = this.name + '_wrapper_' + date.getTime();
        $(wrapper).addClass('select wrapper ' + this.name);
        this.parentNode.insertBefore(wrapper, this);
        
        // Store the information for sending the form
        $('<input type="hidden" />')
            .attr('name', this.name)
            .attr('value', this.value)
            .appendTo(wrapper);
        
        // Title to display the selected item
        $('<div></div>')
            .addClass('title')
            .attr('id', this.name + '_title')
            .click(function()
            {
                if ($(this.parentNode).find('ul.selection').css('display') == 'block')
                {
                    $(this.parentNode).find('ul.selection').hide();
                }
                else
                {
                    $(this.parentNode).find('ul.selection').show();
                }
            })
            .appendTo(wrapper);
        
        // Extra wrapper that can be styled
        $('<span></span>')
            .addClass('title')
            .html($(this).find('option:selected(0)').html())
            .appendTo($('#' + wrapper.id + ' div.title'));
        
        // List for wrapping the emulated selection list
        $('<ul></ul>')
            .addClass('selection')
            .css(
            {
                display: 'none',
                position: 'absolute',
                listStyle: 'none',
                zIndex: '1000',
                margin: '0 -1px',
                padding: '1px',
            })
            .appendTo(wrapper);
        
        // Loop through each option in the original select
        $(this).find('option').each(function(i)
        {
            // Create a new list element for each option
            $('<li></li>')
                .html(this.innerHTML)
                .attr('title', this.value)
                .css(
                {
                    display: 'block',
                    padding: '1px 3px'
                })
                .mouseover(function()
                {
                    $(this).addClass('hover');
                })
                .mouseout(function()
                {
                    $(this).removeClass('hover');
                })
                .click(function()
                {
                    // When the list item has been clicked, update the hidden input field
                    $(this.parentNode.parentNode).find("input[@type='hidden']").attr('value', $(this).attr('title'));
                    
                    // Remove select classnames from the selection list
                    $(this.parentNode).find('li').removeClass('selected');
                    
                    // Add the 'selected' class to currently selected list item
                    $(this).addClass('selected');
                    
                    // Change the visible title
                    $(this.parentNode.parentNode).find('div.title span').html(this.innerHTML);
                    $(this.parentNode).hide();
                })
                .appendTo('#' + wrapper.id + ' ul.selection');
            
            // Set the selected class to the selected list item
            if (this.value == this.parentNode.value)
            {
                $(wrapper).find('li.' + this.value).addClass('selected');
            }
        });
        
        // Remove the original select list
        this.parentNode.removeChild(this);
    });
}*/