Subversion Repositories SmartDukaan

Rev

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

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