/*jQuery.fn.log = function (msg) {
	if ($.browser.mozilla) {
		console.log("%s: %o", msg, this);
	}
	return this;
};*/

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 key(number) {
	return Math.floor(Math.random() * number);
}

function createMarker(point, icon, content, listener, state) {
	var marker = new GMarker(point, icon);
	var html = '<div class="map_popup">';
	var html = html + content;
	var html = html + '</div>';
	
	if (state == 'open') {
		marker.openInfoWindowHtml(html);
	}
	
	GEvent.addListener(marker, listener, function() {
		marker.openInfoWindowHtml(html);
	});
	
	return marker;
}

function createIcon(image) {
	var icon = new GIcon();
	icon.image = "/images/icon/" + image;
	icon.shadow = "/images/icon/shadow.png";
	icon.iconSize = new GSize(27.0, 34.0);
	icon.shadowSize = new GSize(45.0, 34.0);
	icon.iconAnchor = new GPoint(13.0, 34.0);
	icon.infoWindowAnchor = new GPoint(13.0, 17.0);
	return icon;
}

function initializeGoogleMap() {
	
	if (GBrowserIsCompatible()) {
		
		// Create and Center a Map
		var map = new GMap2(document.getElementById("map_canvas"));
		map.setCenter(new GLatLng(-32.686319, 25.422913), 7);
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		
		var html = "";
		
		html = '<font face="Arial" size="1" color="#000000"><a href="http://www.hunteasterncape.com/page/hunter_accommodation_kamalabushlodge" target="_self"><strong>Kamala Bush Lodge</strong></a><br/><br/><a href="http://www.hunteasterncape.com/page/hunter_accommodation_acacialodge" target="_self"><img src="/images/map/kamala_bush_lodge.jpg" /></a></font>';
		map.addOverlay(createMarker(new GLatLng(-32.686319, 25.422913), createIcon('accommodation.png'), html, 'mouseover'));
		
		html = '<font face="Arial" size="1" color="#000000"><a href="http://www.hunteasterncape.com/page/hunter_accommodation_acacialodge" target="_self"><strong>Acacia Lodge</strong></a><br/><br/><a href="http://www.hunteasterncape.com/page/hunter_accommodation_acacialodge" target="_self"><img src="/images/map/acacia_lodge.jpg" /></a></font>';
		map.addOverlay(createMarker(new GLatLng(-32.670933, 25.434350), createIcon('accommodation.png'), html, 'mouseover'));
		
		html = '<font face="Arial" size="1" color="#000000"><a href="http://www.hunteasterncape.com/page/hunter_accommodation_grahamstownbushlodge" target="_self"><strong>Grahamstown Bush Lodge</strong></a><br/><br/><a href="http://www.hunteasterncape.com/page/hunter_accommodation_acacialodge" target="_self"><img src="/images/map/grahamstown_bush_lodge.jpg" /></a></font>';
		map.addOverlay(createMarker(new GLatLng(-32.995717, 26.204883), createIcon('accommodation.png'), html, 'mouseover'));
		
		html = '<font face="Arial" size="1" color="#000000"><a href="http://www.hunteasterncape.com/page/hunter_accommodation_vaalkranslodge" target="_self"><strong>Vaalkrans Lodge</strong></a><br/><br/><a href="http://www.hunteasterncape.com/page/hunter_accommodation_acacialodge" target="_self"><img src="/images/map/vaalkrans_lodge.jpg" /></a></font>';
		map.addOverlay(createMarker(new GLatLng(-33.192252, 26.328187), createIcon('accommodation.png'), html, 'mouseover'));
	
	} else {
		alert('Your browser is not compatible with Google Maps.');
	}
}

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();
		}
	});
}

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);
			
			Shadowbox.setup(".album", {
				gallery:	"gallery"
			});
		}
	});
}

function fetchMenu(position, selector) {
	$.ajax({
		type	: "GET",
		url		: "/menu.php",
		data	: "position=" + position + "&key=" + key(10000),
		success	: function(html){
			
			$(selector).empty();
			$(selector).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 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 fetchLogin(selector) {
	$.ajax({
		type	: "GET",
		url		: "/login.php",
		data	: "key=" + key(10000),
		success	: function(html){
			$(selector).empty();
			$(selector).append(html);
		}
	});
}

function fetchNews(limit) {
	var selector = '#news';
	$.ajax({
		type	: "GET",
		url		: "/news.php",
		data	: "limit=" + limit + "&key=" + key(10000),
		success	: function(html){
			$(selector).empty();
			$(selector).append(html);
			
			/*Shadowbox.setup(".listing", {
				gallery:	"listing"
			});*/
			
		}
	});
}

function fetchSlideshow(name, width, height, selector) {
	
	$.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: 8000, 
				//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 fetchSpecials() {
	var selector = '#specials';
	$(selector).show();
	$.ajax({
		type	: "GET",
		url		: "/specials.php",
		data	: "&key=" + key(10000),
		success	: function(html){
			$(selector).empty();
			$(selector).append(html);
		}
	});
}

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 validateCheckout() {
	
	// first_name
	if (document.getElementById("first_name").value.length < 1) {
		alert("Please enter a valid first name.");
		document.getElementById("first_name_label").style.color = "red";
		document.getElementById("first_name").focus();
		return false;
	} else {
		document.getElementById("first_name_label").style.color = "black";
	}
	
	// last_name
	if (document.getElementById("last_name").value.length < 1) {
		alert("Please enter a valid last name.");
		document.getElementById("last_name_label").style.color = "red";
		document.getElementById("last_name").focus();
		return false;
	} else {
		document.getElementById("last_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";
	}
	
	// street_address
	if (document.getElementById("street").value.length < 1) {
		alert("Please enter a valid street address.");
		document.getElementById("street_label").style.color = "red";
		document.getElementById("street").focus();
		return false;
	} else {
		document.getElementById("street_label").style.color = "black";
	}
	
	// town
	if (document.getElementById("town").value.length < 1) {
		alert("Please enter a valid town.");
		document.getElementById("town_label").style.color = "red";
		document.getElementById("town").focus();
		return false;
	} else {
		document.getElementById("town_label").style.color = "black";
	}
	
	// province
	if (document.getElementById("province").value.length < 1) {
		alert("Please enter a valid province.");
		document.getElementById("province_label").style.color = "red";
		document.getElementById("province").focus();
		return false;
	} else {
		document.getElementById("province_label").style.color = "black";
	}
	
	// postal_code
	if (document.getElementById("postal_code").value.length < 1) {
		alert("Please enter a valid postal code.");
		document.getElementById("postal_code_label").style.color = "red";
		document.getElementById("postal_code").focus();
		return false;
	} else {
		document.getElementById("postal_code_label").style.color = "black";
	}
	
	// phone
	if (document.getElementById("phone").value.length < 1) {
		alert("Please enter a valid telephone number.");
		document.getElementById("phone_label").style.color = "red";
		document.getElementById("phone").focus();
		return false;
	} else {
		document.getElementById("phone_label").style.color = "black";
	}
	
	/*if (!document.getElementById("agree").checked) {
		alert("Please read our terms and conditions and tick the checkbox if you agree with them.");
		return false;
	}*/
			
	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.checkout.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;
	}	
}

function validateEnquire() {
	
	// first_name
	if (document.getElementById("first_name").value.length < 1) {
		alert("Please enter a valid first name.");
		document.getElementById("first_name_label").style.color = "red";
		document.getElementById("first_name").focus();
		return false;
	} else {
		document.getElementById("first_name_label").style.color = "black";
	}
	
	// last_name
	if (document.getElementById("last_name").value.length < 1) {
		alert("Please enter a valid last name.");
		document.getElementById("last_name_label").style.color = "red";
		document.getElementById("last_name").focus();
		return false;
	} else {
		document.getElementById("last_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";
	}
	
	// phone
	if (document.getElementById("phone").value.length < 1) {
		alert("Please enter a valid telephone number.");
		document.getElementById("phone_label").style.color = "red";
		document.getElementById("phone").focus();
		return false;
	} else {
		document.getElementById("phone_label").style.color = "black";
	}
	
	// message
	if (document.getElementById("message").value.length < 10) {
		alert("Please enter a valid message.");
		document.getElementById("message_label").style.color = "red";
		document.getElementById("message").focus();
		return false;
	} else {
		document.getElementById("message_label").style.color = "black";
	}
	
	/*if (!document.getElementById("agree").checked) {
		alert("Please read our terms and conditions and tick the checkbox if you agree with them.");
		return false;
	}*/
			
	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.enquire.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;
	}	
}