// Slider
var productSlider = {
	
	autoplay : false,
	interval : null,
	max : 0,
	current : 0,
	
	init : function( autoplay ) {
		this.max = jQuery( '#productsSlider img' ).length;
		/*
		for( i = 0; i < this.max; i++ ) {
			jQuery( '#numbers' ).append( '<span class="num" id="num_' + ( i+1 ) + '"><a href="javascript:;" onclick="headerSlider.goto( ' + i + ' )">' + ( i+1 ) + '</a></span>' );
			if( i != this.max-1 ) { jQuery( '#numbers' ).append( ' &bull; ' ); }
		}
		
		jQuery( '#num_1 a' ).addClass( 'active' );
		*/
		if( autoplay ) {
			this.autoplay = true;
			this.play();
		}
	},
	
	next : function() {
		if( this.current >= this.max-1 ) { this.current = -1; }
		this.current++;
		jQuery( '.bullit' ).removeClass( 'active' );
		jQuery( '#product_' + ( this.current ) ).addClass( 'active' );
		jQuery( '#productsSlider' ).scrollTo( ( 930 * this.current ) + 'px', 800 );
	},
	
	prev : function() {
		if(this.current > 0) {
			this.current--;
		}
		else {
			this.current = this.max-1;
		}
		
		jQuery( '.bullit' ).removeClass( 'active' );
		jQuery( '#product_' + ( this.current ) ).addClass( 'active' );
		jQuery( '#productsSlider' ).scrollTo( ( 930 * this.current ) + 'px', 800 );
	},
	
	goto : function( num) {
		this.current = num-1;
		if( this.autoplay ) {
			clearInterval( this.interval );
			this.next();
		}
		else {
			this.next();
		}		
	},
	
	play : function() {
		this.interval = setInterval( "productSlider.next()", 3000 );
	},
	
	stopAutoplay : function() {
		clearInterval(this.interval);
	}
	
};


function IsNumeric(strString)
   //  check for valid numeric strings	
   {
   var strValidChars = "0123456789.-";
   var strChar;
   var blnResult = true;

   if (strString.length == 0) return false;

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
   return blnResult;
   }

var currentData;
function checkQuantity(product, color) {
	$.ajax({
		type: 'POST',
		data: {
			idProduct: product,
			idColor: color
		},
		url: SITE_PATH + 'json/quantity/',
		dataType: 'json',
		success: function( data ) {
			currentData = data;
			sizeSelector();
		}	
	});
}

function sizeSelector() {
	$('#sizeRow').remove();
	var selectBox = $('<select/>');
	selectBox.attr('name', 'size');
	$(currentData.size).each(function(i,el){
		if(parseInt(currentData.quantity[i]) > 0) {
			var opt = $('<option/>');
			opt.attr('value', el.data.id);
			opt.text(el.data.label);
			selectBox.append(opt);
		}
	});
	
	var row = $('<tr/>').attr('id','sizeRow');
	row.append($('<th/>').html('Maat'));
	row.append($('<td/>').append(selectBox));
	$('#colorRow').after(row);
	
	if($('#quantity').length == 0){
		$(row).after('<tr><th>Aantal</th><td><input type="text" id="quantity" style="width:35px" maxlength="2" value="" name="quantity"></td></tr>');
	}
	$('#quantity').keyup(function(evt){
		if( $('#quantity').val().length == 0 ) {
			$('#addToCart').attr( 'disabled', 'disabled' );
			$('#addToCart').animate({'opacity':'.3'});
			return;
		}
		
		if( IsNumeric($('#quantity').val()) == false ) {
			$('#quantity').val('');
			return;
		}
		
		// Check quantity
		var quantity = 0;
		$(currentData.size).each(function(i,el){
			if(el.data.id==$('#sizeRow').find('select').val()){
				quantity = currentData.quantity[i];
			}
		});

		if( $('#quantity').val() <= quantity ) {
			$('#addToCart').animate({'opacity':'1'});
			$('#addToCart').removeAttr('disabled');
		}
		else {
			$('#addToCart').animate({'opacity':'.3'});
			$('#addToCart').attr('disabled', 'disabled');
			$('#status').html('Er zijn nog maximaal <strong>' + quantity + '</strong> artikelen in deze maat beschikbaar');
			$('#status').fadeIn();
		}
	});
}

$(function() {
	
	$('#color').change(function(evt){
		
		if(!IsNumeric($('#color').val())) return false;
		checkQuantity($('#idProduct').val(), $('#color').val());
		$('#zoom').remove();

		$('#picture').html('<a id="zoom" href="' + colors[$('#color').val()] +'_large.png"><img class="small" src="' +colors[$('#color').val()]+ '.png" alt="Picture"></a>');
		$('#zoom').jqzoom({  
	        zoomType: 'innerzoom' ,
	        preloadText: 'Bezig met laden'
	    });
	});
	
	$('.product').mouseover(function(){
		$(this).css({ 'box-shadow' : '0px 0px 5px #ffd300' });
	});
	
	$('.product').mouseout(function(){
		$(this).removeAttr('style');
	});
});

function updateAddress( el ) {
	if( $(el).attr('checked') ) {
		$( "#invoiceAddress" ).slideUp();
	}else {
		$("#invoiceAddress").slideDown();
	}
}
