gallerijPointer = 0;
gallerijTimer = 0;
klantreactiePointer = 0;

$(document).ready(function () {
  /* topmenu */
    $('.topmenu .topa').hover(function() {
      $(this).parent().find('.submenu:hidden').slideDown('fast');
    }, function() {

    });
    
    $('.topmenu > li').hover(function() {
    
    }, function() {
      $(this).find('.submenu:visible').slideUp('fast');
    });
    
  /* social icons */
    $('.socialIcons a').click(function(e) {
      e.preventDefault();
      window.open($(this).attr('href'));    
    });

  /* fancybox */
  	$(".fancy").fancybox({
  		'titleShow'		: false,
  		'type' : 'image'
  	});
    
  /* google maps in fancybox */

    $('.googleMaps').click(function(e) {
      e.preventDefault();
      
      var googleMap = '<iframe style="border: 1px solid #000000" width="700" height="500" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.nl/maps?hl=nl&amp;ie=UTF8&amp;q=Offenga+Special+Car+Parts,+Van+Harinxmaweg,+Franeker&amp;fb=1&amp;gl=nl&amp;hq=Offenga+Special+Car+Parts,&amp;hnear=Van+Harinxmaweg,+Franeker&amp;cid=0,0,11980604754413324403&amp;iwloc=A&amp;ll=53.184518,5.557709&amp;spn=0.006295,0.006295&amp;output=embed"></iframe>';
            
      $.fancybox({ 'content' : googleMap, 'autoDimensions' : false, 'width' : 702, 'height' : 502, 'padding' : 0 });
    });
  
  /* winkel werking */
    $(".bestellijstToevoegen").click(function() {
      var product = $(this).parent().parent().find('.productID').val();
      var aantal = $(this).parent().parent().find('.aantal').val();
      
      $.fancybox({
       'type' : 'ajax',
       'href' : '/php/verwerk/productToevoegen.php?product=' + product + '&aantal=' + aantal,
       'autoDimensions' : false,
       'width' : 550,
       'height' : 'auto'
      });
    });
  
  /* klantreacties */
    $('.klantreactieVorige').click(function(e) {
      e.preventDefault();
      
      klantreactiePointer--;
      
      if (klantreactiePointer < 0) {
        klantreactiePointer = $('.klantreactie').length - 1;
      }
      
      $('.reactieActief').fadeOut('fast', function() {
        $(this).removeClass('reactieActief');
        $('.klantreactie:eq(' + klantreactiePointer + ')').fadeIn('fast').addClass('reactieActief');
      });
    });
  
    $('.klantreactieVolgende').click(function(e) {
      e.preventDefault();
      
      klantreactiePointer++;
      
      if (klantreactiePointer > $('.klantreactie').length - 1) {
        klantreactiePointer = 0;
      }
      
      $('.reactieActief').fadeOut('fast', function() {
        $(this).removeClass('reactieActief');
        $('.klantreactie:eq(' + klantreactiePointer + ')').fadeIn('fast').addClass('reactieActief');
      });
    });
  
	var projecti = 0;
	

	
  /* gallerij */
    $('.gallerijVolgende').click(function(e) {
      e.preventDefault();
      
      gallerijPointer++;
      
      if (gallerijPointer > $('.paginaGallerij .gallerijItem').length - 1) {
        gallerijPointer = 0;
        $('.paginaGallerij .gallerijItems').scrollTo({ top: 0, left: 0},0);
      }
      
      volgendeGallerijLaden();
    });
    
    $('.gallerijVorige').click(function(e) {
      e.preventDefault();
      
      gallerijPointer--;
      
      if (gallerijPointer < 0) {
        gallerijPointer = 0;
        //gallerijPointer = $('.paginaGallerij .gallerijItem').length - 1;
        //$('.paginaGallerij .gallerijItems').scrollTo({ top: 0, left: ($('.paginaGallerij .gallerijItem').length * 748) + 'px'},0);
      } else {
        volgendeGallerijLaden();
      }
    });
    
    gallerijTimer = setTimeout('volgendeGallerijInLaden()', 6000);
    
    $('.faqTitel').click(function() {
      if ($(this).parent().find('.faq:visible').length > 0) {
        $('.faq:visible').slideUp('fast');
        $('.actieveCategorie').removeClass('actieveCategorie');
      } else {
        $('.faq:visible').slideUp('fast');
        $('.actieveCategorie').removeClass('actieveCategorie');
        $(this).parent().find('.faq').slideDown('fast');
        $(this).addClass('actieveCategorie');
      }
    });
    
    $('.faqCategorie .faq h4').click(function() {
      if($(this).parent().find('.antwoord:visible').length > 0) {
        $('.faq .antwoord:visible').slideUp('fast');
        $('.actieveVraag').removeClass('actieveVraag');
      } else {
        $('.faq .antwoord:visible').slideUp('fast');
        $('.actieveVraag').removeClass('actieveVraag');
        $(this).parent().find('.antwoord').slideDown('fast');
        $(this).addClass('actieveVraag');
      }
    });
  
  /* Chassis nummer */
    $('.zoekChassis').submit(function(e) {
      e.preventDefault();
      
      var trefwoord = $(this).find('.trefwoord').val();
      
      $.fancybox.showActivity();
      
      $.ajax({
        type: 'POST',
        url: '/php/verwerk/chassis.php?chassis=' + trefwoord,
        dataType: 'xml',
        success: function(xml) {
          var serie = $(xml).find('dbserieid').text();
          var model = $(xml).find('dbmodelid').text();
          var type = $(xml).find('dbtypeid').text();
          
          if(serie && model && type) {
            document.location.href = '/webshop/nieuwe-bmw-onderdelen/serie=' + serie + '/model=' + model + '/type=' + type;
          } else if (serie) {
            document.location.href = '/webshop/nieuwe-bmw-onderdelen/serie=' + serie;
          } else {
            $.fancybox.hideActivity();
            $.fancybox({ content: 'Chassis nummer niet gevonden'} );
          }
        }
      });
    });
  
  /* Webshop side nav */
    $('.serieKiezenWeergeven').click(function() {
      $(this).parent().hide();
      $(this).parent().parent().find('.verfijnLijst').slideDown();
    });
  
  /* Afspraak maken */
    $('.afspraakAgenda').click(function(e) {
      e.preventDefault();
      
      $.fancybox({
        type: 'ajax',
        href: webadres + "php/verwerk/afspraakMaken.php",
        onComplete: function() {
          afspraakAgendaBind();
        }
      });    
    });
    
  /* Product voorraad */
    $('.geenVoorraad').click(function(){
      $.fancybox({
        content: $(this).attr('alt').replace("\n", "<br/>")
      });
    });
  
  // project thumbnails
    $('.projectThumbnail').click(function() {
      var src = $(this).attr('src').replace('thumbnail', 'bestanden') + '&naam=bestand.jpg';
    
      $.fancybox({
        href: src
      });
    });
});

function volgendeGallerijInLaden() {
    gallerijPointer++;
		
//		alert('test');
    
    if (gallerijPointer > $('.paginaGallerij .gallerijItem').length - 1) {
 //     alert('reset');
      gallerijPointer = 0;
      
      $('.paginaGallerij .gallerijItems').scrollTo({ top: 0, left: 0}, 0);
      
      volgendeGallerijLaden();
    } else {
      volgendeGallerijLaden();
    }
}

function volgendeGallerijLaden() {
  /*
  $('.paginaGallerij .actiefItem').fadeOut('normal', function() {
    $(this).removeClass('actiefItem');
  
    $('.paginaGallerij .gallerijItem:eq(' + gallerijPointer + ')').show().fadeIn('fast', function() {
      $(this).addClass('actiefItem');
    });
   });
  */
  
  var links = 0;
  
  links = gallerijPointer * 748;
  
  //$('.paginaGallerij .innerGallerij').css('left', '-' + links + 'px');
//  $('.paginaGallerij .innerGallerij').scrollTo({ top: 0, left: '-' + links + 'px'});
    $('.paginaGallerij .gallerijItems').scrollTo({ top: 0, left: links + 'px'}, 500,{onAfter: function() {
        if (gallerijPointer > $('.paginaGallerij .gallerijItem').length - 2) {
          gallerijPointer = 0;
          $('.paginaGallerij .gallerijItems').scrollTo({ top: 0, left: 0}, 0);
        }
      }}
    );
  
  clearTimeout(gallerijTimer);
  gallerijTimer = setTimeout('volgendeGallerijInLaden()', 6000);
}

function weergeefSWF(src, width, height, id, flashvars) {
   document.write('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="' + width + '" height="' + height + '" id="' + id + '" align="middle">');
   document.write('<param name="wmode" value="transparent"><param name="flashvars" value="' + flashvars + '"><param name="allowScriptAccess" value="sameDomain" /><param name="movie" value="' + webadres + 'swf/' + src + '" /><param name="quality" value="high" /><param name="bgcolor" value="#ffffff" />');
   document.write('<embed src="' + webadres + 'swf/' + src + '" flashvars="' + flashvars + '" quality="high" bgcolor="#ffffff" width="' + width + '" height="' + height + '" name="' + id + '" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />');
   document.write('</object>');
}

function swfMovie (movie) {
  document.write('<object type="application/x-shockwave-flash" data="' + webadres + 'flv/flvplayer.swf" width="450" height="337"><param name="movie" value="' + webadres + 'flv/flvplayer.swf"><param name="flashvars" value="file=' + movie + '"></object>');
}

function afspraakAgendaBind() {
    $('.afspraakFormulier').submit(function(e) {
      e.preventDefault();

      $.fancybox({
        type: 'ajax',
        href: webadres + "php/verwerk/afspraakMaken.php",
        ajax: { data: $(this).serializeArray() },
        onComplete: function() {
          afspraakAgendaBind();
        }
      });
    });
}

function toevoegenProduct(product) {

  $.fancybox({
  type: 'ajax',
  href: webadres + "php/verwerk/productToevoegen.php?product=" + product,
  autoDimensions: false,
  width: 500,
  height: 400
  });
	
	return false;
}	

function vraagStellenProduct(product) {

  $.fancybox({
    type: 'ajax',
    href: webadres + "php/verwerk/vraagStellen.php?product=" + product,
    onComplete: function() {
      vraagStellenProductBind(product);
    }
  });
	
	return false;
}

function vraagStellenProductBind(product) {
    $('.vraagStellenFormulier').submit(function(e) {
      e.preventDefault();

      $.fancybox({
        type: 'ajax',
        href: webadres + "php/verwerk/vraagStellen.php?product=" + product + "&versturen",
        ajax: { data: $(this).serializeArray() },
        onComplete: function() {
          vraagStellenProductBind(product);
        }
      });
    });
}

function centerDiv(object)
 {
  object.style.marginLeft = "-" + parseInt(object.offsetWidth / 2) + "px";
  object.style.marginTop = "-" + parseInt(object.offsetHeight / 2) + "px";
 }

function verbergProductDiv() {
  $.fancybox.close();
  
  document.getElementById('productStatus').style.display = 'none';
	clearTimeout(verbergTimeout);
}

function vernieuwCaptcha (t) {
  document.getElementById('captchaKeyBox').innerHTML = '<img src="' + webadres + 'php/verwerk/captcha/index.php?t=' + t + '&a=' + (Math.random()*11) + '">'; 
}

function locatie(url) {
  window.location.href = url;
}

function testFunctie() {
  alert('test');
}
