function implode( glue, pieces ) { 
    return ( ( pieces instanceof Array ) ? pieces.join ( glue ) : pieces );
}

function add_to_basket(id)
{                                      
    quant = $("#quant_" + id).val();
    psid = $("#psid_" + id).attr("value");
	jQuery('.add_basket').attr("disabled", "true");
	jQuery('#shoppingBasket').show();
	jQuery('#shoppingBasket_content').slideUp(300, function(){
		jQuery('#shoppingBasket_content').html("<div align='center'><img src='/images/loading.gif'/></div>").css( "font-weight", "bold" ).show('slow');
	});
	jQuery.get("/index.php", {type:"add_to_basket", n:quant, id:""+id+"", psid:psid,region:"ajax"}, function(dat){
		if(dat.length >0) {
			if(dat=="false")
			{
				window.location = "/";
			}
			else
			{
				if(dat=="nsize")
				{
					alert("You must select a size for this product");
				}
				jQuery.get("/index.php", {type:"refresh_basket",region:"ajax"}, function(dat){
					if(dat.length >0) {
						setTimeout(function(){
						jQuery('#shoppingBasket_content').slideUp(300, function(){
							jQuery('#shoppingBasket_content').html(dat).css( "font-weight", "bold" );
							jQuery('.add_basket').attr("disabled", "");
							jQuery('#shoppingBasket_content').slideDown(300);
						});},500);
					}
				});	
			}
		}
	});	
}

function refresh_basket()
{
	jQuery('#shoppingBasket_content').slideUp(300, function(){
		jQuery('#shoppingBasket_content').html("<div align='center'><img src='/images/loading.gif'/></div>").css( "font-weight", "bold" ).show('slow');
		
		jQuery.get("/index.php", {func:"refresh_basket"}, function(dat){
			if(dat.length >0) {
				setTimeout(function(){
				jQuery('#shoppingBasket_content').slideUp(300, function(){
					jQuery('#shoppingBasket_content').html(dat).css( "font-weight", "bold" );
					jQuery('.add_basket').attr("disabled", "");
					jQuery('#shoppingBasket_content').slideDown(300);
				});},500);
			}
		});
	});	
}

function delete_basket_item(id)
{
	quant = $("#quant_" + id).val();
	jQuery('#shoppingBasket_content').slideUp(300, function(){
		jQuery('#shoppingBasket_content').html("<div align='center'><img src='/images/loading.gif'/></div>").css( "font-weight", "bold" ).show('slow');
	});
	jQuery.get("/includes/shoppingBasket_ajax.php", {func:"delete_basket_item", id:""+id+""}, function(dat){
		if(dat.length >0) {
			jQuery.get("/includes/shoppingBasket_ajax.php", {func:"refresh_basket"}, function(dat){
				if(dat.length >0) {
					setTimeout(function(){
					jQuery('#shoppingBasket_content').slideUp(300, function(){
						jQuery('#shoppingBasket_content').html(dat).css( "font-weight", "bold" );
						jQuery('#shoppingBasket_content').slideDown();
					});},500);
				}
			});	
		}
	});	
}

function delete_cd(id)
{
	jQuery('div.checkout').slideUp(500, function(){
		jQuery('div.checkout').html("<div align='center'><img src='/images/loading.gif'/></div>").slideDown(500, function(){
			jQuery('div.checkout').stop();
			jQuery.get("/index.php", {type:"delete_cd", s:""+id+"",region:"ajax"}, function(dat){
				if(dat.length >0) {
					jQuery.get("/index.php", {type:"refresh_checkout",region:"ajax"}, function(dat){
						if(dat.length >0) {
							jQuery('div.checkout').slideUp(500, function(){
									jQuery('div.checkout').html(dat).css( "font-weight", "bold" ).slideDown(500,function(){
										jQuery('div.checkout').stop();
									});
									refresh_basket();
							});
						}
					});	
				}
			});	
		});
	});	
}

function refresh_checkout_tab(str)
{
	var quant=[];
	ids=str.split(",");
	for(i=0;i<ids.length;i++) {
		quant.push($("#quant_" + ids[i]).val());
	}
	quant_str = implode(",",quant);
	jQuery('div.checkout').slideUp(500, function(){
		jQuery('div.checkout').html("<div align='center'><img border='0' src='/images/loading.gif'/></div>").slideDown(500, function(){
			jQuery('div.checkout').stop();
			jQuery.get("/index.php", {type:"update_checkout_quants", s:""+str+"|"+quant_str+"",region:"ajax"}, function(dat){
				if(dat.length >0) {
					jQuery.get("/index.php", {type:"refresh_checkout",region:"ajax"}, function(dat){
						if(dat.length >0) {
							jQuery('div.checkout').slideUp(500, function(){
									jQuery('div.checkout').html(dat).css( "font-weight", "bold" ).slideDown(500,function(){
										jQuery('div.checkout').stop();
									});
									refresh_basket();
							});
						}
					});	
				}
			});	
		});
	});
} 

function delete_checkout_item(id)
{
	quant = $("#quant_" + id).val();
	jQuery('div.checkout').slideUp(500, function(){
		jQuery('div.checkout').html("<div align='center'><img src='/images/loading.gif'/></div>").slideDown(500, function(){
			jQuery('div.checkout').stop();
			jQuery.get("/index.php", {type:"delete_basket_item", id:""+id+"",region:"ajax"}, function(dat){
				if(dat.length >0) {
					jQuery.get("/index.php", {type:"refresh_checkout",region:"ajax"}, function(dat){
						if(dat.length >0) {
							jQuery('div.checkout').slideUp(500, function(){
									jQuery('div.checkout').html(dat).css( "font-weight", "bold" ).slideDown(500,function(){
										jQuery('div.checkout').stop();
									});
									refresh_basket();
							});
						}
					});	
				}
			});	
		});
	});	
}

function update_cp(id)
{
	old_height =jQuery('#cp_p_list').height();
	offset = (old_height/2)-26;
	id=$(id).attr("value");
	if(jQuery('#cg_img_'+id).hasClass("selected"))
	{
		jQuery('#cg_img_'+id).removeClass("selected");
	}
	else 
	{
		jQuery('#cg_cp ul li').removeClass("selected");
		jQuery('#cg_img_'+id).addClass("selected");
	}
	jQuery('#cp_p_list #cont').fadeOut(300, function(){
		jQuery('#cp_p_list #cont').css("height",old_height).html("<div class='loader' align='center'><img style='margin-top:"+offset+"px;' src='/images/loading-white.gif'/></div>").fadeIn(300, function(){
			jQuery.get("/includes/colourPicker_ajax.php", {func:"upd_cp_list",id:id}, function(dat){
				jQuery.get("/includes/colourPicker_ajax.php", {func:"get_cp_list",id:id}, function(dat){
					if(dat.length >0) {
						jQuery('#cp_p_list #cont').fadeOut(300, function(){
								jQuery('#cp_p_list #cont').html(dat).fadeIn(300);
						});
					}
				});	
			});	
		});
	});
}

function update_cp_page(id,p){
	old_height =jQuery('#cp_p_list').height();
	offset = (old_height/2)-26;
	jQuery('#cp_p_list #cont').fadeOut(300, function(){
		jQuery('#cp_p_list #cont').css("height",old_height).html("<div class='loader' align='center'><img style='margin-top:"+offset+"px;' src='/images/loading-white.gif'/></div>").fadeIn(300, function(){
			jQuery.get("/includes/colourPicker_ajax.php", {func:"upd_cp_list",id:id,page:p}, function(dat){
				jQuery.get("/includes/colourPicker_ajax.php", {func:"get_cp_list",id:id,page:p}, function(dat){
					if(dat.length >0) {
						jQuery('#cp_p_list #cont').fadeOut(300, function(){
								jQuery('#cp_p_list #cont').html(dat).fadeIn(300);
						});
					}
				});	
			});	
		});
	});
}

function update_cp_p(id){
	jQuery('#cp_p_img #cont').fadeOut(300, function(){
		jQuery('#cp_p_img #cont').html("<div align='center'><img style='margin-top:100px;' src='/images/loading-white.gif'/></div>").fadeIn(300, function(){
			jQuery.get("/includes/colourPicker_ajax.php", {func:"get_product_detail",id:id}, function(dat){
				if(dat.length >0) {
					jQuery('#cp_p_img #cont').fadeOut(300, function(){
							jQuery('#cp_p_img #cont').html(dat).fadeIn(300);
					});
				}
			});	
		});
	});
}

function clear_cp(){
	jQuery.get("/includes/colourPicker_ajax.php", {func:"clear_cp"});		
}
jQuery(document).ready(function(){  
	if(jQuery('#input-exp-mth').length > 0){
		jQuery('#input-exp-mth,#input-start-mth,#input-exp-yr,#input-start-yr').focus(function(){
			var val=$(this).attr("value");
			if(val=='mm' || val=='yy'){
				$(this).attr("value","");
			}
		});
		jQuery('#input-exp-mth,#input-start-mth').blur(function(){
			var val=$(this).attr("value");
			if(val==''){
				$(this).attr("value","mm");
			}
		});
		jQuery('#input-exp-yr,#input-start-yr').blur(function(){
			var val=$(this).attr("value");
			if(val==''){
				$(this).attr("value","yy");
			}
		});
	}
	$('#pay_opt').change(function() {
		var amount=$(this).val();
		if(amount==''){
			jQuery('#aa_row').slideUp(300, function(){
				jQuery('#aa').attr("value",'');
			});
			
		}
		else{
			jQuery('#aa_row').slideUp(300, function(){
				jQuery('#aa').attr("value",amount);
				jQuery('#aa_row').slideDown(300);
			});
		}
	});
});
