//if(window['console'] === undefined) window.console = {log: function(){}};

var FieldFocus = Class.create({
    initialize: function() {

        this.oldvals = {};

        $$('input', 'textarea').each(this.elementFocus.bind(this));
    },
    elementFocus: function(el) {
        if (el.type == 'text' || el.tagName.toLowerCase() == 'textarea') {

            this.oldvals[el.id] = (el.tagName.toLowerCase() == 'input') ? el.value : el.innerHTML;

            el.observe('focus', function(e) {
                el = e.target;
                if (el.tagName.toLowerCase() == 'input' && el.value == this.oldvals[el.id]) {
                    el.value = '';
                } else if (el.tagName.toLowerCase() == 'textarea' && el.innerHTML == this.oldvals[el.id]) {
                    el.innerHTML = '';
                }
            }.bind(this));

            el.observe('blur', function(e){
                el = e.target;
                if (el.tagName.toLowerCase() == 'input' && el.value == '') {
                    el.value = this.oldvals[el.id];
                } else if (el.tagName.toLowerCase() == 'textarea' && el.innerHTML == '') {
                    console.log(el);
                    el.innerHTML = this.oldvals[el.id];
                }
            }.bind(this));
        }
    }
});

function loadCountryObserver(){
	if($('country') != undefined){
		$('country').observe('change', function(){
			xajax_xajaxHandler('DealersPlugin', 'xajaxCitySelector', $(this).value);
		});
	}
}

function loadCityObserver(){
	$('city').observe('change', function(){
		var data = new Array($('country').value, $(this).value);
		xajax_xajaxHandler('DealersPlugin', 'xajaxFetchDealers', data);
	});
}

function loadJSON(jsonDATA){
	new TableOrderer('data_container',{
		data : jsonDATA,
		pagination: 'bottom',
		search: true,
		pageCount: 8,
	});
}

document.observe('dom:loaded', function() {
//	new FieldFocus();
	loadCountryObserver();
});
