/*  
 *  Utilities for vendio gallery
 */


// global variables
var IMAGE_BASE_URL = "http://gimages.vendio.com/";
var DEFAULT_SITE_ID = 1;
var DEFAULT_CURRENCY_SYMBOL = "$";
var DEFAULT_THOUSANDS_DELIM = ",";
var DEFAULT_DECIMAL_DELIM = ".";


// default currencies for each country
var g_defaultCurrencies = new Object();
g_defaultCurrencies["1"] = "$";
g_defaultCurrencies["87"] = "$";
g_defaultCurrencies["89"] = "GBP ";
g_defaultCurrencies["90"] = "AU $";
g_defaultCurrencies["91"] = "C $";
g_defaultCurrencies["92"] = "EUR ";

// default price format 
var g_localeFormat = new Object();
g_localeFormat["1"] = [",", "."];
g_localeFormat["87"] = [",", "."];
g_localeFormat["89"] = [",", "."];
g_localeFormat["90"] = [",", "."];
g_localeFormat["91"] = [",", "."];
g_localeFormat["92"] = [".", ","];  
  
//
// return unix_timestamp for a string date
// date string format: YYYY-MM-DD HH:MM:SS
// Note: return current timestampt if string 
//       date is missing or if has invalid format
//
function get_uts_date(strDate)
{
  var dObj = new Date();  
  
  if((typeof(strDate) == 'undefined') ||  (strDate == ""))
    return dObj.getTime();
  
  var enforcedString = new String(strDate);  
  
  
  var array = enforcedString.split(" ");
  if (array.length != 2)
  {
    return dObj.getTime();
  }
  
  var timepart = array[1].split(":");
  var datepart = array[0].split("-");  
  
  if ((timepart.length != 3) || (datepart.length != 3))
  {
    return dObj.getTime();
  }
  
  return Date.UTC(datepart[0], stripZeros(datepart[1]) - 1, stripZeros(datepart[2]),
                  stripZeros(timepart[0]), stripZeros(timepart[1]),
                  stripZeros(timepart[2])); 


}

//
// create a number from a string
//
function stripZeros(strInt)
{
  return Number(strInt);
}

//
// make a difference from 2 dates
// return a formated string
//
function computeTimeDifference(d1, d2)
{
  if ((typeof(d1) == 'undefined') || (typeof(d2) == 'undefined'))
    return ' invalid date';
    
  var first = d1;
  var second = d2;
  var difference = (first - second) / 1000;
  
  if(difference < 0)
    return " closed";
  
  var days = 0x15180;
  var nDays = Math.floor(parseFloat(difference) / parseFloat(days));
  var hours = Math.floor((difference - (nDays * days)) / 3600);
  
  if(nDays > 0)
  {
    var retStr = nDays + "d, " + hours + "h ";
    return retStr;
  }
  else
  {
    var minutes = Math.floor((difference - (nDays * days) - (hours * 3600)) / 60);
    if( hours > 0 )
    {
      var retStr = hours + "h, " + minutes + "m";
      return retStr;
    }
    else
    {
// SEEMEv -- TBD
      var retStr = hours + "h, " + minutes + "m";
//      var retStr = minutes + "m";
      return retStr;
    }  
  }  
}


//
// format currency
//
function format_currency(amount, currencySymbol, siteId)
{ 
  var currentSite = ((typeof(siteId) == 'undefined') || (siteId == "")) ? DEFAULT_SITE_ID : siteId;
  
  currencySymbol = ((typeof(currencySymbol) == 'undefined') || (currencySymbol == "")) ? 
                    g_defaultCurrencies[currentSite]: currencySymbol;
  
  var locale = g_localeFormat[currentSite];
  
  return currency_support(amount, currencySymbol, locale[0], locale[1]);
}

//
// currency support
//
function currency_support(num, currencySymbol, thousandsDelim, decimalDelim)
{
  if(typeof(currencySymbol) == 'undefined')
    currencySymbol = DEFAULT_CURRENCY_SYMBOL;
  
  if(typeof(thousandsDelim) == 'undefined')
    thousandsDelim = DEFAULT_THOUSANDS_DELIM;
  
  if(typeof(decimalDelim) == 'undefined')
    decimalDelim = DEFAULT_DECIMAL_DELIM; 
  
  return currencySymbol + number_format(num, thousandsDelim, decimalDelim);
}

//
// format a number
//
function number_format(num, thousandsDelim, decimalDelim, decimalDigits)
{
  if(typeof(num) == undefined)
    return "";  
  
  if(typeof(thousandsDelim) == 'undefined')
    thousandsDelim = DEFAULT_THOUSANDS_DELIM;
    
  if(typeof(decimalDelim) == 'undefined')
    decimalDelim = DEFAULT_DECIMAL_DELIM;
  
  parts = String(num).split(".");
  intAr = parts[0].split("");
  intAr.reverse();
  var i = 0;
  var counter = 0;
  while (i < intAr.length)
  {
    counter = counter + 1;
    if( counter > 3 )
    {
      counter = 0;
      intAr.splice(i, 0, thousandsDelim);
    }
    i = i + 1;
  }
  intAr.reverse();
  var val = intAr.join("");  
  
  if (!parts[1]) { parts[1] = 0; };
  val = val + (decimalDelim + zero_append(parts[1], decimalDigits));
    
  return val;
}

//
// format a number based on local type format 
//
function locale_number_format(siteId, num)
{
  if ((typeof(siteId) == 'undefined') || (siteId < 1) || (typeof(g_localeFormat[siteId]) == 'undefined'))
    siteId = DEFAULT_SITE_ID;
    
  // SEEMEk: define a constant with decimal Digits  
     
  var locale = g_localeFormat[siteId];
  
  return number_format(num, locale[0], locale[1], 2);  
}

//
// format a price != 0
//
function price_format(siteId, num)
{
  if ((typeof(num) == 'undefined') || (num == "") || (num == 0))
    return null;
    
  return locale_number_format(siteId, num);   
}

//
// return currency
//
function price_currency(siteId, currency)
{
  if ((typeof(currency) != 'undefined') && (currency != ""))
    return currency;
    
  if ((typeof(siteId) != 'undefined') && (siteId != "") && typeof(g_defaultCurrencies[siteId] != 'undefined'))  
    return g_defaultCurrencies[siteId];
    
  return g_defaultCurrencies[DEFAULT_SITE_ID];
}

//
// padd an integer with O
//
function zero_append(num, digits)
{
  var filled = String(num);
  var len = filled.length;
  var i = 0;
  while (i < (digits - len))
  {
    filled = filled + "0";
    i = i + 1;
  }
  return filled;
}

//
// create the path to thumbnails from a imageID
//
function decode_image_url(bigintID)
{
  if((typeof(bigintID) == 'undefined') || (bigintID == "0"))
  {    
    url = "/my/gallery/image_unavailable.jpg";
    return url;
  }
  
  var hexrep = new String(bigintID);
  if( hexrep.length < 16)
  {
    var padNumber = 16 - hexrep.length;
    ip = 0;
    while (ip < padNumber)
    {
      hexrep = "0" + hexrep;
      ip = ip + 1;
    }
  }
  var first = hexrep.substring(0, 4);
  var second = hexrep.substring(4, 7);
  var third = hexrep.substring(7, 10);
  var ifilename = hexrep.substring(10, 16);  
  
  url = IMAGE_BASE_URL + first + "/" + second + "/" + third + "/" + ifilename + "g.jpg";
  return url;
}
