// /*
//  Behaviour for usages static display
// */
//
/*Array.prototype.sum = function(){
 for(var i=0,sum=0;i<this.length;sum+=this[i++]);
 return sum;
};
Array.prototype.max = function(){
 return Math.max.apply({},this);
};
Array.prototype.min = function(){
 return Math.min.apply({},this);
};
*/


if(typeof ThreeScale == 'undefined'){
  var ThreeScale = {};
};

if(typeof ThreeScale.Helper == 'undefined'){
  ThreeScale.Helper = {};
};


ThreeScale.Helper.Charts = {

  processDateFromDatePicker: function(dateText){
    var  d = new Date(dateText),
         h = ThreeScale.Helper.General;

    $('#date-name').html(d.getDate() + " " + h.months[d.getMonth()].substr(0, 3));
    Stats.SearchOptions.since = h.dateDashed(d);
    $('#current-date').attr("data-date", h.dateDashed(d));
    $('#datepicker-box').toggle();

  },

  defaultOptions: {
    global: {
      useUTC: true
    },
    chart: {
        renderTo: 'chart-1',
        defaultSeriesType: 'area',
        events: {},
        margin: [20,20,30,55]
    },
    title: { text: null },
    credits: {enabled: false},
    xAxis: {
        type: 'datetime'
    },
    yAxis: {
        title: {enabled: false},
        lineWidth: 1,
        min: 0,
        minPadding: 0,
        gridLineWidth: 1,
        tickWidth: 0,
        gridLineColor: '#e7e7e7',
        lineColor: '#e7e7e7',
        tickColor: '#e7e7e7',
        labels: {
          enabled: true,
          formatter: function(){
            return Highcharts.numberFormat(this.value, 0);
          }
        }
    },
    tooltip: {
    		backgroundColor: {
    			linearGradient: [0, 0, 0, 50],
    			stops: [
    				[0, 'rgba(66, 60, 50, .9)'],
    				[1, 'rgba(29, 27, 21, .8)']
    			]
    		},
    		borderWidth: 0,
    		style: {
    			color: '#FFF'
    		}
    	},
    plotOptions: {
        series: {
            fillColor: '#E6F2FA',
            lineWidth: 3,
            animation: false,
            shadow: false,
            marker: {
              lineWidth: 1,
              radius: 4
            }
        }
    },
    series: []
  },

  periodTostrf: function(period){
    var d;
    switch (period) {
      case 'month':
          d = '%d %b';
          break;
      case 'week':
          d = '%d %b %I:%M%p';
          break;
      case 'day':
          d = '%I:%M%p';
          break;
      }
      return d;
  },

  periodToDate: function(period){
    var value;
    switch (period) {
      case 'year':
          value = 2628000  * 1000;
          break;
      case 'month':
          value = 24 * 3600 * 1000;
          break;
      case 'week':
          value = 3600 * 1000 * 6;
          break;
      case 'day':
          value = 3600 * 1000;
          break;
    }
    return value;
  },

  colours: ['#B23600', '#FF8A00', '#FFC887', '#CC5400', '#89B233'],

  // Takes an integer and returns a corresponding element from colours array.
  pickColour: function(i){
    return this.colours[i] || this.colours[i%5];
  }

};

// Helper object contains an assortment of methods for general assistance.

ThreeScale.Helper.General = {
	months: ['January', 'February', 'March', 'April', 'May', 'June', 'July',
			'August', 'September', 'October', 'November', 'December'],

  numberPadding: function(number, decimals){
    var n = number, c = isNaN(decimals = Math.abs(decimals)) ? 2 : decimals,
        d = ".",
        t = ",",
        i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "",
        s = n < 0 ? "-" : "",
        j = (j = i.length) > 3 ? j % 3 : 0;

    return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) +
      (c ? d + Match.abs(n - i).toFixed(c).slice(2) : "");
    },

    // Formats a date to work with the 3scale stats API
    dateDashed: function(date) {
      if(typeof date == 'undefined'){
        date = new Date();
      }
      var y = date.getFullYear(),
          d = date.getDate(),
          m = date.getMonth() + 1;

      return y + "-" + m + "-" + d;
    },

    currentDateDashed: function(){
      var date = new Date();

      var y = date.getFullYear(),
          d = date.getDate(),
          m = date.getMonth() + 1;

      return y + "-" + m + "-" + d;
    },


    // Takes a string representing a URL and returns an object of URL hash key value pairs.
    // If no URL is passed, current browser location is used.
    urlParamsToHash: function(url) {
        if (!url) {
            url = location.href;
        }

        var map = {};
        var parts = url.replace(/[#?&]+([^=&]+)=([^&]*)/gi,
        function(m, key, value) {
            map[key] = value;
        });
        return map;
    },

    dateParser: function(date, period){

      // If period set to year, then set to 1st of month

      // 2010-06-01T00:00:00+02:00
      var exp = /^(\d{4})-(\d\d)-(\d\d)[T](\d\d:\d\d:\d\d)(\+\d\d:\d\d)*(((-|\+)\d\d:\d\d)|Z)$/,
          p = exp.exec(date);
      if(p) {
        var t = p[4].split(":"),
            h = t[0],
            m = t[1],
            s = t[2];

        if(typeof period != null && period == 'year') p[3] = 10;

        var x = new Date(Date.UTC(p[1],(p[2]-1),p[3],h,m,s));
            return x;
        } else {
          alert(date)
          throw "Invalid string representation of date";
      }
    },


    /**
     * Based on http://www.php.net/manual/en/function.strftime.php
     * Taken straight from Highcharts.js
     * @param {String} format
     * @param {Number} timestamp
     * @param {Boolean} capitalize
     */
    dateFormat: function(format, timestamp, capitalize) {
    	function pad (number) {
    		return number.toString().replace(/^([0-9])$/, '0$1');
    	}

    	if (!defined(timestamp)) return 'Invalid date';


    	var date = new Date(timestamp * timeFactor),

    		// get the basic time values
    		hours = date[getHours](),
    		day = date[getDay](),
    		dayOfMonth = date[getDate](),
    		month = date[getMonth](),
    		fullYear = date[getFullYear](),
    		lang = defaultOptions.lang,
    		langWeekdays = lang.weekdays,
    		langMonths = lang.months,

    		// list all format keys
    		replacements = {

    			// Day
    			'a': langWeekdays[day].substr(0, 3), // Short weekday, like 'Mon'
    			'A': langWeekdays[day], // Long weekday, like 'Monday'
    			'd': pad(dayOfMonth), // Two digit day of the month, 01 to 31
    			'e': dayOfMonth, // Day of the month, 1 through 31

    			// Week (none implemented)

    			// Month
    			'b': langMonths[month].substr(0, 3), // Short month, like 'Jan'
    			'B': langMonths[month], // Long month, like 'January'
    			'm': pad(month + 1), // Two digit month number, 01 through 12

    			// Year
    			'y': fullYear.toString().substr(2, 2), // Two digits year, like 09 for 2009
    			'Y': fullYear, // Four digits year, like 2009

    			// Time
    			'H': pad(hours), // Two digits hours in 24h format, 00 through 23
    			'I': pad((hours % 12) || 12), // Two digits hours in 12h format, 00 through 11
    			'l': (hours % 12) || 12, // Hours in 12h format, 1 through 12
    			'M': pad(date[getMinutes]()), // Two digits minutes, 00 through 59
    			'p': hours < 12 ? 'AM' : 'PM', // Upper case AM or PM
    			'P': hours < 12 ? 'am' : 'pm', // Lower case AM or PM
    			'S': pad(date.getSeconds()) // Two digits seconds, 00 through  59

    		};


    	// do the replaces
    	for (var key in replacements) format = format.replace('%'+ key, replacements[key]);

    	// Optionally capitalize the string and return
    	return capitalize ? format.substr(0, 1).toUpperCase() + format.substr(1) : format;
    }

};
