(function($){
	
	$(document).ready(function(){
		$.tabControl();
		$.menuControl();
		$.faqControl();
		//$.frontpageControl();
		$('#insiders').teaser_slider({
			scollTime: 7,
			fadeTime: 0.5,
			itemsClass: 'insider-item',
			boxWidth: 923
		});
		$.lightboxControl();
		$.winkelWagenControl();
	});
	
	$.fn.extend({
		teaser_slider: function( opt ){
			var settings = opt;
			this.each(function(){
				var self = $(this);
				var opt = $.extend({
					scollTime: 4,
					fadeTime: 0.5,
					itemsClass: 'teaser-slider',
					boxWidth: 200
				}, settings);
				
				var items = {
					count: $('.' + opt.itemsClass).size(),
					array: $('.' + opt.itemsClass)
				};
				
				var posLeft = 0;
				
				if(items.count > 1){
					items.array.each(function(){
						$(this).css('left', posLeft);
						posLeft += opt.boxWidth;
					});
					
					$(self).everyTime((opt.scollTime * 1000), function(){
						items.array.each(function(){
							$(this).animate({
								left: '-=' + opt.boxWidth
							}, (opt.fadeTime * 1000), function(){
								if(parseInt($(this).css('left')) < 0){
									$(this).css('left', posLeft - opt.boxWidth);
								};
							});
						});
					});
				}
			});
		}
	});
	
	$.extend({
		lastItem: 'scooters',
		tabControl: function(){
			$('.tab').bind('click', function(e){
				e.preventDefault();
				
				var tabClass = $(this).parent().attr('class');
				$(this).parent().parent().parent().attr('class',tabClass);
			});
		},
		winkelWagenControl: function(){
			$('input[name=ophalen]').bind('click', function(){
				$('.ophaalForm').find('input[type=text], select').attr("disabled",this.checked);
			});
		},
		menuControl: function(){
			$('.dropdown, .filter-form').bind('mouseenter', function(){
				$('#menu').clearQueue();
				$('#menu').stopTime();
				$('#menu').animate({
					'height': '155px'
				}, 500);
			});
			$('.dropdown, .filter-form').bind('mouseleave', function(){
				$('#menu').oneTime(300, "trans", function(){
					$('#menu').clearQueue();
					$('#menu').animate({
						'height': '29px'
					}, 500);
				});
			});
			$('.dropdown').bind('mouseenter', function(){
				var newTemp = $(this).text().toLowerCase();
				if($.lastItem != $(this).text().toLowerCase()){
					$('.' + $.lastItem + '-filter').css('display','none');
					$('.' + newTemp + '-filter').css('display','block');
					$.lastItem = newTemp;
				}
			});
			/*$('.arrow').bind('mouseenter', function(){
				if($.lastItem != $(this).text().toLowerCase()){
					$.lastItem = $(this).text().toLowerCase();
					$.ajax({
						beforeSend: function(){},
						url: 'menu-dropdown.asp',
						data: 'type=' + $(this).text().toLowerCase(),
						success: function(data) {
						
							$('#filter').fadeOut('fast', function(){
								$('#filter').html(data);
								$('#filter').fadeIn('fast');
							});
							
						},
						async: false
					});
				}
			});*/
		},
		faqControl: function(){
			$('.answer_button').bind('click', function(e){
				e.preventDefault();
				if($(this).is('.show_answer')){
					$(this).removeClass('show_answer');
					$(this).addClass('hide_answer');
				} else {
					$(this).addClass('show_answer');
					$(this).removeClass('hide_answer');
				}
				$(this).next().toggle();
			});
		},
		/*frontpageControl: function(){
			var items = {
				count: $('.insider-item').size(),
				array: $('.insider-item'),
				curItem: 0,
				nextItem: 1
			}
			
			if(items.count > 1){
				$('.insider-item').hide().css('opacity',0);
				$('.insider-item:first').show().css('opacity',1);
				$('#insiders').everyTime(7000, "frontpage", function(){
					if(parseInt(items.curItem) < items.count) items.curItem = (parseInt(items.curItem) + 1);
					else items.curItem = 1;
					if(parseInt(items.nextItem) < items.count) items.nextItem = (parseInt(items.nextItem) + 1);
					else items.nextItem = 1;
					
					// Fade out
					$('#item_' + items.curItem).animate({
						'opacity': 0
					}, 1500, function(){
						$(this).css('display','none');
					});
					
					// Fade in
					$('#item_' + items.nextItem).css('opacity',0).css('display','block');
					$('#item_' + items.nextItem).animate({
						'opacity': 1
					}, 1500);
				});
			};
		},*/
		lightboxControl: function(){
			$('input[name=colors]').bind('click', function(){
				var srcImage = $(this).parent().find('label a').attr("href");
				
				$('.image a img').attr('src', srcImage);
				$('.image a').attr('label',$('.image a img').attr('src')); 
			});
			$('a[rel=lytebox]').each(function(){
				if($(this).attr('rev') != "disabled" && !$(this).parent().is('.image')){
					$(this).bind('click', function(){
						if($(this).parent().next().is('input[name=colors]')){
							$('.image a img').attr('src', $(this).attr('href'));
							$('.image a').attr('label',$('.image a img').attr('src')); 
							if($(this).parent().next().is(':checked')){
								// do nothing
							} else {
								$(this).parent().next().attr('checked', true);
							}
						}
					})
					.bind('mouseover', function(){
						var selfHref = $(this).attr('href');
						//$('.image a img').fadeOut('fast', function(){
							$('.image a').attr('label',$('.image a img').attr('src')); 
							$('.image a img').attr('src', selfHref);
							//$('.image a img').fadeIn('fast');
						//});
					})
					.bind('mouseout', function(){
						//$('.image a img').fadeOut('fast', function(){
							$('.image a img').attr('src', $('.image a').attr('label'));
							//$('.image a img').fadeIn('fast');
						//});
					});
				}
			});
		}
	});
	
})(jQuery);
