﻿$(function () {
	// Market page tabs
	$("#markets, #advantages").tabs(
		"div.panes > div",
		{ history: true,
			effect: "fade",
			fadeInSpeed: 500,
			fadeOutSpeed: 500
		}
	);
	$("#bemisadvantages dl").tabs("#bemisadvantages dl dd", { tabs: "dt", effect: "fade", initialIndex: 0 });
	$("div.compare-styles").hide();
	$("a.compare").click(function () {
		$(this).toggleClass("open").blur().parent().next("div.compare-styles").slideToggle();
		return false;
	});
	$("ul.tabs a").click(function () { $(this).blur(); });

	/* Bag Features
	---------------------------------- */
	// enable the first option
	$("#bag-features h4:first").addClass('open').next("ul").show();
	$("#bag-features").tabs("#bag-features ul",
		{ tabs: "h4",
			effect: "slide",
			current: "open",
			initialIndex: null
		}
	);

	//Insert Overly Markup
	$("a[rel^=#overlay]").each(function () {
		//console.log($(this).text() + ", " + $(this).attr("rel"));
		var id = $(this).attr("rel");
		// ensure unique overlays
		if (!$(id).length) {
			var overlay = "<div class=\"overlay\" id=\"" + id.replace("#", "") + "\"><div class=\"loadwrapper\"></div></div>";
			$("body").append(overlay);
		}
	}).overlay({
		mask: {
			color: "#000",
			opacity: 0.7
		},
		onBeforeLoad: function () {
			var trigger = this.getTrigger(),
				loadwrapper = this.getOverlay().find("div.loadwrapper"),
				href = trigger.attr("href"),
				isImage = (href.search(/\.(jpg|gif|png)/i) > 0);
			//console.log(href.search(/#/));
			//console.log("isImage: " + isImage);

			// we're loading external content section
			if (href.search(/#/) != 0 && !isImage) {
				loadwrapper.load(href.replace("#", " #"));
			} else if (isImage) { //check if we're loading an image
				var img = new Image();
				img.alt = trigger.text();
				img.src = href;
				loadwrapper.html($(img));
			}
			// otherwise continue
		}

	});

	$("#afterspotlights").insertAfter("#spotlights");

	// sustainabilty behavior for older browsers
	try {
		if ($.browser.msie && parseInt($.browser.version) < 8) {
			$("div.container").hover(function () {
				//console.log("over");
				$(this).find("div").addClass("display");
			}, function () {
				//console.log("out");
				$(this).find("div").removeClass();
			});
		}
	} catch (e) { };
});
