$(document).ready(function(){
  $('#l_login').focus(function(){
    fieldTextSwitch(this, 'Login...',true);
  }).blur(function(){
    fieldTextSwitch(this, 'Login...',false);
  });
  
  $('#l_password').focus(function(){
    fieldTextSwitch(this, 'Hasło...',true);
  }).blur(function(){
    fieldTextSwitch(this, 'Hasło...',false);
  });
  
  $('#n_email').focus(function(){
    fieldTextSwitch(this, 'Adres e-mail...',true);
  }).blur(function(){
    fieldTextSwitch(this, 'Adres e-mail...',false);
  });  
  
  $('#n_namesurrname').focus(function(){
    fieldTextSwitch(this, 'Imię i Nazwisko...',true);
  }).blur(function(){
    fieldTextSwitch(this, 'Imię i Nazwisko...',false);
  });   
  
  $('#qs_text').focus(function(){
    fieldTextSwitch(this, 'Model / Słowo kluczowe...',true);
  }).blur(function(){
    fieldTextSwitch(this, 'Model / Słowo kluczowe...',false);
  });

  $('#logout').click(function() {
	var post_data = "logout=true";
  
	$.ajax({
		type: 'POST',
        url: 'index.php?module=default&xml=1',
        dataType: 'xml', 
        data: post_data,
        async : true,
		
        beforeSend : function() {
    		$('#logout').hide();
    		$('#l_loading').show();
    	},		
		
		success : function(xml) {			
			window.location = 'default,pl,,,.html';
		}
	});    	
  });
  
  $('#qs_category_id').change(function() {
	  refreshSelectFields('category');
  });
  
  $('#qs_size_id').change(function() {
	  refreshSelectFields('size');
  });
  
  $('#qs_color_id').change(function() {
	  refreshSelectFields('color');
  });  
});

function refreshSelectFields(what_changed) {
	var post_data;
	
	var qs_category_id = $('#qs_category_id').val();
	var qs_size_id = $('#qs_size_id').val();
	var qs_color_id = $('#qs_color_id').val();
	
	post_data='qs_category_id='+qs_category_id+'&qs_size_id='+qs_size_id+'&qs_color_id='+qs_color_id+'&what_changed='+what_changed;
	
    $.ajax({
        type: 'POST',
        url: 'index.php?module=left_column_update&xml=1',
        dataType: 'xml', 
        data: post_data,
        async : false,
        
        beforeSend : function() {
    		$('#qs_ok').hide();
    		$('#qs_loading').show();
    	},

        success : function(xml) {    		
    		var categories_list = $('lupoline left_column_update categories_list', xml);
    		var sizes_list = $('lupoline left_column_update sizes_list', xml);
    		var colors_list = $('lupoline left_column_update colors_list', xml);
    		
    		if ($('category', categories_list).size()) {
    			$('#qs_category_id').html('');
    			
    			var categories_html = '<option value="-1">Kategoria...</option>';
    			
    			$('category', categories_list).each(function() {
    				var category_name = $('name', this).text();
    				
    				categories_html += '<option '+((category_name==qs_category_id)?('selected="selected"'):(''))+' value="'+category_name+'">'+category_name+'</option>';
    			});
    			
    			$('#qs_category_id').html(categories_html);
    		}
    		
    		if ($('size', sizes_list).size()) {
    			$('#qs_size_id').html('');
    			
    			var sizes_html = '<option value="-1">Rozmiar...</option>';
    			
    			$('size', sizes_list).each(function() {
    				var size_id = $('id', this).text();
    				var size_name = $('name', this).text();
    				
    				sizes_html += '<option '+((size_id==qs_size_id)?('selected="selected"'):(''))+' value="'+size_id+'">'+size_name+'</option>';
    			});
    			
    			$('#qs_size_id').html(sizes_html);
    		}
    		
    		if ($('color', colors_list).size()) {
    			$('#qs_color_id').html('');
    			
    			var colors_html = '<option value="-1">Kolor...</option>';
    			
    			$('color', colors_list).each(function() {
    				var color_id = $('id', this).text();
    				var color_name = $('name', this).text();
    				
    				colors_html += '<option '+((color_id==qs_color_id)?('selected="selected"'):(''))+' value="'+color_id+'">'+color_name+'</option>';
    			});
    			
    			$('#qs_color_id').html(colors_html);
    		}
    		
    		$('#qs_loading').hide();
    		$('#qs_ok').show();
    	},

        error : function(request, errortype, exception) {            
            alert('Błąd podczas asynhronicznego wywołania ('+errortype+'): '+exception);
        }
    });	
}