﻿var jqHoverBoxTarget;

$(function () {

	/* master template */

	$("#ActivitiesNavigationLink").click(function () {
		if ($("#ActivitiesNavigation").is(":visible")) {
			if ($.browser.isIE6()) {
				$("#ActivitiesNavigation").hide();
				$("#ActivitiesNavigationArrow").hide();
				$("#HomeHeader, .banner").css("position", "relative");
			} else {
				$("#ActivitiesNavigation").slideUp(400, function () { $("#ActivitiesNavigationArrow").hide() });
			}
		} else {
			if ($("#HomeActivitiesNavigation").is(":visible")) $("#HomeActivitiesNavigation").slideUp(400, function () { $("#HomeActivitiesNavigationArrow").hide() });
			$("#ActivitiesNavigationArrow").css("left", (168 + $(this).position().left + ($(this).width() / 2) - ($("#ActivitiesNavigationArrow").width() / 2)) + "px").show();
			if ($.browser.isIE6()) {
				$("#HomeHeader, .banner").css("position", "");
				$("#ActivitiesNavigation").show();
			} else {
				$("#ActivitiesNavigation").slideDown();
			}
		}
	});

	var searchPrompt = "Search";
	$("#Navigation input").focus(function () {
		if ($(this).val() == searchPrompt) {
			$(this).removeClass("idle");
			$(this).val("");
		}
	}).blur(function () {
		if ($.trim($(this).val()) == "") {
			$(this).addClass("idle");
			$(this).val(searchPrompt);
		}
	}).keypress(function (e) {
		if (e.which == 13) $.navigate("/providers/?k=" + encodeURIComponent($(this).val()));
	});

	if ($.trim($("#Navigation input").val()) == "") $("#Navigation input").val(searchPrompt).addClass("idle");

	$("#ProviderSearchKeywords").keypress(function (e) {
		if (e.which == 13) $.queryString.set("k", $(this).val());
	});

	$(document.body).append("<div id=\"HoverBox\">&nbsp;</div>");
	$("#HoverBox").mouseleave(function () {
		$("#HoverBox").hide();
	}).click(function () {
		if (jqHoverBoxTarget.attr("onclick")) {
			jqHoverBoxTarget.click();
		} else {
			var jqA = jqHoverBoxTarget.find("a");
			if (jqA.attr("target") == "_blank") {
				$.openWindow(jqA.attr("href"));
			} else {
				$.navigate(jqA.attr("href"));
			}
		}
	});
	$(".hoverBox").mouseenter(function () {
		jqHoverBoxTarget = $(this);
		/*if (jqHoverBoxTarget.is(":animated")) {
		jqHoverBoxTarget = null;
		return;
		}*/
		$("#HoverBox").css({ "top": jqHoverBoxTarget.offset().top + "px", "left": jqHoverBoxTarget.offset().left + "px", "width": (jqHoverBoxTarget.outerWidth() - 10) + "px", "height": (jqHoverBoxTarget.outerHeight() - 10) + "px" }).show();
	});


	/*$(".banner h2").clone().addClass("shadow").css("opacity", "0.75").appendTo(".banner");
	$(".mini-banner").each(function () {
	$(this).find("h2").clone().addClass("shadow").css("opacity", "0.75").appendTo(this);
	});
	$("#RotatorFeature h2, #RotatorFeature p").clone().addClass("shadow").css("opacity", "0.75").appendTo("#RotatorFeature");*/


	var mH = 0;
	$(".matchHeight").each(function () { mH = Math.max(mH, $(this).height()); }).css("height", mH + "px");


	/* home page */

	if ($("#Rotator").length > 0) {
		$("#HomeNavigation .linkBox").append("<div class=\"arrow\"><img src=\"/i/inverse-arrow.png\" alt=\"\" class=\"unimportant\" /></div>");
		Rotator.initialise();
	}

	$("#HomeMap div.label a").attr("href", "javascript:void(0)").click(function () {
		$("#HomeMap img.blocker").show();
		$("#Panel" + $(this).attr("id").substr(5)).slideDown();
	});

	$("#HomeMap img.blocker").css("cursor", "pointer").click(function () {
		$("#HomeMap div.panel").slideUp();
		$("#HomeMap img.blocker").hide();
	});

	$("#HomeActivitiesNavigationLink").css("cursor", "pointer").click(function () {
		if ($("#HomeActivitiesNavigation").is(":visible")) {
			if ($.browser.isIE6()) {
				$("#HomeActivitiesNavigation").hide();
				$("#HomeActivitiesNavigationArrow").hide();
			} else {
				$("#HomeActivitiesNavigation").slideUp(400, function () { $("#HomeActivitiesNavigationArrow").hide() });
			}
		} else {
			if ($("#ActivitiesNavigation").is(":visible")) {
				if ($.browser.isIE6()) {
					$("#ActivitiesNavigation").hide();
					$("#ActivitiesNavigationArrow").hide();
					$("#HomeHeader, .banner").css("position", "relative");
				} else {
					$("#ActivitiesNavigation").slideUp(400, function () { $("#ActivitiesNavigationArrow").hide(); });
				}
			}
			$("#HomeActivitiesNavigationArrow").css("left", ($(this).position().left + ($(this).width() / 2) - ($("#HomeActivitiesNavigationArrow").width() / 2)) + "px").show();
			if ($.browser.isIE6()) {
				$("#HomeActivitiesNavigation").show();
			} else {
				$("#HomeActivitiesNavigation").slideDown();
			}
		}
	});

	$("#HomeNavigation div.box").append("<div class=\"arrow\">&gt;</div>");


	/* events page */

	$(".filter-definition h5:first").css("margin-top", "0px");

	$("tr.week").mouseover(function () { $(this).addClass("focus"); }).mouseout(function () { $(this).removeClass("focus"); }).click(function () { $.queryString.set("w", $(this).attr("week")); });


	/* pin map */

	$("#PinList div.listing").mouseover(function () {
		$("#PinList div.mouseover").removeClass("mouseover");
		$(this).addClass("mouseover");
		document.getElementById("PinMap").focusPin($(this).attr("id").substr(1));
	}).click(function () {
		var href = $(this).find("a").attr("href");
		if (href != null && href.indexOf("javscript") != 0) $.navigate($(this).find("a").attr("href"));
	});

	$(".place-popup").appendTo(document.body);

	//$("#PinList div.listing div").append($(this).parent().position().top);

	$(document.body).append(
		'<div id="Contact" style="display:none;position:absolute;z-index:10;width:450px;padding:20px 20px 20px 20px;background-color:#ffffff;">' +
			'<h1 style="margin:0px 0px 20px 0px;font-size:36pt;font-weight:900;font-family:Lato,Arial;letter-spacing:-2px;">Contact Us</h1>' +
			'<p>OutdoorNI.com is an initiative of the Countryside Access and Activities Network. To find out more about the network and what we do, you can contact us directly:</p>' +
			'<p style="font-weight:bold;">Countryside Access and Activities Network<br />The Stableyard<br />Barnett’s Demesne<br />Malone Road<br />Belfast<br />N Ireland<br />BT9 5PB</p>' +
			'<p style="font-weight:bold;"><i>Tel:</i> 028 90303930<br /><i>Fax:</i> 028 90626248<br /><i>Email:</i> <a href="mailto:info@outdoorni.com">info@outdoorni.com</a><br /><i>Web:</i> <a href="http://www.countrysiderecreation.com">www.countrysiderecreation.com</a></p>' +
			'<p style="font-style:italic;">PLEASE NOTE: If you require booking or pricing information on any of the exciting activities on offer please contact the relevant activity provider or club directly.</p>' +
		'</div>'
	);

	$(document.body).append(
		'<div id="Disclaimer" style="display:none;position:absolute;z-index:10;width:600px;padding:20px 20px 20px 20px;background-color:#ffffff;">' +
			'<h1 style="margin:0px 0px 20px 0px;font-size:36pt;font-weight:900;font-family:Lato,Arial;letter-spacing:-2px;">OutdoorNI.com Disclaimer</h1>' +
			'<p style="margin:0px;">Whilst all the clubs, associations and activity operators listed on this website generally operate according to that which is accepted as current best practice, it is the responsibility of the participant to ensure that they are credible and all appropriate safety standards are adhered to. The Countryside Access and Activities Network neither have, nor assume, any responsibility for the accuracy or the completeness of the information supplied or the service and level of care afforded by any of the clubs, associations and activity operators listed on this website.</p>' +
		'</div>'
	);

});



var SocialSharing = {

	writeLinks: function (className) {
		var url = window.location.href;
		if (!className) className = "social-sharing-links";
		document.write(
			"<table class=\"" + className + "\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\">" +
				"<tr>" +
					"<td valign=\"top\" style=\"padding:1px 10px 0px 0px;\"><a href=\"http://twitter.com/share\" class=\"twitter-share-button\" data-count=\"horizontal\">Tweet</a><script type=\"text/javascript\" src=\"http://platform.twitter.com/widgets.js\"></script></td>" +
					"<td valign=\"top\"><iframe src=\"http://www.facebook.com/plugins/like.php?href=" + url + "&amp;layout=standard&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font=arial&amp;width=305&amp;height=44\" scrolling=\"no\" frameborder=\"0\" style=\"border:none; overflow:hidden; width:305px; height:44px;\" allowtransparency=\"true\"><xsl:value-of select=\"''\" /></iframe></td>" +
				"</tr>" +
			"</table>"
		);
	}

};



var Contact = {
	show: function () {
		$("#Contact").overlay({ click: function () { Contact.hide(); } });
	},
	hide: function () {
		$("#Contact").unoverlay();
	}
};


var Disclaimer = {
	show: function () {
		$("#Disclaimer").overlay({ click: function () { Disclaimer.hide(); } });
	},
	hide: function () {
		$("#Disclaimer").unoverlay();
	}
};



var Rotator = {

	currentId: null,
	timeoutId: null,
	rotateDelay: 5000,

	initialise: function () {
		this.currentId = parseInt($("#Rotator .feature:visible").attr("id").substr(1));
		$("#L" + this.currentId).find(".arrow").show();
		this.timeoutId = setTimeout(Rotator.increment, this.rotateDelay);
	},

	show: function (id, andRotate) {
		if (!andRotate) clearTimeout(Rotator.timeoutId);

		var jqTarget = $("#Rotator .feature[id=F" + id + "]");
		if ($("#Rotator .feature:animated").length > 0 || jqTarget.is(":visible")) return;

		this.currentId = id;

		var jqSource = $("#HomeNavigation .linkBox[id=L" + id + "]");

		$("#HomeNavigation .linkBox").not(jqSource).find(".arrow").hide();
		jqSource.find(".arrow").show();

		jqTarget.css({ "z-index": "2"/*, "left": "600px"*/ }).fadeIn(400, function () {
			$("#Rotator .feature").css("z-index", "1").not(jqTarget).hide();
			if (andRotate) Rotator.timeoutId = setTimeout(Rotator.increment, Rotator.rotateDelay);
		});
	},

	increment: function () {
		//alert(Rotator.currentId + " -> " + (((Rotator.currentId) % 6) + 1));
		Rotator.show(((Rotator.currentId) % 6) + 1, true);
	}

}



var PinMap = {

	onMouseOver: function (id) {
		$("#PinList div.mouseover").removeClass("mouseover");
		$("#I" + id).addClass("mouseover");
		$("#PinList").scrollTop($("#PinList").scrollTop() + $("#I" + id).position().top);
	},

	onClick: function (id) {
		$("#I" + id).click();
	}

}



var Place = {

	show: function (id) {
		var jq = $("#Place" + id)
		jq.overlay();
		X.attachTo(jq, Place.hide);
	},

	hide: function () {
		$('.place-popup:visible').unoverlay();
		X.unattach();
	}
}



var Gallery = {

	path: "",
	maxWidth: 1,
	maxHeight: 1,
	urls: null,
	index: -1,

	initialise: function (path, maxWidth, maxHeight, urls) {
		this.path = path;
		this.maxWidth = maxWidth;
		this.maxHeight = maxHeight;
		this.urls = urls;
	},

	show: function (index) {
		this.index = index;
		$("#Gallery th img").attr("src", "/i/loading.gif");
		$("#Gallery").overlay({ color: "#000000", opacity: 0.8 });
		this.update();
		//setTimeout(function () { $(document).bind("click", Gallery.hide); }, 100);
	},

	previous: function () {
		if (this.index == 0) return;
		this.index--;
		this.update();
	},

	next: function () {
		if (this.index == this.urls.length - 1) return;
		this.index++;
		this.update();
	},

	update: function () {
		$("#Gallery th img").attr("src", "/i/loading.gif").removeClass("framed");
		$("#Gallery th img").load(function () { $(this).addClass("framed"); X.attachTo($(this), Gallery.hide); }).attr("src", "/Ti.ashx?p=" + Gallery.path + "%5c" + Gallery.urls[Gallery.index] + "&mw=" + Gallery.maxWidth + "&mh=" + Gallery.maxHeight);
		$("#Gallery td:eq(0) a").css("opacity", Gallery.index == 0 ? "0" : "1.0");
		$("#Gallery td:eq(1) a").css("opacity", Gallery.index == Gallery.urls.length - 1 ? "0" : "1.0");
	},

	hide: function () {
		$("#Gallery").unoverlay();
		X.unattach();
	}

}


var X = {

	lastBoundFunction: null,

	attachTo: function (jq, func) {
		if ($("#X").length == 0) $(document.body).append("<img id=\"X\" src=\"/i/x.png\" style=\"display:none;position:absolute;width:20px;height:20px;z-index:5000;cursor:pointer;position:fixed;\" alt=\"close\" />");

		if (this.lastBoundFunction != null) $("#X").unbind("click", this.lastBoundFunction);

		$("#X").css({
			"top": (jq.offset().top - $(window).scrollTop() - 10) + "px",
			"left": (jq.offset().left + jq.outerWidth() - 10) + "px"
		}).bind("click", func);

		if (!$("#X").is(":visible")) $("#X").fadeIn(250);

		this.lastBoundFunction = func;
	},

	unattach: function () {
		if (this.lastBoundFunction != null) $("#X").unbind("click", this.lastBoundFunction);
		$("#X").fadeOut(250);
	}

}
