﻿// ============  GLOBAL JQUERY PARAMS ===========
$(document).ready(function () {
	//do the following wether logged in to OW or not
	//
	//jquery for parsing url - used to highlitght main nav
	jQuery.url = function () { var segments = {}; var parsed = {}; var options = { url: window.location, strictMode: false, key: ["source", "protocol", "authority", "userInfo", "user", "password", "host", "port", "relative", "path", "directory", "file", "query", "anchor"], q: { name: "queryKey", parser: /(?:^|&)([^&=]*)=?([^&]*)/g }, parser: { strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/, loose: /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/} }; var parseUri = function () { str = decodeURI(options.url); var m = options.parser[options.strictMode ? "strict" : "loose"].exec(str); var uri = {}; var i = 14; while (i--) { uri[options.key[i]] = m[i] || "" } uri[options.q.name] = {}; uri[options.key[12]].replace(options.q.parser, function ($0, $1, $2) { if ($1) { uri[options.q.name][$1] = $2 } }); return uri }; var key = function (key) { if (!parsed.length) { setUp() } if (key == "base") { if (parsed.port !== null && parsed.port !== "") { return parsed.protocol + "://" + parsed.host + ":" + parsed.port + "/" } else { return parsed.protocol + "://" + parsed.host + "/" } } return (parsed[key] === "") ? null : parsed[key] }; var param = function (item) { if (!parsed.length) { setUp() } return (parsed.queryKey[item] === null) ? null : parsed.queryKey[item] }; var setUp = function () { parsed = parseUri(); getSegments() }; var getSegments = function () { var p = parsed.path; segments = []; segments = parsed.path.length == 1 ? {} : (p.charAt(p.length - 1) == "/" ? p.substring(1, p.length - 1) : path = p.substring(1)).split("/") }; return { setMode: function (mode) { strictMode = mode == "strict" ? true : false; return this }, setUrl: function (newUri) { options.url = newUri === undefined ? window.location : newUri; setUp(); return this }, segment: function (pos) { if (!parsed.length) { setUp() } if (pos === undefined) { return segments.length } return (segments[pos] === "" || segments[pos] === undefined) ? null : segments[pos] }, attr: key, param: param} } ();
	//highlight tab function
	var page = jQuery.url.segment(2); //get the section name from the url
	var path = "/en/home/" + page + "/default.aspx"; //set the var for the link that matches the parent page of that section
	$("#mainMenu a[href$='" + path + "']").addClass("current"); //find that link in the mainmenu and apply 'current' class
	//travel tools rollover
	$("ul.ttools > li > a").bind("mouseenter", function () {
		$("#traveltools_title").text(this.innerHTML);
	}).bind("mouseleave", function () {
		$("#traveltools_title").text("Roll over the icons below");
	});
	//default search field text
	var active_color = '#fff'; // Colour of user provided text
	var inactive_color = '#fff'; // Colour of default text
	$("input.default-value").css("color", inactive_color);
	var default_values = new Array();
	$("input.default-value").focus(function () {
		if (!default_values[this.id]) {
			default_values[this.id] = this.value;
		}
		if (this.value == default_values[this.id]) {
			this.value = '';
			this.style.color = active_color;
		}
		$(this).blur(function () {
			if (this.value == '') {
				this.style.color = inactive_color;
				this.value = default_values[this.id];
			}
		});
	});
	//only run these scripts when NOT logged in to OW
	if (typeof OneWeb.Admin === "undefined") {
		$("body").addClass("public");
		//mega menu drop down
		function addMega() {
			$(this).addClass("hovering");
		}
		function removeMega() {
			$(this).removeClass("hovering");
		}
		var megaConfig = {
			interval: 100,
			sensitivity: 4,
			over: addMega,
			timeout: 100,
			out: removeMega
		};
		$("li.mega").hoverIntent(megaConfig)
		//add double arrows to links
		$(".menu_links > ul > li").prepend("&raquo; ");
		$("a.more_link").append(" &raquo;");
		//footer feeds tabs
		//$(".ins_forum_tabs > ul").tabs("#tab_blocks > div").tabs({ fx: { opacity: "toggle", effect: "fade", fadeOutSpeed: 2} });
		$(".ins_forum_container").tabs({ fx: { opacity: "toggle", effect: "fade", fadeOutSpeed: 2 }, selected: 1 });
		//experience lightbox
		if ($(".exp-col").length > 0) {
			$(".exp-col").colorbox({ width: "630px", inline: true, opacity: 0.3 });
		}
		if ($("#regions_regionsmap").length > 0) {
			$("#regions_regionsmap area").colorbox({ transition: "fade", width: "686px", height: "350px", inline: true });

			$(".explore_regions_ss").cycle({
				speed: 300,
				timeout: 0,
				clip: 'zoom',
				fx: 'scrollHorz',
				slideExpr: 'div.region_slide',
				next: $('.region_next'),
				prev: $('.region_prev')
			});
		}
		/* page toggler - used on halloween page - can be used elsewhere */
		$('div.accordion h4').click(function () {
			$(this).toggleClass('plus').toggleClass('minus');
			$(this).next().toggle(400);
			return false;
		}).next().hide();
	}

	//do this only when logged in to OW
	if (typeof OneWeb.Admin != "undefined") {
		$("div").removeClass("public");
		//set up travel widget
		$("#tmpl_cbtravel_planner").css({ 'display': 'inline-block', 'width': '100px', 'height': '25px' });
		$("#tmpl_cbtravel_planner > a.language").css({ 'display': 'inline-block', 'margin-left': '-145px' });
		$("#tmpl_cbtravel_planner > div.myItins").css({ 'margin-left': '-145px' });
		// setup mega menu for display when in admin view
		$("div.mainmenu").css({ "background-image": "none" });
		$("li.mega").addClass("admin");
		// setup footer feeds tabs for display when in admin view
		$("div.ins_forum_copy").css({ 'display': 'block', 'height': 'inherit', 'margin-bottom': '30px' });
		$("div.featured_sr div.sr_block div.ow_block").css({ "display": "block", "margin-bottom": "10px" });
	}
	//pop up mobile video

});     //end document.ready

//mobile banner stuff
function initMobileXml(lang, thisDir, lure) {
	$.ajax({
		type: "GET",
		url: "/site/" + lang + "/config/home/mobile.xml",
		dataType: "xml",
		success: function (xml) {
			var show_banner = 0;
			if (thisDir == "ce_cuisine") {
				show_banner = 0;
			} else if (thisDir == "ce_culture") {
				show_banner = 1;
			} else if (thisDir == "ce_urban") {
				show_banner = 2;
			} else if (thisDir == "ce_beaches") {
				show_banner = 3;
			} else if (thisDir == "ce_history") {
				show_banner = 4;
			} else if (thisDir == "ce_outdoors") {
				show_banner = 5;
			} else if (thisDir == "discoverns") {
				show_banner = 6;
			} else {
				var numItems = $(xml).find("banners").children().length;
				show_banner = Math.floor(Math.random() * numItems);
			}

			var mobileVidPlayer = '<iframe title="YouTube video player" width="467" height="291" src="http://www.youtube.com/embed/' + $(xml).find("youtube").eq(show_banner).text() + '?rel=0&amp;hd=1" frameborder="0" allowfullscreen></iframe>';

			$("#mobile_banner").append('<div class="mobile_box" style="background:transparent url(' + $(xml).find("thumbnail").eq(show_banner).text() + ') 0 0 no-repeat;"><a id="learn_lnk" href="' + $(xml).find("link").eq(show_banner).text() + '">Learn More</a><a id="video_lnk">Play Video</a><div id="mobile_vid"><div id="closevideo_lnk">Close</div><div id="mvb">' + mobileVidPlayer + '</div></div></div>');
			//pop up mobile video
			$("#video_lnk").click(function () {
				$("#mobile_vid").animate({
					top: '0'
				}, 500, function () {
					// Animation complete.
					if ($("#mvb").html() == ".") {
						$("#mvb").html(mobileVidPlayer);
					}
				});
			});
			$("#closevideo_lnk").click(function () {
				$("#mobile_vid").animate({
					top: '293px'
				}, 200, function () {
					// Animation complete.
					$("#mvb").html(".");
				});
			});
			//$("#mobile_banner").append('<a href="' + $(xml).find("link").eq(show_banner).text() + '"><img src="' + $(xml).find("thumbnail").eq(show_banner).text() + '" border="0" /></a>');
		}
	});
	$.ajax({
		type: "GET",
		url: lure,
		dataType: "xml",
		success: function (xml) {
			$(xml).find("lure").each(function (index) {
				$(".items").append('<div class="feature"><a href="' + $(this).find("link").text() + '"><img src="' + $(this).find("thumbnail").text() + '" border="0" /><span class="feature_cap"><strong>' + $(this).find("title").text() + '</strong><br />' + $(this).find("description").text() + '</span></a></div>');
			});
			// initialize scrollable together with the navigator plugin for homepage
			if ($("#browsable").length) {
				$("#browsable").scrollable({ size: 4 }).navigator();
			}
		}
	});
}
