Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
14386 anikendra 1
var livePriceCalls = [];
14432 anikendra 2
var outOfStockCount = 0;
13984 anikendra 3
$(function(){	
4
	if($('.scroll > .card').length>0) {
5
		setTimeout(function(){ document.location.reload(); }, 1000*60*35);
13820 anikendra 6
		$('.scroll').jscroll({
7
			loadingHtml: '<center><img src="/img/ajax-loader.gif" alt="Loading" /></center>',
8
		    autoTriggerUntil: 3,
9
		    padding: 20,
10
		});
11
	}
13901 anikendra 12
	if($('.storeminprice').length>0) {
13
		var globalminprice = 9999999;
14313 anikendra 14
		var globalminsku;		
14345 anikendra 15
		var calls = [];
13901 anikendra 16
		$('.storeminprice').each(function(){
14345 anikendra 17
			var temp = {'sort':$(this).data('searchorder'),'val':$(this)};
18
			calls.push(temp);
19
		});
20
		calls = sortByKey(calls,'sort');		
21
		for(var i in calls){
22
			fetchLivePrice(calls[i]['val'])
14432 anikendra 23
		}		
14345 anikendra 24
	};
25
	function sortByKey(array, key) {
26
	    return array.sort(function(a, b) {
27
	        var x = a[key]; var y = b[key];
28
	        return ((x < y) ? -1 : ((x > y) ? 1 : 0));
29
	    });
30
	};
14432 anikendra 31
	function fetchLivePrice(obj){		
32
		var that = obj;	
33
		var inStock = false;		
14386 anikendra 34
		var req = $.ajax({
14345 anikendra 35
			url: '/store_products/getliveprice/'+$(that).data('bundle_id')+'/'+$(that).data('id'),
36
			// Tell jQuery we're expecting JSONP
37
			dataType: "json",
38
			// Tell YQL what we want and that we want JSON			
39
			method: 'get',
14430 anikendra 40
			// timeout: 30000,
14345 anikendra 41
			// Work with the response
42
			success: function( response ) {
14685 anikendra 43
				// console.log(response);
14345 anikendra 44
				if(response.success){
45
					var i = 0;	
46
					var minpriceindex = 0;				
47
					if(response.products[i].in_stock == 1){inStock = true;}
48
					var minprice = response.products[0]['available_price'];
49
					var minpriceurl = response.products[0]['marketPlaceUrl'];
50
					if(response.products.length>0){
51
						//More than one products in store
52
						var variants = [];
53
						// var i = 0;
54
						for(var i in response.products){	
14685 anikendra 55
							if(response.products[i].in_stock == 1){
56
								inStock = true;
57
								// console.log(response.products[i]);
58
								if(response.products[i].available_price && response.products[i].available_price < globalminprice) {
59
									globalminprice = Math.round(response.products[i].available_price);
60
									globalminsku = response.products[i]._id;
61
									var globalminsource = response.products[i].source_id;
62
									var globalminurl = response.products[i].marketPlaceUrl;									
63
								}
64
								if(response.products[i].available_price <= minprice) {
65
									minprice = Math.round(response.products[i].available_price);
66
									minpriceindex = i;
67
								} 
68
								variants.push({'source_id' : response.products[i].source_id, 'available_price' : Math.round(response.products[i].available_price), 'url' : response.products[i].marketPlaceUrl,'source_product_name' : response.products[i].source_product_name,'id' : response.products[i]._id});
69
							}						
13901 anikendra 70
						}
14685 anikendra 71
						if(variants.length>1){
72
							var variantslink = $('<span class="variants">+ '+(variants.length-1)+' Options</span>');
14345 anikendra 73
							$(that).parent().append(variantslink);							
74
							$(variantslink).data('variants',variants)
13948 anikendra 75
						}
14345 anikendra 76
					}
77
					if(inStock){
78
						$(that).html(minprice);
14685 anikendra 79
						// console.log(response.products[minpriceindex]);
80
						$('#sku-'+$(that).data('id')).data('id',response.products[minpriceindex]['_id']).data('source',response.products[minpriceindex]['source_id']).data('price',response.products[minpriceindex]['available_price']).data('url',response.products[minpriceindex]['marketPlaceUrl']).removeClass('hidden');
14357 anikendra 81
						// console.log(globalminprice+' '+globalminsku);
14345 anikendra 82
						if(globalminprice != 9999999) {
83
							$('#bestprice').html(globalminprice);
14685 anikendra 84
							$('#beststorelink').data('id',globalminsku).data('source',globalminsource).data('price',globalminprice).data('url',globalminurl).removeClass('hidden');
14345 anikendra 85
							$('#bestpricecontainer').removeClass('hidden');
14432 anikendra 86
						} 
14386 anikendra 87
					} else{
14432 anikendra 88
						outOfStockCount++;
14345 anikendra 89
						$(that).parent().parent().remove();	
13901 anikendra 90
					}
14345 anikendra 91
					if(!response.products[0].available_price){
14434 anikendra 92
						// outOfStockCount++;
93
						// $(that).parent().parent().remove();	
94
						var btn = $("<a class='tryagainforliveprice btn btn-xs btn-warning' title='Try Again'>Try Again</a>");
95
						$(that).html(btn);
14345 anikendra 96
					}
14386 anikendra 97
				} else{
14434 anikendra 98
					// outOfStockCount++;
99
					// $(that).parent().parent().remove();
100
					var btn = $("<a class='tryagainforliveprice btn btn-xs btn-warning' title='Try Again'>Try Again</a>");
101
					$(that).html(btn);
13901 anikendra 102
				}
14432 anikendra 103
				checkForOutStocks(outOfStockCount);
14345 anikendra 104
			},
14386 anikendra 105
			error: function(request, status, err) {
106
				//console.log(status+' '+err+' '+request);
14430 anikendra 107
            	// if (status == "timeout") {
108
            		var btn = $("<a class='tryagainforliveprice btn btn-xs btn-warning' title='Try Again'>Try Again</a>");
14434 anikendra 109
					$(that).html(btn);
14430 anikendra 110
				// } else {
111
					// $(that).parent().parent().remove();
112
				// }
14345 anikendra 113
			}
13901 anikendra 114
		});
14386 anikendra 115
		livePriceCalls.push(req);
14357 anikendra 116
	};
14432 anikendra 117
	function checkForOutStocks(count){
118
		if(count>=livePriceCalls.length){
119
			$('#bestpricecontainer').html('<h4>Out of stock</h4>').removeClass('hidden');	
120
		}
121
	};
13901 anikendra 122
	$(document).on('click','.variants',function(){
123
		$('.storeproductinfo').empty();
14685 anikendra 124
		var variants = $(this).data('variants');
125
		// console.log(variants);
14026 anikendra 126
		$('#variantscount').html(variants.length);
13901 anikendra 127
		for(var i in variants){
14392 anikendra 128
			// console.log(variants[i]);
14685 anikendra 129
			// 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>');
130
			// $('#storeproducts').parent().append(row);
131
			var row = $('<div class="clearfix"></div><div class="col-xs-4">&nbsp;</div><div class="col-xs-5"><span class="cashbackrupee"></span> <span>'+variants[i].available_price+'</span></div><div class="col-xs-3 viewproduct" data-source="'+variants[i].source_id+'" data-price="'+variants[i].available_price+'" data-url="'+variants[i].url+'" data-id="'+variants[i].id+'"><button type="button" class="btn-xs btn btn-success">BUY</button></div>');
132
			$(this).parent().parent().append(row);
13901 anikendra 133
		}
14026 anikendra 134
		// console.log($(this).parent());
14685 anikendra 135
		// $('#storename').html($(this).parent().data('storename'));
136
		// $('#variantModal').modal();
13901 anikendra 137
	});
13695 anikendra 138
	$('.categorytab').on('click','.categorytabcontrol.active',function(){
139
		$(this).toggleClass('active');
140
		$('#preferences-'+$(this).data('id')).addClass('hidden');
141
		$(this).parent().parent().children().find('.savecategorypreferences',0).removeClass('savecategorypreferences').addClass('editcategorypreferences').html('Edit').removeClass('btn-success');
142
	});
13759 anikendra 143
	$('.categorytab').on('click','.categorytabcontrol:not(.active)',function(){
144
		$(this).parent().parent().find('.btn',0).click();
145
	});
13695 anikendra 146
	$('.categorytab').on('click','.editcategorypreferences',function(e){
147
		var that = $(this);
148
		$(that).removeClass('editcategorypreferences').addClass('savecategorypreferences').html('Done').addClass('btn-success');
149
		$('#togglepreferences-'+$(that).data('id')).toggleClass('active');
150
		$('#preferences-'+$(that).data('id')).removeClass('hidden');
151
		$('.brandselector').each(function(){
152
			if($(this).data('catid')==$(that).data('id')){
153
				$(this).addClass('active');
154
			}
155
		});
156
	});
157
	$('.categorytab').on('click','.savecategorypreferences',function(e){
158
		var that = $(this);
159
		$(that).removeClass('savecategorypreferences').addClass('editcategorypreferences').html('Edit').removeClass('btn-success');
160
		$('#togglepreferences-'+$(that).data('id')).toggleClass('active');
161
		$.ajax({
162
			url: $('#categorypreference-'+$(that).data('id')).attr('action'),
163
			data: $('#categorypreference-'+$(that).data('id')).serialize(),
164
			// Tell jQuery we're expecting JSONP
165
			dataType: "json",
166
			// Tell YQL what we want and that we want JSON			
167
			method: 'post',
168
			// Work with the response
169
			success: function( response ) {
170
				if(response.success){
171
					$('#preferences-'+$(that).data('id')).addClass('hidden');
172
				}
173
			}
174
		});
175
		ga('send', 'event', 'preferences', 'update', me);
176
	});
13901 anikendra 177
	// $('.row').on('click','.viewproduct',function(e){
178
	$(document).on('click','.viewproduct',function(e){
14386 anikendra 179
		if(typeof livePriceCalls != undefined) {
180
	    	for(var i in livePriceCalls){
14392 anikendra 181
	    		// console.log('killing ajax call '+i);
14386 anikendra 182
	    		livePriceCalls[i].abort();
183
	    	}
184
	    }
13686 anikendra 185
		$('#loadingModal').modal();
14392 anikendra 186
		// console.log($(this).data('id'));
14345 anikendra 187
		var url = apihost+"clicks/add/"+me+"/"+$(this).data('id')+'/'+$(this).data('source')+'/?url='+encodeURIComponent($(this).data('url'))+'&price='+$(this).data('price');
13579 anikendra 188
		$.ajax({
189
			url: url,
190
			// The name of the callback parameter, as specified by the YQL service
191
			jsonp: "callback",
192
			// Tell jQuery we're expecting JSONP
193
			dataType: "jsonp",
194
			// Tell YQL what we want and that we want JSON
195
			data: {
196
				format: "json"
197
			},
198
			// Work with the response
199
			success: function( response ) {
200
				if(response.success && response.type=='redirect'){
201
					document.location = response.url;
202
				}
203
			}
204
		});
14432 anikendra 205
		ga('send', 'event', 'product', 'click', $(this).data('url'));
13583 anikendra 206
	});
13719 anikendra 207
	$('.jscroll-inner').on('click','.likedeal',function(e){	
13583 anikendra 208
		var that = $(this);
13682 anikendra 209
		if($(that).hasClass('active')){
13583 anikendra 210
			//User has already liked it,so remove like
211
			var url = apihost+"/user_actions/rem/"+me+"/"+$(this).data('id')+"/like";	
212
		}else{
213
			var url = apihost+"/user_actions/update/"+me+"/"+$(this).data('id')+"/like";
214
		}
215
		$.ajax({
216
			url: url,
217
			// The name of the callback parameter, as specified by the YQL service
218
			jsonp: "callback",
219
			// Tell jQuery we're expecting JSONP
220
			dataType: "jsonp",
221
			// Tell YQL what we want and that we want JSON
222
			data: {
223
				format: "json"
224
			},
225
			// Work with the response
226
			success: function( response ) {
227
				if(response.success){
13672 anikendra 228
					$(that).toggleClass('active');
229
					$(that).parent().find('li.dislikedeal',0).removeClass('active');
13583 anikendra 230
				}
231
			}
232
		});
13686 anikendra 233
		ga('send', 'event', 'product', 'like', $(this).data('id'));
13583 anikendra 234
	});
13688 anikendra 235
	$('#myModal').on('click','#unlikebtn',function(e){
13682 anikendra 236
		e.preventDefault();
237
		var url = $('#unlikeproductform').attr('action')+'?'+$('#unlikeproductform').serialize()
14392 anikendra 238
		// console.log(url);
13682 anikendra 239
		$.ajax({
240
			url: url,
241
			// The name of the callback parameter, as specified by the YQL service
242
			jsonp: "callback",
243
			// Tell jQuery we're expecting JSONP
244
			dataType: "jsonp",
245
			// Tell YQL what we want and that we want JSON
246
			data: {
247
				format: "json"
248
			},
249
			// Work with the response
250
			success: function( response ) {
251
				// console.log(response);						
252
			}
253
		});
254
		$('#myModal').modal('hide');
13686 anikendra 255
		ga('send', 'event', 'brand', 'hide', $('#myModal').find('#productToHide',0).val());
13682 anikendra 256
	})
13719 anikendra 257
	$('.jscroll-inner').on('click','.dislikedeal',function(e){			
13583 anikendra 258
		var that = $(this);
13682 anikendra 259
		if($(that).hasClass('active')){
13583 anikendra 260
			//User has already liked it,so remove like
261
			var url = apihost+"/user_actions/rem/"+me+"/"+$(this).data('id')+"/dislike";	
262
		}else{
14392 anikendra 263
			// console.log('show modal');
13682 anikendra 264
			$('#myModal').find('#productToHide',0).val($(this).data('id'));
14139 anikendra 265
			//$('#myModal').modal();
13583 anikendra 266
			var url = apihost+"/user_actions/update/"+me+"/"+$(this).data('id')+"/dislike";
267
		}
268
		$.ajax({
269
			url: url,
270
			// The name of the callback parameter, as specified by the YQL service
271
			jsonp: "callback",
272
			// Tell jQuery we're expecting JSONP
273
			dataType: "jsonp",
274
			// Tell YQL what we want and that we want JSON
275
			data: {
276
				format: "json"
277
			},
278
			// Work with the response
279
			success: function( response ) {
280
				if(response.success){
13672 anikendra 281
					$(that).toggleClass('active');
282
					$(that).parent().find('li.likedeal',0).removeClass('active');
13583 anikendra 283
				}
284
			}
285
		});
13686 anikendra 286
		ga('send', 'event', 'product', 'dislike', $(this).data('id'));
13583 anikendra 287
	});
13731 anikendra 288
	$('.deletefav').on('click',function(){
289
		var that = $(this);
290
		$('#loadingModal').modal();
291
		var url = apihost+"/user_actions/deletefav/"+me+"/"+$(this).data('id');			
292
		$.ajax({
293
			url: url,
294
			// The name of the callback parameter, as specified by the YQL service
295
			jsonp: "callback",
296
			// Tell jQuery we're expecting JSONP
297
			dataType: "jsonp",
298
			// Tell YQL what we want and that we want JSON
299
			data: {
300
				format: "json"
301
			},
302
			// Work with the response
303
			success: function( response ) {
304
				$('#loadingModal').modal('hide');
305
				if(response.success){			
306
					$('#fav-'+$(that).data('id')).hide('slow');
307
				}
308
			}
309
		});
310
		ga('send', 'event', 'favourites', 'remove', $(this).data('id'));
311
	});
312
	$('.clearfavs').on('click',function(){
313
		var that = $(this);
314
		$('#loadingModal').modal();
14300 anikendra 315
		var url = apihost+"/user_actions/deleteallfavs/"+me+'/'+$(this).data('type');			
13731 anikendra 316
		$.ajax({
317
			url: url,
318
			// The name of the callback parameter, as specified by the YQL service
319
			jsonp: "callback",
320
			// Tell jQuery we're expecting JSONP
321
			dataType: "jsonp",
322
			// Tell YQL what we want and that we want JSON
323
			data: {
324
				format: "json"
325
			},
326
			// Work with the response
327
			success: function( response ) {
328
				$('#loadingModal').modal('hide');
329
				if(response.success){			
330
					$('.deletefav').each(function(){
331
						$('#fav-'+$(this).data('id')).hide('slow');
332
					})					
333
				}
334
				$(that).hide();
335
			}
336
		});
337
		ga('send', 'event', 'favourites', 'removeall', me);
338
	});
13759 anikendra 339
	$('.revealbrands').on('click',function(){
340
		$(this).parent().find('.notfeatured').toggleClass('hidden');
341
		if($(this).html()=='Others'){
342
			$(this).html('Hide');
343
		}else{
344
			$(this).html('Others');
345
		}
346
	});
14068 anikendra 347
	$(document).on('click','.creditedcashbacks',function(){
348
		if($(this).find('.glyphicon-plus',0).length>0){
349
			$(this).find('.glyphicon-plus',0).removeClass('glyphicon-plus').addClass('glyphicon-minus');
350
		}else{
351
			$(this).find('.glyphicon-minus',0).removeClass('glyphicon-minus').addClass('glyphicon-plus');
352
		}
353
		$(this).next().toggleClass('hidden');
354
	});
355
	$(document).on('click','.panel-heading',function(){
356
		if($(this).find('.glyphicon-plus',0).length>0){
357
			$(this).find('.glyphicon-plus',0).removeClass('glyphicon-plus').addClass('glyphicon-minus');
358
		}else{
359
			$(this).find('.glyphicon-minus',0).removeClass('glyphicon-minus').addClass('glyphicon-plus');
360
		}
361
	});
14135 anikendra 362
	$(document).on('click','.prefcatselect',function(){
363
		$('.prefcatselect').removeClass('active')
364
		$(this).addClass('active');
365
		$('.categorypreferences').addClass('hidden');
366
		$('#cat-'+$(this).data('id')).toggleClass('hidden');
14150 anikendra 367
	});
368
	$(document).on('click','.refresh',function(){
369
		document.location.reload();
370
	});
14215 anikendra 371
	$(document).on('click','.hasmoretext',function(){
372
		$('#fulltext').html($(this).data('fulltext'));
373
		$('#fullTextModal').modal();
374
	});
14300 anikendra 375
	$(document).on('click','.favswitch',function(){
376
		$('.favswitch').removeClass('active')
377
		$(this).addClass('active');
378
		$('.clearfavs').addClass('hidden');
379
		$('.card').addClass('hidden');
380
		$('.'+$(this).data('type')).removeClass('hidden');
381
	});
14386 anikendra 382
	$(document).on('click','.brandpreferences .brands',function(){
383
		$(this).parents('.row').siblings('.controls').find('.editcategorypreferences',0).click();
384
	});
385
	$(document).on('click','.tryagainforliveprice',function(){		
386
		fetchLivePrice($(this).parent());
387
		$(this).parent().html('Getting Live Prices');
388
	});
14579 anikendra 389
	$(document).on('click','.alert > a',function(){		
390
		ga('send', 'event', 'notifications', 'click', $(this).html());
391
	});
392
	$(document).on('click','.banner',function(){		
393
		ga('send', 'event', 'banners', 'click', $(this).data('name'));
394
	});
13992 anikendra 395
});