// JavaScript Document
$(document).ready(function(){

	$(".popup").fancybox();

});


// onclick function for the red Xs
function resetQuantity(id)
{
	$("#"+id).val('0');	
	updateTotals();
}

/*
	Calculate total num pieces, total price and the total m2 of the products ordering
*/
function updateTotals()
{
	var totalpieces = 0;
	var totalprice = 0;
	var totalm2 = 0;
	
	var products = new Array();
	var categoryid;
	var i = 0;
		
	$("#addtobasket input.text").each(function() {
	
		var id = $(this).attr('id');
		if(id == 'productid' || id == 'atb_submit')
		{
			return;
		} else if(id == 'catid')
		{
			categoryid = $(this).val();
			return;
		} else if(id == 'totalm2')
		{
			totalm2 = $(this).val();
			return;			
		}
		
		if((isNaN($(this).val()) === false) && parseInt($(this).val()) > 0)
		{
			var quantity = parseInt($(this).val());			
			products[i] = id+'-'+quantity;

			/* total pieces */
			var p = id.split('-');
			if(p['0'] == 'p')
			{
				totalpieces += parseInt(($("#"+id+'_pieces').html() * quantity));
			} else
			{			
				totalpieces += quantity;
			}			
			i += 1;
			
			$(this).siblings('.sign').fadeIn();
		} else
		{
			$(this).siblings('.sign').fadeOut();
		}
	});
	var prodString = products.join('_');
	$("#totalpieces").html(totalpieces);	
	
	$.ajax({
		type: "post",
		url: "/ajax/updateTotals.php",
		cache: false,
		data: "&totalm2="+totalm2+"&categoryid="+categoryid+"&products="+prodString,
		success:function(data)
				{
					var details = data.split('/');
					var productsReturn = details['0'].split('_');
									
					$("#totalm2Display").html(details['1']+'m');
					
					if(categoryid == 2)
					{					
						$("#totalm2Send").val(details['1']);					
					}
					$("#totalprice").html('&pound;'+details['2']);
					$("#pricesize").val(details['3']);
					
					$("#pricepm2").html('&pound;'+details['4']+' / m');
					
					for(i=0;i<productsReturn.length;i++)
					{
						var prod = productsReturn[i].split('-');
						$('#'+prod['0']+'-'+prod['1']+'-price').html('&pound;'+prod['2']);
					}					
				}
	});
	
	if(totalpieces > 0)
	{
		$("#atb_submit").fadeIn();
	} else
	{
		$("#atb_submit").fadeOut();
	}
}

/*
	hide / show the add to basket button
*/
function showBasketButton()
{
	var totalpieces = 0;
	$("#addtobasket input.text").each(function() {

		if((isNaN($(this).val()) === false) && parseInt($(this).val()) > 0)
		{		
			var quantity = parseInt($(this).val());		
			if(quantity > 0)
			{
				totalpieces += quantity;
				$(this).siblings('.sign').fadeIn();			
			} else
			{
				$(this).siblings('.sign').fadeOut();
			}
		}
	});

	if(totalpieces > 0)
	{
		$("#atb_submit").fadeIn();
	} else
	{
		$("#atb_submit").fadeOut();
	}
}

/*
	Javascript round to decimal places
*/
function roundNumber(num, dec) 
{
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
}

/*
	Confirm an action
*/
function confirmAction(msg)
{
	if(confirm(msg))
	{
		return true;
	} else
	{
		return false;
	}
}

function clearBox(input) {
	if(input.value=input.defaultValue) {
		input.value = '';	
	}
}

function resetBox(input) {
	if(!input.value) {
		input.value = input.defaultValue;	
	}
}

$(document).ready(function()
{
	$('div#banner').fadeGallery({
		slideElements:'ul > li',
		btnNext:'',
		btnPrev:'',
		pauseOnHover:false,
		autoRotation:true,
		switchTime:2500,
		duration:650,
		event:'click'
	});
});

// slideshow plugin
jQuery.fn.fadeGallery = function(_options){
	var _options = jQuery.extend({
		slideElements:'div.slideset > div',
		pagerLinks:'div.pager a',
		btnNext:'a.next',
		btnPrev:'a.prev',
		btnPlayPause:'a.play-pause',
		pausedClass:'slideshow-paused',
		playClass:'slideshow-playing',
		activeClass:'active',
		currentNum:false,
		allNum:false,
		pauseOnHover:true,
		autoRotation:false,
		autoHeight:false,
		switchTime:5000,
		duration:650,
		event:'click'
	},_options);

	return this.each(function(){
		// gallery options
		var _this = jQuery(this);
		var _slides = jQuery(_options.slideElements, _this);
		var _pagerLinks = jQuery(_options.pagerLinks, _this);
		var _btnPrev = jQuery(_options.btnPrev, _this);
		var _btnNext = jQuery(_options.btnNext, _this);
		var _btnPlayPause = jQuery(_options.btnPlayPause, _this);
		var _pauseOnHover = _options.pauseOnHover;
		var _autoRotation = _options.autoRotation;
		var _activeClass = _options.activeClass;
		var _pausedClass = _options.pausedClass;
		var _playClass = _options.playClass;
		var _autoHeight = _options.autoHeight;
		var _duration = _options.duration;
		var _switchTime = _options.switchTime;
		var _controlEvent = _options.event;
		var _currentNum = (_options.currentNum ? jQuery(_options.currentNum, _this) : false);
		var _allNum = (_options.allNum ? jQuery(_options.allNum, _this) : false);

		// gallery init
		var _prevIndex = 0;
		var _currentIndex = 0;
		var _slideCount = _slides.length;
		var _timer;
		if(!_slideCount) return;
		_slides.hide().eq(_currentIndex).show();
		if(_autoRotation) _this.removeClass(_pausedClass).addClass(_playClass);
		else _this.removeClass(_playClass).addClass(_pausedClass);

		// gallery control
		if(_btnPrev.length) {
			_btnPrev.bind(_controlEvent,function(){
				prevSlide();
				return false;
			});
		}
		if(_btnNext.length) {
			_btnNext.bind(_controlEvent,function(){
				nextSlide();
				return false;
			});
		}
		if(_pagerLinks.length) {
			_pagerLinks.each(function(_ind){
				jQuery(this).bind(_controlEvent,function(){
					if(_currentIndex != _ind) {
						_prevIndex = _currentIndex;
						_currentIndex = _ind;
						switchSlide();
					}
					return false;
				});
			});
		}

		// play pause section
		if(_btnPlayPause.length) {
			_btnPlayPause.bind(_controlEvent,function(){
				if(_this.hasClass(_pausedClass)) {
					_this.removeClass(_pausedClass).addClass(_playClass);
					_autoRotation = true;
					autoSlide();
				} else {
					if(_timer) clearTimeout(_timer);
					_this.removeClass(_playClass).addClass(_pausedClass);
				}
				return false;
			});
		}

		// gallery animation
		function prevSlide() {
			_prevIndex = _currentIndex;
			if(_currentIndex > 0) _currentIndex--;
			else _currentIndex = _slideCount-1;
			switchSlide();
		}
		function nextSlide() {
			_prevIndex = _currentIndex;
			if(_currentIndex < _slideCount-1) _currentIndex++;
			else _currentIndex = 0;
			switchSlide();
		}
		function refreshStatus() {
			if(_pagerLinks.length) _pagerLinks.removeClass(_activeClass).eq(_currentIndex).addClass(_activeClass);
			if(_currentNum) _currentNum.text(_currentIndex+1);
			if(_allNum) _allNum.text(_slideCount);
			_slides.eq(_prevIndex).removeClass(_activeClass);
			_slides.eq(_currentIndex).addClass(_activeClass);
		}
		function switchSlide() {
			_slides.eq(_prevIndex).fadeOut(_duration);
			_slides.eq(_currentIndex).fadeIn(_duration);
			refreshStatus();
			autoSlide();
		}

		// autoslide function
		function autoSlide() {
			if(!_autoRotation) return;
			if(_timer) clearTimeout(_timer);
			_timer = setTimeout(nextSlide,_switchTime+_duration);
		}
		if(_pauseOnHover) {
			_this.hover(function(){
				_this.removeClass(_playClass).addClass(_pausedClass);
				if(_timer) clearTimeout(_timer);
			},function(){
				_this.removeClass(_pausedClass).addClass(_playClass);
				autoSlide();
			});
		}
		refreshStatus();
		autoSlide();
	});
}



