var currentProduct = 0, numProducts = 0;

$.fx.interval = 20;

function setProduct(n)
{	
	$('#product_info>div:nth-child('+(currentProduct+1)+')').fadeOut(500);
	currentProduct = n>=numProducts ? 0 : (n<0 ? numProducts-1 : n);

	if ($.browser.safari || $.browser.chrome){
		$('#slide').css('-webkit-transform','translate3d(-'+currentProduct*960+'px, 0, 0)');
	} else {
		console.log('current: ' + currentProduct + ', total: '+numProducts );
			$('#slide').animate({left:'-'+currentProduct*960}, 500);
	}	
	$('#product_info>div:nth-child('+(currentProduct+1)+')').fadeIn(500, function(){
		var product = $(this).children('h2').text();
		_gaq.push(['_trackEvent', 'Product', 'Viewed', product]);
	});
	$('#thumb_line').animate({left:currentProduct*24}, 400);
	
	if (!$.browser.msie){
		$('title').html('brodie neill | products | '+$('#product_info>div:nth-child('+(currentProduct+1)+')>h2').html());
	}
	(currentProduct == 0) ?	$('#l_arrow, #l_a_wrap').hide() : $('#l_arrow, #l_a_wrap').show();
	(currentProduct == numProducts - 1) ?	$('#r_arrow, #r_a_wrap').hide() : $('#r_arrow, #r_a_wrap').show();
}

$(document).ready(function (){
	var img = new Image();
	$(img)
		.load(function(){
			$('#fullBG').hide();

			$('#fullBG').append(this);

			$("#fullBG img").fullBg();
			
			$('#fullBG').fadeIn(1000);
			
	})
	.error(function(){

	}).attr('src', 'img/bg.jpg');

	$('#l_arrow, #l_a_wrap').hide()
	numProducts = $('#slide>div').length;
	
	$('#product_info>div:first-child').show();
		
	var buttonArea = 'a_wrap';	// element that triggers left/right button clicks and rollovers
	
	if (document.ontouchmove===null) {
		buttonArea = 'arrow';
		var prevX = 0;
		var touchX = 0;
		var canSlide = true;
		$('#slide_wrap')
			.bind('touchstart', function (e) { prevX = touchX = e.originalEvent.touches[0].pageX; })
			.bind('touchend', function (e) { canSlide = true; })
			.bind('touchmove', function (e) {
				e.preventDefault();
				if (canSlide) {
					if (touchX-prevX < -10) {
						if(currentProduct !== numProducts - 1){
							setProduct(currentProduct+1);
							canSlide = false;
						}
					}
					else if (touchX-prevX > 10) {
						if(currentProduct !== 0){
							setProduct(currentProduct-1);
							canSlide = false;
						}
					}
				}
			});
		
		document.body.ontouchmove = function (e) {
			prevX = touchX;
			touchX = e.touches[0].pageX;
			$('#debug').html(touchX-prevX);
		};
	}
	$('#l_'+buttonArea)
		.mouseover( function () { $('#l_arrow').addClass('over'); })
		.mouseout( function () { $('#l_arrow').removeClass('over'); })
		.click( function () { setProduct(currentProduct-1); });
	$('#r_'+buttonArea)
		.mouseover( function () { $('#r_arrow').addClass('over'); })
		.mouseout( function () { $('#r_arrow').removeClass('over'); })
		.click( function () { setProduct(currentProduct+1); });
	
	$('#thumbs>img').click(function () {
		for (var i=0; i<numProducts; i++)
			if ($('#thumbs>img:nth-child('+(i+1)+')').attr('src')===$(this).attr('src'))
				break;
		setProduct(i);
	});
	
	if ($.browser.safari || $.browser.chrome)
		$('#slide').css('-webkit-transition','all 0.5s');
	
	if ($.browser.msie && $.browser.version==='6.0') {
		$('#product_info>div').hide();
		$('#product_info>div:first-child').show();
		$('#l_arrow, #r_arrow').css('background-image', 'url("img/arrows.gif")');
	}
	
	// Validate contact form
	$('#content form').validate({
		rules: {
		
			name: 'required',
			email: {
				required: true,
				email: true
			},
			message: 'required'
		
		}
	
	});
	
	$('#newsWrapper').jScrollPane({showArrows: true});
	$('.external').attr('target', '_blank');
	
	// Galleries
	
	$('.viewGallery').click(function(e){
		e.preventDefault();
		ref = this.rel;
		
		//$('#' + ref).children('a:first').click();
		$.ajax({
			data: 'gallery=' + ref,
			type: 'POST',
			dataType: 'HTML',
			url: 'index.php/webservice/lightbox',
			success: function(data){
				$('.lightbox').html(data);
				initLightbox();
			},
			complete: function(){
				$('.lightbox a:first').click();
			
			}
		});
			
	});

});

function initLightbox(){

	$('.lightbox a').lightBox({
		imageBtnPrev: 'img/btn-prev.gif',
		imageBtnNext: 'img/btn-next.gif',
		imageBtnClose: 'img/btn-close.gif',
		imageLoading: 'img/ico-loading.gif',
		imageBlank: 'img/blank.gif'
	});


}
