﻿//--- Util functions ---
var createnamespace = function() {
    var o, d;
    $.each(arguments, function(v) {
        d = arguments[1].split(".");
        o = window[d[0]] = window[d[0]] || { };
        $.each(d.slice(1), function(v2) {
            o = o[arguments[1]] = o[arguments[1]] || { };
        });
    });
    return o;
};

String.prototype.beginsWith = function(t, i) {
    if (i == false) {
        return (t == this.substring(0, t.length));
    } else {
        return (t.toLowerCase() == this.substring(0, t.length).toLowerCase());
    }
};

String.prototype.endsWith = function(t, i) {
    if (i == false) {
        return (t == this.substring(this.length - t.length));
    } else {
        return (t.toLowerCase() == this.substring(this.length - t.length).toLowerCase());
    }
};

String.format = function(text) {
    //check if there are two arguments in the arguments list
    if (arguments.length <= 1) {
        //if there are not 2 or more arguments there’s nothing to replace
        //just return the original text
        return text;
    }

    //decrement to move to the second argument in the array
    var tokenCount = arguments.length - 2;
    for (var token = 0; token <= tokenCount; token++) {
        //iterate through the tokens and replace their placeholders from the original text in order
        text = text.replace(new RegExp("\\{" + token + "\\}", "gi"), arguments[token + 1]);
    }
    return text;
};

FormatJsonDate = function(jsonDate, formatString) {
    return Date.formatDate(Date.deserialiseFromJSON(jsonDate), formatString);
};

Date.deserialiseFromJSON = function(jsonDate) {
    return eval('new' + jsonDate.replace( /\//g , ' '));
};

Date.formatDate = function(date, formatString) {
    var dateString = $.datepicker.formatDate(formatString, date);
    dateString = dateString.replace('HH', date.getHours());
    dateString = dateString.replace('nn', date.getMinutes());
    dateString = dateString.replace('ss', date.getSeconds());
    return dateString;
};

createnamespace( 'utils', 'utils.setTimeoutHelper' );
utils.setTimeoutHelper = function(params) { this.init(params); }
$.extend(utils.setTimeoutHelper.prototype, {
    params: null,

    init: function(params) {
		this.params = $.extend({
		    id: null,
			timeout: 1000,
			callback: null,
			args: null
		}, params);
    },
    
    start: function() {
        if( typeof( this.params.callback ) !== 'function' ) return false;
        var self = this;
        window.setTimeout(function() { self.complete(); }, this.params.timeout);
    },
    
    complete: function() {
        this.params.callback.apply(window, this.params.args);
    }
});

// 
//// implement JSON.stringify serialization
//JSON.stringify = JSON.stringify || function(obj) {
//    var t = typeof (obj);
//    if (t != "object" || obj === null) {
//        // simple data type
//        if (t == "string") obj = '"' + obj + '"';
//        return String(obj);
//    }
//    else {
//        // recurse array or object
//        var n, v, json = [], arr = (obj && obj.constructor == Array);
//        for (n in obj) {
//            v = obj[n]; t = typeof (v);
//            if (t == "string") v = '"' + v + '"';
//            else if (t == "object" && v !== null) v = JSON.stringify(v);
//            json.push((arr ? "" : '"' + n + '":') + String(v));
//        }
//        return (arr ? "[" : "{") + String(json) + (arr ? "]" : "}");
//    }
//};


//serializePriceData = function( priceContainer ) {
//    if( typeof( priceContainer ) !== 'object' ) return '';
//    
//    var priceData = null;
//    if( typeof( priceContainer.TotalPrice ) !== 'undefined' ) {    
//        priceData = priceContainer.TotalPrice;
//        if( typeof( priceContainer.DayRates ) === 'object' ) {
//            // Got some day rates to add in
//	        priceData = $.extend({
//	            DayRates: priceContainer.DayRates
//	        }, priceData);
//        }
//    } else if( typeof( priceContainer.OriginalCurrencyValue ) !== 'undefined' ) {
//        priceData = priceContainer;
//    }
//    if( priceData == null ) return null;
//    var jsonString = JSON.stringify(priceData);
//    return jsonString;
//};
