Subversion Repositories SmartDukaan

Rev

Rev 13992 | Rev 14068 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
13984 anikendra 1
$(function(){	
2
	if($('.scroll > .card').length>0) {
3
		setTimeout(function(){ document.location.reload(); }, 1000*60*35);
13820 anikendra 4
		$('.scroll').jscroll({
5
			loadingHtml: '<center><img src="/img/ajax-loader.gif" alt="Loading" /></center>',
6
		    autoTriggerUntil: 3,
7
		    padding: 20,
8
		});
9
	}
13901 anikendra 10
	if($('.storeminprice').length>0) {
11
		var globalminprice = 9999999;
12
		var globalminsku;
13
		$('.storeminprice').each(function(){
14
			var that = $(this);			
15
			$.ajax({
16
				url: '/store_products/getliveprice/'+$(this).data('bundle_id')+'/'+$(this).data('id'),
17
				// Tell jQuery we're expecting JSONP
18
				dataType: "json",
19
				// Tell YQL what we want and that we want JSON			
20
				method: 'get',
21
				// Work with the response
22
				success: function( response ) {
23
					if(response.success){
24
						var i = 0;	
25
						var minpriceindex = 0;										
26
						var minprice = response.products[0]['available_price'];
27
						var minpriceurl = response.products[0]['marketPlaceUrl'];
28
						if(response.products.length>0){
29
							//More than one products in store
30
							var variants = [];
31
							// var i = 0;
32
							for(var i in response.products){
33
								if(response.products[i].available_price && response.products[i].available_price < globalminprice) {
34
									globalminprice = response.products[i].available_price;
35
									globalminsku = response.products[i]._id;
36
								}
37
								if(response.products[i].available_price <= minprice) {
38
									minprice = response.products[i].available_price;
39
									minpriceindex = i;
40
								} 
14026 anikendra 41
								console.log(response);
13901 anikendra 42
								variants.push({'available_price' : response.products[i].available_price, 'url' : response.products[i].marketPlaceUrl,'source_product_name' : response.products[i].source_product_name,'id' : response.products[i]._id});
43
							}
44
							if(i>0){
45
								var variantslink = $('<span class="variants">+ '+i+' Options</span>');
46
								$(that).parent().append(variantslink);							
47
								$(variantslink).data('variants',variants)
48
							}
49
						}
50
						$(that).html(minprice);
51
						$('#sku-'+$(that).data('id')).data('id',response.products[minpriceindex]['_id']).removeClass('hidden');
52
						console.log(globalminprice+' '+globalminsku);
13948 anikendra 53
						if(globalminprice != 9999999) {
54
							$('#bestprice').html(globalminprice);
13992 anikendra 55
							$('#beststorelink').data('id',globalminsku).removeClass('hidden');
56
							$('#bestpricecontainer').removeClass('hidden');
13948 anikendra 57
						}
13901 anikendra 58
						if(!response.products[0].available_price){
59
							$(that).parent().parent().remove();	
60
						}
61
					}else{
62
						$(that).parent().parent().remove();
63
					}
64
				},
65
				error : function() {
66
					$(that).parent().parent().remove();
67
				}
68
			});
69
		});
70
	}
71
	$(document).on('click','.variants',function(){
72
		$('.storeproductinfo').empty();
14026 anikendra 73
		var variants = $(this).data('variants');	
74
		$('#variantscount').html(variants.length);
13901 anikendra 75
		for(var i in variants){
76
			console.log(variants[i]);
14026 anikendra 77
			var row = $('<div class="row storeproductinfo"><div class="col-xs-7">'+variants[i].source_product_name+'</div><div class="col-xs-3">'+variants[i].available_price+'</div><div class="col-xs-2"><button class="btn btn-primary btn-xs viewproduct" type="button" data-id="'+variants[i].id+'">Buy</button></div></div>');
13901 anikendra 78
			$('#storeproducts').parent().append(row);
79
		}
14026 anikendra 80
		// console.log($(this).parent());
81
		$('#storename').html($(this).parent().data('storename'));
13901 anikendra 82
		$('#variantModal').modal();
83
	});
13695 anikendra 84
	$('.categorytab').on('click','.categorytabcontrol.active',function(){
85
		$(this).toggleClass('active');
86
		$('#preferences-'+$(this).data('id')).addClass('hidden');
87
		$(this).parent().parent().children().find('.savecategorypreferences',0).removeClass('savecategorypreferences').addClass('editcategorypreferences').html('Edit').removeClass('btn-success');
88
	});
13759 anikendra 89
	$('.categorytab').on('click','.categorytabcontrol:not(.active)',function(){
90
		$(this).parent().parent().find('.btn',0).click();
91
	});
13695 anikendra 92
	$('.categorytab').on('click','.editcategorypreferences',function(e){
93
		var that = $(this);
94
		$(that).removeClass('editcategorypreferences').addClass('savecategorypreferences').html('Done').addClass('btn-success');
95
		$('#togglepreferences-'+$(that).data('id')).toggleClass('active');
96
		$('#preferences-'+$(that).data('id')).removeClass('hidden');
97
		$('.brandselector').each(function(){
98
			if($(this).data('catid')==$(that).data('id')){
99
				$(this).addClass('active');
100
			}
101
		});
102
	});
103
	$('.categorytab').on('click','.savecategorypreferences',function(e){
104
		var that = $(this);
105
		$(that).removeClass('savecategorypreferences').addClass('editcategorypreferences').html('Edit').removeClass('btn-success');
106
		$('#togglepreferences-'+$(that).data('id')).toggleClass('active');
107
		$.ajax({
108
			url: $('#categorypreference-'+$(that).data('id')).attr('action'),
109
			data: $('#categorypreference-'+$(that).data('id')).serialize(),
110
			// Tell jQuery we're expecting JSONP
111
			dataType: "json",
112
			// Tell YQL what we want and that we want JSON			
113
			method: 'post',
114
			// Work with the response
115
			success: function( response ) {
116
				if(response.success){
117
					$('#preferences-'+$(that).data('id')).addClass('hidden');
118
				}
119
			}
120
		});
121
		ga('send', 'event', 'preferences', 'update', me);
122
	});
13901 anikendra 123
	// $('.row').on('click','.viewproduct',function(e){
124
	$(document).on('click','.viewproduct',function(e){
13686 anikendra 125
		$('#loadingModal').modal();
13901 anikendra 126
		console.log($(this).data('id'));
13579 anikendra 127
		var url = apihost+"clicks/add/"+me+"/"+$(this).data('id');
128
		$.ajax({
129
			url: url,
130
			// The name of the callback parameter, as specified by the YQL service
131
			jsonp: "callback",
132
			// Tell jQuery we're expecting JSONP
133
			dataType: "jsonp",
134
			// Tell YQL what we want and that we want JSON
135
			data: {
136
				format: "json"
137
			},
138
			// Work with the response
139
			success: function( response ) {
140
				if(response.success && response.type=='redirect'){
141
					document.location = response.url;
142
				}
143
			}
144
		});
13686 anikendra 145
		ga('send', 'event', 'product', 'click', $(this).data('id'));
13583 anikendra 146
	});
13719 anikendra 147
	$('.jscroll-inner').on('click','.likedeal',function(e){	
13583 anikendra 148
		var that = $(this);
13682 anikendra 149
		if($(that).hasClass('active')){
13583 anikendra 150
			//User has already liked it,so remove like
151
			var url = apihost+"/user_actions/rem/"+me+"/"+$(this).data('id')+"/like";	
152
		}else{
153
			var url = apihost+"/user_actions/update/"+me+"/"+$(this).data('id')+"/like";
154
		}
155
		$.ajax({
156
			url: url,
157
			// The name of the callback parameter, as specified by the YQL service
158
			jsonp: "callback",
159
			// Tell jQuery we're expecting JSONP
160
			dataType: "jsonp",
161
			// Tell YQL what we want and that we want JSON
162
			data: {
163
				format: "json"
164
			},
165
			// Work with the response
166
			success: function( response ) {
167
				if(response.success){
13672 anikendra 168
					$(that).toggleClass('active');
169
					$(that).parent().find('li.dislikedeal',0).removeClass('active');
13583 anikendra 170
				}
171
			}
172
		});
13686 anikendra 173
		ga('send', 'event', 'product', 'like', $(this).data('id'));
13583 anikendra 174
	});
13688 anikendra 175
	$('#myModal').on('click','#unlikebtn',function(e){
13682 anikendra 176
		e.preventDefault();
177
		var url = $('#unlikeproductform').attr('action')+'?'+$('#unlikeproductform').serialize()
178
		console.log(url);
179
		$.ajax({
180
			url: url,
181
			// The name of the callback parameter, as specified by the YQL service
182
			jsonp: "callback",
183
			// Tell jQuery we're expecting JSONP
184
			dataType: "jsonp",
185
			// Tell YQL what we want and that we want JSON
186
			data: {
187
				format: "json"
188
			},
189
			// Work with the response
190
			success: function( response ) {
191
				// console.log(response);						
192
			}
193
		});
194
		$('#myModal').modal('hide');
13686 anikendra 195
		ga('send', 'event', 'brand', 'hide', $('#myModal').find('#productToHide',0).val());
13682 anikendra 196
	})
13719 anikendra 197
	$('.jscroll-inner').on('click','.dislikedeal',function(e){			
13583 anikendra 198
		var that = $(this);
13682 anikendra 199
		if($(that).hasClass('active')){
13583 anikendra 200
			//User has already liked it,so remove like
201
			var url = apihost+"/user_actions/rem/"+me+"/"+$(this).data('id')+"/dislike";	
202
		}else{
13682 anikendra 203
			console.log('show modal');
204
			$('#myModal').find('#productToHide',0).val($(this).data('id'));
205
			$('#myModal').modal();
13583 anikendra 206
			var url = apihost+"/user_actions/update/"+me+"/"+$(this).data('id')+"/dislike";
207
		}
208
		$.ajax({
209
			url: url,
210
			// The name of the callback parameter, as specified by the YQL service
211
			jsonp: "callback",
212
			// Tell jQuery we're expecting JSONP
213
			dataType: "jsonp",
214
			// Tell YQL what we want and that we want JSON
215
			data: {
216
				format: "json"
217
			},
218
			// Work with the response
219
			success: function( response ) {
220
				if(response.success){
13672 anikendra 221
					$(that).toggleClass('active');
222
					$(that).parent().find('li.likedeal',0).removeClass('active');
13583 anikendra 223
				}
224
			}
225
		});
13686 anikendra 226
		ga('send', 'event', 'product', 'dislike', $(this).data('id'));
13583 anikendra 227
	});
13731 anikendra 228
	$('.deletefav').on('click',function(){
229
		var that = $(this);
230
		$('#loadingModal').modal();
231
		var url = apihost+"/user_actions/deletefav/"+me+"/"+$(this).data('id');			
232
		$.ajax({
233
			url: url,
234
			// The name of the callback parameter, as specified by the YQL service
235
			jsonp: "callback",
236
			// Tell jQuery we're expecting JSONP
237
			dataType: "jsonp",
238
			// Tell YQL what we want and that we want JSON
239
			data: {
240
				format: "json"
241
			},
242
			// Work with the response
243
			success: function( response ) {
244
				$('#loadingModal').modal('hide');
245
				if(response.success){			
246
					$('#fav-'+$(that).data('id')).hide('slow');
247
				}
248
			}
249
		});
250
		ga('send', 'event', 'favourites', 'remove', $(this).data('id'));
251
	});
252
	$('.clearfavs').on('click',function(){
253
		var that = $(this);
254
		$('#loadingModal').modal();
255
		var url = apihost+"/user_actions/deleteallfavs/"+me;			
256
		$.ajax({
257
			url: url,
258
			// The name of the callback parameter, as specified by the YQL service
259
			jsonp: "callback",
260
			// Tell jQuery we're expecting JSONP
261
			dataType: "jsonp",
262
			// Tell YQL what we want and that we want JSON
263
			data: {
264
				format: "json"
265
			},
266
			// Work with the response
267
			success: function( response ) {
268
				$('#loadingModal').modal('hide');
269
				if(response.success){			
270
					$('.deletefav').each(function(){
271
						$('#fav-'+$(this).data('id')).hide('slow');
272
					})					
273
				}
274
				$(that).hide();
275
			}
276
		});
277
		ga('send', 'event', 'favourites', 'removeall', me);
278
	});
13759 anikendra 279
	$('.revealbrands').on('click',function(){
280
		$(this).parent().find('.notfeatured').toggleClass('hidden');
281
		if($(this).html()=='Others'){
282
			$(this).html('Hide');
283
		}else{
284
			$(this).html('Others');
285
		}
286
	});
13992 anikendra 287
});