/*
   Fietswereld
   Javascripts
   07-05-2007 i-Aspect B.V.
*/



/* initialisatie */
function init(){
   /* formulieren behaviors */
   var forms = document.getElementsByTagName('form');
   for(x=0;x<forms.length;x++){
      if(forms[x].className!='smallform') continue;
      forms[x].verzenden.value='';
      forms[x].waarde.onfocus=function(){
         if(this.value=='Zoek op uw provincie' || this.value=='Uw emailadres'){
            this.value='';
            this.className='activated';
         }
      }
      forms[x].onsubmit=function(){
         var field=this.waarde;
         if(field.value=='Zoek op uw provincie' || field.value=='Uw emailadres' || field.value==''){
            alert('Voer een waarde in.');
            return false;
         }
         return true;
      }
   }
   /* assortiment werelden submenu */
// if(document.getElementById('werelden'))
   // initWorldSubmenu(document.getElementById('werelden'));

   /* uitleg segmenten offset */
   if(document.getElementById('uitlegsegmenten'))
      initSegmentExplanation(document.getElementById('uitlegsegmenten'));

   /* popup links koppelen */
   initPopupLinks();
}

addEventSimple(window,'load',init);



/* init uitleg segmenten elke 2e item */
function initSegmentExplanation(segmentholder){
   var children = segmentholder.childNodes;
   var count=0;
   for(i=0;i<children.length;i++){
      if(children[i].nodeName != 'LI') continue;
      if(++count % 2==0) children[i].className='secondItem';
   }
}



/* ajax assortiment functies */

   function ChangeLocation() {
      document.location = this.value;
   }

   function ChangeModel() {
      str_url = document.location.toString();
      str_pos = str_url.indexOf('product/');
   //   document.getElementById('brand').value = 0;
   //   document.getElementById('price').value = 0;
      if(str_pos > 0) {
         str_url = str_url.substring(0,str_pos);
         str_url = str_url+'product/'+this.value;
      } else {
         str_url = str_url+'/product/'+this.value;
      }
      document.location = str_url+FormToString();
   }

   function FormToString() {
      str="";
      if(parseInt(document.getElementById('brand').value) > 0)
         str+= "&s-product|assortiment_brand_ID="+document.getElementById('brand').value;
      if(parseInt(document.getElementById('price').value) > 0)
         str+= "&s-price="+document.getElementById('price').value;
      return str;
   }


   function ShowProductList() {
      return internShowProduct('List', this);
   }
   function ShowProduct() {
      return internShowProduct('', this);
   }
   function internShowProduct(type, obj) {
      var opt = {
          method: 'post',
          postBody: FormToString(),
          onSuccess: function(t) {
              response_element.innerHTML = t.responseText;
              if(document.getElementById('btnnext'))
                  document.getElementById('btnnext').onclick = ShowProduct;
              if(document.getElementById('btnprev'))
                  document.getElementById('btnprev').onclick = ShowProduct;
              if(document.getElementById('list-btnnext'))
                  document.getElementById('list-btnnext').onclick = ShowProductList;
              if(document.getElementById('list-btnprev'))
                  document.getElementById('list-btnprev').onclick = ShowProductList;
          },
          on404: function(t) {
              alert('Error 404: location "' + t.statusText + '" was not found.');
          },
          onFailure: function(t) {
              alert('Error ' + t.status + ' -- ' + t.statusText);
          }
      }

      var opt2 = {
          method: 'post',
          postBody: FormToString(),
          onSuccess: function(t) {
              response_element2.innerHTML = t.responseText;
              if (type == 'List') {
                    document.getElementById('brand').onchange = ShowProductList;
                    document.getElementById('price').onchange = ShowProductList;
              } else {
                    document.getElementById('brand').onchange = ShowProduct;
                    document.getElementById('price').onchange = ShowProduct;
              }
              document.getElementById('modelname').onchange = ChangeModel
          },
          on404: function(t) {
              alert('Error 404: location "' + t.statusText + '" was not found.');
          },
          onFailure: function(t) {
              alert('Error ' + t.status + ' -- ' + t.statusText);
          }
      }


      str_url = obj.href;
      if(!str_url)
         str_url = document.location.toString();


//      if(ShowProduct.arguments.length > 1) {
 //        str_url = ShowProduct.arguments[0];
   //   }

      str_pos = str_url.indexOf('onze-producten');
      if(str_pos > 0) {
         str_url = str_url.substring(str_pos+14);
         ajax_url = '/ajaxhandler/productfietswereldmaster'+type+'/'+str_url;
         ajax_url2 = '/ajaxhandler/searchfietswereldmaster/'+str_url;
         response_element = document.getElementById('product_holder');
         response_element2 = document.getElementById('searchform');
         response_element.innerHTML ='<div style="margin-top:50px;margin-left:150px;position:absolute;z-index:10;text-align:center;"><img src="/images/loading.gif"/><br/><br/>Een ogenblik geduld ..</div>';
         new Ajax.Request(ajax_url, opt);
         new Ajax.Request(ajax_url2, opt2);

      } else {
         alert('error 409: Invalid URL please send mailto: profile@i-aspect.net');
         alert('Error ' + t.status + ' -- ' + t.statusText);
      }

      return false;
   }


   function ShowProductAccessoiresList() {
      return internShowProductAccessoires('List', this);
   }

   function ShowProductAccessoires() {
      return internShowProductAccessoires('', this);
   }

   function internShowProductAccessoires(type, obj) { 
      var opt = {
          method: 'post',
          postBody: FormToString(),
          onComplete: function(t) {
          
              response_element.innerHTML = t.responseText;
      if(document.getElementById('btnprev'))
        document.getElementById('btnprev').onclick = ShowProductAccessoires;
      if(document.getElementById('btnnext'))
         document.getElementById('btnnext').onclick = ShowProductAccessoires;
      if(document.getElementById('list-btnprev'))
        document.getElementById('list-btnprev').onclick = ShowProductAccessoiresList;
      if(document.getElementById('list-btnnext'))
         document.getElementById('list-btnnext').onclick = ShowProductAccessoiresList;

          },
          on404: function(t) {
              alert('Error 404: location "' + t.statusText + '" was not found.');
          },
          onFailure: function(t) {
              alert('Error ' + t.status + ' -- ' + t.statusText);
          }
      }

      var opt2 = {
          method: 'post',
          postBody: FormToString(),
          onComplete: function(t) {
        
              response_element2.innerHTML = t.responseText;
              if (type == 'List') {
                    document.getElementById('brand').onchange = ShowProductAccessoiresList;
                    document.getElementById('price').onchange = ShowProductAccessoiresList;
              } else {
                    document.getElementById('brand').onchange = ShowProductAccessoires;
                    document.getElementById('price').onchange = ShowProductAccessoires;
              }
              document.getElementById('modelname').onchange = ChangeModel
          },
          on404: function(t) {
              alert('Error 404: location "' + t.statusText + '" was not found.');
          },
          onFailure: function(t) {
              alert('Error ' + t.status + ' -- ' + t.statusText);
          }
      }


      str_url = obj.href;
      if(!str_url)
         str_url = document.location.toString();


//      if(ShowProductAccessoires.arguments.length > 1) {
//         str_url = ShowProductAccessoires.arguments[0];
//      }

      str_pos = str_url.indexOf('onze-producten');
      if(str_pos > 0) {
         str_url = str_url.substring(str_pos+14);
         ajax_url = '/ajaxhandler/productaccessoiresfietswereldmaster'+type+'/'+str_url;
         ajax_url2 = '/ajaxhandler/searchaccessoiresfietswereldmaster/'+str_url;
         response_element = document.getElementById('product_holder');
         response_element2 = document.getElementById('searchform');
         response_element.innerHTML ='<div style="margin-top:50px;margin-left:150px;position:absolute;z-index:10;text-align:center;"><img src="/images/loading.gif"/><br/><br/>Een ogenblik geduld ..</div>';
         new Ajax.Request(ajax_url, opt);
         new Ajax.Request(ajax_url2, opt2);

      } else {
         alert('error 409: Invalid URL please send mailto: profile@i-aspect.net');
         alert('Error ' + t.status + ' -- ' + t.statusText);
      }

      return false;
   }

   function SetCatalogNavigationAccessoires() {
      if(document.getElementById('btnprev'))
        document.getElementById('btnprev').onclick = ShowProductAccessoires;
      if(document.getElementById('btnnext'))
         document.getElementById('btnnext').onclick = ShowProductAccessoires;
      if(document.getElementById('list-btnprev'))
        document.getElementById('list-btnprev').onclick = ShowProductAccessoiresList;
      if(document.getElementById('list-btnnext'))
         document.getElementById('list-btnnext').onclick = ShowProductAccessoiresList;
   }

   function SetCatalogNavigation() {
      if(document.getElementById('btnprev'))
        document.getElementById('btnprev').onclick = ShowProduct;
      if(document.getElementById('btnnext'))
         document.getElementById('btnnext').onclick = ShowProduct;
      if(document.getElementById('list-btnprev'))
        document.getElementById('list-btnprev').onclick = ShowProductList;
      if(document.getElementById('list-btnnext'))
         document.getElementById('list-btnnext').onclick = ShowProductList;
 
   }


   function popitup(url) {
      newwindow=window.open(url,'name','height=300,width=440');
      if (window.focus) {newwindow.focus()}
      return false;
   }
   function popitupbig(url) {
      newwindow=window.open(url,'bigpopup','height=800,width=440,scrollbars=yes');
      if (window.focus) {newwindow.focus()}
      return false;
   }


   function initPopupLinks(){
      var x = document.getElementsByTagName('a');
      for (var i=0;i<x.length;i++) {
         if (x[i].getAttribute('type') == 'popup') {
            x[i].onclick = function () {
               return popitup(this.href)
            }
            x[i].title += ' (Popup)';
         }
         if (x[i].getAttribute('type') == 'popupfolder') {
            x[i].onclick = function () {
               return popitupbig(this.href)
            }
         }
      }
   }


   function initHomeSegmentExplaination(){
      $$('ul#accordion div').each(

         function(node){
            //alert(node);
            if(node.hasClassName('panel-body')){
              // alert('ja heeft class');
               node.style.display='none';

            }
         }
      );

      var avis = document.getElementById('visible').parentNode.id+'-body';
      document.getElementById(avis).style.display = 'block';


      $$('ul#accordion h3').each(
         function(node){
            node.onclick=function(){
               accordion(node);
            }
         }
      );

//alert('do it?');
   }


   //addEventSimple(window,'load',initHomeSegmentExplaination);



   function accordion(el) {
       if ($('visible') == el) {
           return;
       }
       if ($('visible')) {
           var eldown = el.parentNode.id+'-body';
           var elup = $('visible').parentNode.id+'-body';
           new Effect.Parallel(
           [
               new Effect.SlideUp(elup),
               new Effect.SlideDown(eldown)
           ], {
               duration: 0.1
           });
           $('visible').id = '';
       }
       el.id = 'visible';
   }
   /*
   function init() {

       // hide all elements apart from the one with id visible
       var acc = document.getElementById('accordion');
       var apanels = acc.getElementsByTagName('div');
       for (i = 0; i < apanels.length; i++) {
           if (apanels[i].className == 'panel_body') {
               apanels[i].style.display = 'none';
           }
       }
       var avis = document.getElementById('visible').parentNode.id+'-body';
       document.getElementById(avis).style.display = 'block';
   }
   function addEvent(elm, evType, fn, useCapture) {
       elm["on"+evType]=fn;return;
   }

   addEvent(window, "load", init);
   */