//OPTIONS
var cartProcessPath="plugs/cart/process.php"; //cart directory
var pgeArr=['basket.php']; //cart page


//DO NOT EDIT
var thsDoc=CurrentDocName();
$(document).ready(function(){
    $('.cartHead li a').refreshCartSum();
    $(window).pageFuncs();

    $(window).bind("load",function(){
        if(thsDoc==pgeArr[0]){
            $('.frmCartTbl').refreshBasket();
            $().backetPgeFunctions();
        }
    });

    //tool tip
    $().mousemove(function(e){
        mX=e.pageX;
        mY=e.pageY;
        $('.ttC').css({left:mX,top:mY+20});
    });

    $('.ttC').animate({opacity:0},{duration:300,queue:false}).css({display:'none'});

    
});

(function($) {
    $.fn.extend({ 
        refreshCartSum:function(){
            thsObj1=$(this).eq(0);
            thsObj2=$(this).eq(1);
            thsObj1.html("Loading...");thsObj2.html("");
            $.ajax({
                type: "POST",
                url: cartProcessPath,
                data: "func=refreshCartSum",
                success:function(data){
                    splData=data.split(",");
                    thsObj1.html("<span>items:</span>"+splData[0]);
                    thsObj2.html("<span>total:</span>"+splData[1]);
                }
            });
        },
        refreshBasket:function(){
            thsObj=this;
            thsObj.children('.tableC').css({display:'none'}).parent()
            .children('.crtNfo').text("Refreshing basket....")
            .css({display:'block'});
            
            $.ajax({
                type: "POST",
                url: cartProcessPath,
                data: "func=refreshBasket",
                success:function(data){
                    if(data!=""){
                        caData=data.split("{sTot}");
                        if(caData[1]=="mTCart"){
                            thsObj.children('.crtNfo').html(caData[0]);
                        }
                        else{
                            totObj=$('.cartTble2 td');
                            totObj2=$('.cartTble3 select');

                            thsObj.children('.crtNfo').css({display:'none'}).parent()
                            .children('.tableC').css({display:'block'})
                            .children('.cartTble').children('tbody').html(caData[0]);

                            //sub total
                            totObj.eq(2).html(caData[1]);

                            //postage
                            totObj2.html(caData[2]);
                            totObj.eq(5).html(caData[3]);

                            //total
                            totObj.eq(8).html(caData[4]);
							
							
							
							
							
						if($('#spay').length){
							str="";
							
							$('.cartTble tbody tr').each(function(){
																	  
														  
								//quantity
								qty=$(this).children('td:nth-child(2)').children('input').val();
								//name
								itemid=$(this).children('td:nth-child(1)').children('input').val();
								
								
						
								//add to a string
								str=str+qty+" of "+itemid+",";		
								
								
							});
							
							
							str=str.slice(0,str.length-1);
							
							$('#spay').click(function(){
								window.location="sp/orderConfirmation.php?strCart=" + str;
								
								return false;
							});
						}
							
							
							
							
							
                        }
                    }
                    else{
                        thsObj.children('.crtNfo').text("Your Shopping basket is empty.");
                    }
                }
            });
        },
        backetPgeFunctions:function(){
           //select,deselect all items
           $('.cartTble thead input[type="checkbox"]').click(function(){
               if($(this).is(':checked')){chkAll=true}else{chkAll=false;}
               $('.cartTble tbody input[type="checkbox"]').attr('checked',chkAll);
           });

           //amend,remove items
           $('.cartTble2 button').eq(0).click(function(){
               delItms=[];
               qtyAmnd="";
               $('.cartTble tbody tr').each(function(){
                   fstCol=$(this).children('td').eq(0).children('input[type="checkbox"]');
                   fstCol2=$(this).children('td').eq(1).children('input[type="text"]');
                   thsVal=fstCol.val();
                   thsQty=fstCol2.val();
                   qtyAmnd=qtyAmnd+thsVal+","+thsQty+"{spl}";
                   if(fstCol.is(':checked')){delItms.push(thsVal);}
               });

                $.ajax({
                    type: "POST",
                    url: cartProcessPath,
                    data: "func=AddEditBasket&delArr="+delItms+"&qtyAmd="+qtyAmnd,
                    success:function(data){
                        $('.cartHead li a').refreshCartSum();
                        $('.frmCartTbl').refreshBasket();
                    }
                });

                return false;
           });
           //select postage
           $('.cartTble3 .selPostage').change(function(){
               thsVal=$(this).val();
               alert(thsVal);

               return false;
           });

        },
        pageFuncs:function(){
            $('.addToBasket').click(function(){
                thsRel=$(this).attr('rel');

                //tool tip
                $('.ttC').css({display:'block'}).animate({opacity:.9},{duration:300,queue:false})
                .children('span').text("Adding to cart");

                if(thsRel!=""){
                    $.ajax({
                        type: "POST",
                        url: cartProcessPath,
                        data: "func=AddToCart&itmIndx="+thsRel,
                        success:function(data){
                            $('.cartHead li a').refreshCartSum();

                            //tool tip
                            $('.ttC').children('span').text("Item added");
                            ttInt=setInterval(function(){
                                clearInterval(ttInt);
                                $('.ttC').animate({opacity:0},300,function(){
                                    $(this).css({display:'none'});
                                });
                            },1000);

                            
                        }
                    });
                }
                return false;
            })
        }
    });
})(jQuery);

function CurrentDocName()
{
    thisURL=document.location.href;
    strUrl=thisURL.split('/');
    strUrl=strUrl.pop().split('?');
    strUrl=strUrl[0].split('#');
    return strUrl[0];
}
