var Metrosplash;

/**
 * Utility Functions 
 */
Metrosplash.util = {}

    /**
     * Converts a timestamp into a Ext Date Object via the format
     * @param {String} timestamp
     * [@param {String} format] 
     */
Metrosplash.util.convertTimestamp = function (timestamp) {
    var format = 'YmdHisZ';
    if(arguments.length > 1)
    {
        format = arguments[1];       
    }    
    return Ext.Date.parse(timestamp, format);    
}
    
    /**
     * Shows or hides an element depending on its visual status
     */
Metrosplash.util.showHideElement = function() {
            
    var show = false;
    var hide = new Array();
    
    for(var argument in arguments)
    {
        el = Ext.get(argument);
        if(el)
        {
            el.disable();
            el.hide();
        }
        if(!show)
        {
            show = argument;
        }    
                
    }
    
    show.enable();
    show.show();
}


/**
 * Calls function after a certain amount of time has passed
 * default of 20 minutes, this aggregates calls to defer/setTimeOut
 * 
 * @param {Object} callback
 */
Metrosplash.util.timeoutCall = function(callback) {
    
     
    this.load_time = new Date();
    this.duration = this.millisFromMinutes(20);
    this.callback = callback;
    
    if(arguments.length > 1 && typeof(arguments[1]) == "object" )
    {
        this.args = arguments[1];        
    }
    else
    {
        this.args = {};
    } 
}

Metrosplash.util.timeoutCall.prototype = {
    /**
     * Start's the timer, by setting load_time to new Date 
     */
    start: function() {
        this.load_time = new Date();
    },
    /**
     * Start's the timer,  and starts off the deferred call
     */
    startCallback: function() {
        this.start();
        this.deferId = this.lifeSpan.defer(this.duration, this);
    },
    /**
     * Clear's the callback timeout call but still keeps track of the original instance time
     */
    clear: function() {
        if(this.deferId)
        {
            clearTimeout(this.deferId);
        }
    },
    /**
     * Sets the duration of the callback timer
     * @param {integer} duration
     */
    setDuration: function(duration) {
        this.duration = duration;
    },
    setArgs: function(args) {
        this.args = args;
    },
    /**
     * Returns the amount of time that has passed
     */
    timeElapsed: function() {
        return new Date() - this.load_time;
    },
    /**
     * If timeElapsed is greater than the duration, call the callback
     */
    lifeSpan: function() {
        if(this.timeElapsed() >= this.duration)
        {
            this.callback(this.args);
        }
    },
    /**
     * Unfinished, if there's any activity on the loaded page, start the timer again
     */
    checkActivity: function() {
        var activity = true;
        if(activity)
        {
            this.load_time = new Date();
        }
    },
    /**
     * returns Milliseconds from passed Seconds
     * @param {integer} seconds
     */
    millisFromSeconds: function (seconds) {
        return seconds * 1000;    
    },    
    /**
     * returns Milliseconds from passed Minutes
     * @param {integer} minutes
     */
    millisFromMinutes: function (minutes) {
        return this.millisFromSeconds(minutes*60);    
    }
}

Metrosplash.util.sendHome = function() {
	window.parent.location="/";
}

Metrosplash.util.displayError = function(params) {
    
    if(params.callback)
    {
        params.callback();
    }
        

    Ext.MessageBox.show({
       title: params.title,
       msg: params.message,
       buttons: Ext.MessageBox.OK,
       //animEl: 'mb9',
       fn: Metrosplash.util.sendHome,
       icon: Ext.MessageBox.ERROR 
   });
}

/**
 * getQueryParams - Gives back a static object of name value pairs for the query parameters
 */
Metrosplash.util.getQueryParams = function() {
    var return_object = {}
    var uri_split = document.location.href.split("?");
    if(uri_split.length > 1)
    {
        var query_string = uri_split[1].split("&");
        for(var x in query_string)
        {
            if(typeof query_string[x] == "string")
            {
                param = query_string[x].split("=");
                return_object[param[0]] = param[1];    
            }            
        }
    }
    return return_object;
}

/**
 * statesArray - Gives back abbreviation to state list.
 */
Metrosplash.util.getStateArray= function() {

   var states = [
      [ 'AL' , 'Alabama' ],
      [ 'AK' , 'Alaska' ],
      [ 'AS' , 'American Samoa' ],
      [ 'AZ' , 'Arizona' ],
      [ 'AR' , 'Arkansas' ],
      [ 'CA' , 'California' ],
      [ 'CO' , 'Colorado' ],
      [ 'CT' , 'Connecticut' ],
      [ 'DE' , 'Delaware' ],
      [ 'DC' , 'District of Columbia' ],
      [ 'FL' , 'Florida' ],
      [ 'GA' , 'Georgia' ],
      [ 'GU' , 'Guam' ],
      [ 'HI' , 'Hawaii' ],
      [ 'ID' , 'Idaho' ],
      [ 'IL' , 'Illinois' ],
      [ 'IN' , 'Indiana' ],
      [ 'IA' , 'Iowa' ],
      [ 'KS' , 'Kansas' ],
      [ 'KY' , 'Kentucky' ],
      [ 'LA' , 'Louisiana' ],
      [ 'ME' , 'Maine' ],
      [ 'MD' , 'Maryland' ],
      [ 'MA' , 'Massachusetts' ],
      [ 'MI' , 'Michigan' ],
      [ 'MN' , 'Minnesota' ],
      [ 'MS' , 'Mississippi' ],
      [ 'MO' , 'Missouri' ],
      [ 'MT' , 'Montana' ],
      [ 'NE' , 'Nebraska' ],
      [ 'NV' , 'Nevada' ],
      [ 'NH' , 'New Hampshire' ],
      [ 'NJ' , 'New Jersey' ],
      [ 'NM' , 'New Mexico' ],
      [ 'NY' , 'New York' ],
      [ 'NC' , 'North Carolina' ],
      [ 'ND' , 'North Dakota' ],
      [ 'MP' , 'Northern Marian Islands' ],
      [ 'OH' , 'Ohio' ],
      [ 'OK' , 'Oklahoma' ],
      [ 'OR' , 'Oregon' ],
      [ 'PA' , 'Pennsylvania' ],
      [ 'RI' , 'Rhode Island' ],
      [ 'SC' , 'South Carolina' ],
      [ 'SD' , 'South Dakota' ],
      [ 'TN' , 'Tennessee' ],
      [ 'TX' , 'Texas' ],
      [ 'UT' , 'Utah' ],
      [ 'VT' , 'Vermont' ],
      [ 'VI' , 'Virgin Islands' ],
      [ 'VA' , 'Virginia' ],
      [ 'WA' , 'Washington' ],
      [ 'WV' , 'West Virginia' ],
      [ 'WI' , 'Wisconsin' ],
      [ 'WY' , 'Wyoming' ],
      [ 'PR' , 'Puero Rico' ]
     ]      

    return states;
}

/**
 * Toggles helpEl based off of the openEl with pos
 * 
 * @param {Object} helpEl
 * @param {Object} openEl
 * @param {Object} pos
 */
Metrosplash.util.toggleHelp = function(helpEl, alignToEl, pos)
{
   Ext.getBody().appendChild(Ext.get(helpEl));
   Ext.fly(helpEl).toggle(true);
   Ext.fly(helpEl).alignTo(alignToEl, pos);
   Ext.fly(helpEl).addClassOnOver("pointer");
   Ext.fly(helpEl).on("click", Ext.fly(helpEl).toggle.createCallback(true));
   Ext.fly(helpEl).setStyle("z-index", "999999");   
   
   
}

