script.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. // List of alpums elmints
  2. var alpums =[
  3. {'title':'Art', 'url':'Art.html', 'mainUrl':'imges/Art'},
  4. {'title':'Nature', 'url':'Nature.html','mainUrl':'imges/Nature'},
  5. {'title':'Cats', 'url':'Cats.html','mainUrl':'imges/Cats'}
  6. ];
  7. function set(num){
  8. document.getElementById("title_of_alpum").innerHTML = alpums[num].title;
  9. document.getElementById("url_of_alpum").href = alpums[num].url;
  10. document.getElementById("img_of_alpum").src = alpums[num].mainUrl + '/' + Math.floor((Math.random()*15)+1) + '.jpg';
  11. }
  12. // Current alpum
  13. var current_alpum = Math.floor(Math.random()*3); // Get random value from 0 to 2
  14. // Set alpum
  15. function set_alpum(){
  16. set(current_alpum);
  17. }
  18. // Next buttn
  19. function next(){
  20. if(current_alpum == 2)
  21. current_alpum = 0;
  22. else
  23. current_alpum++;
  24. // Set
  25. set(current_alpum);
  26. }
  27. // Prev buttn
  28. function prev(){
  29. if(current_alpum == 0)
  30. current_alpum = 2;
  31. else
  32. current_alpum--;
  33. // Set
  34. set(current_alpum);
  35. }
  36. //load imges
  37. function loadImgs(alpum){
  38. var tempHtml= ' ';
  39. current_alpum = alpum;
  40. for(var i = 0; i < 15; i++)
  41. tempHtml += '<div class="card" id="card_' + (i+1) + '">' +
  42. '<div class="card-cild">' +
  43. '<img alt="" src="' + alpums[alpum].mainUrl + '/' + (i+1) + '.jpg' + '"/>' +
  44. '<div class="toolbar">' +
  45. '<div class="link">' +
  46. '<a href="' + alpums[alpum].mainUrl + '/' + (i+1) + '.jpg' + '" download>' +
  47. '<div class="download">' +
  48. '<p>Download</p>' +
  49. '</div>' +
  50. '</a>' +
  51. '</div>' +
  52. '<div id= "favrots_' + (i+1) + '">' +
  53. '<div class="like_button" id ="favrotsButton_' + (i+1) + '" onclick="'+ (cheackFavrots((i+1), 1) ? 'delFavrots(':'addFavrots(') + (i+1) +')' +
  54. '"><p id="favrotsButtonText_' + (i+1) + '">' + (cheackFavrots((i+1), 1)? 'Remove from ':'Add to ') + 'my fivrots</p></div>' +
  55. '</div>' +
  56. '</div>' +
  57. '</div>' +
  58. '</div>' +
  59. '<br>';
  60. document.getElementById("loding").parentNode.removeChild(document.getElementById("loding"));
  61. document.getElementById("header").innerHTML = '<div class="title"><a href="./index.html"><h1>Beautiful wallpapers</h1></a></div>';
  62. document.getElementById("imgeees").innerHTML = '<div id="imges_scrol"></div>';
  63. document.getElementById("imges_scrol").innerHTML = tempHtml;
  64. }
  65. // Get random wallpaper
  66. function getRandomWallpaper(){
  67. var randImg = Math.floor(Math.random()*15) + 1;
  68. document.getElementById("loding").parentNode.removeChild(document.getElementById("loding"));
  69. document.getElementById("looooad").innerHTML = '<div id="imges_scrol"></div>';
  70. document.getElementById("imges_scrol").innerHTML =
  71. '<div class="card" id="card_' + randImg + '">' +
  72. '<div class="card-cild">' +
  73. '<img alt="" src="' + alpums[current_alpum].mainUrl + '/' + randImg + '.jpg' + '"/>' +
  74. '<div class="toolbar">' +
  75. '<div class="link">' +
  76. '<a href="' + alpums[current_alpum].mainUrl + '/' + randImg + '.jpg' + '" download>' +
  77. '<div class="download">' +
  78. '<p>Download</p>' +
  79. '</div>' +
  80. '</a>' +
  81. '</div>' +
  82. '<div id= "favrots_' + randImg + '">' +
  83. '<div class="like_button" id ="favrotsButton_' + randImg + '" onclick="'+ (cheackFavrots(randImg, 1) ? 'delFavrots(':'addFavrots(') + randImg+')' +
  84. '"><p id="favrotsButtonText_' + randImg + '">' + (cheackFavrots(randImg, 1)? 'Remove from ':'Add to ') + 'my fivrots</p></div>' +
  85. '</div>' +
  86. '</div>' +
  87. '</div>' +
  88. '</div>' +
  89. '<br>';
  90. }
  91. // Add me
  92. function me(){
  93. document.getElementById("footer").innerHTML = '<a href="https://github.com/Anas-Elgarhy"><p>Coding and design by me ^-^</p></a>';
  94. }
  95. // Cookies for save favertos
  96. // Cheack cookies fun
  97. function cheakCookies(){
  98. var favrots = getCookies("favrotsIn" + alpums[current_alpum].title);
  99. if(favrots != "")
  100. return true;
  101. else
  102. return false;
  103. }
  104. // Set cookie
  105. function setCookies(cvalue, exdays, option){
  106. var temp = "favrotsIn" + alpums[current_alpum].title;
  107. // end date
  108. var endDate = new Date();
  109. var date = new Date();
  110. endDate.setTime(endDate.getTime() + (exdays*24*60*60*1000));
  111. // Cheak
  112. if(cheakCookies() && option == 1)
  113. temp += getCookies();
  114. else
  115. temp += "=";
  116. //
  117. if(option == 1)
  118. document.cookie = temp + cvalue + "|" + date.toLocaleTimeString() + ",;" + "expires="+ endDate.toUTCString() + ";path=/" + alpums[current_alpum].url;
  119. else
  120. {
  121. let tempSize = cvalue.length -1;
  122. document.cookie = temp + cvalue + (cvalue == ""? ";": ((cvalue[tempSize] != ",")? ",;":";")) + "expires="+ endDate.toUTCString() + ";path=/" + alpums[current_alpum].url;
  123. }
  124. }
  125. // Get cookie
  126. function getCookies(){
  127. let cname = "favrotsIn" + alpums[current_alpum].title;
  128. var decodedCookie = decodeURIComponent(document.cookie);
  129. var ca = decodedCookie.split(';');
  130. for(var i = 0; i <ca.length; i++) {
  131. var c = ca[i];
  132. while (c.charAt(0) == ' ') {
  133. c = c.substring(1);
  134. }
  135. if (c.indexOf(cname) == 0) {
  136. return c.substring(cname.length, c.length);
  137. }
  138. }
  139. return "";
  140. }
  141. // Delete cookikes
  142. function delCookies(){
  143. document.cookie = "favrotsIn" + alpums[current_alpum].title + "=0|1:39:14 AM,;expires=Thu, 18 Dec 2013 12:00:00 UTC;path=/" + alpums[current_alpum].url;
  144. }
  145. /// This function want tow parameters, one parameter kye to you want cheak or search and tow parameter option to you want use
  146. // Cheack alrdy in favrots
  147. function cheackFavrots(key, option){
  148. let fav = new Array();
  149. // Get favrots in current alpum
  150. let favrots = getCookies().substring(1); // del "=" from return value
  151. // Split return value in array
  152. let alrdyIn = favrots.split(",");
  153. // Get favrots without Date
  154. for(let i=0; i < alrdyIn.length-1; i++)
  155. {
  156. let temp = alrdyIn[i].split("|");
  157. fav[i] = parseInt(temp[0]);
  158. }
  159. //
  160. if(fav.indexOf(parseInt(key)) < 0)
  161. if(option == 1)
  162. return false;
  163. else
  164. return (-1);
  165. else
  166. if(option == 1)
  167. return true;
  168. else
  169. return (fav.indexOf(parseInt(key)));
  170. }
  171. // add favrots
  172. function addFavrots(key){
  173. // Cheack secure
  174. if(!cheackFavrots(key, 1))
  175. {
  176. setCookies(key, 30, 1);
  177. // set favrotsButton
  178. document.getElementById("favrots_" + key).innerHTML = '<div class="like_button" id ="favrotsButton_' + key + '" onclick="delFavrots('+ key +')">'
  179. document.getElementById("favrotsButton_" + key).innerHTML = '<p>Remove from my favrots</p>';
  180. }
  181. else
  182. window.alert("The picture is already in you favorites");
  183. }
  184. // delete favrots
  185. function delFavrots(key){
  186. // Get kay pos with binry search
  187. let pos = cheackFavrots(key, 2);
  188. // Delete
  189. if(pos > -1)
  190. {
  191. // Get all favrots and store in locale array
  192. let allFavrots = getCookies().substring(1).split(",");
  193. allFavrots.splice(pos,1);
  194. let temp = allFavrots.toString();
  195. setCookies(temp, 30, 2);
  196. // Set button favorts
  197. document.getElementById("favrots_" + key).innerHTML = '<div class="like_button" id ="favrotsButton_' + key + '" onclick="addFavrots('+ key +')">'
  198. document.getElementById("favrotsButton_" + key).innerHTML = '<p>Add to my favrots</p>';
  199. }
  200. else
  201. window.alert("Error!!!!");
  202. }
  203. // Get all favorts
  204. function getAllFavrots(){
  205. window.alert("Soooooooooooooooooooooooooooooooooooooooon ");
  206. }