Subversion Repositories SmartDukaan

Rev

Rev 4372 | Rev 4926 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

$(function(){
        
        function updateCompareCount()   {
                $('#research_compare_count').html($('#myresearch input[type="checkbox"]:checked').length);
        }
        
        /** Sidebar Widgets **/
        
        $('#shoppingExpInfo').click(function(){
                $.colorbox({
                width: "550px",
                height: "320px",
                inline: true,
                href: "<h3>Shopping with Saholic.com is completely safe</h3><br />" +
                "<b>1.</b> We are part of Spice Group - India's # 1 Mobile retailer.<br /><br />" +
                "<b>2.</b> We procure directly from the Manufacturers - Hence we only sell genuine products with full Manufacturer Warranty.<br /><br />" +
                "<b>3.</b> We provide free Next Day Delivery to most locations - You can check the delivery time to your location by entering your pin code on the product page.<br /><br />" +
                "<b>4.</b> We use the best Internet Security technlology - Your payment is 100% secure.<br />",
                        onComplete: function(){
                                trackEventWithGA('Widget', 'Empty Cart Message Show', '');
                        }
        });
        });

        $('.common-widget-top-bar').live('click', function(){
                var isColapsed = $(this).data('is_colapsed') == true ? true : false;
                var widgetBox = $(this).siblings('.common-widget-content-area');
                var controlBox = $(this).siblings('.common-widget-control-bar');
                var controlBoxArrowImg = $(this).find('img');
                
                if (isColapsed) {
                        if(controlBox.length > 0)       {
                                $(controlBox).slideDown('fast', function(){
                                        $(widgetBox).slideDown();
                                        $(controlBoxArrowImg).attr('src', '/images/IconDownArrow_UnselectedTab.png');
                                });
                        } else  {
                                $(widgetBox).slideDown();
                                $(controlBoxArrowImg).attr('src', '/images/IconDownArrow_UnselectedTab.png');
                        }
                } else  {
                        $(widgetBox).slideUp(function(){
                                $(controlBox).slideUp('fast');
                                $(controlBoxArrowImg).attr('src', '/images/IconRightArrow_UnselectedTab.png');
                        });
                }
                $(this).data('is_colapsed', !isColapsed);
        });
        
        /**
         * Update count of products checked for comparison
         **/
        $('#myresearch input[type="checkbox"]').live('click', function(){
                updateCompareCount();
        });
        
        $("#research_compare").live('click', function(){
                var seldata = "";
                var tot = 1;
                var saprt = "";
                var par = $('#myresearch').find('input[type=checkbox]:checked');

                $(par).each(function(){
                        if(tot == 1)    {
                                seldata += "p" + tot + "=" + $(this).val();
                        } else  {
                                seldata += "&p" + tot + "=" + $(this).val();
                        }
                        tot ++;
                });

                if(tot > 6)     {
                        alert("Can compare upto five products only.");
                } else if(tot > 2)      {
                        window.location = "/compare-mobile-phones?" + seldata;
                } else  {
                        alert("Please select atleast two products");
                }
        });
        
        $("#research_delete").live('click', function(){
                var research_tot = $("#research_total").val();
                var seldata = [];
                var tot = 0;
                var saprt = "";
                var par = $('#myresearch').find('input[type=checkbox]:checked');

                $(par).each(function(){
                        seldata.unshift($(this).val());
                        tot ++;
                });

                if(tot > 0)     {
                        var t = (research_tot * 1) - (tot * 1);
                        $("#research_total").val(t);

                        jQuery.ajax({
                                type: "GET",
                                url: "/deletefromresearch/[" + seldata + "]?_method=delete",
                                success: function(msg)  {
                                        jQuery.each(seldata, function(intIndex, objValue){
                                                delete_from_storage_set("resitems", objValue);
                                                $("#myresearch").find("#" + objValue).fadeOut('slow', function() {
                                                        $(this).remove();
                                                        if(t === 0)     {
                                                                $("#research_default").css("display", "block");
                                                        }
                                                });
                                        });
                                }
                        });
                } else  {
                        alert("Please select atleast one product");
                }
        });
        
        // No uid cookie
        if (!$.cookie("uid"))   {
                // No uid in storage
                if (!$.Storage.get("uid"))      {
                        if (!$.Storage.get("resitems")) {
                                $.Storage.set("resitems", "[]");
                        }
                        if (!$.Storage.get("histitems"))        {
                                $.Storage.set("histitems", "[]");
                        }
                }
                else    {                       // uid in storage : logout
                        $.Storage.remove("uid");
                        $.Storage.set("resitems", "[]");
                        $.Storage.set("histitems", "[]");
                }
                load_research_widget();
                load_history_widget();
        }
        else    {                               // With uid cookie
                var cookie = $.cookie("uid");
                // Just logged in merge storage items
                if (!$.Storage.get("uid"))      {
                        $.Storage.set("uid", cookie);
                        merge_history_items();
                        load_research_items();
                }
                else    {
                        // UID changed
                        if ( cookie != $.Storage.get("uid"))    {
                                $.Storage.set("uid", cookie);
                                $.Storage.set("resitems", "[]");
                                $.Storage.set("histitems", "[]");
                                merge_history_items();
                                load_research_items();
                        }
                        else    {               // Uid is same
                                if (!$.Storage.get("resitems")) {
                                        $.Storage.set("resitems", "[]");
                                        load_research_items();
                                }
                                else    {
                                        load_research_widget();
                                }
                                
                                if (!$.Storage.get("histitems"))        {
                                        $.Storage.set("histitems", "[]");
                                        merge_history_items();
                                }
                                else    {
                                        load_history_widget();
                                }
                        }
                }
        }
        
        function load_research_items(){
                jQuery.ajax({
                        type: "GET",
                        url: "/myresearch",
                        cache: false,
                        success: function(json) {
                            $.Storage.set("resitems", json);
                            load_research_widget();
                    }
                });
        }

        function load_research_widget(){
                var myResearchWidgetContainer = $("#myresearch");
                
                if($(myResearchWidgetContainer).length == 0) {
                        return;
                }
                var resitems = $.Storage.get("resitems");
                
                if (resitems == "[]") {
                        var emptyResWidgetHtml = '<table border="0" width="100%" cellspacing="1" cellpadding="0" id="research_default">\
                      <tbody>\
                        <tr><td align="center"><b>Add items to compare list</b></td></tr>\
                       </tbody>\
                      </table>';
                        $(myResearchWidgetContainer).find(".common-widget-content-area").html(emptyResWidgetHtml);
                        $(myResearchWidgetContainer).children(".common-widget-top-bar").trigger('click');
                        return;
                }
                var params = "/" + resitems;
                
                jQuery.ajax({
                        type : "GET",
                        url : "/myresearch" + params,
                        cache: true,
                        success : function(html) {
                            $("#myresearch").html(html);
                                updateCompareCount();

                            // Product Title
                            $("#myresearch table td div a.truncate").each(function() {
                                                $(this).truncate({addtitle : true});
                            });

                            // Product Price
                            $("#myresearch table td div div.price").each(function() {
                                                $(this).truncate({addtitle : true});
                                });

                            // Product Details
                            $("#myresearch table td div div.text").each(function() {
                                                $(this).truncate( {addtitle : true});
                                });
                    }
                });
        }
});