Subversion Repositories SmartDukaan

Rev

Rev 15311 | Rev 15521 | 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) {
4
	if(value==0){
5
		return '';
6
	}
7
	if(type==2){
14789 anikendra 8
		return ' +'+value+' Cashback';
14773 anikendra 9
	}else if(type==1){
14789 anikendra 10
		return ' +'+value+'% Cashback';
14773 anikendra 11
	}else{
12
		return '';
13
	}
14
}
15
 
13984 anikendra 16
$(function(){	
17
	if($('.scroll > .card').length>0) {
14969 anikendra 18
		setTimeout(function(){ document.location.reload(); }, 1000*15*60);
13820 anikendra 19
		$('.scroll').jscroll({
20
			loadingHtml: '<center><img src="/img/ajax-loader.gif" alt="Loading" /></center>',
21
		    autoTriggerUntil: 3,
22
		    padding: 20,
23
		});
24
	}
13901 anikendra 25
	if($('.storeminprice').length>0) {
26
		var globalminprice = 9999999;
14773 anikendra 27
		var globalminsku;
28
		var globalmincashback = '';		
14345 anikendra 29
		var calls = [];
13901 anikendra 30
		$('.storeminprice').each(function(){
14345 anikendra 31
			var temp = {'sort':$(this).data('searchorder'),'val':$(this)};
32
			calls.push(temp);
33
		});
34
		calls = sortByKey(calls,'sort');		
35
		for(var i in calls){
36
			fetchLivePrice(calls[i]['val'])
14969 anikendra 37
		}				
14345 anikendra 38
	};
39
	function sortByKey(array, key) {
40
	    return array.sort(function(a, b) {
41
	        var x = a[key]; var y = b[key];
42
	        return ((x < y) ? -1 : ((x > y) ? 1 : 0));
43
	    });
44
	};
14432 anikendra 45
	function fetchLivePrice(obj){		
14969 anikendra 46
		ga('send', 'event', 'liveprice', 'fetch', $(obj).data('bundle_id'));
14432 anikendra 47
		var that = obj;	
48
		var inStock = false;		
14386 anikendra 49
		var req = $.ajax({
14345 anikendra 50
			url: '/store_products/getliveprice/'+$(that).data('bundle_id')+'/'+$(that).data('id'),
51
			// Tell jQuery we're expecting JSONP
52
			dataType: "json",
53
			// Tell YQL what we want and that we want JSON			
54
			method: 'get',
14430 anikendra 55
			// timeout: 30000,
14345 anikendra 56
			// Work with the response
57
			success: function( response ) {
58
				if(response.success){
59
					var i = 0;	
60
					var minpriceindex = 0;				
14797 anikendra 61
					if(response.products[i].in_stock == 1) {inStock = true;}
62
					if(response.products[i].in_stock == 1) {
63
						var minprice = Math.round(response.products[i]['available_price']);
64
						var minpriceurl = response.products[i]['marketPlaceUrl'];
65
					}
14345 anikendra 66
					if(response.products.length>0){
67
						//More than one products in store
68
						var variants = [];
69
						// var i = 0;
70
						for(var i in response.products){	
14685 anikendra 71
							if(response.products[i].in_stock == 1){
72
								inStock = true;
73
								if(response.products[i].available_price && response.products[i].available_price < globalminprice) {
74
									globalminprice = Math.round(response.products[i].available_price);
75
									globalminsku = response.products[i]._id;
76
									var globalminsource = response.products[i].source_id;
14773 anikendra 77
									var globalminurl = response.products[i].marketPlaceUrl;
78
									var globalmincashback = getcashbackstring(response.products[i].cash_back_type,response.products[i].cash_back);
14685 anikendra 79
								}
14797 anikendra 80
								if(typeof minprice == 'undefined' || response.products[i].available_price <= minprice) {
14685 anikendra 81
									minprice = Math.round(response.products[i].available_price);
82
									minpriceindex = i;
14700 anikendra 83
								}
14799 anikendra 84
								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 85
							}						
13901 anikendra 86
						}
14685 anikendra 87
						if(variants.length>1){
14700 anikendra 88
							variants.splice(minpriceindex,1);
14713 anikendra 89
							var variantslink = $('<span class="variants"> '+(variants.length+1)+' Options</span>');
90
							$(that).parent().parent().find('.pull-left',0).append(variantslink);							
14799 anikendra 91
							$(variantslink).data('variants',variants).data('minprice',minprice);
13948 anikendra 92
						}
14345 anikendra 93
					}
94
					if(inStock){
14797 anikendra 95
						$(that).html(minprice).siblings('.cashbackamount').html(getcashbackstring(response.products[minpriceindex].cash_back_type,response.products[minpriceindex].cash_back)).parent().addClass('viewproduct');
14689 anikendra 96
						$('#name-'+$(that).data('id')).html(response.products[minpriceindex]['source_product_name']);
14685 anikendra 97
						$('#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');
14345 anikendra 98
						if(globalminprice != 9999999) {
14773 anikendra 99
							$('#bestprice').html(globalminprice).siblings('.cashbackamount').html(globalmincashback);
14685 anikendra 100
							$('#beststorelink').data('id',globalminsku).data('source',globalminsource).data('price',globalminprice).data('url',globalminurl).removeClass('hidden');
14345 anikendra 101
							$('#bestpricecontainer').removeClass('hidden');
14432 anikendra 102
						} 
14386 anikendra 103
					} else{
14969 anikendra 104
						ga('send', 'event', 'liveprice', 'outofstock', response.products[0]._id);
14432 anikendra 105
						outOfStockCount++;
14345 anikendra 106
						$(that).parent().parent().remove();	
13901 anikendra 107
					}
14345 anikendra 108
					if(!response.products[0].available_price){
14434 anikendra 109
						// outOfStockCount++;
110
						// $(that).parent().parent().remove();	
111
						var btn = $("<a class='tryagainforliveprice btn btn-xs btn-warning' title='Try Again'>Try Again</a>");
112
						$(that).html(btn);
14969 anikendra 113
						ga('send', 'event', 'liveprice', 'failed', response.products[0]._id);
114
					}					
14386 anikendra 115
				} else{
14434 anikendra 116
					// outOfStockCount++;
117
					// $(that).parent().parent().remove();
118
					var btn = $("<a class='tryagainforliveprice btn btn-xs btn-warning' title='Try Again'>Try Again</a>");
119
					$(that).html(btn);
14969 anikendra 120
					ga('send', 'event', 'liveprice', 'failure', response.products[0]._id);
13901 anikendra 121
				}
14432 anikendra 122
				checkForOutStocks(outOfStockCount);
14345 anikendra 123
			},
15310 anikendra 124
			error: function(request, status, err) {            	
125
        		var btn = $("<a class='tryagainforliveprice btn btn-xs btn-warning' title='Try Again'>Try Again</a>");
126
				$(that).html(btn);
127
				try{
128
					if(response){
129
						ga('send', 'event', 'liveprice', 'error', response.products[0]._id);
130
					}						
131
				} catch(e){}
14345 anikendra 132
			}
13901 anikendra 133
		});
14386 anikendra 134
		livePriceCalls.push(req);
14357 anikendra 135
	};
14432 anikendra 136
	function checkForOutStocks(count){
137
		if(count>=livePriceCalls.length){
138
			$('#bestpricecontainer').html('<h4>Out of stock</h4>').removeClass('hidden');	
14969 anikendra 139
		}		
14432 anikendra 140
	};
14687 anikendra 141
	$(document).on('click','.showless',function(){
142
		$(this).addClass('hidden').siblings('.varnts').hide();
143
		$(this).parent().find('.variants',0).removeClass('hidden').addClass('justshow');
144
	});
13901 anikendra 145
	$(document).on('click','.variants',function(){
146
		$('.storeproductinfo').empty();
14685 anikendra 147
		var variants = $(this).data('variants');
14799 anikendra 148
		var minprice = $(this).data('minprice');
14026 anikendra 149
		$('#variantscount').html(variants.length);
13901 anikendra 150
		for(var i in variants){
14849 anikendra 151
			if(minprice == variants[i].available_price) {
152
				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>');
153
			} else {
14797 anikendra 154
				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 155
			}			
14687 anikendra 156
			$(this).parent().parent().append(row);			
13901 anikendra 157
		}
14713 anikendra 158
		var showless = $('<div class="showless col-xs-9 text-right">Less <i class="glyphicon glyphicon-chevron-up"></i></span>');
14687 anikendra 159
		$(this).parent().parent().append(showless);
160
		$(this).addClass('hidden');
13901 anikendra 161
	});
13695 anikendra 162
	$('.categorytab').on('click','.categorytabcontrol.active',function(){
163
		$(this).toggleClass('active');
164
		$('#preferences-'+$(this).data('id')).addClass('hidden');
165
		$(this).parent().parent().children().find('.savecategorypreferences',0).removeClass('savecategorypreferences').addClass('editcategorypreferences').html('Edit').removeClass('btn-success');
166
	});
13759 anikendra 167
	$('.categorytab').on('click','.categorytabcontrol:not(.active)',function(){
168
		$(this).parent().parent().find('.btn',0).click();
169
	});
13695 anikendra 170
	$('.categorytab').on('click','.editcategorypreferences',function(e){
171
		var that = $(this);
172
		$(that).removeClass('editcategorypreferences').addClass('savecategorypreferences').html('Done').addClass('btn-success');
173
		$('#togglepreferences-'+$(that).data('id')).toggleClass('active');
174
		$('#preferences-'+$(that).data('id')).removeClass('hidden');
175
		$('.brandselector').each(function(){
176
			if($(this).data('catid')==$(that).data('id')){
177
				$(this).addClass('active');
178
			}
179
		});
180
	});
181
	$('.categorytab').on('click','.savecategorypreferences',function(e){
182
		var that = $(this);
183
		$(that).removeClass('savecategorypreferences').addClass('editcategorypreferences').html('Edit').removeClass('btn-success');
184
		$('#togglepreferences-'+$(that).data('id')).toggleClass('active');
185
		$.ajax({
186
			url: $('#categorypreference-'+$(that).data('id')).attr('action'),
187
			data: $('#categorypreference-'+$(that).data('id')).serialize(),
188
			// Tell jQuery we're expecting JSONP
189
			dataType: "json",
190
			// Tell YQL what we want and that we want JSON			
191
			method: 'post',
192
			// Work with the response
193
			success: function( response ) {
194
				if(response.success){
195
					$('#preferences-'+$(that).data('id')).addClass('hidden');
196
				}
197
			}
198
		});
199
		ga('send', 'event', 'preferences', 'update', me);
200
	});
13901 anikendra 201
	$(document).on('click','.viewproduct',function(e){
14386 anikendra 202
		if(typeof livePriceCalls != undefined) {
203
	    	for(var i in livePriceCalls){
204
	    		livePriceCalls[i].abort();
205
	    	}
206
	    }
13686 anikendra 207
		$('#loadingModal').modal();
14952 anikendra 208
		if($(this).data('source')==2){
209
			//in case of flipkart, close popup after 5 seconds
15311 anikendra 210
			setTimeout(function(){ $('#loadingModal').modal('hide');$('#customMessageModal').modal('hide'); }, 1000*10);			
14952 anikendra 211
		}
15159 anikendra 212
		var store = $(this).data('source');
14345 anikendra 213
		var url = apihost+"clicks/add/"+me+"/"+$(this).data('id')+'/'+$(this).data('source')+'/?url='+encodeURIComponent($(this).data('url'))+'&price='+$(this).data('price');
13579 anikendra 214
		$.ajax({
215
			url: url,
216
			// The name of the callback parameter, as specified by the YQL service
217
			jsonp: "callback",
218
			// Tell jQuery we're expecting JSONP
219
			dataType: "jsonp",
220
			// Tell YQL what we want and that we want JSON
221
			data: {
222
				format: "json"
223
			},
224
			// Work with the response
225
			success: function( response ) {
226
				if(response.success && response.type=='redirect'){
15159 anikendra 227
					if(store == 1) {
15203 anikendra 228
						document.location = '/r.html?'+Base64.encode(response.url);
15310 anikendra 229
					} else if (store == 3 || store == 2) {
230
						if (store == 2){
231
							response.url = 'http://mobilehotindia.com/r.html?'+Base64.encode(response.url);
232
						}
15188 anikendra 233
						if(response.showmessage == 1) {
234
							$('#loadingModal').modal('hide');
235
							$('#customMessage').html(response.message);
15310 anikendra 236
							$('#customMessageModal').modal('show');							
15311 anikendra 237
							setTimeout(function(){ document.location = response.url;}, 1000*3);			
15310 anikendra 238
						} else{
15206 anikendra 239
							document.location = response.url;	
15188 anikendra 240
						}
15203 anikendra 241
					} else {
15128 anikendra 242
						document.location = response.url;
243
					}										
13579 anikendra 244
				}
15310 anikendra 245
			},
246
			error: function() {
247
				document.location = url;
13579 anikendra 248
			}
249
		});
14432 anikendra 250
		ga('send', 'event', 'product', 'click', $(this).data('url'));
13583 anikendra 251
	});
13719 anikendra 252
	$('.jscroll-inner').on('click','.likedeal',function(e){	
13583 anikendra 253
		var that = $(this);
13682 anikendra 254
		if($(that).hasClass('active')){
13583 anikendra 255
			//User has already liked it,so remove like
256
			var url = apihost+"/user_actions/rem/"+me+"/"+$(this).data('id')+"/like";	
257
		}else{
258
			var url = apihost+"/user_actions/update/"+me+"/"+$(this).data('id')+"/like";
259
		}
260
		$.ajax({
261
			url: url,
262
			// The name of the callback parameter, as specified by the YQL service
263
			jsonp: "callback",
264
			// Tell jQuery we're expecting JSONP
265
			dataType: "jsonp",
266
			// Tell YQL what we want and that we want JSON
267
			data: {
268
				format: "json"
269
			},
270
			// Work with the response
271
			success: function( response ) {
272
				if(response.success){
13672 anikendra 273
					$(that).toggleClass('active');
274
					$(that).parent().find('li.dislikedeal',0).removeClass('active');
13583 anikendra 275
				}
276
			}
277
		});
13686 anikendra 278
		ga('send', 'event', 'product', 'like', $(this).data('id'));
13583 anikendra 279
	});
13688 anikendra 280
	$('#myModal').on('click','#unlikebtn',function(e){
13682 anikendra 281
		e.preventDefault();
282
		var url = $('#unlikeproductform').attr('action')+'?'+$('#unlikeproductform').serialize()
283
		$.ajax({
284
			url: url,
285
			// The name of the callback parameter, as specified by the YQL service
286
			jsonp: "callback",
287
			// Tell jQuery we're expecting JSONP
288
			dataType: "jsonp",
289
			// Tell YQL what we want and that we want JSON
290
			data: {
291
				format: "json"
292
			},
293
			// Work with the response
294
			success: function( response ) {
295
			}
296
		});
297
		$('#myModal').modal('hide');
13686 anikendra 298
		ga('send', 'event', 'brand', 'hide', $('#myModal').find('#productToHide',0).val());
13682 anikendra 299
	})
13719 anikendra 300
	$('.jscroll-inner').on('click','.dislikedeal',function(e){			
13583 anikendra 301
		var that = $(this);
13682 anikendra 302
		if($(that).hasClass('active')){
13583 anikendra 303
			//User has already liked it,so remove like
304
			var url = apihost+"/user_actions/rem/"+me+"/"+$(this).data('id')+"/dislike";	
305
		}else{
13682 anikendra 306
			$('#myModal').find('#productToHide',0).val($(this).data('id'));
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
});
15310 anikendra 437
function setCookie(cname, cvalue, days, forceCookie) {
438
	if(typeof(Storage) !== "undefined" && !forceCookie) {
439
		localStorage.setItem(cname, cvalue);
440
	} else{
441
		var d = new Date();
442
	    d.setTime(d.getTime() + (days*24*60*60*1000));
443
	    var expires = "expires="+d.toUTCString();
444
	    document.cookie = cname + "=" + cvalue + "; " + expires + "; path=/";
445
	}    
14849 anikendra 446
}
447
 
15310 anikendra 448
function getCookie(cname,forceCookie) {
449
	if(typeof(Storage) !== "undefined" && !forceCookie) {
450
	    return localStorage.getItem(cname);
451
	} else {	   
452
	    var name = cname + "=";
453
	    var ca = document.cookie.split(';');
454
	    for(var i=0; i<ca.length; i++) {
455
	        var c = ca[i];
456
	        while (c.charAt(0)==' ') c = c.substring(1);
457
	        if (c.indexOf(name) == 0) return c.substring(name.length, c.length);
458
	    }
459
	    return "";
460
	}
14849 anikendra 461
}
15310 anikendra 462
 
14849 anikendra 463
function showpopup(id,count,interval){
14851 anikendra 464
	var cname = 'notif-count-'+me+'-'+id;
15456 anikendra 465
	var cookieval = getCookie(cname,0);
14851 anikendra 466
	var cname = 'notif-lastshown-'+me+'-'+id;
15456 anikendra 467
	var lastshown = getCookie(cname,0);
15310 anikendra 468
	if(!cookieval || cookieval == 'NaN' || cookieval==''){		
15077 anikendra 469
		incrementPopupCount(id,0,'notif-count-');
470
		setLastShown(id,'notif-lastshown-');
14849 anikendra 471
		return true;
472
	} else{
473
		var d = new Date();
474
		var t = d.getTime();
475
		if(t-lastshown>interval*60*1000){
476
			if(cookieval>=count){
477
				return false;
478
			}else{
15077 anikendra 479
				setLastShown(id,'notif-lastshown-');
480
				incrementPopupCount(id,cookieval,'notif-count-');
14849 anikendra 481
				return true;	
482
			}
483
		}
484
	}
485
}
15077 anikendra 486
function setLastShown(id,cookiename){
487
	var cname = cookiename+me+'-'+id;
14849 anikendra 488
	var d = new Date();
489
	setCookie(cname, d.getTime(), 365);
490
}
15077 anikendra 491
function incrementPopupCount(id,currentCount,cookiename) {
492
	var cname = cookiename+me+'-'+id;
14849 anikendra 493
	setCookie(cname, parseInt(currentCount)+1, 365);
14858 anikendra 494
}
495
var hidden, visibilityChange; 
496
function handleVisibilityChange() {
497
  if (document[hidden]) {
15058 anikendra 498
  	// pagetitle = document.title;
14858 anikendra 499
  } else {
15058 anikendra 500
  	// document.title = pagetitle;
14858 anikendra 501
  }
502
}
503
if (typeof document.hidden !== "undefined") { // Opera 12.10 and Firefox 18 and later support 
504
  hidden = "hidden";
505
  visibilityChange = "visibilitychange";
506
} else if (typeof document.mozHidden !== "undefined") {
507
  hidden = "mozHidden";
508
  visibilityChange = "mozvisibilitychange";
509
} else if (typeof document.msHidden !== "undefined") {
510
  hidden = "msHidden";
511
  visibilityChange = "msvisibilitychange";
512
} else if (typeof document.webkitHidden !== "undefined") {
513
  hidden = "webkitHidden";
514
  visibilityChange = "webkitvisibilitychange";
515
}
516
if (typeof document.addEventListener === "undefined" || 
517
  typeof document[hidden] === "undefined") {
15077 anikendra 518
  console.log("This feature requires a browser, such as Google Chrome or Firefox, that supports the Page Visibility API.");
14858 anikendra 519
} else {
520
  // Handle page visibility change   
521
  document.addEventListener(visibilityChange, handleVisibilityChange, false);
522
}    
14936 anikendra 523
$('.notificationok').on('click',function(e){
14932 anikendra 524
	e.preventDefault();
14931 anikendra 525
	$('.notificationmodal').modal('hide');
15159 anikendra 526
	// setTimeout(function(){ $('.notificationmodal').modal('hide'); }, 1000);
14975 anikendra 527
	ga('send', 'event', 'popupnotification', 'ok', me);
14932 anikendra 528
	document.location = $(this).parent().prop('href');
15015 anikendra 529
});
530
$(document).on('click','.clearfilter',function(){	
531
	$('.filterbrand').each(function(){
532
		$(this).prop('checked',false);
533
	});	
15058 anikendra 534
	ga('send', 'event', 'filter', 'brand', 'clearfilter');
15015 anikendra 535
});
536
$(document).on('click','.applyfilter',function(){	
537
	var brands = [];
15026 anikendra 538
	var brandnames = [];
15015 anikendra 539
	$('.filterbrand').each(function(){
540
		if($(this).prop('checked')){
541
			brands.push($(this).val());
15026 anikendra 542
			brandnames.push($(this).parent().siblings('.brandname').html());
15015 anikendra 543
		}		
544
	});
545
	if(brands.length==0){
546
		$('#message').html('Please choose a few brands first').removeClass('hidden');
547
		setTimeout(function(){ $('#message').addClass('hidden'); }, 3000);
548
		return false;
549
	}else{
15310 anikendra 550
		setCookie('brandschosen', brands, 1, true);
15015 anikendra 551
		var url = $('#brandselecter').prop('action');
15026 anikendra 552
		ga('send', 'event', 'filter', 'brand', brandnames.join('|'));
15085 anikendra 553
		var postdata = {'user_id':me,'type':'brand','filters':brandnames.join('|')};
554
		$.post( "/user_filters/add", postdata, function( data ) {
555
		  document.location = url+'&brands='+brands.join('^');
556
		});		
15015 anikendra 557
	}	
558
});
559
$(document).on('click','#showallbrands',function(){	
560
	$('.hidden').removeClass('hidden');
561
	$(this).hide();
15019 anikendra 562
});
563
$(document).on('input','#brandfilter',function(){	
564
	$('.brand').addClass('hidden');
565
	$('#showallbrands').hide();
566
	var that = $(this);
567
	$('li.brand').filter(function() { 
568
	  return $(this).data("brand").toLowerCase().indexOf($(that).val().toLowerCase()) != -1;
569
	}).removeClass('hidden','slow');
15042 anikendra 570
});
15077 anikendra 571
$(document).on('click','.filterbrand',function(){
572
	var clicked = $(this).parent().parent();
15042 anikendra 573
	// all the LIs above the clicked one
574
	var previousAll = clicked.prevAll();
575
 
576
	// only proceed if it's not already on top (no previous siblings)
577
	if(previousAll.length > 0) {
578
	// top LI
579
	var top = $(previousAll[previousAll.length - 1]);
580
 
581
	// immediately previous LI
582
	var previous = $(previousAll[0]);
583
 
584
	// how far up do we need to move the clicked LI?
585
	var moveUp = clicked.attr('offsetTop') - top.attr('offsetTop');
586
 
587
	// how far down do we need to move the previous siblings?
588
	var moveDown = (clicked.offset().top + clicked.outerHeight()) - (previous.offset().top + previous.outerHeight());
589
 
590
	// let's move stuff
591
	clicked.css('position', 'relative');
592
	previousAll.css('position', 'relative');
593
	clicked.animate({'top': -moveUp});
594
	previousAll.animate({'top': moveDown}, {complete: function() {
595
	  // rearrange the DOM and restore positioning when we're done moving
596
	  clicked.parent().prepend(clicked);
597
	  clicked.css({'position': 'static', 'top': 0});
598
	  previousAll.css({'position': 'static', 'top': 0}); 
599
	}});
600
	}
601
})
602
$(document).on('click','.clearfilters',function(){
15310 anikendra 603
	setCookie('brandschosen', '', -1, true);
15058 anikendra 604
	ga('send', 'event', 'filter', 'brand', 'clearfilters');
15085 anikendra 605
	var postdata = {'user_id':me,'type':'clear'};
606
	$.post("/user_filters/add", postdata, function( data ) {
607
	  document.location.reload();
608
	});	
15042 anikendra 609
});
610
function selectChosenBrands(){
15310 anikendra 611
	var brandschosen = getCookie('brandschosen',true);
15042 anikendra 612
	if(brandschosen && brandschosen.length>0){
613
		var brands = brandschosen.split(',');
614
		for(var i in brands){
615
			$('.brand').filter(function() { 
616
			  	return $(this).data("brandid") == brands[i];
15077 anikendra 617
			}).removeClass('hidden').find('.filterbrand',0).trigger( "click" );
15042 anikendra 618
		}
619
	}
15065 anikendra 620
}
621
$(function () {
15077 anikendra 622
	var poid = 'popovertrigger';
15310 anikendra 623
	var cname = 'popover-count-'+me+'-'+poid;
15456 anikendra 624
	var cookieval = getCookie(cname,0);
15310 anikendra 625
	$('#dropdownMenu1').on('click',function(){
626
		var cname = 'popover-count-'+me+'-'+poid;
15456 anikendra 627
		var cookieval = getCookie(cname,0);
15310 anikendra 628
		$('#popovertrigger').popover('hide');
629
		if(!cookieval || cookieval == 'NaN' || cookieval == ''){
630
			incrementPopupCount(poid,0,'popover-count-');
631
		} else {
632
			incrementPopupCount(poid, cookieval, 'popover-count-');
633
		}
634
		setLastShown(poid,'popover-lastshown-');
635
	})
636
 
15077 anikendra 637
	var cname = 'popover-lastshown-'+me+'-'+poid;
15456 anikendra 638
	var lastshown = getCookie(cname,0);
15310 anikendra 639
	if(cookieval=='' || cookieval == 'NaN' || !cookieval){				
15077 anikendra 640
		showpopover(poid);
641
	} else{
642
		var d = new Date();
643
		var t = d.getTime();
15085 anikendra 644
		if(t-lastshown>18*3600*1000){
15310 anikendra 645
			if(cookieval>=2){
15077 anikendra 646
				return false;
647
			}else{
15310 anikendra 648
				showpopover(poid);				
15077 anikendra 649
			}
650
		}
651
	}
652
});
653
function showpopover(id){
654
	$('[data-toggle="popover"]#'+id).popover({'html':true});
655
	$('[data-toggle="popover"]#'+id).trigger('click');		
15085 anikendra 656
}
657
$(document).on('click','.nodeals',function(){
658
	document.location = $(this).data('href');
15128 anikendra 659
})
660
var Base64 = {
661
    _keyStr: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
662
    encode: function(input) {
663
        var output = "";
664
        var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
665
        var i = 0;
666
        input = Base64._utf8_encode(input);
667
        while (i < input.length) {
668
            chr1 = input.charCodeAt(i++);
669
            chr2 = input.charCodeAt(i++);
670
            chr3 = input.charCodeAt(i++);
671
 
672
            enc1 = chr1 >> 2;
673
            enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
674
            enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
675
            enc4 = chr3 & 63;
676
            if (isNaN(chr2)) {
677
                enc3 = enc4 = 64;
678
            } else if (isNaN(chr3)) {
679
                enc4 = 64;
680
            }
681
            output = output + this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) + this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
682
        }
683
        return output;
684
    },
685
 
686
 
687
    decode: function(input) {
688
        var output = "";
689
        var chr1, chr2, chr3;
690
        var enc1, enc2, enc3, enc4;
691
        var i = 0;
692
        input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
693
        while (i < input.length) {
694
            enc1 = this._keyStr.indexOf(input.charAt(i++));
695
            enc2 = this._keyStr.indexOf(input.charAt(i++));
696
            enc3 = this._keyStr.indexOf(input.charAt(i++));
697
            enc4 = this._keyStr.indexOf(input.charAt(i++));
698
            chr1 = (enc1 << 2) | (enc2 >> 4);
699
            chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
700
            chr3 = ((enc3 & 3) << 6) | enc4;
701
            output = output + String.fromCharCode(chr1);
702
            if (enc3 != 64) {
703
                output = output + String.fromCharCode(chr2);
704
            }
705
            if (enc4 != 64) {
706
                output = output + String.fromCharCode(chr3);
707
            }
708
        }
709
        output = Base64._utf8_decode(output);
710
        return output;
711
    },
712
 
713
    _utf8_encode: function(string) {
714
        string = string.replace(/\r\n/g, "\n");
715
        var utftext = "";
716
        for (var n = 0; n < string.length; n++) {
717
            var c = string.charCodeAt(n);
718
            if (c < 128) {
719
                utftext += String.fromCharCode(c);
720
            }
721
            else if ((c > 127) && (c < 2048)) {
722
                utftext += String.fromCharCode((c >> 6) | 192);
723
                utftext += String.fromCharCode((c & 63) | 128);
724
            }
725
            else {
726
                utftext += String.fromCharCode((c >> 12) | 224);
727
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
728
                utftext += String.fromCharCode((c & 63) | 128);
729
            }
730
        }
731
        return utftext;
732
    },
733
 
734
    _utf8_decode: function(utftext) {
735
        var string = "";
736
        var i = 0;
737
        var c = c1 = c2 = 0;
738
        while (i < utftext.length) {
739
            c = utftext.charCodeAt(i);
740
            if (c < 128) {
741
                string += String.fromCharCode(c);
742
                i++;
743
            }
744
            else if ((c > 191) && (c < 224)) {
745
                c2 = utftext.charCodeAt(i + 1);
746
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
747
                i += 2;
748
            }
749
            else {
750
                c2 = utftext.charCodeAt(i + 1);
751
                c3 = utftext.charCodeAt(i + 2);
752
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
753
                i += 3;
754
            }
755
        }
756
        return string;
757
    }
758
}