main.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. window.onload = function() {
  2. var req = new XMLHttpRequest();
  3. // first add raf shim
  4. // http://www.paulirish.com/2011/requestanimationframe-for-smart-animating/
  5. window.requestAnimFrame = (function(){
  6. return window.requestAnimationFrame ||
  7. window.webkitRequestAnimationFrame ||
  8. window.mozRequestAnimationFrame ||
  9. function( callback ){
  10. window.setTimeout(callback, 1000 / 60);
  11. };
  12. })();
  13. // main function
  14. function scrollToY(scrollTargetY, speed, easing) {
  15. // scrollTargetY: the target scrollY property of the window
  16. // speed: time in pixels per second
  17. // easing: easing equation to use
  18. var scrollY = window.scrollY,
  19. scrollTargetY = scrollTargetY || 0,
  20. speed = speed || 2000,
  21. easing = easing || 'easeOutSine',
  22. currentTime = 0;
  23. // min time .1, max time .8 seconds
  24. var time = Math.max(.1, Math.min(Math.abs(scrollY - scrollTargetY) / speed, .8));
  25. // easing equations from https://github.com/danro/easing-js/blob/master/easing.js
  26. var PI_D2 = Math.PI / 2,
  27. easingEquations = {
  28. easeOutSine: function (pos) {
  29. return Math.sin(pos * (Math.PI / 2));
  30. },
  31. easeInOutSine: function (pos) {
  32. return (-0.5 * (Math.cos(Math.PI * pos) - 1));
  33. },
  34. easeInOutQuint: function (pos) {
  35. if ((pos /= 0.5) < 1) {
  36. return 0.5 * Math.pow(pos, 5);
  37. }
  38. return 0.5 * (Math.pow((pos - 2), 5) + 2);
  39. }
  40. };
  41. // add animation loop
  42. function tick() {
  43. currentTime += 1 / 60;
  44. var p = currentTime / time;
  45. var t = easingEquations[easing](p);
  46. if (p < 1) {
  47. requestAnimFrame(tick);
  48. window.scrollTo(0, scrollY + ((scrollTargetY - scrollY) * t));
  49. } else {
  50. console.log('scroll done');
  51. window.scrollTo(0, scrollTargetY);
  52. }
  53. }
  54. // call it once to get started
  55. tick();
  56. }
  57. var availableWidth = [ 500, 1024, 1400, 1920];
  58. var size=0;
  59. var banner=document.getElementById("banner");
  60. while (window.innerWidth > availableWidth[size] && size < availableWidth.length-1 )
  61. size++;
  62. banner.src="Images/slider/slide1-"+availableWidth[size]+".jpg";
  63. var contact= document.getElementsByClassName("contact");
  64. var tit_curso = document.getElementsByClassName("titulo_curso");
  65. var desc_curso = document.getElementsByClassName("descripcion_curso");
  66. var list = document.getElementsByTagName("li");
  67. var overlay=document.getElementById('overlay-contact');
  68. var exit=document.getElementById('exit');
  69. var basicgrey=document.getElementById('basic-grey');
  70. var logo=document.getElementById('logo');
  71. var btnconsulta=document.getElementById('btnconsulta');
  72. var btnok=document.getElementById('ok');
  73. function curso_click(){
  74. for(var j =0; j < desc_curso.length; j++){
  75. if (desc_curso[j].getAttribute("title") == this.getAttribute("title")) {
  76. var child = this.firstElementChild;
  77. if(desc_curso[j].style.display=="block") {
  78. child.innerHTML = child.innerHTML.replace("-","+");
  79. desc_curso[j].style.display="none";
  80. } else {
  81. child.innerHTML = child.innerHTML.replace("+","-");
  82. desc_curso[j].style.display="block";
  83. }
  84. }
  85. }
  86. }
  87. overlay.onclick = function(){
  88. overlay.style.display="none";
  89. basicgrey.style.display="none";
  90. }
  91. exit.onclick=function(){
  92. overlay.style.display="none";
  93. basicgrey.style.display="none";
  94. }
  95. btnok.onclick=function(){
  96. var params="mail="+document.getElementById("suscribe").value;
  97. req.open("POST", "newsletter.php", true);
  98. req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  99. req.setRequestHeader("Content-length", params.length);
  100. req.setRequestHeader("Connection", "close");
  101. req.onreadystatechange=function(){
  102. if(req.readyState == 4 && req.status == 200)
  103. document.getElementById("respuestanewsletter").innerHTML=req.responseText;
  104. }
  105. req.send(params);
  106. };
  107. btnconsulta.onclick=function(){
  108. var params = "nombre="+document.getElementById("name").value;
  109. params=params+"&mail="+document.getElementById("email").value;
  110. params=params+"&consulta="+document.getElementById("message").value;
  111. req.open("POST", "contacto.php", true);
  112. req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  113. req.setRequestHeader("Content-length", params.length);
  114. req.setRequestHeader("Connection", "close");
  115. req.onreadystatechange=function(){
  116. if(req.readyState == 4 && req.status == 200)
  117. document.getElementById("respuestacontacto").innerHTML=req.responseText;
  118. }
  119. req.send(params);
  120. };
  121. for(var i =0; i < tit_curso.length; i++)
  122. tit_curso[i].onclick=curso_click;
  123. for(var i =0; i < contact.length; i++)
  124. contact[i].onclick=showContact;
  125. for(var i =0; i < list.length; i++)
  126. if(list[i].firstChild.tagName=="A")
  127. list[i].firstChild.addEventListener("click", function(e) {
  128. var target=document.getElementById(this.hash.replace("#",""))
  129. scrollToY(target.offsetTop, 1500, 'easeInOutQuint');
  130. if (this.hash=='#footer-outer')
  131. showContact();
  132. e.preventDefault();
  133. });
  134. function showContact() {
  135. basicgrey.style.display="block";
  136. overlay.style.display="block";
  137. }
  138. var startY=banner.scrollHeight;
  139. window.onscroll = function () {
  140. logo.style.opacity=1*(window.pageYOffset > startY ); //bool->int
  141. };
  142. var curImg=1;
  143. setInterval(slide, 8000);
  144. var distrib;
  145. if (window.innerWidth <= 890) { //distribuidoras
  146. var xhr = new XMLHttpRequest();
  147. xhr.open('GET', encodeURI('lista_distrib.php'));
  148. xhr.onload = function() {
  149. if (xhr.status === 200) {
  150. distrib=JSON.parse(xhr.responseText);
  151. var sel = document.createElement("select");
  152. var selProv= document.createElement("select");
  153. var selZona = document.createElement("select");
  154. var result= document.createElement("div");
  155. sel.id="pais";
  156. selProv.id="prov";
  157. selZona.id="zona";
  158. result.id="result";
  159. for (country in distrib) {
  160. var opt = document.createElement("option");
  161. var node = document.createTextNode(country);
  162. opt.appendChild(node);
  163. sel.appendChild(opt);
  164. }
  165. sel.selectedIndex=-1;
  166. sel.onchange=changeCountry;
  167. selProv.onchange=changeProv;
  168. selZona.onchange=changeZona;
  169. document.getElementById("distribuidoras").appendChild(sel);
  170. document.getElementById("distribuidoras").appendChild(selProv);
  171. document.getElementById("distribuidoras").appendChild(selZona);
  172. document.getElementById("distribuidoras").appendChild(result);
  173. }
  174. else {
  175. alert('Request failed. Returned status of ' + xhr.status);
  176. }
  177. };
  178. xhr.send();
  179. }
  180. function changeCountry() {
  181. var pais = document.getElementById("pais").value;
  182. var prov = document.getElementById("prov");
  183. var zona = document.getElementById("zona");
  184. zona.innerHTML="";
  185. prov.innerHTML="";
  186. if(distrib[pais] == undefined) return;
  187. for (provincia in distrib[pais]) {
  188. var opt = document.createElement("option");
  189. var node = document.createTextNode(provincia);
  190. opt.appendChild(node);
  191. prov.appendChild(opt);
  192. }
  193. prov.selectedIndex=-1;
  194. }
  195. function changeProv() {
  196. var pais = document.getElementById("pais").value;
  197. var prov = document.getElementById("prov").value;
  198. var zona = document.getElementById("zona");
  199. prov.innerHTML="";
  200. zona.innerHTML="";
  201. if(distrib[pais] == undefined || distrib[pais][prov] == undefined) return;
  202. for (zone in distrib[pais][prov]) {
  203. var opt = document.createElement("option");
  204. var node = document.createTextNode(zone);
  205. opt.appendChild(node);
  206. zona.appendChild(opt);
  207. }
  208. zona.selectedIndex=-1;
  209. }
  210. function changeZona() {
  211. var pais = document.getElementById("pais").value;
  212. var prov = document.getElementById("prov").value;
  213. var zona = document.getElementById("zona").value;
  214. var result = document.getElementById("result");
  215. result.innerHTML="";
  216. var z = distrib[pais][prov][zona];
  217. var text="";
  218. for (distr in z ) {
  219. for (p in z[distr]) {
  220. result.appendChild(document.createTextNode(p+":"+z[distr][p]))
  221. result.appendChild(document.createElement("br"))
  222. }
  223. result.appendChild(document.createElement("br"))
  224. }
  225. }
  226. function slide() {
  227. banner.className='transp';
  228. setTimeout(transp, 500);
  229. curImg++;
  230. if(curImg>5) curImg=1;
  231. }
  232. function transp() {
  233. banner.src="Images/slider/slide"+curImg+"-"+availableWidth[size]+".jpg";
  234. banner.removeAttribute('class');
  235. }
  236. }