$(document).ready(function() {
	$("#featured .tabs").tabs();
	$("#pavilions .tabs").tabs();
	$("#timberlodges .tabs").tabs();
	$("#pavilion-stage .tabs").tabs();
	$(".slideshow .tabs").tabs();
	$('div#environment').accordion();
	$('div.sections').accordion();
	$('div.widget').accordion({ autoHeight: false });
    $('div.priceWidget').accordion({ autoHeight: false, active: false });
	
	$('ul.list6 li').click(function() {
		$(this).find('div').toggleClass('active' );
	});
	$('ul.list5 li a').click(function() {
		$(this).parent().remove();
	});
});

function getProductList(input) {
	$.getJSON('/modules/search/ajax.search.php',{'string': input},function(data) {
		$('#suggest').empty();
		$.each(data[0],function(key,prd){
			 $('#suggest').css("display","block");
			 $('#suggest').append("<a href=\"javascript:void(0)\" id=\"search_" + prd.id_prd + "\" onclick=\"populate(this.innerHTML,this.id);return false;\" >" + prd.name_prd + "</a><br />");
		});
	});
}

function populate(text,id){
	document.getElementById('s').value = text;
	var split = id.split("_");
	$('#id_prd').val(split[1]);
	document.getElementById("suggest").style.display = 'none';
}

function updatePriceAdd(optId) {
	$.getJSON('modules/products/updateOption.ajax.php',{'optId': optId },function(data) {
		var currentPrice = $('#price').html() * 1;
		var optionPrice = data * 1;
		var newPrice = currentPrice + optionPrice;
		newPrice = newPrice.toFixed(2);
		$('#price').html(newPrice);
		$('#price2').html(newPrice);
		var key = "optionExtra_" + optId;
		document.getElementById(key).onclick = function() { updatePriceRemove(this.value); };
	});
}

function updatePriceRemove(optId) {
	$.getJSON('modules/products/updateOption.ajax.php',{'optId': optId },function(data) {
		var currentPrice = $('#price').html() * 1;
		var optionPrice = data * 1;
		var newPrice = currentPrice - optionPrice;
		newPrice = newPrice.toFixed(2);
		$('#price').html(newPrice);
		$('#price2').html(newPrice);
		var key = "optionExtra_" + optId;
		document.getElementById(key).onclick = function() {updatePriceAdd(this.value);};
	});
}

function updateFilter(type,filter,range,level,idCtg) {
		var loadImage = "<img src='resources/images/misc/loading.gif' alt='Loading' style='margin-left:100px;' />";
		$('#productArea').html(loadImage);
		if (range) {
			var rangeMinId = "#" + filter + "_range_min";
			var rangeMaxId = "#" + filter + "_range_max";
			var min = $(rangeMinId).val();
			var max = $(rangeMaxId).val();	
		} else {
			var min = 0;
			var max = 0;
		}
		$.getJSON("../../modules/ajax/filter.ajax.php", {"type":type,"filter":filter,"min":min,"max":max,"level":level,"id_ctg":idCtg} ,function(data){
			$('ul#currentFilters').empty();
			$.each(data[0], function(id,value) {
				if (value.min == "" && value.max == "") {
					var range = "";
				} else {
					var range = "Range " + value.min + " - " + value.max;
				}
				if (id > 0) {
					var key2 = "filter_" + id + "/" + value.type;
				} else {
					var key2 = value.name + "/" + value.type;
				}
				onclickId = value.type + "_" + id;
				var name = value.name.replace(/_/g," ");
				$('ul#currentFilters').append("<li class='png-bg'><a href='javascript:void(0);' id='" + key2 + "' onclick='removeFilter(this.id," + level + "," + idCtg + ")'><img src='resources/images/content/list5-li.png' alt='X' /></a>&nbsp;" + name + " " + range + "</li>"); 
				if (!range) {
					document.getElementById(onclickId).onclick = function() {removeFilter(key2,level,idCtg);};	
				}
				
			});
		});
		pausecomp(30);
		updateProducts(level,idCtg);
	}

function removeFilter(filterId,level,idCtg) {
	var loadImage = "<img src='resources/images/misc/loading.gif' alt='Loading' style='margin-left:100px;' />";
	$('#productArea').html(loadImage);
	
	var filterArr = filterId.split("/");
	var filter = filterArr[0];
	var type = filterArr[1];
	var idSplit = filter.split("_");
	var filterId = idSplit[1];
	$.getJSON("../../modules/ajax/removeFilter.ajax.php", {"filter":filter,"type":type,"level":level,"id_ctg":idCtg} ,function(data){
			$('ul#currentFilters').empty();
			$.each(data[0], function(id,value) {
				if (value.min == "" && value.max == "") {
					var range = "";
					
				} else {
					var range = "Range " + value.min + " - " + value.max;
					
				}
				if (id > 0) {
					var key2 = "filter_" + id + "/" + value.type;
				} else {
					var key2 = value.name + "/" + value.type;
				}
				
				if (id == 'none') {
					$('ul#currentFilters').append("<li>Please refine your search by using the search criteria below.</li>"); 
				} else { 
					var name = value.name.replace(/_/g," ");
					$('ul#currentFilters').append("<li class='png-bg'><a href='javascript:void(0);' id='" + key2 + "' onclick='removeFilter(this.id," + level + "," + idCtg + ")'><img src='resources/images/content/list5-li.png' alt='X' /></a>&nbsp;" + name + " " + range + "</li>"); 
				}
				
			});
			var replaceTxt = "#" + filterArr[1] + "_" + filter;
			var removeActive = replaceTxt.replace("filter_","");
			$(removeActive).removeClass('active');
			var onclickId = removeActive.replace("#","");
			var key = filter.replace("filter_","");
			if (type != 'range') {
				document.getElementById(onclickId).onclick = function() {updateFilter(type,key,false,level,idCtg);};	
			}
	});
	pausecomp(10);
	updateProducts(level,idCtg);
}

function updateProducts(level,idCtg) {
	$.get("../../modules/ajax/products.ajax.php",{"level":level,"id_ctg":idCtg},function(data){
		$('#productArea').html(data);
	});
}

function resetFilter(level,idCtg) {
	var loadImage = "<img src='resources/images/misc/loading.gif' alt='Loading' style='margin-left:100px;' />";
	$('#productArea').html(loadImage);
	
	$.getJSON("../../modules/ajax/resetFilter.ajax.php", function(data){
			$('ul#currentFilters').empty();
	});
	updateProducts(level,idCtg);
}

function addToWishlist(id) {
	txt = "";
	if(document.extras) {
		if (document.extras.optionExtra){
			var totalExtras = document.extras.optionExtra.length;
			for (i = 0; i < totalExtras; i++) {
				if (document.extras.optionExtra[i].checked) {
					txt = txt + document.extras.optionExtra[i].value + ",";
				}
			}
		}
	}
	if (txt == ""){
		var options = "none";
	} else {
		var options = txt;
	}
	$.getJSON("../../modules/ajax/wishlist.ajax.php",{"id":id,"options":options},function(data){
		var num = data.count;
		$('#wishListNum').html(num);
		if (num > 1) {
			num = num + " Items";
		} else {
			num = num + " Item";
		}
		$('#wishListItem').html(num);
	});
	amendButtons('remove',id);
	
}
function removeFromWishlist(id) {
	$.getJSON("../../modules/ajax/removeWishlist.ajax.php",{"id":id},function(data){
		var num = data.count;
		$('#wishListNum').html(num);
		if (num > 1) {
			num = num + " Items";
		} else {
			num = num + " Item";
		}
		$('#wishListItem').html(num);
	});
	amendButtons('add',id);
}

function amendButtons(method,id) {
	var cnt = 1;
	if (method == 'add'){
		for (cnt = 1;cnt < 6;cnt++) {
			var idSplit = id.split("_");
			var wishId = idSplit[0] + "_" + cnt + "_" + idSplit[2];
			document.getElementById(wishId).onclick = function() {addToWishlist(this.id);};		
			document.getElementById(wishId).innerHTML = "<span>Add to Wishlist</span>";
		}	
	} else {
		for (cnt = 1;cnt < 6;cnt++) {
			var idSplit = id.split("_");
			var wishId = idSplit[0] + "_" + cnt + "_" + idSplit[2];
			document.getElementById(wishId).onclick = function() {removeFromWishlist(this.id);};		
			document.getElementById(wishId).innerHTML = "<span>Remove from Wishlist</span>";
		}
	}
}

function getdealer(county) {
	$.getJSON("../../modules/ajax/getDealer.ajax.php",{"county":county},function(data){
		if (data[0]['none'] == true) {
			var retHtml = "<li>Sorry we have no display centers in your area at present... but don't worry we have hundreds of installations we can arrange for you to see please call us on 0845 6066475 to arrange this.</li>";
		} else {
			var retHtml = "<li>" + data[0]['dealer_name_del'] + "</li><li>" + data[0]['address_1_del'] + "</li><li>" + data[0]['address_2_del'] + "</li><li>" + data[0]['address_3_del'] + "</li><li>" + data[0]['town_del'] + "</li><li>" + data[0]['county_del'] + "</li><li>" + data[0]['contact_number_del'] + "</li><li>" + data[0]['website_del'] + "</li><li>" + data[0]['details_del'] + "</li>";
	
		}
		$('#dealerInfo').html(retHtml);
	});
}

function pausecomp(millis)
{
	var date = new Date();
	var curDate = null;
	
	do { curDate = new Date(); }
	while(curDate-date < millis);
} 