function added_to_bag(id){
	$('#button_'+id).attr({src : "/images/en/buttons/stl_added.gif"});
}

function add_to_bag(id){
	$('#button_'+id).attr({src : "/downloads/Image/shop_the_look/buy_now.gif"});
}

function fade_ajax_notifier(id){
	$('#ajax_notifier_'+id).fadeOut("slow");
    window.setTimeout("fade_view_details('"+id+"')", 500);
}

function fade_view_details(id){
    $("#viewdetails_"+id).fadeIn("slow");
}

function update_basket_total(){
    $.ajax({
       type: "POST",
       url: "/shopping_bag/",
       data: "&ajax=1&act=update_basket_count",
       success: function(msg){
                    $('#hd_basket_total').replaceWith("<span id='hd_basket_total'>"+msg+"</span>");   
                }        
    });
}

$(document).ready(function(){
    
    $(".prod_info_header").mouseover(
        function(){
            $(this).css("cursor","pointer");
        }
    )
    
    $("#coni").mouseover(
        function(){
            $(this).css("cursor","pointer");
        }
    )
    
    // Handle the hide/show of product details
    $(".prod_info_panels").hide();
    $(".prod_info_tabs").click(
        function(){
            var idClicked = $(this).attr("id");
            var details = idClicked.split("_");
            var tabId = "#details_"+details[1]+"_"+details[2];
            $(tabId).toggle();
        }
    );
      
    //Handle AJAX Basket Add Process
    $(".cart").submit(
        function(){
        
            var idClicked = $(this).attr("id");	
            var idClicked_frags = idClicked.split('_');
            var idClicked = idClicked_frags[1]+"_"+idClicked_frags[2];
            
            //Setup the id's for values
            //There are multiple forms posting data need to know which form we're dealing with
            var name_prod_desc_id = "#prod_desc_id_"+idClicked;
            var name_quantity = "#quantity_"+idClicked;
            var name_act = "#act_"+idClicked;
            
            //Get the vars
            var prod_desc_id = $(name_prod_desc_id).attr("value");
            var quantity = $(name_quantity).attr("value");
            var act = $(name_act).attr("value");
            
            //Set id's for notifications
            var target_id = "#add_to_basket"+idClicked;
            var notifier_id = "#ajax_notifier_"+idClicked;
	
            
            //Some debugging alerts, these should show the submitted values form the form,
            //if they don't then your problem is ^ up there somewhere
            //alert(idClicked+'submitted');
            //alert("act: "+act)
            //alert("quantity:"+quantity);
            //alert("prod_desc_id: "+prod_desc_id);
                        
            if(prod_desc_id == 0){
                alert("Please select a size");
            }else{						
                $.ajax({
                       type: "POST",
                       url: "/shopping_bag/",
                       data: "prod_desc_id="+prod_desc_id+"&quantity="+quantity+"&act="+act+"&ajax=1",
                       success: function(msg){
                                    if(msg == ""){
                                        // There has been an error of some sort.
                                        $(notifier_id).replaceWith('<div id="ajax_notifier">There has been an error adding this item to your basket</div>');
                                        added_to_bag(idClicked);
                                        window.setTimeout("add_to_bag('"+idClicked+"')", 5000);
                                    }else{
                                        // There is an message to display
                                        added_to_bag(idClicked);
                                        update_basket_total();
                                        minibasketShowAfterUpdate(msg);
                                        
                                        
                                        //Handle continue shopping button
                                        $(".continueBtn").mouseover(
                                            function(){
                                                $(this).css("cursor","pointer");
                                            }
                                        );
                                        
                                        $(".continueBtn").click(
                                            function(){
                                                var idClicked = $(this).attr("id");	
                                                var idClicked_frags = idClicked.split('_');
                                                var idClicked = idClicked_frags[1]+"_"+idClicked_frags[2];
                                                add_to_bag(idClicked);
                                                fade_ajax_notifier(idClicked);
                                            }
                                        );
                                        
                                        window.setTimeout("add_to_bag('"+idClicked+"')", 7000);
                                        window.setTimeout("fade_ajax_notifier('"+idClicked+"')", 7000);
                                    }
                        }
                });
            }
        
              //Prevents the default form submit					
              return false; 
        }
    );
});