/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */


  var siteurl='https://www.venuelook.com';
  var urlSearch =siteurl+'/website/search/default/venues';
  var catalog = {    
			facetJson: [],
			setUrl: function(b) {
				//alert(b)
				//lastViewedCatalogDetail.removeCatalogCookie();
					var a = location.protocol + "//" + location.host;
					if (b == "" || b == "/") {
						window.location.href = a;
						return false
					}
					window.history.pushState("object or string",b, b);
					catalog.getProducts(b)
			   
			},
			getProducts: function(a) {
			    $(".ajxload").css("display", "block");
				if (catalog.productReq != "") {
					//catalog.productReq.abort()
				}
				a += a.indexOf("?") == -1 ? "?ajax=1" : "&ajax=1";
				catalog.productReq = $.ajax({
					url: a,
					type: "get",
					success: function(b) {
					   	$(".ajxload").css("display", "none");
						$("#ajax_data").html(b);
						$(".ajxload").css("display", "none");
					},
					error: function() { $(".ajxload").css("display", "none");}
				});
				return false
			},
			resetUrl: function() {
				var a = location.protocol + "//" + location.host;
				if (redirectedUrl == "" || redirectedUrl == "/") {
					window.location.href = a
				}
				if ($.browser.msie) {
					window.location.hash = redirectedUrl;
					redirectedUrl = ""
				} else {
					window.history.replaceState("object or string", "Title", redirectedUrl)
				}
			}
		};
  
		$(function() {
			//catalog.init();
			catalog.facetJson = $.parseJSON($("#facetJson").html());	
			//console.log(catalog.facetJson);
			//return false;
		});

        
		var catalogCustomFilters={
			
			    toggleSelectClass: function(a) {
					$(a).toggleClass("select")
				},
				//loadProducts: function(a, d, c, b,rem,atid)
				loadProducts: function(itm,itmValue,inputType,b,deleteCls,attrId) {
					    
					 /*console.log(
						 "Item" +itm
						 +"Itm Val" +itmValue
						 +"Tyep" +inputType
						 +"" +deleteCls
						 +"Attr" +attrId
					 );*/
					   // $(".ajxload").css("display", "block");
						//$('html, body').animate({ scrollTop:150},500);
						if (!catalog.facetJson.hasOwnProperty(itm)) {
							catalog.facetJson[itm] = []
						}
					// catalog.setUrl(url);
					// console.log(catalog);
					//alert(catalog);
					if (inputType == "multi") {
					 
						 if ($.inArray(itmValue,catalog.facetJson[itm]) == -1) {
								catalog.facetJson[itm].push(itmValue)
							} else {
								catalog.facetJson[itm].splice($.inArray(itmValue, catalog.facetJson[itm]), 1)
							} 
					   //  return false;
					} else {
					   //alert(inputType);
						if (inputType == "single") {
							
							if(deleteCls=='removF'){
								//alert(catalog.facetJson.itm);
								//if (catalog.facetJson.hasOwnProperty(itm)) {
									delete catalog.facetJson[itm];
									//alert(catalog.facetJson[itm]);
								//}
							}else{
								if ($.inArray(itmValue, catalog.facetJson[itm]) == -1) {
									console.log(catalog.facetJson);
									if (catalog.facetJson.hasOwnProperty("query") && catalog.facetJson.query.length > 0) {
										console.log("inside 1 "+catalog.facetJson);
										//nothing to do
									}else{
										delete catalog.facetJson[itm];
									}										
						
									
									//console.log("pre"+catalog.facetJson);
									catalog.facetJson[itm] = [];
									catalog.facetJson[itm].push(itmValue);
									//console.log("test"+catalog.facetJson);
								}else {
									
									console.log("inside 2 "+catalog.facetJson);
									//console.log(catalog.facetJson);
									catalog.facetJson[itm] = [];
									catalog.facetJson[itm].push(itmValue);
									
									
									//catalog
									//	.facetJson[itm]
									//.splice($.inArray(itmValue,catalog.facetJson[itm]), 1);
																  
								} 
							}
							
							
						}
					
					}
					//alert(catalog);
					//console.log(catalog);
					
					//build url from Json
					catalogCustomFilters.jsonToUrl()
					 
				},
				jsonToUrl: function() {
            
					var stringset="";
					var b = "";
				
					
					if (catalog.facetJson.hasOwnProperty("segment") && catalog.facetJson.segment.length > 0) {
						b += "/" + catalog.facetJson.segment
					}
					if (catalog.facetJson.hasOwnProperty("root") && catalog.facetJson.root.length > 0) {
						b += "/" + catalog.facetJson.root+"?area="+catalog.facetJson.city
					}
					
					if (catalog.facetJson.hasOwnProperty("cat") && catalog.facetJson.cat.length > 0) {
						if (catalog.facetJson.hasOwnProperty("root")) {
							if (catalog.facetJson.cat[0] != catalog.facetJson.root[0]) {
								b += "/" + catalog.facetJson.cat
							}
						} else {
							b += "/" + catalog.facetJson.cat
						}
					} 

					 if (catalog.facetJson.hasOwnProperty("query") && catalog.facetJson.query.length > 0) {
                        b += "&query=" + catalog.facetJson.query;
                    	} 

					 
					if (catalog.facetJson.hasOwnProperty("capacity") && catalog.facetJson.capacity.length > 0) {
						b += "/capacity/"+catalog.facetJson.capacity;
					   stringset=1;
					}
				   // console.log(catalog.facetJson);
					   // return false;
					if (catalog.facetJson.hasOwnProperty("locality") && catalog.facetJson.locality.length > 0) {
						b += "&locality/" + catalog.facetJson.locality.join(",");
					  //  b += "/page/" + 1;
					   // stringset=catalog.facetJson.locality.join(",");
					   stringset=1;
					}
					if (catalog.facetJson.hasOwnProperty("location") && catalog.facetJson.location.length > 0) {

						if (catalog.facetJson.hasOwnProperty("region") && catalog.facetJson.region.length > 0) {
						 	delete catalog.facetJson["region"];
						 }

						b += "&location=" + catalog.facetJson.location.join("|");

					  //  b += "/page/" + 1;
					   // stringset=catalog.facetJson.locality.join(",");
					   stringset=1;
					}

					if (catalog.facetJson.hasOwnProperty("region") && catalog.facetJson.region.length > 0) {
						b += "&region=" + catalog.facetJson.region.join("|");
					   stringset=1;
					}
				
					if (catalog.facetJson.hasOwnProperty("venuetype") && catalog.facetJson.venuetype.length > 0) {
						b += "/venuetype/" + catalog.facetJson.venuetype.join(",");
					  //  b += "/page/" + 1;
						stringset=1;
					}
					if (catalog.facetJson.hasOwnProperty("event") && catalog.facetJson.event.length > 0) {
						b += "&event=" + catalog.facetJson.event;
					  //  b += "/page/" + 1;
						stringset=1;
					}
					if (catalog.facetJson.hasOwnProperty("safe") && catalog.facetJson.safe.length > 0) {
						b += "&safe=" + catalog.facetJson.safe;
					   stringset=1;
					}
					if (catalog.facetJson.hasOwnProperty("type") && catalog.facetJson.type.length > 0) {
						b += "&type=" + catalog.facetJson.type;
					  //  b += "/page/" + 1;
						stringset=1;
					}
					if (catalog.facetJson.hasOwnProperty("cuisine") && catalog.facetJson.cuisine.length > 0) {
						b += "&cuisine=" + catalog.facetJson.cuisine;
					  //  b += "/page/" + 1;
						stringset=1;
					}
					if (catalog.facetJson.hasOwnProperty("otherfilter") && catalog.facetJson.otherfilter.length > 0) {
						b += "&otherfilter=" + catalog.facetJson.otherfilter;
						stringset=1;
					}
					if (catalog.facetJson.hasOwnProperty("price") && catalog.facetJson.price.length > 0) {
						b += "&"+catalog.facetJson.price;
					  //  b += "/page/" + 1;
						stringset=1;
					}
					if (catalog.facetJson.hasOwnProperty("guest") && catalog.facetJson.guest.length > 0) {
						b += "&"+catalog.facetJson.guest;
					  //  b += "/page/" + 1;
						stringset=1;
					}
					if (catalog.facetJson.hasOwnProperty("rating") && catalog.facetJson.rating.length > 0) {
						b += "&"+catalog.facetJson.rating;
					  //  b += "/page/" + 1;
						stringset=1;
					}
					
					if (catalog.facetJson.hasOwnProperty("amenities") && catalog.facetJson.amenities.length > 0) {
						b += "/amenities/" + catalog.facetJson.amenities.join(",");
					  //  b += "/page/" + 1;
						stringset=1;
					}
					if (catalog.facetJson.hasOwnProperty("budget") && catalog.facetJson.budget.length > 0) {
						b += "/budget/" + catalog.facetJson.budget.join(",");
						stringset=1;
						
					}
					if (catalog.facetJson.hasOwnProperty("page") && catalog.facetJson.page.length > 0) {
						b += "/page/" + catalog.facetJson.page;
						
					}
					if (catalog.facetJson.hasOwnProperty("forder") && catalog.facetJson.forder.length > 0) {
						b += "/sort/" + catalog.facetJson.forder;
					   stringset=1;
					}
					
					if(stringset==1){
						catalog.facetJson.page=1;
					}
					//b += "";

					if (catalog.facetJson.hasOwnProperty("clearall")) {
						// alert('hi');
						if (catalog.facetJson.hasOwnProperty("query") && catalog.facetJson.query.length > 0) {
							delete catalog.facetJson["query"];
                    	}
						if (catalog.facetJson.hasOwnProperty("capacity") && catalog.facetJson.capacity.length > 0) {
							delete catalog.facetJson["capacity"];
						}
						if (catalog.facetJson.hasOwnProperty("locality") && catalog.facetJson.locality.length > 0) {
							delete catalog.facetJson["locality"];
						}
						if (catalog.facetJson.hasOwnProperty("location") && catalog.facetJson.location.length > 0) {
							delete catalog.facetJson["location"];
						}
						if (catalog.facetJson.hasOwnProperty("region") && catalog.facetJson.region.length > 0) {
							delete catalog.facetJson["region"];
						}
						if (catalog.facetJson.hasOwnProperty("venuetype") && catalog.facetJson.venuetype.length > 0) {
							delete catalog.facetJson["venuetype"];
						}
						if (catalog.facetJson.hasOwnProperty("event") && catalog.facetJson.event.length > 0) {
							delete catalog.facetJson["event"];
						}
						if (catalog.facetJson.hasOwnProperty("safe") && catalog.facetJson.safe.length > 0) {
							delete catalog.facetJson["safe"];
						}
						if (catalog.facetJson.hasOwnProperty("type") && catalog.facetJson.type.length > 0) {
							delete catalog.facetJson["type"];
						}
						if (catalog.facetJson.hasOwnProperty("cuisine") && catalog.facetJson.cuisine.length > 0) {
							delete catalog.facetJson["cuisine"];
						}
						if (catalog.facetJson.hasOwnProperty("otherfilter") && catalog.facetJson.otherfilter.length > 0) {
							delete catalog.facetJson["otherfilter"];
						}
						if (catalog.facetJson.hasOwnProperty("price") && catalog.facetJson.price.length > 0) {
							delete catalog.facetJson["price"];
						}
						if (catalog.facetJson.hasOwnProperty("guest") && catalog.facetJson.guest.length > 0) {
							delete catalog.facetJson["guest"];
						}
						if (catalog.facetJson.hasOwnProperty("rating") && catalog.facetJson.rating.length > 0) {
							delete catalog.facetJson["rating"];
						}
						if (catalog.facetJson.hasOwnProperty("amenities") && catalog.facetJson.amenities.length > 0) {
							delete catalog.facetJson["amenities"];
						}
						if (catalog.facetJson.hasOwnProperty("budget") && catalog.facetJson.budget.length > 0) {
							delete catalog.facetJson["budget"];
						}
						if (catalog.facetJson.hasOwnProperty("page") && catalog.facetJson.page.length > 0) {
							delete catalog.facetJson["page"];
						}
						delete catalog.facetJson["clearall"];
						b = "/" + catalog.facetJson.root+"?area="+catalog.facetJson.city

					}
					 
				    //console.log(catalog.facetJson);
				 //console.log("finalUrl"+b);
					catalog.setUrl(b)
				}
			
		};