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_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:"add_to_basket", quant:quant, id:""+id+"", psid:psid}, function(dat){
		if(dat.length >0) {
			if(dat=="false")
			{
				window.location = "/register/";
			}
			else
			{
				jQuery.get("/includes/shoppingBasket_ajax.php", {func:"refresh_basket"}, function(dat){
					if(dat.length >0) {
						jQuery('#shoppingBasket_content').hide('slow', function(){
							jQuery('#shoppingBasket_content').html(dat).css( "font-weight", "bold" ).slideDown();
							jQuery('.add_basket').attr("disabled", "");
						});
					}
				});	
			}
		}
	});	
}

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) {
					jQuery('#shoppingBasket_content').hide('slow', function(){
						jQuery('#shoppingBasket_content').html(dat).css( "font-weight", "bold" ).slideDown();
					});
				}
			});	
		}
	});	
}

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 src='/images/loading.gif'/></div>").slideDown(500, function(){
			jQuery.get("/includes/shoppingBasket_ajax.php", {func:"update_checkout_quants", ids:""+str+"", quants:""+quant_str+""}, function(dat){
				if(dat.length >0) {
					jQuery.get("/includes/shoppingBasket_ajax.php", {func:"refresh_checkout"}, function(dat){
						if(dat.length >0) {
							jQuery('div.checkout').slideUp(500, function(){
									jQuery('div.checkout').html(dat).css( "font-weight", "bold" ).slideDown(500);
							});
						}
					});	
				}
			});	
		});
	});
}

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.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_checkout"}, function(dat){
						if(dat.length >0) {
							jQuery('div.checkout').slideUp(500, function(){
									jQuery('div.checkout').html(dat).css( "font-weight", "bold" ).slideDown(500);
							});
						}
					});	
				}
			});	
		});
	});	
}

function update_cp(id)
{
	old_height =jQuery('#cp_p_list').height();
	offset = (old_height/2)-26;
	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.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.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.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"});
				
}