var bind_event_choose_provincies = function(obj) {
	var form = obj;
	var rel = jQuery(form).attr('rel');
	
	// Bindo l'evento change sulla regione
	jQuery('.jq-region', form).live('change', function() {
		var region = jQuery('option:selected', this).val();
		
		// Popolo la select provincia
		jQuery('.jq-province', form).empty().append(jQuery('<option/>').val('').html('---'));
		jQuery.each(json_regions.provincies[region], function(index, value) {
			jQuery('.jq-province', form).append( jQuery('<option/>').val(value).html(value) );
		});
	});
	
	// Bindo l'evento change sulla provincia
	jQuery('.jq-submit', form).live('change', function() {
		jQuery(this).parents('form').trigger('submit');
	});
	
	jQuery('.jq-submit-ajax', form).live('change', (function(obj) {
		return function() {
			var form = jQuery(obj);
			var container = form.parent();
			var action = form.attr('action');
			
			jQuery.ajax({
				url: action,
				type: 'POST',
				dataType: 'html',
				data: jQuery(form).serialize(),
				complete: function(jqXHR, textStatus) {
					jQuery(container).html(jqXHR.responseText);
				}
			});
		}
	})(form));
	// TODO remove if it's possible to merge with the previous one
  jQuery('.jq-submit-ajax-refresh', form).live('change', (function(obj) {
    return function() {
      var form = jQuery(obj);
      var container = form.parent();
      var action = form.attr('action');
      
      // TODO better to show a spinner
      $.nmTop().close();
      
      jQuery.ajax({
        async: true,
        url: action,
        type: 'POST',
        dataType: 'html',
        data: jQuery(form).serialize(),
        complete: function(jqXHR, textStatus) {
          document.location.href = document.location.href; 
        }
      });
    }
  })(form));
};

jQuery(document).ready(function() {
	ddsmoothmenu.init({
		mainmenuid: "menu", //menu DIV id
		orientation: 'h', //Horizontal or vertical menu: Set to "h" or "v"
		classname: 'ddsmoothmenu', //class added to menu's outer DIV
		//customtheme: ["#1c5a80", "#18374a"],
		contentsource: "markup" //"markup" or ["container_id", "path_to_menu_file"]
	})
	
	jQuery('form[rel*="jq-choose-province"]').live('focus', function() { bind_event_choose_provincies(this); });
	
	// jQuery('#agenda').load('/calendars/ajax_get_wizard/calendar');
    if(jQuery('#sidebar.default .box.offerta').length) {
    	jQuery('#sidebar.default .box.offerta').load('/offers/ajax_get_wizard/4');
    }

    if(jQuery('#sidebar.default .box.offerta').length) {
	    jQuery('#sidebar.default .box.locali').load('/locals/ajax_get_wizard/4');
    }

    if(jQuery('#sidebar.default .box.offerta').length) {
	    jQuery('#sidebar.default .box.games').load('/games/ajax_get_wizard/4');
    }

    if(jQuery('#sidebar.default .box.offerta').length) {
	    jQuery('#sidebar.default .box.dischi').load('/disks/ajax_get_wizard/20/6');
    }

    if(jQuery('#sidebar.default .box.offerta').length) {
	    jQuery('#sidebar.search .box.offerta').load('/offers/ajax_get_wizard/4');
    }

    if(jQuery('#sidebar.default .box.offerta').length) {
	    jQuery('#sidebar.game .box.locali').load('/offers/ajax_get_wizard/20');
    }
});
