$(document).ready(function() {
//  init();

$(".jsadd").show();

setupbasket();

slideshow();

});

//basket

function setupbasket() {
 $('button:contains("Update")').bind("click",update);
};

function update() {
 var quantityarray = $('select[name*="quantity"]');
 var pricearray = $('td.ppi span');
 var quantity = 0;
 var ppi = 0;
 var itemtotal = 0;
 var subtotal = 0;
 var itemtotalarray = $('td.total span');
 for (var i=0; i<quantityarray.length; i++){ 
   quantity = quantityarray[i].value;
   ppi = pricearray[i].innerHTML;
   itemtotal = ppi*quantity;
   subtotal += itemtotal;
   itemtotal = itemtotal.toFixed(2);
   itemtotalarray[i].innerHTML = itemtotal;
 }
   $('td.total span.subtotal').html(subtotal.toFixed(2));
      
  return false;
};



//slideshow

function slideshow() {
	var slidearray = $('ul#slides img');
	var i = 1;
	var j = 0;
	var k = 0;
	var intervalTime = 5000;
	var intervalID; //  = setInterval (control, intervalTime);
 	var slideshowrun = true;

	if (slideshowrun == true) {
	 	intervalID = setInterval (control, intervalTime);
	}
 
	// set all slides except first to have opacity of 0
	$(slidearray).each(function() {
		$(slidearray[i]).css('opacity',0);
		i++;
	})
 
	// add event listeners to controls
	$('#slideshow #buttons a').each(function() {
		$(this).bind('click', control);
	})

	function control() {
		var action = this.className || '';
		var numimages = slidearray.length;
		
		if (action.indexOf('pause') != -1) {
			if (slideshowrun == true) {
				clearInterval(intervalID);
				slideshowrun = false;
				$('#slideshow #buttons a.pause').removeClass('playing');
			} else {
			 	intervalID = setInterval (control, intervalTime);
				slideshowrun = true;
				$('#slideshow #buttons a.pause').addClass('playing');
			}

			console.log(slideshowrun);
		} else {
			// fade slides in/out
			if (action == 'prev') {
				if (j == -1) {
					j = numimages-1; 
					k = j - 1;
				} else if (j == 0) {
					k = numimages-1; 
				} else {
					k = j - 1;
				}
			} else {
				if (j == numimages) {
					j = 0;
					k = j + 1;
				} else if (j == numimages-1) {
					k = 0; 
				} else {
					k = j + 1;
				}
			}

	
	
			$(slidearray[j]).animate({opacity: 0},2000);
			$(slidearray[k]).animate({opacity: 1},2000);
	
			if (action == 'prev') {
			j--; } else {
			j++; }
		} 
	}
}








function init() {
  checkboxFormat();
};

function checkboxFormat() {
  var checkBoxArray = $(".searchblock input[type=checkbox]"); 

  checkBoxArray.each (function() {
    $(this).css({
    /* 'left':'-999em', 
      'position': 'absolute' */
    }); 
    
    $(this).bind('change', function() {
      if ($(this).attr('checked') == true) { 
	$(this).siblings('label').addClass('selected');
      } else {
	$(this).siblings('label').removeClass('selected');
      }
    }, false); 
  });
};



