Subversion Repositories SmartDukaan

Rev

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

$(function(){
        $('.scroll').jscroll({
                loadingHtml: '<center><img src="/img/ajax-loader.gif" alt="Loading" /></center>',
            autoTriggerUntil: 3,
            padding: 20,
        });
        $('.categorytab').on('click','.categorytabcontrol.active',function(){
                $(this).toggleClass('active');
                $('#preferences-'+$(this).data('id')).addClass('hidden');
                $(this).parent().parent().children().find('.savecategorypreferences',0).removeClass('savecategorypreferences').addClass('editcategorypreferences').html('Edit').removeClass('btn-success');
        });
        $('.categorytab').on('click','.editcategorypreferences',function(e){
                var that = $(this);
                $(that).removeClass('editcategorypreferences').addClass('savecategorypreferences').html('Done').addClass('btn-success');
                $('#togglepreferences-'+$(that).data('id')).toggleClass('active');
                $('#preferences-'+$(that).data('id')).removeClass('hidden');
                $('.brandselector').each(function(){
                        if($(this).data('catid')==$(that).data('id')){
                                $(this).addClass('active');
                        }
                });
        });
        $('.categorytab').on('click','.savecategorypreferences',function(e){
                var that = $(this);
                $(that).removeClass('savecategorypreferences').addClass('editcategorypreferences').html('Edit').removeClass('btn-success');
                $('#togglepreferences-'+$(that).data('id')).toggleClass('active');
                $.ajax({
                        url: $('#categorypreference-'+$(that).data('id')).attr('action'),
                        data: $('#categorypreference-'+$(that).data('id')).serialize(),
                        // Tell jQuery we're expecting JSONP
                        dataType: "json",
                        // Tell YQL what we want and that we want JSON                  
                        method: 'post',
                        // Work with the response
                        success: function( response ) {
                                if(response.success){
                                        $('#preferences-'+$(that).data('id')).addClass('hidden');
                                }
                        }
                });
                ga('send', 'event', 'preferences', 'update', me);
        });
        $('.row').on('click','.viewproduct',function(e){
                $('#loadingModal').modal();
                var url = apihost+"clicks/add/"+me+"/"+$(this).data('id');
                $.ajax({
                        url: url,
                        // The name of the callback parameter, as specified by the YQL service
                        jsonp: "callback",
                        // Tell jQuery we're expecting JSONP
                        dataType: "jsonp",
                        // Tell YQL what we want and that we want JSON
                        data: {
                                format: "json"
                        },
                        // Work with the response
                        success: function( response ) {
                                if(response.success && response.type=='redirect'){
                                        document.location = response.url;
                                }
                        }
                });
                ga('send', 'event', 'product', 'click', $(this).data('id'));
        });
        $('.jscroll-inner').on('click','.likedeal',function(e){ 
                var that = $(this);
                if($(that).hasClass('active')){
                        //User has already liked it,so remove like
                        var url = apihost+"/user_actions/rem/"+me+"/"+$(this).data('id')+"/like";       
                }else{
                        var url = apihost+"/user_actions/update/"+me+"/"+$(this).data('id')+"/like";
                }
                $.ajax({
                        url: url,
                        // The name of the callback parameter, as specified by the YQL service
                        jsonp: "callback",
                        // Tell jQuery we're expecting JSONP
                        dataType: "jsonp",
                        // Tell YQL what we want and that we want JSON
                        data: {
                                format: "json"
                        },
                        // Work with the response
                        success: function( response ) {
                                if(response.success){
                                        $(that).toggleClass('active');
                                        $(that).parent().find('li.dislikedeal',0).removeClass('active');
                                }
                        }
                });
                ga('send', 'event', 'product', 'like', $(this).data('id'));
        });
        $('#myModal').on('click','#unlikebtn',function(e){
                e.preventDefault();
                var url = $('#unlikeproductform').attr('action')+'?'+$('#unlikeproductform').serialize()
                console.log(url);
                $.ajax({
                        url: url,
                        // The name of the callback parameter, as specified by the YQL service
                        jsonp: "callback",
                        // Tell jQuery we're expecting JSONP
                        dataType: "jsonp",
                        // Tell YQL what we want and that we want JSON
                        data: {
                                format: "json"
                        },
                        // Work with the response
                        success: function( response ) {
                                // console.log(response);                                               
                        }
                });
                $('#myModal').modal('hide');
                ga('send', 'event', 'brand', 'hide', $('#myModal').find('#productToHide',0).val());
        })
        $('.jscroll-inner').on('click','.dislikedeal',function(e){                      
                var that = $(this);
                if($(that).hasClass('active')){
                        //User has already liked it,so remove like
                        var url = apihost+"/user_actions/rem/"+me+"/"+$(this).data('id')+"/dislike";    
                }else{
                        console.log('show modal');
                        $('#myModal').find('#productToHide',0).val($(this).data('id'));
                        $('#myModal').modal();
                        var url = apihost+"/user_actions/update/"+me+"/"+$(this).data('id')+"/dislike";
                }
                $.ajax({
                        url: url,
                        // The name of the callback parameter, as specified by the YQL service
                        jsonp: "callback",
                        // Tell jQuery we're expecting JSONP
                        dataType: "jsonp",
                        // Tell YQL what we want and that we want JSON
                        data: {
                                format: "json"
                        },
                        // Work with the response
                        success: function( response ) {
                                if(response.success){
                                        $(that).toggleClass('active');
                                        $(that).parent().find('li.likedeal',0).removeClass('active');
                                }
                        }
                });
                ga('send', 'event', 'product', 'dislike', $(this).data('id'));
        });
        $('.deletefav').on('click',function(){
                var that = $(this);
                $('#loadingModal').modal();
                var url = apihost+"/user_actions/deletefav/"+me+"/"+$(this).data('id');                 
                $.ajax({
                        url: url,
                        // The name of the callback parameter, as specified by the YQL service
                        jsonp: "callback",
                        // Tell jQuery we're expecting JSONP
                        dataType: "jsonp",
                        // Tell YQL what we want and that we want JSON
                        data: {
                                format: "json"
                        },
                        // Work with the response
                        success: function( response ) {
                                $('#loadingModal').modal('hide');
                                if(response.success){                   
                                        $('#fav-'+$(that).data('id')).hide('slow');
                                }
                        }
                });
                ga('send', 'event', 'favourites', 'remove', $(this).data('id'));
        });
        $('.clearfavs').on('click',function(){
                var that = $(this);
                $('#loadingModal').modal();
                var url = apihost+"/user_actions/deleteallfavs/"+me;                    
                $.ajax({
                        url: url,
                        // The name of the callback parameter, as specified by the YQL service
                        jsonp: "callback",
                        // Tell jQuery we're expecting JSONP
                        dataType: "jsonp",
                        // Tell YQL what we want and that we want JSON
                        data: {
                                format: "json"
                        },
                        // Work with the response
                        success: function( response ) {
                                $('#loadingModal').modal('hide');
                                if(response.success){                   
                                        $('.deletefav').each(function(){
                                                $('#fav-'+$(this).data('id')).hide('slow');
                                        })                                      
                                }
                                $(that).hide();
                        }
                });
                ga('send', 'event', 'favourites', 'removeall', me);
        });
});