/**
 * Provides a set of methods in order to configure some parts of the application
 *
 * @author ppolyzos
 */
function UIHelper(){
}

/**
 * Returns the pager limit size for each grid that uses pager
 * @return limit for the pager
 */
UIHelper.getPagerSize = function(){
    return 25;
}

/**
 * Get the period of time to check if the user is connected
 * @return period time
 */
UIHelper.getSessionRefreshTime = function(){
    return 1000 * 60 * 1; //1 minutes
}

/**
 * Print the specified div
 * @param {String} div
 */
UIHelper.printDiv = function(div){
    var a = window.open('', '', 'width=800,height=600');
    a.document.open("text/html");
    a.document.write('<html><head><link rel="stylesheet" href="script/css/tables.css" />');
    a.document.write('<link rel="stylesheet" href="script/lib/ext/resources/css/ext-all.css" />');
    a.document.write('<link rel="stylesheet" href="script/css/general.css" />');
    a.document.write('</head></html>');
    a.document.write(document.getElementById(div).innerHTML);
    a.document.close();
    a.print();
}

/**
 * Calculate the appropriate height of the results grid in order to display the pager
 * appropriatelly
 * @return resultsGridHeight
 */
UIHelper.getResultsGridHeight = function(){
    return UIHelper.getMarketPlaceHeight() - 54;
}

/**
 * Returns the actual height of the center component in MarketPlace index
 * @return adminPageHeight
 */
UIHelper.getAdminPageHeight = function(){
    return 600;
}

/**
 * Returns the actual height of the center component in MarketPlace
 * @return marketPlaceHeight
 */
UIHelper.getMarketPlaceHeight = function(){
    return 735;
}

/**
 * According to the screen height optimize the height of the products / categories grid
 * @return height
 */
UIHelper.getGridHeight = function(){
    if (screen.height <= 768) {
        //250
		return 450; //Grid size for the 1024x768 resolution
    }
    else {
		//325
        return 460; //For higher resolutions
    }
};

/**
 * Set visible or not a specified field
 * @param {field} field to show or hide
 * @param {value} true or false
 */
UIHelper.setVisible = function(field, value){
    if (value) {
        field.enable();
        field.show();
        field.getEl().up('.x-form-item').setDisplayed(true);// show label
    }
    else {
        field.disable();// for validation
        field.hide();
        field.getEl().up('.x-form-item').setDisplayed(false); // hide label
    }
    
}

/**
 * Hide Field but also it's label
 * @param {Object} field to hide
 */
UIHelper.hideField = function(field){
    field.disable();// for validation
    field.hide();
    field.getEl().up('.x-form-item').setDisplayed(false); // hide label
};

/**
 * Show field and also it's label
 * @param {Object} field to show
 */
UIHelper.showField = function(field){
    field.enable();
    field.show();
    field.getEl().up('.x-form-item').setDisplayed(true);// show label
};

/**
 * When a panel get's activated rerender inner components
 * @param {Object} p panel to rerender components
 */
UIHelper.handleActivate = function(p){
    p.doLayout();
}

/**
 * When a tab get's activated rerender inner components
 * @param {Object} tab
 */
UIHelper.handleActivate = function(tab){
    tab.doLayout();
}

/**
 * Before the tap panel is layouted recalculate to avoid crashes
 * @param {Object} tab
 */
UIHelper.handleAfterLayout = function(tab){
    tab.doLayout();
}

/**
 * Get url parameter - Used mostly for marketId and focus params
 * @param {Object} name
 */
UIHelper.getURLParameter = function(name){
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);
    if (results == null) {
        return "";
    }
    else {
        return results[1];
    }
}

/**
 * Create a class that will hold to variables, one for the key and one for the value
 * There will be two two-dimensional arrays, one for english and one for greek
 * Each element of the array will hold a variable
 * @author ppolyzos
 */
var Localization = function(){
    var dictionary;
    
    return {
        setDictionary: function(dictionaryValue){
            dictionary = dictionaryValue;
        },
        getDictionary: function(){
            return dictionary;
        },
        /**
         * Return the appropriate message for the specified key
         */
        getMessage: function(key){
            for (var i = 0; i < dictionary.length; i++) {
                if (dictionary[i].key === key) {
                    return dictionary[i].message;
                }
            }
            return 'Missing...';
        }
    }
}
();


// A reusable error reader class for XML forms
Ext.form.XmlErrorReader = function(){
    Ext.form.XmlErrorReader.superclass.constructor.call(this, {
        record: 'field',
        success: '@success'
    }, ['id', 'msg']);
};
Ext.extend(Ext.form.XmlErrorReader, Ext.data.XmlReader);

