Subversion Repositories SmartDukaan

Rev

Rev 15077 | Rev 15128 | 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;
14773 anikendra 3
function getcashbackstring(type,value) {
14797 anikendra 4
	// console.log(type+' '+value);
14773 anikendra 5
	if(value==0){
6
		return '';
7
	}
8
	if(type==2){
14789 anikendra 9
		return ' +'+value+' Cashback';
14773 anikendra 10
	}else if(type==1){
14789 anikendra 11
		return ' +'+value+'% Cashback';
14773 anikendra 12
	}else{
13
		return '';
14
	}
15
}
16
 
13984 anikendra 17
$(function(){	
18
	if($('.scroll > .card').length>0) {
14969 anikendra 19
		setTimeout(function(){ document.location.reload(); }, 1000*15*60);
13820 anikendra 20
		$('.scroll').jscroll({
21
			loadingHtml: '<center><img src="/img/ajax-loader.gif" alt="Loading" /></center>',
22
		    autoTriggerUntil: 3,
23
		    padding: 20,
24
		});
25
	}
13901 anikendra 26
	if($('.storeminprice').length>0) {
27
		var globalminprice = 9999999;
14773 anikendra 28
		var globalminsku;
29
		var globalmincashback = '';		
14345 anikendra 30
		var calls = [];
13901 anikendra 31
		$('.storeminprice').each(function(){
14345 anikendra 32
			var temp = {'sort':$(this).data('searchorder'),'val':$(this)};
33
			calls.push(temp);
34
		});
35
		calls = sortByKey(calls,'sort');		
36
		for(var i in calls){
37
			fetchLivePrice(calls[i]['val'])
14969 anikendra 38
		}				
14345 anikendra 39
	};
40
	function sortByKey(array, key) {
41
	    return array.sort(function(a, b) {
42
	        var x = a[key]; var y = b[key];
43
	        return ((x < y) ? -1 : ((x > y) ? 1 : 0));
44
	    });
45
	};
14432 anikendra 46
	function fetchLivePrice(obj){		
14969 anikendra 47
		ga('send', 'event', 'liveprice', 'fetch', $(obj).data('bundle_id'));
14432 anikendra 48
		var that = obj;	
49
		var inStock = false;		
14386 anikendra 50
		var req = $.ajax({
14345 anikendra 51
			url: '/store_products/getliveprice/'+$(that).data('bundle_id')+'/'+$(that).data('id'),
52
			// Tell jQuery we're expecting JSONP
53
			dataType: "json",
54
			// Tell YQL what we want and that we want JSON			
55
			method: 'get',
14430 anikendra 56
			// timeout: 30000,
14345 anikendra 57
			// Work with the response
58
			success: function( response ) {
14685 anikendra 59
				// console.log(response);
14345 anikendra 60
				if(response.success){
61
					var i = 0;	
62
					var minpriceindex = 0;				
14797 anikendra 63
					if(response.products[i].in_stock == 1) {inStock = true;}
64
					if(response.products[i].in_stock == 1) {
65
						var minprice = Math.round(response.products[i]['available_price']);
66
						var minpriceurl = response.products[i]['marketPlaceUrl'];
67
						console.log(minprice);
68
					}
14345 anikendra 69
					if(response.products.length>0){
70
						//More than one products in store
71
						var variants = [];
72
						// var i = 0;
73
						for(var i in response.products){	
14685 anikendra 74
							if(response.products[i].in_stock == 1){
75
								inStock = true;
76
								// console.log(response.products[i]);
77
								if(response.products[i].available_price && response.products[i].available_price < globalminprice) {
78
									globalminprice = Math.round(response.products[i].available_price);
79
									globalminsku = response.products[i]._id;
80
									var globalminsource = response.products[i].source_id;
14773 anikendra 81
									var globalminurl = response.products[i].marketPlaceUrl;
82
									var globalmincashback = getcashbackstring(response.products[i].cash_back_type,response.products[i].cash_back);
14685 anikendra 83
								}
14797 anikendra 84
								if(typeof minprice == 'undefined' || response.products[i].available_price <= minprice) {
14685 anikendra 85
									minprice = Math.round(response.products[i].available_price);
86
									minpriceindex = i;
14700 anikendra 87
								}
14799 anikendra 88
								variants.push({'name' : response.products[i].source_product_name, '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,'cash_back_type' : response.products[i].cash_back_type,'cash_back' : response.products[i].cash_back});
14685 anikendra 89
							}						
13901 anikendra 90
						}
14685 anikendra 91
						if(variants.length>1){
14700 anikendra 92
							variants.splice(minpriceindex,1);
14713 anikendra 93
							var variantslink = $('<span class="variants"> '+(variants.length+1)+' Options</span>');
94
							$(that).parent().parent().find('.pull-left',0).append(variantslink);							
14799 anikendra 95
							$(variantslink).data('variants',variants).data('minprice',minprice);
13948 anikendra 96
						}
14345 anikendra 97
					}
98
					if(inStock){
14797 anikendra 99
						$(that).html(minprice).siblings('.cashbackamount').html(getcashbackstring(response.products[minpriceindex].cash_back_type,response.products[minpriceindex].cash_back)).parent().addClass('viewproduct');
14689 anikendra 100
						$('#name-'+$(that).data('id')).html(response.products[minpriceindex]['source_product_name']);
14685 anikendra 101
						// console.log(response.products[minpriceindex]);
102
						$('#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 103
						// console.log(globalminprice+' '+globalminsku);
14345 anikendra 104
						if(globalminprice != 9999999) {
14773 anikendra 105
							$('#bestprice').html(globalminprice).siblings('.cashbackamount').html(globalmincashback);
14685 anikendra 106
							$('#beststorelink').data('id',globalminsku).data('source',globalminsource).data('price',globalminprice).data('url',globalminurl).removeClass('hidden');
14345 anikendra 107
							$('#bestpricecontainer').removeClass('hidden');
14432 anikendra 108
						} 
14386 anikendra 109
					} else{
14969 anikendra 110
						ga('send', 'event', 'liveprice', 'outofstock', response.products[0]._id);
14432 anikendra 111
						outOfStockCount++;
14345 anikendra 112
						$(that).parent().parent().remove();	
13901 anikendra 113
					}
14345 anikendra 114
					if(!response.products[0].available_price){
14434 anikendra 115
						// outOfStockCount++;
116
						// $(that).parent().parent().remove();	
117
						var btn = $("<a class='tryagainforliveprice btn btn-xs btn-warning' title='Try Again'>Try Again</a>");
118
						$(that).html(btn);
14969 anikendra 119
						ga('send', 'event', 'liveprice', 'failed', response.products[0]._id);
120
					}					
14386 anikendra 121
				} else{
14434 anikendra 122
					// outOfStockCount++;
123
					// $(that).parent().parent().remove();
124
					var btn = $("<a class='tryagainforliveprice btn btn-xs btn-warning' title='Try Again'>Try Again</a>");
125
					$(that).html(btn);
14969 anikendra 126
					ga('send', 'event', 'liveprice', 'failure', response.products[0]._id);
13901 anikendra 127
				}
14432 anikendra 128
				checkForOutStocks(outOfStockCount);
14345 anikendra 129
			},
14386 anikendra 130
			error: function(request, status, err) {
131
				//console.log(status+' '+err+' '+request);
14430 anikendra 132
            	// if (status == "timeout") {
133
            		var btn = $("<a class='tryagainforliveprice btn btn-xs btn-warning' title='Try Again'>Try Again</a>");
14434 anikendra 134
					$(that).html(btn);
14969 anikendra 135
					ga('send', 'event', 'liveprice', 'error', response.products[0]._id);
14430 anikendra 136
				// } else {
137
					// $(that).parent().parent().remove();
138
				// }
14345 anikendra 139
			}
13901 anikendra 140
		});
14386 anikendra 141
		livePriceCalls.push(req);
14357 anikendra 142
	};
14432 anikendra 143
	function checkForOutStocks(count){
144
		if(count>=livePriceCalls.length){
145
			$('#bestpricecontainer').html('<h4>Out of stock</h4>').removeClass('hidden');	
14969 anikendra 146
		}		
14432 anikendra 147
	};
14687 anikendra 148
	$(document).on('click','.showless',function(){
149
		$(this).addClass('hidden').siblings('.varnts').hide();
150
		$(this).parent().find('.variants',0).removeClass('hidden').addClass('justshow');
151
	});
13901 anikendra 152
	$(document).on('click','.variants',function(){
153
		$('.storeproductinfo').empty();
14685 anikendra 154
		var variants = $(this).data('variants');
14799 anikendra 155
		var minprice = $(this).data('minprice');
14685 anikendra 156
		// console.log(variants);
14026 anikendra 157
		$('#variantscount').html(variants.length);
13901 anikendra 158
		for(var i in variants){
14392 anikendra 159
			// console.log(variants[i]);
14685 anikendra 160
			// 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>');
161
			// $('#storeproducts').parent().append(row);
14789 anikendra 162
			// var row = $('<div class="clearfix varnts"></div><div class="col-xs-6 varnts text-small">'+variants[i].name+'</div><div class="col-xs-4 varnts"><span class="cashbackrupee"></span> <span>'+variants[i].available_price+'</span><span class="cashbackamount">'+getcashbackstring(variants[i].cash_back_type,variants[i].cash_back)+'</span></div><div class="col-xs-2 viewproduct varnts text-right" 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>');
14849 anikendra 163
			if(minprice == variants[i].available_price) {
164
				var row = $('<div class="clearfix varnts"></div><div class="col-xs-6 varnts text-small">'+variants[i].name+'</div><div class="col-xs-6 varnts viewproduct" data-source="'+variants[i].source_id+'" data-price="'+variants[i].available_price+'" data-url="'+variants[i].url+'" data-id="'+variants[i].id+'"><span class="cashbackrupee varnts text-right" ></span> <span class="storeminprice">'+variants[i].available_price+'</span><span class="cashbackamount">'+getcashbackstring(variants[i].cash_back_type,variants[i].cash_back)+'</span><span class="pull-right arrowright"></span></div></div>');
165
			} else {
14797 anikendra 166
				var row = $('<div class="clearfix varnts"></div><div class="col-xs-6 varnts text-small">'+variants[i].name+'</div><div class="col-xs-6 varnts viewproduct" data-source="'+variants[i].source_id+'" data-price="'+variants[i].available_price+'" data-url="'+variants[i].url+'" data-id="'+variants[i].id+'"><span class="cashbackrupee varnts text-right" ></span> <span>'+variants[i].available_price+'</span><span class="cashbackamount">'+getcashbackstring(variants[i].cash_back_type,variants[i].cash_back)+'</span><span class="pull-right arrowright"></span></div></div>');
14849 anikendra 167
			}			
14687 anikendra 168
			$(this).parent().parent().append(row);			
13901 anikendra 169
		}
14713 anikendra 170
		var showless = $('<div class="showless col-xs-9 text-right">Less <i class="glyphicon glyphicon-chevron-up"></i></span>');
14687 anikendra 171
		$(this).parent().parent().append(showless);
172
		$(this).addClass('hidden');
13901 anikendra 173
	});
13695 anikendra 174
	$('.categorytab').on('click','.categorytabcontrol.active',function(){
175
		$(this).toggleClass('active');
176
		$('#preferences-'+$(this).data('id')).addClass('hidden');
177
		$(this).parent().parent().children().find('.savecategorypreferences',0).removeClass('savecategorypreferences').addClass('editcategorypreferences').html('Edit').removeClass('btn-success');
178
	});
13759 anikendra 179
	$('.categorytab').on('click','.categorytabcontrol:not(.active)',function(){
180
		$(this).parent().parent().find('.btn',0).click();
181
	});
13695 anikendra 182
	$('.categorytab').on('click','.editcategorypreferences',function(e){
183
		var that = $(this);
184
		$(that).removeClass('editcategorypreferences').addClass('savecategorypreferences').html('Done').addClass('btn-success');
185
		$('#togglepreferences-'+$(that).data('id')).toggleClass('active');
186
		$('#preferences-'+$(that).data('id')).removeClass('hidden');
187
		$('.brandselector').each(function(){
188
			if($(this).data('catid')==$(that).data('id')){
189
				$(this).addClass('active');
190
			}
191
		});
192
	});
193
	$('.categorytab').on('click','.savecategorypreferences',function(e){
194
		var that = $(this);
195
		$(that).removeClass('savecategorypreferences').addClass('editcategorypreferences').html('Edit').removeClass('btn-success');
196
		$('#togglepreferences-'+$(that).data('id')).toggleClass('active');
197
		$.ajax({
198
			url: $('#categorypreference-'+$(that).data('id')).attr('action'),
199
			data: $('#categorypreference-'+$(that).data('id')).serialize(),
200
			// Tell jQuery we're expecting JSONP
201
			dataType: "json",
202
			// Tell YQL what we want and that we want JSON			
203
			method: 'post',
204
			// Work with the response
205
			success: function( response ) {
206
				if(response.success){
207
					$('#preferences-'+$(that).data('id')).addClass('hidden');
208
				}
209
			}
210
		});
211
		ga('send', 'event', 'preferences', 'update', me);
212
	});
13901 anikendra 213
	// $('.row').on('click','.viewproduct',function(e){
214
	$(document).on('click','.viewproduct',function(e){
14386 anikendra 215
		if(typeof livePriceCalls != undefined) {
216
	    	for(var i in livePriceCalls){
14392 anikendra 217
	    		// console.log('killing ajax call '+i);
14386 anikendra 218
	    		livePriceCalls[i].abort();
219
	    	}
220
	    }
13686 anikendra 221
		$('#loadingModal').modal();
14952 anikendra 222
		if($(this).data('source')==2){
223
			//in case of flipkart, close popup after 5 seconds
224
			setTimeout(function(){ $('#loadingModal').modal('hide'); }, 1000*5);			
225
		}
14392 anikendra 226
		// console.log($(this).data('id'));
14345 anikendra 227
		var url = apihost+"clicks/add/"+me+"/"+$(this).data('id')+'/'+$(this).data('source')+'/?url='+encodeURIComponent($(this).data('url'))+'&price='+$(this).data('price');
13579 anikendra 228
		$.ajax({
229
			url: url,
230
			// The name of the callback parameter, as specified by the YQL service
231
			jsonp: "callback",
232
			// Tell jQuery we're expecting JSONP
233
			dataType: "jsonp",
234
			// Tell YQL what we want and that we want JSON
235
			data: {
236
				format: "json"
237
			},
238
			// Work with the response
239
			success: function( response ) {
240
				if(response.success && response.type=='redirect'){
15085 anikendra 241
					// document.location = response.url;
242
					document.location = '/r.html?'+response.url;
13579 anikendra 243
				}
244
			}
245
		});
14432 anikendra 246
		ga('send', 'event', 'product', 'click', $(this).data('url'));
13583 anikendra 247
	});
13719 anikendra 248
	$('.jscroll-inner').on('click','.likedeal',function(e){	
13583 anikendra 249
		var that = $(this);
13682 anikendra 250
		if($(that).hasClass('active')){
13583 anikendra 251
			//User has already liked it,so remove like
252
			var url = apihost+"/user_actions/rem/"+me+"/"+$(this).data('id')+"/like";	
253
		}else{
254
			var url = apihost+"/user_actions/update/"+me+"/"+$(this).data('id')+"/like";
255
		}
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
				if(response.success){
13672 anikendra 269
					$(that).toggleClass('active');
270
					$(that).parent().find('li.dislikedeal',0).removeClass('active');
13583 anikendra 271
				}
272
			}
273
		});
13686 anikendra 274
		ga('send', 'event', 'product', 'like', $(this).data('id'));
13583 anikendra 275
	});
13688 anikendra 276
	$('#myModal').on('click','#unlikebtn',function(e){
13682 anikendra 277
		e.preventDefault();
278
		var url = $('#unlikeproductform').attr('action')+'?'+$('#unlikeproductform').serialize()
14392 anikendra 279
		// console.log(url);
13682 anikendra 280
		$.ajax({
281
			url: url,
282
			// The name of the callback parameter, as specified by the YQL service
283
			jsonp: "callback",
284
			// Tell jQuery we're expecting JSONP
285
			dataType: "jsonp",
286
			// Tell YQL what we want and that we want JSON
287
			data: {
288
				format: "json"
289
			},
290
			// Work with the response
291
			success: function( response ) {
292
				// console.log(response);						
293
			}
294
		});
295
		$('#myModal').modal('hide');
13686 anikendra 296
		ga('send', 'event', 'brand', 'hide', $('#myModal').find('#productToHide',0).val());
13682 anikendra 297
	})
13719 anikendra 298
	$('.jscroll-inner').on('click','.dislikedeal',function(e){			
13583 anikendra 299
		var that = $(this);
13682 anikendra 300
		if($(that).hasClass('active')){
13583 anikendra 301
			//User has already liked it,so remove like
302
			var url = apihost+"/user_actions/rem/"+me+"/"+$(this).data('id')+"/dislike";	
303
		}else{
14392 anikendra 304
			// console.log('show modal');
13682 anikendra 305
			$('#myModal').find('#productToHide',0).val($(this).data('id'));
14139 anikendra 306
			//$('#myModal').modal();
13583 anikendra 307
			var url = apihost+"/user_actions/update/"+me+"/"+$(this).data('id')+"/dislike";
308
		}
309
		$.ajax({
310
			url: url,
311
			// The name of the callback parameter, as specified by the YQL service
312
			jsonp: "callback",
313
			// Tell jQuery we're expecting JSONP
314
			dataType: "jsonp",
315
			// Tell YQL what we want and that we want JSON
316
			data: {
317
				format: "json"
318
			},
319
			// Work with the response
320
			success: function( response ) {
321
				if(response.success){
13672 anikendra 322
					$(that).toggleClass('active');
323
					$(that).parent().find('li.likedeal',0).removeClass('active');
13583 anikendra 324
				}
325
			}
326
		});
13686 anikendra 327
		ga('send', 'event', 'product', 'dislike', $(this).data('id'));
13583 anikendra 328
	});
13731 anikendra 329
	$('.deletefav').on('click',function(){
330
		var that = $(this);
331
		$('#loadingModal').modal();
332
		var url = apihost+"/user_actions/deletefav/"+me+"/"+$(this).data('id');			
333
		$.ajax({
334
			url: url,
335
			// The name of the callback parameter, as specified by the YQL service
336
			jsonp: "callback",
337
			// Tell jQuery we're expecting JSONP
338
			dataType: "jsonp",
339
			// Tell YQL what we want and that we want JSON
340
			data: {
341
				format: "json"
342
			},
343
			// Work with the response
344
			success: function( response ) {
345
				$('#loadingModal').modal('hide');
346
				if(response.success){			
347
					$('#fav-'+$(that).data('id')).hide('slow');
348
				}
349
			}
350
		});
351
		ga('send', 'event', 'favourites', 'remove', $(this).data('id'));
352
	});
353
	$('.clearfavs').on('click',function(){
354
		var that = $(this);
355
		$('#loadingModal').modal();
14300 anikendra 356
		var url = apihost+"/user_actions/deleteallfavs/"+me+'/'+$(this).data('type');			
13731 anikendra 357
		$.ajax({
358
			url: url,
359
			// The name of the callback parameter, as specified by the YQL service
360
			jsonp: "callback",
361
			// Tell jQuery we're expecting JSONP
362
			dataType: "jsonp",
363
			// Tell YQL what we want and that we want JSON
364
			data: {
365
				format: "json"
366
			},
367
			// Work with the response
368
			success: function( response ) {
369
				$('#loadingModal').modal('hide');
370
				if(response.success){			
371
					$('.deletefav').each(function(){
372
						$('#fav-'+$(this).data('id')).hide('slow');
373
					})					
374
				}
375
				$(that).hide();
376
			}
377
		});
378
		ga('send', 'event', 'favourites', 'removeall', me);
379
	});
13759 anikendra 380
	$('.revealbrands').on('click',function(){
381
		$(this).parent().find('.notfeatured').toggleClass('hidden');
382
		if($(this).html()=='Others'){
383
			$(this).html('Hide');
384
		}else{
385
			$(this).html('Others');
386
		}
387
	});
14068 anikendra 388
	$(document).on('click','.creditedcashbacks',function(){
389
		if($(this).find('.glyphicon-plus',0).length>0){
390
			$(this).find('.glyphicon-plus',0).removeClass('glyphicon-plus').addClass('glyphicon-minus');
391
		}else{
392
			$(this).find('.glyphicon-minus',0).removeClass('glyphicon-minus').addClass('glyphicon-plus');
393
		}
394
		$(this).next().toggleClass('hidden');
395
	});
396
	$(document).on('click','.panel-heading',function(){
397
		if($(this).find('.glyphicon-plus',0).length>0){
398
			$(this).find('.glyphicon-plus',0).removeClass('glyphicon-plus').addClass('glyphicon-minus');
399
		}else{
400
			$(this).find('.glyphicon-minus',0).removeClass('glyphicon-minus').addClass('glyphicon-plus');
401
		}
402
	});
14135 anikendra 403
	$(document).on('click','.prefcatselect',function(){
404
		$('.prefcatselect').removeClass('active')
405
		$(this).addClass('active');
406
		$('.categorypreferences').addClass('hidden');
407
		$('#cat-'+$(this).data('id')).toggleClass('hidden');
14150 anikendra 408
	});
409
	$(document).on('click','.refresh',function(){
410
		document.location.reload();
411
	});
14215 anikendra 412
	$(document).on('click','.hasmoretext',function(){
413
		$('#fulltext').html($(this).data('fulltext'));
414
		$('#fullTextModal').modal();
415
	});
14300 anikendra 416
	$(document).on('click','.favswitch',function(){
417
		$('.favswitch').removeClass('active')
418
		$(this).addClass('active');
419
		$('.clearfavs').addClass('hidden');
420
		$('.card').addClass('hidden');
421
		$('.'+$(this).data('type')).removeClass('hidden');
422
	});
14386 anikendra 423
	$(document).on('click','.brandpreferences .brands',function(){
424
		$(this).parents('.row').siblings('.controls').find('.editcategorypreferences',0).click();
425
	});
426
	$(document).on('click','.tryagainforliveprice',function(){		
427
		fetchLivePrice($(this).parent());
428
		$(this).parent().html('Getting Live Prices');
429
	});
14579 anikendra 430
	$(document).on('click','.alert > a',function(){		
431
		ga('send', 'event', 'notifications', 'click', $(this).html());
432
	});
433
	$(document).on('click','.banner',function(){		
434
		ga('send', 'event', 'banners', 'click', $(this).data('name'));
435
	});
13992 anikendra 436
});
14849 anikendra 437
function setCookie(cname, cvalue, days) {
438
    var d = new Date();
439
    d.setTime(d.getTime() + (days*24*60*60*1000));
440
    var expires = "expires="+d.toUTCString();
15042 anikendra 441
    document.cookie = cname + "=" + cvalue + "; " + expires + "; path=/";
14849 anikendra 442
}
443
 
444
function getCookie(cname) {
445
    var name = cname + "=";
446
    var ca = document.cookie.split(';');
447
    for(var i=0; i<ca.length; i++) {
448
        var c = ca[i];
449
        while (c.charAt(0)==' ') c = c.substring(1);
450
        if (c.indexOf(name) == 0) return c.substring(name.length, c.length);
451
    }
452
    return "";
453
}
454
function showpopup(id,count,interval){
14851 anikendra 455
	var cname = 'notif-count-'+me+'-'+id;
14849 anikendra 456
	var cookieval = getCookie(cname);
14851 anikendra 457
	var cname = 'notif-lastshown-'+me+'-'+id;
14849 anikendra 458
	var lastshown = getCookie(cname);
459
	if(cookieval==''){		
15077 anikendra 460
		incrementPopupCount(id,0,'notif-count-');
461
		setLastShown(id,'notif-lastshown-');
14849 anikendra 462
		return true;
463
	} else{
464
		var d = new Date();
465
		var t = d.getTime();
466
		console.log(t-lastshown);
467
		console.log(interval*60*1000);
468
		if(t-lastshown>interval*60*1000){
469
			if(cookieval>=count){
470
				return false;
471
			}else{
15077 anikendra 472
				setLastShown(id,'notif-lastshown-');
473
				incrementPopupCount(id,cookieval,'notif-count-');
14849 anikendra 474
				return true;	
475
			}
476
		}
477
	}
478
}
15077 anikendra 479
function setLastShown(id,cookiename){
480
	var cname = cookiename+me+'-'+id;
14849 anikendra 481
	var d = new Date();
482
	setCookie(cname, d.getTime(), 365);
483
}
15077 anikendra 484
function incrementPopupCount(id,currentCount,cookiename) {
485
	var cname = cookiename+me+'-'+id;
14849 anikendra 486
	setCookie(cname, parseInt(currentCount)+1, 365);
14858 anikendra 487
}
488
var hidden, visibilityChange; 
489
function handleVisibilityChange() {
490
  if (document[hidden]) {
15058 anikendra 491
  	// pagetitle = document.title;
14858 anikendra 492
  } else {
15058 anikendra 493
  	// document.title = pagetitle;
14858 anikendra 494
  }
495
}
496
if (typeof document.hidden !== "undefined") { // Opera 12.10 and Firefox 18 and later support 
497
  hidden = "hidden";
498
  visibilityChange = "visibilitychange";
499
} else if (typeof document.mozHidden !== "undefined") {
500
  hidden = "mozHidden";
501
  visibilityChange = "mozvisibilitychange";
502
} else if (typeof document.msHidden !== "undefined") {
503
  hidden = "msHidden";
504
  visibilityChange = "msvisibilitychange";
505
} else if (typeof document.webkitHidden !== "undefined") {
506
  hidden = "webkitHidden";
507
  visibilityChange = "webkitvisibilitychange";
508
}
509
if (typeof document.addEventListener === "undefined" || 
510
  typeof document[hidden] === "undefined") {
15077 anikendra 511
  console.log("This feature requires a browser, such as Google Chrome or Firefox, that supports the Page Visibility API.");
14858 anikendra 512
} else {
513
  // Handle page visibility change   
514
  document.addEventListener(visibilityChange, handleVisibilityChange, false);
515
}    
14936 anikendra 516
$('.notificationok').on('click',function(e){
14932 anikendra 517
	e.preventDefault();
14931 anikendra 518
	$('.notificationmodal').modal('hide');
14935 anikendra 519
	setTimeout(function(){ $('.notificationmodal').modal('hide'); }, 1000);
14975 anikendra 520
	ga('send', 'event', 'popupnotification', 'ok', me);
14932 anikendra 521
	document.location = $(this).parent().prop('href');
15015 anikendra 522
});
523
$(document).on('click','.clearfilter',function(){	
524
	$('.filterbrand').each(function(){
525
		$(this).prop('checked',false);
526
	});	
15058 anikendra 527
	ga('send', 'event', 'filter', 'brand', 'clearfilter');
15015 anikendra 528
});
529
$(document).on('click','.applyfilter',function(){	
530
	var brands = [];
15026 anikendra 531
	var brandnames = [];
15015 anikendra 532
	$('.filterbrand').each(function(){
533
		if($(this).prop('checked')){
15042 anikendra 534
			// console.log($(this).parent().siblings('.brandname').html());
15015 anikendra 535
			brands.push($(this).val());
15026 anikendra 536
			brandnames.push($(this).parent().siblings('.brandname').html());
15015 anikendra 537
		}		
538
	});
539
	if(brands.length==0){
540
		$('#message').html('Please choose a few brands first').removeClass('hidden');
541
		setTimeout(function(){ $('#message').addClass('hidden'); }, 3000);
542
		return false;
543
	}else{
15058 anikendra 544
		setCookie('brandschosen', brands, 1);
15015 anikendra 545
		var url = $('#brandselecter').prop('action');
15026 anikendra 546
		ga('send', 'event', 'filter', 'brand', brandnames.join('|'));
15085 anikendra 547
		var postdata = {'user_id':me,'type':'brand','filters':brandnames.join('|')};
548
		$.post( "/user_filters/add", postdata, function( data ) {
549
		  document.location = url+'&brands='+brands.join('^');
550
		});		
15015 anikendra 551
	}	
552
});
553
$(document).on('click','#showallbrands',function(){	
554
	$('.hidden').removeClass('hidden');
555
	$(this).hide();
15019 anikendra 556
});
557
$(document).on('input','#brandfilter',function(){	
558
	$('.brand').addClass('hidden');
559
	$('#showallbrands').hide();
560
	var that = $(this);
561
	$('li.brand').filter(function() { 
562
	  return $(this).data("brand").toLowerCase().indexOf($(that).val().toLowerCase()) != -1;
563
	}).removeClass('hidden','slow');
15042 anikendra 564
});
15077 anikendra 565
$(document).on('click','.filterbrand',function(){
566
	var clicked = $(this).parent().parent();
567
	console.log(clicked.prop('class'));
568
	// clicked.find('.filterbrand',0).prop('checked',true);
15042 anikendra 569
	// all the LIs above the clicked one
570
	var previousAll = clicked.prevAll();
571
 
572
	// only proceed if it's not already on top (no previous siblings)
573
	if(previousAll.length > 0) {
574
	// top LI
575
	var top = $(previousAll[previousAll.length - 1]);
576
 
577
	// immediately previous LI
578
	var previous = $(previousAll[0]);
579
 
580
	// how far up do we need to move the clicked LI?
581
	var moveUp = clicked.attr('offsetTop') - top.attr('offsetTop');
582
 
583
	// how far down do we need to move the previous siblings?
584
	var moveDown = (clicked.offset().top + clicked.outerHeight()) - (previous.offset().top + previous.outerHeight());
585
 
586
	// let's move stuff
587
	clicked.css('position', 'relative');
588
	previousAll.css('position', 'relative');
589
	clicked.animate({'top': -moveUp});
590
	previousAll.animate({'top': moveDown}, {complete: function() {
591
	  // rearrange the DOM and restore positioning when we're done moving
592
	  clicked.parent().prepend(clicked);
593
	  clicked.css({'position': 'static', 'top': 0});
594
	  previousAll.css({'position': 'static', 'top': 0}); 
595
	}});
596
	}
597
})
598
$(document).on('click','.clearfilters',function(){
599
	setCookie('brandschosen', '', -1);
15058 anikendra 600
	ga('send', 'event', 'filter', 'brand', 'clearfilters');
15085 anikendra 601
	var postdata = {'user_id':me,'type':'clear'};
602
	$.post("/user_filters/add", postdata, function( data ) {
603
	  document.location.reload();
604
	});	
15042 anikendra 605
});
606
function selectChosenBrands(){
607
	var brandschosen = getCookie('brandschosen');
608
	if(brandschosen && brandschosen.length>0){
609
		var brands = brandschosen.split(',');
610
		for(var i in brands){
611
			$('.brand').filter(function() { 
612
			  	return $(this).data("brandid") == brands[i];
15077 anikendra 613
			}).removeClass('hidden').find('.filterbrand',0).trigger( "click" );
15042 anikendra 614
		}
615
	}
15065 anikendra 616
}
617
$(function () {
15077 anikendra 618
	var poid = 'popovertrigger';
619
	/*
15065 anikendra 620
  var popovershown = getCookie('popovershown');
621
  if(!popovershown){
15077 anikendra 622
  	popovershown = 0;
623
  }
624
  if(popovershown < 2){
15065 anikendra 625
  	$('[data-toggle="popover"]').popover({'html':true});
626
  	$('[data-toggle="popover"]').trigger('click');
627
  }  
15077 anikendra 628
  */
15065 anikendra 629
  $('#dropdownMenu1').on('click',function(){
630
  	$('#popovertrigger').popover('hide');
15077 anikendra 631
  	incrementPopupCount(poid,0,'popover-count-');
632
	setLastShown(poid,'popover-lastshown-');
15065 anikendra 633
  })
15077 anikendra 634
 
635
  	var cname = 'popover-count-'+me+'-'+poid;
636
	var cookieval = getCookie(cname);
637
	var cname = 'popover-lastshown-'+me+'-'+poid;
638
	var lastshown = getCookie(cname);
639
	if(cookieval==''){				
640
		showpopover(poid);
641
	} else{
642
		var d = new Date();
643
		var t = d.getTime();
644
		console.log('time elapsed '+(t-lastshown));
15085 anikendra 645
		if(t-lastshown>18*3600*1000){
15077 anikendra 646
			if(cookieval>2){
647
				return false;
648
			}else{
649
				showpopover(poid);
650
			  	incrementPopupCount(poid,cookieval,'popover-count-');
651
				setLastShown(poid,'popover-lastshown-');
652
			}
653
		}
654
	}
655
});
656
function showpopover(id){
657
	$('[data-toggle="popover"]#'+id).popover({'html':true});
658
	$('[data-toggle="popover"]#'+id).trigger('click');		
15085 anikendra 659
}
660
$(document).on('click','.nodeals',function(){
661
	document.location = $(this).data('href');
662
})