$(document).ready(function($){
    $(".add_to_cart").live("click", function(event) {         
        var productIDVal =this.id;

        //$("#"+productIDVal).removeClass('add_to_cart');
        $(this).addClass("add_to_cart_loading");
        $.ajax(
        {
            url: SITE_URL + "ajax/item",
            type: "POST",
            cache: false,
            dataType: "json",
            data: {
                product_id: productIDVal,
                com:COMPONENT,
                action:ACTION
            },
            success: function(theResponse) 
            {

                $(".add_to_cart").removeClass("add_to_cart_loading");
                //$("#"+productIDVal).addClass('add_to_cart');
                rel_val = $.trim($(".add_to_cart").attr('rel').replace('nofollow', ''));                        
                if(rel_val!= undefined && rel_val!="" )
                {
                    reloadModule(rel_val,".cart_front");
                }
                else{
                    if($('#my_cart').length != 0)
                    {                                
                        $('#my_cart').html("Loading....");

                        $.ajax({
                            url: SITE_URL + "ajax/reloadmodule",
                            type: "GET",
                            dataType: "html",
                            data: {
                                module:'LR_cart'
                            },
                            success: function(theResponse)
                            {
                                $('#my_cart').html(theResponse);
                            }
                        });

                    }  
                            
                    if ($('.cart_top').length != 0)
                    {                               
                        $('.cart_top').html("Loading....");

                        $.ajax({
                            url: SITE_URL + "ajax/reloadmodule",
                            type: "GET",
                            dataType: "html",
                            data: {
                                module:'TOP_HEADER_cart_count'
                            },
                            success: function(theResponse)
                            {
                                $('.cart_top').html(theResponse);
                            }
                        });

                    }
                }
                $("#item").html(theResponse.test);                        
                if(theResponse.refresh==1)
                {
                    loadTab(5);
                    $("#item").html(theResponse.test);
                    return false;
                }

                if ($(".cart").is(":visible")) 
                {

                    if(theResponse.test ==1 || (COMPONENT=="product" && ACTION=="promo") )
                    {
                        loadTab(5);

                    }
                    else
                    {
                        if( $("#cart_" + productIDVal).length > 0){ 
                            $("#cart_" + productIDVal).html(theResponse.html);


                        }
                        else if(theResponse.html!="")
                        {
                            $("#cart dl:first").after('<dl class=cart_times id=cart_'+productIDVal+'>'+theResponse.html+'</dl>'); 						

                        }		

                        $("#cart_" + productIDVal).animate({
                            opacity: 0
                        }, 500);
                        $("#cart_" + productIDVal).animate({
                            opacity: 1
                        }, 500);  	

                    }


                    $(".cart .toll_count .total_count").html('<strong>'+theResponse.order_total+'</strong>');
                    $(".cart .delv_chrg .total_count").html('<strong>'+theResponse.delivary_charge+'</strong>');
                    $(".cart .tol_count_wo_vat .total_count").html('<strong>'+theResponse.total_without_vat+'</strong>');
                    $(".cart .vat_count .total_count").html('<strong>'+theResponse.item_total+'</strong>');


                    $(".cart .admin_cost .total_count").html('<strong>'+theResponse.extra_ammount_text+'</strong>');
                    $("#vat_per").html(theResponse.vat_per*100);
                    loadTab(5);

                }
                else if ($(".tab_5").is(":visible")) 
                {                                
                    $("#scroll").hide();
                }
                else
                {
                    loadTab(5);
                }
                        
                $(".checkout").show();

            },
            complete: function () {


                if( COMPONENT!="product" && ACTION!="details" )
                {

                    var image = $('#img_'+productIDVal).offset();
                    var cart=[];
                    if($('#my_ajax_cart').length != 0){
                        cart  = $('#my_ajax_cart').offset();
                    }
                    else if($('.cart_top').length != 0){
                        cart  = $('.cart_top').offset();
                    }
                    else if($('#my_cart').length != 0){
                        cart  = $('#my_cart').offset();
                    }                                           
                                
                    $('#img_'+productIDVal).before('<img src="' + $('#img_'+productIDVal).attr('src') + '" id="temp" style="position: absolute; top: ' + image.top + 'px; left: ' + image.left + 'px;" />');


                    var params = {
                        top : cart.top + 'px',
                        left : cart.left + 'px',
                        opacity : 1.0,
                        width : '75px',  
                        heigth : '75px'
                    };		

                    $('#temp').animate(params, 1000, false, function () {
                        $('#temp').remove();
                    });	


                }

                $( 'html, body' ).animate( {
                    scrollTop: 0
                }, 0 );	
            }	

        }); 




        return false;
    });
});

function loadTab(id)
{         
    //console.log(reqPageUrl);    
    if(typeof reqPageUrl == 'undefined'){
        return false;
    }       
    if (reqPageUrl[id].length > 0)
    {
        $("#ajax_content").html("");
        $("#ajax_content").addClass('loader');
		
        //alert(reqPageUrl[id]);
        $.ajax(
        {
            url: reqPageUrl[id], 
            type: "GET",
            error: function(XMLHttpRequest, textStatus, errorThrown)
            {
                $("#ajax_content").removeClass('loader');
            },
            success: function(message) 
            {
                var mhtml = ""; 
                var scrollPane = "";
                if( !(COMPONENT =="store" && ACTION == "home") && (id==1))
                {	
                    homePMenu = false;
                //mhtml +="<div id='home-product'><div id='home-product-content'></div><div id='home-product-menu'><a class='down' href="+SITE_URL+"store>More Promos</a></div></div>";
                }
                else
                {
                    scrollPane = '<div id="scroll" class="panel_controller tab_'+id+'"><a id="scollup"></a></div>';
                }
				
                $("#ajax_content").removeClass('loader');
				
                $('#navigation a').removeClass('active'); //remove all class='active' for all anchors
                $("#content_tab"+id).toggleClass('active'); //add class to the current one
                $("#ajax_content").html('<div id="ajax_menu_container">'+message+mhtml+'</div>'+scrollPane);
                $("#ajax_content").show('slide',{
                    direction: "up"
                },1000);
                $("#scroll").html('<a id="scollup">&nbsp;</a>');
            //tb_init('a.thickbox, area.thickbox, input.thickbox'); //call tb_init function to initiate ThichBox into your respective tab panels

            //				if(id==4)
            //				{
            //					var pos = $(".search_panel").offset(); 
            //					$( 'html, body' ).animate( { scrollTop: pos.top }, 0 );
            //				}
            }
			
        });			        
    }
}
