(function($){

	// makeClass - By John Resig (MIT Licensed) http://ejohn.org/blog/simple-class-instantiation/
	var makeClass = function(){
		return function(args){
			if(this instanceof arguments.callee){
			if(typeof(this.init)==="function")
				this.init.apply(this,args.callee?args:arguments);
			}else{
				return new arguments.callee(arguments);
			}
		};
	}

	if(typeof(window.com)==="undefined"){window.com = {};}
	if(typeof(com.timeinc)==="undefined"){com.timeinc = {};}
	if(typeof(com.timeinc.direct2time)==="undefined"){com.timeinc.direct2time = {};}
	if(typeof(com.timeinc.direct2time.site)==="undefined"){com.timeinc.direct2time.site = makeClass();}
	
	var tickerScrollSpeed = 3;
	
	var pageTitleBeforePrint = "";
	
	var windowOptions = {
		width:715,
		height:450,
		resizable:1,
		scrollbars:1,
		status:0,
		top:100,
		left:50,
		menubar:1,
		toolbar:0,
		location:0
	};

	var windowOptionsString = function(){
		var str = "";
		var first = true;
		$.each(windowOptions,function(key,val){
			if(!first){str+=",";}
			first = false;
			str+=key;
			str+="=";
			str+=val;
		});
		return str;
	};
	
	var newWin = function(e){
		if(!e){var e = window.event;}
		(typeof(e.preventDefault)!=="undefined")? e.preventDefault() : e.returnValue = false;
		var theEl = (e.target)? e.target : e.srcElement;
		if(theEl){
			if(e.type.toLowerCase()==="click"){
				var targetLoc = theEl.href;
				var newWinName = "Direct2Time";
				var targetWindowOptions = windowOptionsString();
				var newWin = window.open(targetLoc,newWinName,targetWindowOptions);
				newWin.opener = parent.parent;
				newWin.focus();
			}
		}
	};
	
	com.timeinc.direct2time.site.prototype = {
		
		init:function(){
			com.timeinc.direct2time.site = this;
			pageTitleBeforePrint = $("div.pageContent h1:first").html();
			com.timeinc.direct2time.site.setHeight();
			$(window).resize(com.timeinc.direct2time.site.setHeight);
			$("#printPage").click(com.timeinc.direct2time.site.printPageEVT);
			$("#undoPrint").click(com.timeinc.direct2time.site.undoPrintEVT);
			$(".topPageNavigation A[href='"+document.location.pathname+"']").addClass("current");
			if($("#content div.leftPageNavigation").size()>0){
				$("#content div.leftPageNavigation A").each(function(){
					if($(this).attr("href").indexOf("#")===0){
						$(this).click(com.timeinc.direct2time.site.logLinkEVT)
					}
				});
			}
			$("#content DIV.pageContent DIV.versions A").each(function(){
				if($(this).attr("href").indexOf("#")===0){
					$(this).click(com.timeinc.direct2time.site.logLinkEVT)
				}
			});
			$("div.popup a").each(function(){
				$(this).click(function(e){
					windowOptions.width = 820;
					windowOptions.height = 500;
					newWin(e);
				});
			});
			$("#tickertext").each(function(){
				$("#content DIV.topPageNavigation UL LI.ticker").show();
				com.timeinc.direct2time.site.ticker();
			});
		},
		
		ticker:function(){
			var sp = tickerScrollSpeed;
			var tpn = $("#content DIV.topPageNavigation");
			var tt = $("#tickertext");
			var navLIsHeight = tpn.find("UL LI:not(.ticker)").outerHeight({margin:true});
			var navWidth = tpn.outerWidth({margin:true});
			var logoWidth = tpn.find(".logo IMG").outerWidth({margin:true});
			var navLIsWidth = 0;
			tpn.find("UL LI.nav").each(function(){
				navLIsWidth+=$(this).outerWidth({margin:true});
			});
			var targetWidth = navWidth - (logoWidth+navLIsWidth);
			tpn.find("UL LI.ticker").width(targetWidth);
			tpn.find("UL LI.ticker").height(navLIsHeight);
			tpn.find("UL LI.ticker DIV.tickerwrap").width(targetWidth);
			tpn.find("UL LI.ticker DIV.tickerwrap").height(navLIsHeight);
			tt.css("left","60px");
			var stripWidth = 0;
			tt.find("li").each(function(){
				stripWidth += $(this).outerWidth({margin:true});
			});
			tt.width(stripWidth);
			var defTiming = 2500/sp;
			var scrollnews = function(){
				tt.animate(
					{left: '-='+ 25}, 
					defTiming, 
					"linear", 
					function(){
						var ttL = tt.css("left");
						ttL = Number(ttL.substring(0,ttL.lastIndexOf("px")));
						if(ttL+stripWidth<0){
							tt.css("left", targetWidth+"px");
						}
						scrollnews();
					}
				);
			};
			tt.hover(
				function(){jQuery(this).stop();},
				function(){scrollnews();}
			);
			scrollnews();	
		},
		
		printPageEVT:function(e){
			if(!e){var e = window.event;}
			(typeof(e.preventDefault)!=="undefined")? e.preventDefault() : e.returnValue = false;
			var theEl = (e.target)? e.target : e.srcElement;
			if(theEl){
				if(e.type.toLowerCase()==="click"){
					com.timeinc.direct2time.site.logLinkEVT(e);
					com.timeinc.direct2time.site.setActiveStyleSheet("Print Preview");
					$("div.pageContent h1:first").html(document.title);
					com.timeinc.direct2time.site.setHeight();
					window.print();
				}
			}
		},
		
		undoPrintEVT:function(e){
			if(!e){var e = window.event;}
			(typeof(e.preventDefault)!=="undefined")? e.preventDefault() : e.returnValue = false;
			var theEl = (e.target)? e.target : e.srcElement;
			if(theEl){
				if(e.type.toLowerCase()==="click"){
					com.timeinc.direct2time.site.setActiveStyleSheet("default");
					$("div.pageContent h1:first").html(pageTitleBeforePrint);
					com.timeinc.direct2time.site.setHeight();
				}
			}
		},

		setActiveStyleSheet:function(styleName){
			$("link[rel*='style'][title]").each(function(i){
				this.disabled = true;
				if(this.getAttribute('title') == styleName){ this.disabled = false;}
			});
		},
		
		logLinkEVT:function(e){
			if(!e){var e = window.event;}
			var theEl = (e.target)? e.target : e.srcElement;
			if(theEl){
				if(e.type.toLowerCase()==="click"){
					var target = document.location.pathname;
					target+= "?"+$(theEl).attr("href").substr(1);
					$.ajax({
						type: "HEAD",
   						url: target
					});						
				}
			}
		},

		setHeight:function(){
			if($("#content DIV.leftPageNavigation").css("display")==="none"){
				$("#content DIV.pageContent").css("height","auto");
			}else{
				if($("#content UL.rotationPool").size()>0){
					$("#content DIV.imageHolder").replaceWith(
						$("#content UL.rotationPool").innerfade({ 
							animationtype:"fade", 
							speed:4000, 
							timeout:4500, 
							type:"sequence",
							containerheight:"200px"
						})
					);
				}
				var docHeight = $(window).height();
				var contentTop = $("#content DIV.pageContent").offset().top;
				var contentPadTop = $("#content DIV.pageContent").css("padding-top");
				contentPadTop = Number(contentPadTop.substring(0,contentPadTop.lastIndexOf("px")));
				var contentPadBot = $("#content DIV.pageContent").css("padding-bottom");
				contentPadBot = Number(contentPadBot.substring(0,contentPadBot.lastIndexOf("px")));
				var contentVertPad = contentPadTop+contentPadBot;
				var footerheight = $("#pagefootDIV").outerHeight({margin:true});
				var leftnavHeight = $("#content DIV.leftPageNavigation").outerHeight({margin:true});
				var targetHeight = docHeight - contentTop - footerheight - contentVertPad;
				var newHeight = (targetHeight>leftnavHeight)?targetHeight:leftnavHeight;
				$("#content DIV.pageContent").height(newHeight);
			}
		}
		
	};
	
	
	$(document).ready(function(){
		com.timeinc.direct2time.site();
	});
	
})(jQuery);
