/*Shadowbox.init({
	skipSetup: true, 
	clearCache: function() {
		each(S.cache, function(obj) {
			if( obj.el) {
				S.lib.removeEvent(obj.el, 'click', handleClick);
				try {
					delete obj.el.shadowboxCacheKey;
				} catch(e) {
					if (obj.el.removeAttribute) {
						obj.el.removeAttribute('shadowboxCacheKey');
					}
				}
			}
		});
		S.cache = [];
	}
});*/

function timeDiff(startDate, endDate) {
	
	date2 = startDate;
	date1 = endDate;
	
	laterdate = date1.split("-");
	laterD = laterdate[0];
	laterM = laterdate[1];
	laterY = laterdate[2];
	
	earlierdate = date2.split("-");
	earlierD = earlierdate[0];
	earlierM = earlierdate[1];
	earlierY = earlierdate[2];
	
	var laterdate = new Date(laterY, laterM, laterD);
	var earlierdate = new Date(earlierY, earlierM, earlierD);
	
	var difference = laterdate.getTime() - earlierdate.getTime();
	
	var daysDifference = Math.floor(difference / 1000 / 60/ 60 / 24);
	difference -= daysDifference * 1000 * 60 * 60 * 24
	var hoursDifference = Math.floor(difference / 1000 / 60 / 60);
	difference -= hoursDifference * 1000 * 60 * 60
	var minutesDifference = Math.floor(difference / 1000 / 60);
	difference -= minutesDifference * 1000 * 60
	var secondsDifference = Math.floor(difference / 1000);
	
	return daysDifference;

}

function key(number) {
	return Math.floor(Math.random() * number);
}

function fetchGallery(width) {
	$.ajax({
		type	: "GET",
		url		: "/gallery.php",
		data	: "width=" + width + "&key=" + key(10000),
		success	: function(html){
			
			$("#gallery").empty();
			$("#gallery").append(html);
			
			Shadowbox.clearCache();
			
			$("#albums").ImageOverlay({
				overlay_text_color: '#fff' 
			});
		}
	});
}

function fetchAlbum(gallery_id, width) {
	$.ajax({
		type	: "GET",
		url		: "/gallery.php",
		data	: "width=" + width + "&gallery_id=" + gallery_id + "&key=" + key(10000),
		success	: function(html){
			
			$("#gallery").empty();
			$("#gallery").append(html);
			
			$("#images").ImageOverlay();
			
			Shadowbox.setup("#gallery li a", {
				gallery:	"Gallery"
			});
		}
	});
}

function fetchMenu(position) {
	$.ajax({
		type	: "GET",
		url		: "/menu.php",
		data	: "position=" + position + "&key=" + key(10000),
		success	: function(html){
			
			$("#" + position + "_menu").empty();
			$("#" + position + "_menu").append(html);
			
			if (position == 'top') {
				$("ul.sf-menu").supersubs({ 
					minWidth:    12, 
					maxWidth:    27, 
					extraWidth:  1    
				}).superfish({
					dropShadows : false
				});
			}
			
			if (position == 'left') {
				$("ul.sf-menu").superfish({
					dropShadows : false
				});
			}
			
		}
	});
}
function fetchPlanner(start_date, end_date, days, selector) {
	$.ajax({
		type	: "GET",
		url		: "/planner.php",
		data	: "start_date=" + start_date + "&end_date=" + end_date + "&days=" + days + "&key=" + key(10000),
		success	: function(html){
			$(selector).empty();
			$(selector).append(html);
			
			$('#temp_total').empty();
			$('#temp_total').append($('#default_total').html());
			
			$('#total').empty();
			$('#total').append('<h2>Total: R' + number_format($('#default_total').html(), 2, '.', ',') + '</h2');
		}
	});
}
function fetchAdverts(page, position, selector) {
	$.ajax({
		type	: "GET",
		url		: "/adverts.php",
		data	: "page=" + page + "&position=" + position + "&key=" + key(10000),
		success	: function(html){
			$(selector).empty();
			$(selector).append(html);
		}
	});
}
function fetchPage(page, selector) {
	$.ajax({
		type	: "GET",
		url		: "/page.php",
		data	: "page=" + page + "&key=" + key(10000),
		success	: function(html){
			$(selector).empty();
			$(selector).append(html);
		}
	});
}
function fetchNews(show, selector) {
	$.ajax({
		type	: "GET",
		url		: "/news.php",
		data	: "show=" + show + "&key=" + key(10000),
		success	: function(html){
			$(selector).empty();
			$(selector).append(html);
		}
	});
}
function fetchProducts(show, selector) {
	$.ajax({
		type	: "GET",
		url		: "/product.php",
		data	: "show=" + show + "&key=" + key(10000),
		success	: function(html){
			$(selector).empty();
			$(selector).append(html);
		}
	});
}
function fetchVideos(show, category, selector) {
	$.ajax({
		type	: "GET",
		url		: "/video.php",
		data	: "category=" + category + "&show=" + show + "&key=" + key(10000),
		success	: function(html){
			$(selector).empty();
			$(selector).append(html);
		}
	});
}
function fetchLatestAlbum(show, selector) {
	$.ajax({
		type	: "GET",
		url		: "/album.php",
		data	: "show=" + show + "&key=" + key(10000),
		success	: function(html){
			$(selector).empty();
			$(selector).append(html);
		}
	});
}
function fetchSlideshow(name, width, height) {
	
	var selector = "#" + name;
	
	$.ajax({
		type	: "GET",
		url		: "/slideshow.php",
		data	: "name=" + name + "&width=" + width + "&height=" + height + "&key=" + key(10000),
		success	: function(html){
			
			$(selector).empty();
			$(selector).append(html);
			
			$(selector).cycle({
				fx:     'fade', 
				timeout: 6000, 
				//next:   '#slideshow', 
				pause:   1,
				random:  1
				//before:  onBefore, 
				//after:   onAfter 
			});
		}
	});
}

function fetchBasket() {
	var selector = '#basket';
	$(selector).show();
	$.ajax({
		type	: "GET",
		url		: "/basket.php",
		data	: "&key=" + key(10000),
		success	: function(html){
			$(selector).empty();
			$(selector).append(html);
		}
	});
}

function emptyBasket() {
	var selector = '#basket';
	$.ajax({
		type	: "GET",
		url		: "/basket.php",
		data	: "empty=1&key=" + key(10000),
		success	: function(html){
			$(selector).empty();
			$(selector).append(html);
		}
	});
}

function addToBasket(id) {
	var selector = '#basket';
	var quantity = 1;
	$(selector).show();
	$.ajax({
		type	: "GET",
		url		: "/basket.php",
		data	: "product_id=" + id + "&quantity=" + quantity + "&key=" + key(10000),
		success	: function(html){
			$(selector).empty();
			$(selector).append(html);
			$(selector)
			.animate({ 
				backgroundColor: "#FFFFFF"
			}, 'fast')
			.animate({ 
				backgroundColor: "#DDD7E0"
			}, 'fast')
			.animate({ 
				backgroundColor: "#FFFFFF"
			}, 'fast')
			.animate({ 
				backgroundColor: "#DDD7E0"
			}, 'fast');
		}
	});
}

function echeck(str) {

	var at = "@";
	var dot = ".";
	var lat = str.indexOf(at);
	var lstr = str.length;
	var ldot = str.indexOf(dot);
	
	if (str.indexOf(at) ==- 1) 
	{ return false; }
	if (str.indexOf(at) ==- 1 || str.indexOf(at) == 0 || str.indexOf(at) == lstr) 
	{ return false; }
	if (str.indexOf(dot) ==- 1 || str.indexOf(dot) == 0 || str.indexOf(dot) == lstr) 
	{ return false; }
	if (str.indexOf(at, (lat + 1)) !=- 1) 
	{ return false; }
	if (str.substring(lat - 1, lat) == dot || str.substring(lat + 1, lat + 2) == dot) 
	{ return false; }
	if (str.indexOf(dot, (lat + 2)) ==- 1) 
	{ return false; }
	if (str.indexOf(" ") !=- 1 ) 
	{ return false; }
	 
	return true;
}

function validateTour() {
			
	// name
	if (document.getElementById("name").value.length < 1) {
		alert("Please enter a valid name.");
		document.getElementById("name_label").style.color = "red";
		document.getElementById("name").focus();
		return false;
	} else {
		document.getElementById("name_label").style.color = "black";
	}
	
	// email_1 (validate)
	if (echeck(document.getElementById("email_1").value) == false) {
		alert("Please enter a valid email address.");
		document.getElementById("email_1_label").style.color = "red";
		document.getElementById("email_1").focus();
		return false;
	} else {
		document.getElementById("email_1_label").style.color = "black";
	}
	
	// email_2
	if (document.getElementById("email_2").value != document.getElementById("email_1").value) {
		alert("Please make sure the email addresses are the same.");
		document.getElementById("email_2_label").style.color = "red";
		document.getElementById("email_2").focus();
		return false;
	} else {
		document.getElementById("email_2_label").style.color = "black";
	}
	
	// start_date
	if (document.getElementById("start_date").value.length != 10) {
		alert("Please enter a valid start date.");
		document.getElementById("start_date_label").style.color = "red";
		document.getElementById("start_date").focus();
		return false;
	} else {
		document.getElementById("start_date_label").style.color = "black";
	}
	
	// end_date
	if (document.getElementById("end_date").value.length != 10) {
		alert("Please enter a valid end date.");
		document.getElementById("end_date_label").style.color = "red";
		document.getElementById("end_date").focus();
		return false;
	} else {
		document.getElementById("end_date_label").style.color = "black";
	}
	
	if (document.getElementById("txtCaptcha").value.length == 5) {
		$.ajax({
			type	: "POST",
			url		: "/control/captcha/captcha.php",
			data	: "txtCaptcha=" + document.getElementById("txtCaptcha").value + "&key=" + key(10000),
			success	: function(html){
				$("#result").empty();
				$("#result").append(html);
				
				if (html == "FAIL") {
					alert("The code you entered was incorrect. Please try again.");
					$("#imgCaptcha").attr("src", "/control/captcha/image.php?key=" + key(10000));
				} else {
					document.tour_form.submit();
				}
			}
		});
		return false;
	} else {
		alert("Please enter the exact code on the image into the text box and try again.");
		document.getElementById("txtCaptcha").focus();
		return false;
	}	
	
}