$(document).ready(function()
		{
			var myElement = $('.vordle_koole_holder');
			myElement.css({
			    position: 'absolute',
			    left: '50%',
			    'margin-left': 0 - (myElement.width() / 2)
			});
			
			$('#vordle_koole').click(function()
					{
						$('.vordle_koole_overlay').show();
						$('.vordle_koole_holder').show();
						$('.vordle_koole_overlay').width($(document).width());
						$('.vordle_koole_overlay').height($(document).height());
						$.post('ajax/vordle_koole.php',{ view : 1});
					});
			var i =1;
			$('.vordle_koole_list').find('tr').each(function()
					{
						if ( i == 1) 
						{
						
						}else {	
							$(this).attr('class','row_bg');
							i=0;
						}
						++i;
					});
			
			//search box 
			$('select[name=kate]').change(function(){							  
					if ( $(this).val() != 0 )
					{
						$('select[name=kool]').append('<option value="laen">Laen ...</option>').
						val('laen');
						$.post('ajax/vordle_koole.php',{ kate: $(this).val() },
						function(data){
								$('select[name=kool]').html('');
								$('select[name=kool]').append('<option value="0">Vali kool</option>').
								val('0');
								$('select[name=kool]').attr('disabled',false);
								$('select[name=kool]').val('0');
								$('select[name=kool]').append(data);
						});
					}
			 });
				$('select[name=kool]').change(function(){							  
					if ( $(this).val() != 0 )
					{
						$('input[name=lisa]').attr('disabled',false);	
					}
			 });
				$('input[name=lisa]').click(function()
						{
							if ( cur_items != 3 &&
								$('.shool_'+$('select[name=kool]').val()).html() == null) {
								$('.vordle_koole_list').append('<span id="loadingshool"><strong>Laen kooli......</strong></span>');
								$.post('ajax/vordle_koole.php',
								   { kool_id: $('select[name=kool]').val() },
										function(data){
											
											do_table($('select[name=kool]').val(),data);
										});
							} else {
								alert('Valitud kool on juba lisatud /  maksimum arv koole on juba lisatud');	
							}
							
						});
				
				$('.logos').live('click',function(){
					if ( cur_items != 3 &&
							$('.shool_'+ $(this).attr('rel') ).html() == null) {
							$('.vordle_koole_list').append('<span id="loadingshool"><strong>Laen kooli......</strong></span>');
							$.post('ajax/vordle_koole.php',
							   { kool_id: $(this).attr('rel') },
									function(data){
										
										do_table($(this).attr('rel') ,data);
									});
						} else {
							alert('Valitud kool on juba lisatud /  maksimum arv koole on juba lisatud');	
						}
						
				});
				//remove shool
				$('.remove_kool').live('click',function()
					{
						$('.shool_'+this.id).remove();
						  cur_items =  cur_items - 1;
					});
				
				$('#close_btn').live('click',function()
						{
							$('.vordle_koole_overlay').hide();
							$('.vordle_koole_holder').hide();						
						});
								
		});

	function do_table(id,data)
	{
		s = data.split('|');
					s_in = count(s)-1;
					var output = '<li class="shool_'+id+'">'+
						'<table>';
					var n = 1;
						for (var i=0; i<=s_in; i++)
						{
							if ( i == 0 )
							{
								output += '<tr>'+
											'<th ><span class="left">'+s[i]+'</span><span class="remove_kool" id="'+id+'">X</span></th>'+
										  '</tr>';
							} else {
								if ( n==1) 
								{
									bg = 'class="row_bg"';
								}else {
									bg = '';
								}
								//check if there is mathing values
								bold = "";
								$('.vordle_koole_list li').each(function()
										{
											$(this).find('td').each(function(){
												//$('#var_dump').append($(this).val());
													if ( $(this).text() == s[i] )
													{
														bold = 'class="bold"';
														$(this).attr('class','bold');
													}
												});
										});
								i_empty = s[i].length== 0 ? '-' : s[i];
								output += '<tr '+bg+'>'+
								'<td '+bold+'>'+i_empty+'</td>'+
							  '</tr>';	
								
								if ( n == 2 )
								{
									n=0;
								}
								n++;
							}
					
						}
						
						output += '</table></li>';

					do_table_reload(output);
					
	}
	var cur_items = 0;
	function do_table_reload(new_item)
	{
		
		if ( cur_items != 3 )
		{
		$('#loadingshool').remove();
		$('.vordle_koole_list').append(new_item);
			cur_items++;
		} else {
			alert('Maksimum arv koole on lisatud juba');	
		}
	}
	function count(array)
	{
	var c = 0;
	for(i in array) // in returns key, not object
	if(array[i] != undefined)
	c++;
	
	return c;
	
	}

