helperfunctions.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /* function to create popup window */
  2. function popup(mylink){
  3. if (!window.focus)
  4. return true;
  5. var href;
  6. if (typeof(mylink) == 'string')
  7. href=mylink;
  8. else
  9. href=mylink.href;
  10. window.open(href, "player", 'width=300,height=100,scrollbars=yes');
  11. return false;
  12. }
  13. /* Take a string representing a date in the format 2012-04-25 and return
  14. * a javascript date object representing this date. */
  15. function getDateFromString(time){
  16. var date = time.split("-");
  17. if (date.length != 3){
  18. return null;
  19. }
  20. var year = parseInt(date[0], 10);
  21. var month = parseInt(date[1], 10) -1;
  22. var day = parseInt(date[2], 10);
  23. if (isNaN(year) || isNaN(month) || isNaN(day)){
  24. return null;
  25. }
  26. return new Date(year, month, day);
  27. }
  28. function convertSecondsToDaysHoursMinutesSeconds(seconds){
  29. if (seconds < 0)
  30. seconds = 0;
  31. seconds = parseInt(seconds, 10);
  32. var days = parseInt(seconds / 86400);
  33. seconds -= days*86400;
  34. var hours = parseInt(seconds / 3600);
  35. seconds -= hours*3600;
  36. var minutes = parseInt(seconds / 60);
  37. seconds -= minutes*60;
  38. return {days:days, hours:hours, minutes:minutes, seconds:seconds};
  39. }
  40. /* Takes an input parameter of milliseconds and converts these into
  41. * the format HH:MM:SS */
  42. function convertToHHMMSS(timeInMS){
  43. var time = parseInt(timeInMS);
  44. var hours = parseInt(time / 3600000);
  45. time -= 3600000*hours;
  46. var minutes = parseInt(time / 60000);
  47. time -= 60000*minutes;
  48. var seconds = parseInt(time / 1000);
  49. hours = hours.toString();
  50. minutes = minutes.toString();
  51. seconds = seconds.toString();
  52. if (hours.length == 1)
  53. hours = "0" + hours;
  54. if (minutes.length == 1)
  55. minutes = "0" + minutes;
  56. if (seconds.length == 1)
  57. seconds = "0" + seconds;
  58. return hours + ":" + minutes + ":" + seconds;
  59. }
  60. function convertToHHMMSSmm(timeInMS){
  61. var time = parseInt(timeInMS);
  62. var hours = parseInt(time / 3600000);
  63. time -= 3600000*hours;
  64. var minutes = parseInt(time / 60000);
  65. time -= 60000*minutes;
  66. var seconds = parseInt(time / 1000);
  67. time -= 1000*seconds;
  68. var ms = parseInt(time);
  69. hours = hours.toString();
  70. minutes = minutes.toString();
  71. seconds = seconds.toString();
  72. ms = ms.toString();
  73. if (hours.length == 1)
  74. hours = "0" + hours;
  75. if (minutes.length == 1)
  76. minutes = "0" + minutes;
  77. if (seconds.length == 1)
  78. seconds = "0" + seconds;
  79. if (ms.length == 3)
  80. ms = ms.substring(0, 2);
  81. else if (ms.length == 2)
  82. ms = "0" + ms.substring(0,1);
  83. else if (ms.length == 1)
  84. ms = "00";
  85. if (hours == "00")
  86. return minutes + ":" + seconds + "." + ms;
  87. else
  88. return hours + ":" + minutes + ":" + seconds+ "." + ms;
  89. }
  90. function convertDateToHHMM(epochTime){
  91. var d = new Date(epochTime);
  92. var hours = d.getUTCHours().toString();
  93. var minutes = d.getUTCMinutes().toString();
  94. if (hours.length == 1)
  95. hours = "0" + hours;
  96. if (minutes.length == 1)
  97. minutes = "0" + minutes;
  98. return hours + ":" + minutes;
  99. }
  100. function convertDateToHHMMSS(epochTime){
  101. var d = new Date(epochTime);
  102. var hours = d.getUTCHours().toString();
  103. var minutes = d.getUTCMinutes().toString();
  104. var seconds = d.getUTCSeconds().toString();
  105. if (hours.length == 1)
  106. hours = "0" + hours;
  107. if (minutes.length == 1)
  108. minutes = "0" + minutes;
  109. if (seconds.length == 1)
  110. seconds = "0" + seconds;
  111. return hours + ":" + minutes + ":" + seconds;
  112. }
  113. /* Takes in a string of format similar to 2011-02-07 02:59:57,
  114. * and converts this to epoch/posix time. */
  115. function convertDateToPosixTime(s){
  116. var datetime = s.split(" ");
  117. var date = datetime[0].split("-");
  118. var time = datetime[1].split(":");
  119. var year = date[0];
  120. var month = date[1];
  121. var day = date[2];
  122. var hour = time[0];
  123. var minute = time[1];
  124. var sec = 0;
  125. var msec = 0;
  126. if (time[2].indexOf(".") != -1){
  127. var temp = time[2].split(".");
  128. sec = temp[0];
  129. msec = temp[1];
  130. } else
  131. sec = time[2];
  132. return Date.UTC(year, month-1, day, hour, minute, sec, msec);
  133. }
  134. function getFileExt(filename){
  135. return filename.split('.').pop();
  136. }
  137. function resizeImg(ele, targetWidth, targetHeight){
  138. var img = $(ele);
  139. var width = ele.width;
  140. var height = ele.height;
  141. // resize img proportionaly
  142. if( width > height && width > targetWidth){
  143. var ratio = targetWidth/width;
  144. img.css("width", targetHeight+"px");
  145. var newHeight = height * ratio;
  146. img.css("height", newHeight+"px");
  147. }else if( width < height && height > targetHeight){
  148. var ratio = targetHeight/height;
  149. img.css("height", targetHeight+"px");
  150. var newWidth = width * ratio;
  151. img.css("width", newWidth+"px");
  152. }else if( width == height && width > targetWidth){
  153. img.css("height", targetHeight+"px");
  154. img.css("width", targetWidth+"px" );
  155. }
  156. }
  157. function resizeToMaxHeight(ele, targetHeight){
  158. var img = $(ele);
  159. var width = ele.width;
  160. var height = ele.height;
  161. // resize img proportionaly
  162. if( height > targetHeight){
  163. var ratio = targetHeight/height;
  164. img.css("height", targetHeight+"px");
  165. var newWidth = width * ratio;
  166. img.css("width", newWidth+"px");
  167. }
  168. }