// jQuery
jQuery(function(){
	// All pages
	receiveUpdates.init();
		
	// Home page
	if($('#contentWrapper').hasClass('main') && $('#contentWrapper').hasClass('home')){
		// PRODUCT NAVIGATION
		var bikes = ['sportster', 'dyna', 'softail', 'vrsc', 'touring', 'sportbike', 'street-fighter', 'adventure'];
		productViewer.init(bikes);
		
		// If sIFR isn't enabled we add the event handlers for the product image swap to the anchors
		// If sIFR is enabled the event handers are in the sifr-config.js file
		
		$('#models #buell li a').mouseover(function(){
			var targetClass = $(this).parent().attr('class');
			productViewer.stopAnimation();
			productViewer.displaySpecificPicture(targetClass);
		});
		$('#models #buell li a').mouseout(function(){
			productViewer.startAnimation();
		});		
		
		
		$('#models #harley li a').mouseover(function(){
			var targetClass = $(this).parent().attr('class');
			productViewer.stopAnimation();
			productViewer.displaySpecificPicture(targetClass);
		});
		$('#models #harley li a').mouseout(function(){
			productViewer.startAnimation();
		});
		
		
	}
	
	// New bikes - Group view
	if($('#contentWrapper').hasClass('newbikes') && $('#contentWrapper').hasClass('home')){
		$('ul#subnavigation').addClass('js');
		$('ul#subnavigation li ul').hide();
		
		$('div.range div').before('<a href="#" class="togglerange seerange"></a>');
		$('div.range ul').hide();
		$('div.range a.togglerange').click(
			function(){
				if($(this).next().find('ul').css('display') == 'none') {
					showRange($(this).parent().attr('id'));
					return false;
				} else {
					hideRange($(this).parent().attr('id'));
					return false;
				}
			}
		);
		if(document.getElementsByTagName('html')[0].className.indexOf('sIFR-active') == -1){
			$('#subnavigation h3').click(function(){
				var range = this.parentNode.className;
				
				if($('div#'+range+' ul').css('display') == 'none') {
					showRange(range);
				} else {
					var range = this.parentNode.className.split(' ')[0];
					hideRange(range);
				}		
			});
		}
	}
	
	// New bikes - Product view
	if(($('#contentWrapper').hasClass('newbikes') || $('#contentWrapper').hasClass('usedbikes')) && $('#contentWrapper').hasClass('product')){
		
		// TEST RIDE FORM
		// Hide checkbox fields if they're not checked
		if($('#trContact').attr('checked') !== true){
			$('#trContact').parent().next().hide();
		}
		
		if($('#trBrochure').attr('checked') !== true){
			$('#trBrochure').parent().next().hide();
			$('#trBrochure').parent().next().next().hide();
		}
		
		// !!! Broken in IE 6  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
		$('#trContact').click(
			function(){
				var target = $(this).parent().next();
				
				if(target.css('display') == 'none') {
					target.show();
				} else {
					target.hide();
				}
			}
		);
		
		$('#trBrochure').click(
			function(){
				var target1 = $(this).parent().next();
				var target2 = $(this).parent().next().next();
				
				if(target1.css('display') == 'none') {
					target1.show();
					target2.show();
				} else {
					target1.hide();
					target2.hide();
				}
			}
		);
		
		// NEW BIKES GALLERY
		// Swap images on click
		$('#gallery .thumbnails li a').click(
			function(){
				$('#gallery #galleryPic').attr('src', $(this).attr('href'));
				$('#gallery .enlarge').attr('href', $(this).find('img').attr('rel'));
				return false;
			}
		);

		thumbnailNavigation.init();
	}

});


var receiveUpdates = new function(){
	this.url = null
	this.submitted = false;
	this.origInputText = null;
	var self = this;
	
	this.init = function(){
		this.url = window.location;
		if(this.checkSubmitted() == true){
			this.submitted = true;		
		}
		if(this.submitted == false) {
			this.origInputText = $('#receiveUpdates input').val();
			this.hideUpdatePanel();
			$('#receiveUpdates label').click(
				function(){ self.showUpdatePanel();}
			);
			$('#receiveUpdatesEmail').mouseup(
				function(){ if($(this).val() == self.origInputText) $(this).val('');}
			);
		}
	}
	
	this.checkSubmitted = function(){
		if(this.url.toString().indexOf('receiveUpdate=true') > -1) {
			return true;
		}
	}
	
	this.hideUpdatePanel = function(){
		$('#receiveUpdates div').hide();
	}
	
	this.showUpdatePanel = function(){
		$('#receiveUpdates div').show();
	}
}

var productViewer = new function(){
	this.speed = 7000;
	this.bikes = null;
	this.intervalHandle = null;
	this.target = null;
	var self = this;
	
	this.init = function(bikes, target){
		if(bikes.length > 0) {
			this.target = target || $('.main.home #models');
			this.bikes = bikes;
			this.startAnimation();
		} else {
			return false;
		}
	}
	
	this.randomNumber = function(maxNum){
		var num = Math.random();
		num = Math.round(num * maxNum);
		return num;
	}
	
	this.displayRandomPicture = function(){
		var i = self.randomNumber(4);
		self.target[0].className = self.bikes[i];
	}
	
	this.displaySpecificPicture = function(pic){
		if(jQuery.inArray(pic, this.bikes) > -1){
			this.target[0].className = pic;
		}
	}
	
	this.changeSpeed = function(speed){
		this.speed = speed;
	}
	
	this.startAnimation = function(){
		this.intervalHandle = setInterval(self.displayRandomPicture, self.speed);
	}
	
	this.stopAnimation = function(){
		clearInterval(this.intervalHandle);
		this.intervalHandle = null;
	}
}

var thumbnailNavigation = new function(){
	this.gallery = null
	this.intervalHandler = null;
	this.thumbnailsContainer = null;
	this.thumbnails = null;
	this.thumbnailsContainerWidth = null;
	this.thumbnailsWidth = null;
	this.thumbnailsOverflow = 0;
	this.passedInit = null;
	this.maxLeftPosition = 0;
	this.speed = 50; // 20FPS
	this.movementRate = 15;
	var self = this;
	
	this.init = function(){
		var result = false;
		this.gallery = $('#gallery');
		this.thumbnailsContainer = $('#gallery .thumbnails');
		this.thumbnails = $('#gallery .thumbnails ul');
		
		if(this.gallery.length > 0 && this.thumbnailsContainer.length > 0 && this.thumbnails.length > 0) {
			this.gallery.addClass('js');
			if($('#gallery .thumbnails li').length > 4){
				// Add prev and next navigation
				$('#gallery .thumbnails ul').after('<a href="#" class="previous">Previous</a><a href="#" class="next">Next</a>');
				
				// Find width of the thumbnail container (.thumbnails), and the total width of all the list items lined up next to each other, we'll use this to set the width of the 'ul'
				this.thumbnailsContainerWidth = this.thumbnailsContainer.width();
				this.thumbnailsWidth = ($('#gallery .thumbnails ul li').length * ($('#gallery .thumbnails ul li a img').width() + parseFloat($('#gallery .thumbnails ul li').css('padding-left'))))+parseFloat(this.thumbnails.css('padding-right'));
				
				// Set the width of the 'ul'
				this.thumbnails.width(this.thumbnailsWidth);
				
				this.thumbnailsOverflow = this.thumbnailsWidth - this.thumbnailsContainerWidth;
				this.maxLeftPosition = (this.thumbnailsOverflow - this.thumbnailsOverflow) - this.thumbnailsOverflow;
				this.thumbnails.css('left', 0);
				
				// Add navigation event handlers
				$('#gallery .previous').mousedown(
					function(){
						// Start animation
						thumbnailNavigation.slideForward();
					}
				);
				$('#gallery .next').mousedown(
					function(){
						// Start animation
						thumbnailNavigation.slideBack();
					}
				);
				$('#gallery .previous, #gallery .next').mouseup(
					function(){
						// Stop animation
						thumbnailNavigation.stopAnimation();
					}
				);
				$('#gallery .previous, #gallery .next').mouseout(
					function(){
						// Stop animation
						thumbnailNavigation.stopAnimation();
					}
				);
				$('#gallery .previous, #gallery .next').click(
					function(){
						return false;
					}
				);
				
				//set up image fading for actively selected image
				$('#gallery .thumbnails ul li a').click(
					function(){
						//unset all currentThumb and set on this element
						$('.currentThumb').removeClass('currentThumb');
						$(this).addClass('currentThumb');
					}
				);
				
				//get the first image in the list and set the currentThumb class initially
				var thumbs = $('#gallery .thumbnails ul li a');
				$(thumbs[0]).addClass('currentThumb');
				
				this.passedInit = true;
				result = true;	
			}
		}
		
		return result;		
	}
	
	this.slideBack = function(){
		if(this.passedInit == true){
			this.slideBackAnimation();	
		} else {
			throw new Error('thumbnailNavigation has not been initalised');
		}
	}
	
	this.slideForward = function(){
		if(this.passedInit == true){
			this.slideForwardAnimation();	
		} else {
			throw new Error('thumbnailNavigation has not been initalised');
		}
	}
	
	this.slideForwardAnimation = function(){
		if(parseFloat(self.thumbnails.css('left')) < 0){
			self.thumbnails.css('left', (parseFloat(self.thumbnails.css('left')) + self.movementRate));
		}
		if(self.intervalHandler == null) self.intervalHandler = setInterval(self.slideForwardAnimation, self.speed);
	}
	
	this.slideBackAnimation = function(){
		
		if(parseFloat(self.thumbnails.css('left')) > self.maxLeftPosition){
			self.thumbnails.css('left', (parseFloat(self.thumbnails.css('left')) - self.movementRate));
		}
		if(self.intervalHandler == null) self.intervalHandler = setInterval(self.slideBackAnimation, self.speed);
	}
	
	this.stopAnimation = function(){
		clearInterval(this.intervalHandler);
		this.intervalHandler = null;
	}
}

var hideAllRanges = function(){
	$('div.range ul:visible').slideUp('fast');
	$('#subnavigation li ul:visible').slideUp('fast');
}

var hideRange = function(range){
	$('div#'+range+' ul').slideUp('fast');
	$('div#'+range+' a.togglerange').removeClass('hiderange').addClass('seerange');
	$('#subnavigation li.'+range+' ul').slideUp('fast', function(){
		$('#subnavigation li.'+range).removeClass('selected');
	});
	$('div#'+range+' img.rangeThumb').fadeTo('slow',1);
}

var showRange = function(range){
	$('div#'+range+' ul li').css('visibility', 'hidden');
	$('div#'+range+' ul').slideDown('fast', function(){
		$('div#'+range+' ul li').css('visibility', 'visible').hide().fadeIn('slow');
	});
	$('div#'+range+' img.rangeThumb').fadeTo('slow',0.0);
	$('div#'+range+' a.togglerange').removeClass('seerange').addClass('hiderange');
	$('#subnavigation li.'+range+' ul').slideDown('fast');
	$('#subnavigation li.'+range).addClass('selected');
}