$(document).ready(function() {

    if ($('.scooter_slider').size() > 0) {

        var width = $('.scooter_slider li').length * 217;
	
    	$('.scooter_slider ul').width(width);
    	
    	var max = (width-217) / 1000;
    	max = Math.round(max * 100);		
    		 	
    	// slider	
    	$('#handle').slider({
    		min: 0,
    		max: max,
    		slide: function( event, ui ) {
    			$('.scooter_slider ul').css('left', '-' + ui.value + '%');		
    		}
    	});    
        
        
        $('.scooter_slider li').first().addClass('active');
        $('.scooters li').first().fadeIn(500);
        
        $('.scooter_slider li').click(function() {
        
            var index = $(this).index();
            var li = $('.scooters li').get(index);
            $('.scooters li').hide();
            
            $(li).fadeIn(500);        
            
            $('.scooter_slider li').removeClass('active');
            $(this).addClass('active');
            
            return false;
        
        });
        
    }

	$('#city_select').change(function() {
		$(this).submit();
	});
	
	
	// video switcher
	
	if ($('.video').length > 0) {
	
		$('#video ul li:first-child a').addClass('active');
		
		$('#video ul li a').click(function() {
			
			var href = $(this).attr('href').split('#');

			$('.video_object').attr('src', 'http://www.youtube.com/embed/' + href[1] + '?autoplay=1&amp;fs=1&amp;hl=en_US&amp;rel=0');		
				
			$('.video_object').hide().fadeIn();	
				
			$('#video ul li a').each(function() {
				$(this).removeClass('active');
			});	
			
			$(this).addClass('active');		
			return false;
		});

	}
	
	// vraag en antwoord
	
	$('.accordion').accordion({
		autoHeight: false,
		collapsible: true
	});
	
	
	// side scooter carousel
	function Carousel() {
        var count = 1;
        var active = 1;
        var total = $('.carousel ul li').length;
        var speed = 1000;
        var timeout = 0;
        var width = 317;
        var offset = 0;

        return {

            initialize: function(count) {				
				
				offset = width*(active-1);
				
				$('.carousel ul').css('left', '-' + offset + 'px');				
				$('.carousel ul').css('width', (width*total) + 'px');
				
				$('.previous a').hide();					

            },
            
           restart: function(element) {				
								
				active = 0;		
				width = 628;	
				offset = 0;
				var index = $(element).parent().parent().parent().index();
				var carousel = $('.carousel2').get(index);
				var total = $(carousel).find('li').length;
            }, 
            
            next: function(element) {   
            	
				var car = $(element).parent().parent();
            	var total = $(car).find('li').length; 
            	var width = $(car).find('li').first().width();    

            	if (timeout == 0) {				
	
					$(car).find('ul').animate({
						left: '-=' + width + 'px'							
					}, speed);
					
					offset += width;					
	
					active = offset / width + 1;
										
				}				
				
				timeout = speed;
				
		   		setTimeout(function() {
		   			timeout = 0;
			
					if (active > 1) {
						$(car).find('.previous a').show();
					}		
		   			if (active >= total) {
		   				$(car).find('.next a').hide();
		   			}					   			
		   		}, timeout); 
		   		
		   		return false; 	
							
            },
            
            previous: function(element) {                      	
            
				var car = $(element).parent().parent();	 
            	var total = $(car).find('li').length;
            	var width = $(car).find('li').first().width();            					            
            
            	if (timeout == 0) {            

					$(car).find('ul').animate({
						left: '+=' + width + 'px'				
					}, speed);
					
					offset -= width;

					active = offset / width + 1;          		                       	
				
				}
				
				timeout = speed;
				
		   		setTimeout(function() {
		   			timeout = 0;
		   			
					if (active == 1) {
						$(car).find('.previous a').hide();
					}		
		   			if (active < total) {
		   				$(car).find('.next a').show();
		   			}
		   		}, timeout);
		   		
		   		return false;		   		 				
				
            } 

        }
	
    }
	

    var carousel = new Carousel();
    carousel.initialize();
    
   
    $('.more').click(function() {   
   		carousel.restart($(this));   
   		
   		return false;
    }); 
   
    $('.next a').click(function() {   
   		carousel.next(this);   
   		
   		return false;
    });
    
    $('.previous a').click(function() {
   		carousel.previous(this);
   		
   		return false;	   		
    });
  
	function Fader() {
		var count = 0;
		var first = 1;
		var total = $('#fader li').length;
		var timeout = null;
		var speed = 4000;		
		
		$('#fader li:first-child').fadeIn('slow');
		
		return {
		
			initialize: function() {
				
				var count = 0;
				var first = 0;
									
				timeout = setInterval(function() {
				
					var current = $('#fader li').get(count);
					
					if (first == 0) {					
						$(current).hide();
					}					
					
					if (count < $('#fader li').length-1) {
						count++;
						$(current).next().fadeIn(1000);	
					} else {
						count = 0;
						first = 1;
						$('#fader li').first().fadeIn(1000);
					}													
					
					first = 0;
					
				}, speed);	

			},
			
			show: function() {
			
			}
		
		}
		
	}
	
	var fader = new Fader();
	fader.initialize();	

	
	if ($('.tester').length > 0) {
		
		$('.tester').submit(function() {			
			
			var pattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
			var datePattern = /^\d{2}\-\d{2}\-\d{4}$/;
			var fullname = $(this).find('#fullname').val();
			var address = $(this).find('#address').val();
			var zipcode = $(this).find('#zipcode').val();
			var tester_city = $(this).find('#tester_city').val();			
			var phoneno = $(this).find('#phoneno').val();				
			var emailaddress = $(this).find('#emailaddress').val();										
			var birthdate = $(this).find('#birthdate').val();										
			var tester_class = $(this).find('#tester_class').val();								
			var agree = $(this).find('#agree').val();	
			
			if (fullname == '') {
				$(this).find('#fullname').addClass('error');
				return false;
			} else {
				$(this).find('#fullname').removeClass('error');
			}
			
			if (address == '') {
				$(this).find('#address').addClass('error');
				return false;
			} else {
				$(this).find('#address').removeClass('error');
			}
			
			if (zipcode == '') {
				$(this).find('#zipcode').addClass('error');
				return false;
			} else {
				$(this).find('#zipcode').removeClass('error');
			}
			
			if (tester_city == '') {
				$(this).find('#tester_city').addClass('error');
				return false;
			} else {
				$(this).find('#tester_city').removeClass('error');
			}									
			
			if (phoneno == '') {
				$(this).find('#phoneno').addClass('error');
				return false;
			} else {
				$(this).find('#phoneno').removeClass('error');
			}
			
			if (emailaddress == '') {
				$(this).find('#emailaddress').addClass('error');
				return false;
			} else {
				$(this).find('#emailaddress').removeClass('error');
			}
			
			if (pattern.test(emailaddress) == false) {
				$(this).find('#emailaddress').addClass('error');
				return false;				
			} else {
				$(this).find('#emailaddress').removeClass('error');			
			}			
			
			if (birthdate == '') {
				$(this).find('#birthdate').addClass('error');
				return false;
			} else {
				$(this).find('#birthdate').removeClass('error');
			}
			
			if (datePattern.test(birthdate) == false) {
				$(this).find('#birthdate').addClass('error');
				return false;				
			} else {
				$(this).find('#birthdate').removeClass('error');			
			}																							
			
 			if ($(this).find('#agree').attr('checked') == false) {
				$(this).find('#agree').parent().find('label').addClass('error');			
				return false;
			} else {
				$(this).find('#agree').parent().find('label').removeClass('error');
			}			
	

		});
		
	}
	
	$('#sound li').first().find('.btn').addClass('active');
	
	if ($.cookie('sound') != null) {
		var sound = $.cookie('sound');
		
		$('#sound li .btn').removeClass('active');
		var btn = $('#sound li .btn').get(sound);
		$(btn).addClass('active');
		
		if (sound == 0) {
			$('#soundPlayer').html('');		
		} else if (sound == 1) {
			$('#soundPlayer').html('<embed src=\"assets/sound/gassen.mp3\" autostart=true hidden=true loop=true>');				
		} else if (sound == 2) {
			$('#soundPlayer').html('<embed src=\"assets/sound/elektrisch.mp3\" autostart=true hidden=true loop=true>');				
		}
	}	
	
	$('#sound li .btn').click(function() {
		var switch_sound = $(this).attr('title');
		
		$('#sound li .btn').removeClass('active');		
		$(this).addClass('active');
		
		if (switch_sound == 0) {
			$('#soundPlayer').html('');
		} else if (switch_sound == 1) {
			$('#soundPlayer').html('<embed src=\"assets/sound/gassen.mp3\" autostart=true hidden=true loop=true>');		
		} else if (switch_sound == 2) {
			$('#soundPlayer').html('<embed src=\"assets/sound/elektrisch.mp3\" autostart=true hidden=true loop=true>');		
		}	
		
		$.cookie('sound', switch_sound, { path: '/' });
		return false;
	});

	
	$('.scooters .more').click(function() {
		var index = $(this).parent().parent().parent().index();		
		var dialog = $('.dialog-outer').get(index);
		
		$(dialog).fadeIn('slow');	
	
		return false;
	});
	
	$('.dialog-bg').click(function() {
		$(this).parent().fadeOut('slow');
	});
	
	if ($('.thumbnail_videos').length > 0) {
	
		$('.thumbnail_videos a').click(function() {
			
			var youtube = $(this).attr('href');
			
			$('body').prepend('<div class="dialog-outer" style="display: block"><div class="dialog-bg"></div><div class="dialog-modal_video"><iframe src="http://www.youtube.com/embed/' + youtube + '?autoplay=1&amp;fs=1&amp;hl=en_US&amp;rel=0" class="video_object" frameborder="0" style="width: 460px; height:276px" allowfullscreen></iframe></div></div>');
			
			return false;
		
		});
		
		$('.dialog-bg').live('click', function() {
			$('.dialog-outer').remove();
		});
	
	}
	
	if ($('.twitter_feed').length > 0) {

	// twitter	
		$.getJSON("http://search.twitter.com/search.json?callback=?&q=electricheroes&rpp=2", function(data) {
					
			$.each(data.results, function() {
				var text = this.text;
				var text = text.split(" ");			
		
				var linkText = '';	
		
				for (i = 0; i <= text.length-1; i++) {
		
					var fLetter = text[i].substring(0,1);
					var httpCheck = text[i].substring(0,7);
					
					if (fLetter == '@') {
						var link = text[i].substring(1);
		
						if (link.substr(link.length-1,1) == ':') {
							link = link.substring(0, link.length-1);
						}
		
						text[i] = '<a href="http://www.twitter.com/' + link + '" target="_blank" rel="nofollow">' + text[i] + '</a>';
					} else if (fLetter == '#') {
						var link = text[i].substring(1);
		
						text[i] = '<a href="http://twitter.com/search?q=%23' + link + '" target="_blank" rel="nofollow">' + text[i] + '</a>';			
					} else if (httpCheck == 'http://') {
						var link = text[i];
		
						text[i] = '<a href="' + link + '" target="_blank">' + text[i] + '</a>';						
					}
					
					linkText += text[i] + ' ';			
				}	
				
	    	    $('<div class="twitter_image"><a href="http://www.twitter.com/' + this.from_user + '/" rel="nofollow"><img src="' + this.profile_image_url + '" alt="" title="" /></a></div><div class="twitter_content"><a href="http://www.twitter.com/' + this.from_user + '/" rel="nofollow">' + this.from_user + '</a> ' + linkText + '<br /><span>' + relative_time(this.created_at) + '</span></div><div class="clear"></div>')
	        	.appendTo('.twitter_feed');
		    });
		});				
	}
	
    function relative_time(time_value) {
      var parsed_date = Date.parse(time_value);
      var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
      var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
      if(delta < 60) {
      return 'minder dan een minuut geleden';
      } else if(delta < 120) {
      return 'een minuut geleden';
      } else if(delta < (45*60)) {
      return (parseInt(delta / 60)).toString() + ' minuten geleden';
      } else if(delta < (90*60)) {
      return 'ongeveer een uur geleden';
      } else if(delta < (24*60*60)) {
      return 'ongeveer ' + (parseInt(delta / 3600)+1).toString() + ' uur geleden';
      } else if(delta < (48*60*60)) {
      return 'een dag geleden';
      } else {
      return (parseInt(delta / 86400)).toString() + ' dagen geleden';
      }
    }	
		
	

});

Cufon.replace('h1, .movie_h2, .side_h2', {
	fontFamily: 'Bebas',
}); 
