// JavaScript Document

$(function(){
  $('#imgRotator').rotator({ directory:'images/rotator/', controls: 'false' });		   
  sideBarBike();		   
});

function loadMilitary(){
  $.get('includes/_getMilitary.php',
		{},
		function(data){
		  $('<div>').html(data).appendTo('#centerCol');	
		});
}
function loadAbout(){
  $.get('includes/_getAbout.php',
		{},
		function(data){
		  $('<p>').html(data).insertAfter('#aboutImg'); 	
		})
}
function frontPageNews(){
  $.get('includes/_getNews.php',
		{},
		function(data){
		  $('<div>').addClass('postContainer current').appendTo('#news');
		  $('<h4>').addClass('postTitle').text(data[0].title).appendTo('.current');
		  $('<p>').addClass('postBody').html(data[0].text).appendTo('.current');
		  $('<span>').addClass('postDate').text('Posted '+ data[0].date).appendTo('.current');
		  $('.current').removeClass('current');
	  },
  'json');	
}
function loadNews(){
  $.get('includes/_getNews.php',
		{},
		function(data){
		  for(x = 0; x < data.length; x++){
		    $('<div>').addClass('postContainer current').appendTo('#centerCol');
			$('<h4>').addClass('postTitle').text(data[x].title).appendTo('.current');
			$('<p>').addClass('postBody').html(data[x].text).appendTo('.current');
  			$('<span>').addClass('postDate').text('Posted '+ data[x].date).appendTo('.current');
			$('.current').removeClass('current');
		  }
		},
  'json');
}
function loadAlbums(){
  $.get('includes/_getAlbums.php',
		{},
		function(data){
		  for(x = 0; x < data.length; x++){
			var id = data[x].id;
		    $('<li>').text(data[x].name).attr({ alt: data[x].id }).click(function(){
			  loadGallery($(this).attr('alt'));		
			}).appendTo('#albums');	  
		  }
		},
  'json');
}
function loadGallery(id){
  $.get('includes/_getGallery.php',
		{ id: id },
		function(data){
		  $('#gallery').empty();	
		  for(x = 0; x < data.length; x++){
			var path = 'images/gallery/';
			var img = data[x].img;  
		    $('<img>').addClass('thumbnail current').attr({ src: path+'thumbs/'+img }).appendTo('#gallery');
			$('.current').wrap('<a href="'+path+img+'" rel="lb-cats" />')
			$('.current').removeClass('current');
		  }
		  $("a[rel^='lb']").slimbox({overlayFadeDuration:1}, null, function(el) {
		    return (this == el) || ((this.rel.length > 8) && (this.rel == el.rel));
		  });
		},
  'json');
}
function sideBarBike(){
  $.get('includes/_randomBike.php',
		{},
		function(data){
		  $('<div>').attr({id: 'inventory'}).appendTo('#leftCol');
		  $('<img>').attr({ id: 'invImg', src: 'images/inventory/'+data.image }).appendTo('#inventory');
		  $('<span>').attr({ id: 'invBike'}).text(data.name).appendTo('#inventory');
		  $('<span>').attr({ id: 'invPrice' }).text('MSRP: $'+data.price).appendTo('#inventory');
		  $('<a>').attr({ id: 'seeInv', href: 'inventory.php'}).text('SEE ALL INVENTORY  >').appendTo('#inventory');
		},
  'json');
}
function calcPrice(price){
	var newPrice = price;
	if(!isNaN(price)){
	  $('input[type="checkbox"]:checked').each(function(){
		newPrice = (parseInt($(this).val()) + parseInt(newPrice));
	  });
	 newPrice = '$'+Math.round((newPrice*100)/100).toFixed(2);
	 $('input[name="total"]').val(newPrice);
	}
}
function loadInventory(){
  $.get('includes/_getInv.php',
		{},
		function(data){
		  for(x = 0; x < data.length; x++){
			if(!(x%3) && x > 0){
    		  $('<br />').addClass('clear').appendTo('#centerCol');	
			  $('<br />').addClass('clear').appendTo('#centerCol');	
			}
		    $('<div>').addClass('bikeContainer current').appendTo('#centerCol');
			$('<img>').addClass('bikeImg').attr({ src: 'images/inventory/'+data[x].image }).appendTo('.current')
			$('<span>').addClass('bikeName').text(data[x].name).appendTo('.current');
			$('<span>').addClass('bikePrice').text('MSRP: $'+data[x].price).appendTo('.current');
			$('<p>').addClass('bikeDescription').text(data[x].description).appendTo('.current');
			$('<span>').addClass('bikeStatus').text(data[x].status).appendTo('.current');
			$('.current').removeClass('current');
		  }
		},
  'json');
}
function getBikeSpecs(id){
  $.getJSON('http://www.suckerpunchsallys.com/includes/_getBike.php?callback=?',
	  { 'id' : id },
	  function(bike){
	  
		$('#bikeImg').attr('src','images/bikes/'+bike.image);
		$('#bikeDescription').text(bike.description);
		$('#bikeName').text(bike.name);
		
		$('#bikePrice').text(bike.price);
	  
		$('#bikeEngine').text(bike.engine);
		$('#bikeDisplacement').text(bike.displacement);
		$('#bikeBS').text(bike.boreStroke);
		$('#bikeCompression').text(bike.compression);
		$('#bikeFuel').text(bike.fuelCap);
		$('#bikeOil').text(bike.oilCap);
		$('#bikeTrans').text(bike.transmission);
		$('#bikePrimary').text(bike.primaryDrive);
		$('#bikeFinal').text(bike.finalDrive);
		$('#bikeWeight').text(bike.dryWeight);
		$('#bikeFrame').text(bike.frame);
		$('#bikeSuspension').text(bike.frontSuspension);
		$('#bikeHeight').text(bike.seatHeight);
		$('#bikeClearance').text(bike.groundClearance);
		$('#bikeWheelbase').text(bike.wheelbase);
		$('#bikeBrakeF').text(bike.frontBrake);
		$('#bikeBrakeR').text(bike.rearBrake);
		$('#bikeTireF').text(bike.frontTire);
		$('#bikeTireR').text(bike.rearTire);
		$('#bikePressure').text(bike.tirePressure);
		$('#bikeIgnition').text(bike.ignition);
		$('#bikeWarranty').text(bike.warranty);
		
		$('#bikeSpecs td:even').addClass('part')
		
		$('input[type="checkbox"]').click(function(){
		  calcPrice(bike.price);										 
		});
		
	  });
}



// Rotator Plugin


// JavaScript Document

(function($){
  $.fn.rotator = function(options){
   
	var defaults = {
	  directory : 'images/',
	  speed: 8000,
	  controls : 'true',
	  controlHeight: '55px'
	},
	settings = $.extend({}, defaults, options);
	
	function changePic(images,current,dir){ // Changes the viewer image and resets the rotation timer
	  $('#viewerImg').fadeOut(500,function(){
		rotateTimer();
		$(this).attr('src',dir+images[current]).fadeIn();
	  });
	}
	function rotateTimer(){  // Sets the image rotation countdown
	  $('#arrowRight').stop();
	  $('#arrowRight').animate({ opacity: 1.0 },settings.speed,function(){
		$(this).click();
	  });
	}
	this.each(function(){
	  var $this = $(this);
	  var directory = settings.directory;
	  $.get('includes/_getHomePgImgs.php',
		{ dir : directory },
		function(data){
		  var images = Array();
		  for(x = 0; x < data.length; x++){
			images.push(data[x]);
		  }
		  
		  // Creates image viewer and popup controls
		  $this.css({ 'position':'relative', 'overflow':'hidden' });
		  $('<img>').attr({ src:directory+images[0], id: 'viewerImg' }).appendTo($this);			
		  $('<div>').attr({ id: 'viewerControls'}).css({
						'height': settings.controlHeight,
						 'width' :'100%',
						 'position' : 'absolute',
						 'bottom' : '-'+settings.controlHeight,
						 'cursor' : 'pointer',
						 'background-color' : '#000',
						 'opacity' : '0.6'					 
					   })
		  .appendTo($this);
		  $('<img>').attr({ id:'arrowRight' }).appendTo('#viewerControls');
		  $('<img>').attr({ id:'arrowLeft' }).appendTo('#viewerControls');
		  $this.hover(function(){
			$('#viewerControls').stop();
			$('#viewerControls').animate({
			  'bottom':'0px'						
			}); 
		  }, function(){
			$('#viewerControls').stop();
			$('#viewerControls').animate({
			  'bottom': '-'+settings.controlHeight						
			});  
		  });
		  
		  
		  // Preloads images and attaches 'click' events to the 'next' and 'previous' arrows in the popup controls
		  var current = 0;
		  var length = images.length;
		  $('<img>').attr('src',directory+images[current+1]);
		  $('<img>').attr('src',directory+images[length-1]);
		  $('#arrowRight').unbind().click(function(){
			current = (current+1 < length) ? current+1:0;
			changePic(images,current,directory);
			$('<img>').attr('src',directory+images[current+1]);
		  });
		  $('#arrowLeft').unbind().click(function(){
			current = (current-1 >= 0) ? current-1:length-1;
			changePic(images,current,directory);
			$('<img>').attr('src',directory+images[current-1]);
		  });
		  
		  if(settings.controls != 'true'){
			$('#viewerControls').css('display','none');  
		  }
		  
		  // Starts the image switch timer after the viewer is loaded
		  rotateTimer();
		},
	  'json');
							 
	  });
	  return this; 
    }		 
})(jQuery);
