var windowopen=false;

$(document).ready(function() {
	var issue=getQS("c");
	if (issue.indexOf("A",3)>0) { issue=issue.substr(0,issue.indexOf("A",3)); }
	var c=issue.substr(0,issue.length-4);
	var mo=issue.substr(-4,2);
	var yr=issue.substr(-2,2);
	
	checksize();
	
	$(window).scroll(function() {
		addmenu();
	});
	$(window).resize(function() {
		checksize();
	});
	
	$(".section h3").each(function() {
		var e=$(this);
		var line=e.parent().attr("id").substr(4);
		var perm=$("<a />");
		perm.attr({
				href:"http://www.mpsportsclubco.com/web_clubHapps/?c=" + issue + "A" + line + "#line" + line,
				className:"perm",
				title:"Right-click to copy permalink to this article"
			})
			.text("LINK")
			.appendTo(e)
			.hide();
		e.hover(function() {
			e.children("a").show();
		},function() {
			e.children("a").hide();
		});
	});
	$(document).pngFix();

});
		
function getQS(e) {
	var x=window.location.search.substring(1);
	//alert(x);
	x=x.split("&");
	for (j=0;j<x.length;j++) {
		y=x[j].split("=");
		//alert(y[0] + ":" + y[1]);
		if(y[0]==e) {
			return y[1];
		}
	}
}

function addmenu() {
	// alert("scrolltop: " + $(document).scrollTop() + "\nwindowheight: " + $(window).height() + "\nwindowwidth: " + $(window).width());
	if($(document).scrollTop()>$("#frontpage").height()-100 && $(window).width()>=940) {
		if ($("#scrollingnav").height()>0) {
			$("#scrollingnav").show().stop().animate({opacity:1},300);
		} else {
			var toplink=$("<a />");
			toplink.attr({href:"#top"}).text("Top");
			$("#nav").clone().appendTo("body")
				.attr({id:"scrollingnav"})
				.hide()
				.css({
					 width:"220px",
					 padding:"10px",
					 backgroundColor:$("#frontpage").css("background-color"),
					 fontSize:"1.2em",
					 float:"none",
					 position:"fixed",
					 top:"20px",
					 left:"20px",
					 opacity:0})
			$("#scrollingnav").children("ul").prepend(toplink);
			toplink.wrap("<li />");
			$("#scrollingnav").show()
				.animate({opacity:1},300);
		}
	} else if ($(document).scrollTop()<$(window).height() && $("#scrollingnav").height()>0) {
		$("#scrollingnav").stop().animate({opacity:0},300,function() { $(this).hide(); });
	}
}
		
function checksize() {
	if($(window).width()<940) {
		$("#main").css({marginLeft:"auto",marginRight:"auto"});
		$("#scrollingnav").fadeOut(300,function() { $(this).remove() });
	} else if ($(window).width()<1200) {
		$("#main").css({marginLeft:"260px",marginRight:"0"});
		addmenu();
	} else {
		$("#main").css({marginLeft:"auto",marginRight:"auto"});
		addmenu();
	}
}
