// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

var Flash = {
    timeoutId: null,
    defaultTimeout: 5000,
    
    setMessage: function(message) {
        $('flash').innerHTML = message;
    },
    setTimeout: function (timeoutLength) {
        timeoutLength = timeoutLength || this.defaultTimeout;
        if (this.timeoutId) {
            clearTimeout(this.timeoutId);
        }
        this.timeoutId = setTimeout(this.hide.bind(this), timeoutLength);
    },
    hide: function() {
        if (this.timeoutId) {
             clearTimeout(this.timeoutId);
        }
        Element.hide('flash');
    },
    show: function(timeoutLength) {
        Element.show('flash');
        this.setTimeout(timeoutLength);
    },
    showMessage: function(message, timeoutLength) {
        this.setMessage(message);
        this.show(timeoutLength);
    }
}

var Address = {
    updateCitySelect: function(select) {
        var id = select.getAttribute("id");
        id = id.replace('region_id', 'city_id');
        var regionId = select.options[select.selectedIndex].value;
        new Ajax.Updater(id, '/addresses/update_city_select/'+regionId, {
            asynchronous:true,
            evalScripts:true
        });
    },
    regionChange: function(select) {
        var regionId = select.options[select.selectedIndex].value;
        document.location = '/addresses/' + regionId + '/all/all';
    },
    cityChange: function(select) {
        var cityId = select.options[select.selectedIndex].value;
        var id = select.getAttribute("id");
        id = id.replace('city', 'region');
        var el = document.getElementById(id);
        var regionId = el.options[el.selectedIndex].value;
        document.location = '/addresses/' + regionId + '/' + cityId + '/all';
    }

}

var Article = {
    updateSubcategoriesSelect: function(select) {
        var id = select.getAttribute("id");
        id = id.replace('Cat', 'SubCat');
        var catId = select.options[select.selectedIndex].value;
        new Ajax.Updater(id, '/content/update_subcategories_select/'+catId, {
            asynchronous:true,
            evalScripts:true
        });
    }
}

