/***** Header Script *****//* 
Updated: 08/26/2010
Header script functions

To search for a specific prototype, search the keyword:
- Authentication: fn.authentication
- Weather Section: fn.weather
- On-Air: fn.onair
- Feeds: fn.feeds
- What's Hot / Live: fn.whatshot

******************************/

(function($){

/***** Required script includes *****//* 
These scripts are required for the header implementation
- Recaptcha code for authentication
- Authentication Services
- Weather Services
******************************/

var domainHost = "www.foxnews.com";
var _cbStr = (typeof _cbVarStr!=="undefined") ? _cbVarStr : "b1002";

if (typeof auth_service==="undefined") {
	showToConsole("[head.dependency] Added: Authentication Service");
	document.write(unescape('%3Cscript src="' + document.location.protocol + '\/\/'+domainHost+'/static/all/js/authentication.js?'+_cbStr+'" type="text/javascript"%3E%3C/script%3E'));
}

if (typeof weather_service==="undefined") {
	showToConsole("[head.dependency] Added: Weather Service");
	document.write(unescape('%3Cscript src="' + document.location.protocol + '\/\/'+domainHost+'/static/all/js/weather.js" type="text/javascript"%3E%3C/script%3E'));
}

// opx.css
document.write(unescape('%3Clink rel="stylesheet" type="text/css" href="' + document.location.protocol + '\/\/'+domainHost+'/static/all/css/opx.css" /%3E'));

	
/***** jQuery Plugin Dependency *****//* 
Check for plugin dependency.
******************************/
if (typeof $.fn.jfoxCarousel==="undefined" || typeof $.cookie==="undefined" || typeof $.validate==="undefined") {
	showToConsole("[head.dependency] Added: jQuery Plugins");
	document.write(unescape('%3Cscript src="' + document.location.protocol + '\/\/'+domainHost+'/static/all/js/head.plugins.js" type="text/javascript"%3E%3C/script%3E'));
}

// Constants
var CONST = {
	feed: {
		whatsHotLive: {
			feedFunction: "FeedSource_WhatsHotWatchLive", // feed's set function
			//url: "http://10.2.100.22:86/js/whatshot.js", // point to production
			url: "http://www.foxnews.com/js/whatshot.js",
			show: { // items to show. set to false to show however many
				hot: false,
				live: 2
			},
			rotate: 3, // rotate interval in seconds
			fade: {
				use: true,
				delay: 900
			}
				
		},
		onAir: {
			feedFunction: "FeedSource_FNCTV",
			url: "http://www.foxnews.com/js/onair.js",
			descCharLimit: 25, // limit the character length to not blow up the header
			imageDomainLoc: "" // domain where the on-air feed images are in
		}
	},
	weather: {
		emailTxt: "Enter city or zip",
		hideFormDelay: 3000
	},
	onAir: {
		hideDelay: 200
	},
	authentication: {
		use: "render",
		domain: "foxnews.com", // default - will be overwritten with a domain setting on authentication funciton
		check: "/authentication/registration/validate/"
	}
};

function FoxHeader() {
	this.headObj = {}; // element object holder
}

FoxHeader.prototype = {
	init: function(config) {
		for (i in config) {
			this["_"+i] = config[i];
		}
		
		// extend
		this.feeds.root = this.weather.root = this.onAir.root = this.authentication.root = this.whatsHot.root = this;
		
		this.setHolders();
		
		this.docReady(); // document.ready
		this.weather.set(); // weather
		this.whatsHot.set(); // what's hot / live
		this.onAir.set(); // on air
		this.authentication.set(); // authentication service
		
		this.feeds.set(); // feeds
	},
	
	// initialize element holders
	setHolders: function() {
		
		// header elements
		var elm = $("#section-head");
		this.headObj = {
			elm: elm,
			branding: elm.children(".branding"),
			util: elm.find("#util"),
			userOptions: elm.find("#uo-data"),
			headlines: elm.find("#headlines-primary"),
			primaryNav: elm.find("#nav-primary"),
			onAir: elm.children(".slide.slide-1"),
			authentication: elm.find("#authentication"),
			user: elm.find("#util").find(".username")
		};
		
		this.setListeners();
	},
	
	// document.ready triggers
	docReady: function() {
		
		$(document).ready(function(){
			
		});
		
	},
	
	// general event listeners
	setListeners: function() {
		
	}
	
};

/***** Authentication *****//* 
fn.authentication
Login / Registration via OPX
******************************/
FoxHeader.prototype.authentication = {
	set: function() {
		var FH = this;
		var root = this.root;
		
		if (root.headObj.authentication.size()===0) { return false; }
		
		function setPageDomain() {
			var host = document.location.hostname;
			var hostArr = host.split(".");
			var len = hostArr.length;
			var cookieHost = hostArr[len-2] + "." + hostArr[len-1];
			// override
			CONST.authentication.domain = cookieHost;
		}
		
		function trigger() {
			
			setPageDomain();
			
			if (typeof FH[CONST.authentication.use]==="function") {
				FH[CONST.authentication.use]();
			} else {
				FH.render();
			}
		}
		
		if (typeof AuthService!=="undefined") {
			trigger();
		} else {
			$(document).ready(function(){
				if (typeof AuthService!=="undefined") {
					trigger();
				} else {
					showToConsole("[head.authentication] Authentication Service not found.");
				}
			});
		}
		
		this.setHolders();
	},
	setHolders: function() {
		// set user login value from query string
		var userVal = this.root.helper.getQueryStr("usr");
		if (userVal) { $(".login-fields > .field > input[name='userName']").attr("value",userVal); }
	},
	render: function() {
		var root = this.root;
		var authElm = root.headObj.authentication;
		var userElm = root.headObj.user;
		
		function profileLink() {
			var pTarget = "";
			var usrStr = "";
			
			if (CONST.authentication.domain!=="foxnews.com") {
				pTarget = ' target="_blank"';
				if (typeof $.cookie !=="undefined") { 
					if ($.cookie("p_DN")) { usrStr = "?usr=" + $.cookie("p_DN"); }
				}
			}
			var linkTag = '<a'+pTarget+' href="http://www.foxnews.com/community/user/profile'+usrStr+'">Profile</a>';
			return linkTag;
		}
		
		// from OPX authentication setup
		FNAuthService.prototype = new AuthService();
		function FNAuthService() {
			AuthService.call(this, { domain:CONST.authentication.domain });
			var as = this;
			this.createLoginLogout = function(disableEditProfile) {
				var str = [];
				if(as.isLoggedIn()) {
					
					authElm.children().each(function(){
						var item = $(this);
						if (item.hasClass("first")) {
							if (!as.isThirdParty()) {
								
								item.html(profileLink());
							} else {
								item.css({ display:"none" });
							}
						} else {
							var logoutLink = $('<a href="#">Log Out</a>');
							logoutLink.click(function(){
								try {
									authentication_service.logout(authentication_service.domain);
								} catch(err) {
									showToConsole("[head.authentication] Logout - An error occured: " + err);
								}
								return false;
							});
							item.html(logoutLink);
						}
					});
					
					// show authentication stuff
					authElm.css({ display:"inline" });
					animateFn.show(authElm,{ speed:300,type:"swing" });
					
					userElm.html('<img src="http://www.foxnews.com/static/all/img/head/profile.png" alt="" /> ' + as.getDisplayName());
					userElm.css({ display:"inline" });
					animateFn.show(userElm,{ speed:300,type:"swing" });
				} else {
					
					authElm.children().each(function(){
						var item = $(this);
						var text = item.find("a").text().toLowerCase();

						if (text.indexOf("register")>-1) {
							item.find("a").click(function(){
								try {
									authentication_service.registrationOverlay(this);
								} catch(err) {
									showToConsole("[head.authentication] Register - An error occured: " + err);
								}
								return false;
							});
						}
						
						if (text.indexOf("login")>-1) {
							item.find("a").click(function(){
								try {
									authentication_service.providerOverlay(authentication_service.domain);
								} catch(err) {
									showToConsole("[head.authentication] Login - An error occured: " + err);
								}
								return false;
							});
						}
						
					});
					
					// show
					authElm.css({ display:"inline" });
					animateFn.show(authElm,{ speed:300,type:"swing" });
				}
				return ""; // return blank, rendering is handled here
			};
			
			this.onSuccessLogin = function() {
				authElm.append(authentication_service.createLoginLogout());
				as.closeOverlay("login-provider");
			};

			this.callback = function(jsonMsg) {
				var authObj = as.createObjFromJSON(jsonMsg);
				if(authObj.authentication.status==="ok"){
					// authElm.append(authentication_service.createLoginLogout());
					if (as.thirdPartyAPI) {
						as.thirdPartyAPI.disqus.login(authObj,function(){
							window.location.reload();
						});  
					}
				}
				as.closeOverlay("login-provider");
			};
		}
		
		var authentication_service = window.authentication_service = new FNAuthService();
		authentication_service.init();
		authElm.append(authentication_service.createLoginLogout());
	}
};

/***** Weather Section *****//* 
fn.weather
Note: this may be temporary, depending on what the feed will be
- Form submission & weather data display
******************************/
FoxHeader.prototype.weather = {
	set: function() {
		var FH = this;
		var root = this.root;
		var weatherElm = root.headObj.util.find(".weather-search");
		if (weatherElm.size()===0) { return false; }
		
		var formElm = weatherElm.children("form");
		var tickerElm = weatherElm.children("p");
		
		function trigger() {
			try  {
				weather_service.displayHeader("http://www.foxnews.com");
			} catch(err) {
				showToConsole("[header.weather] Error in weather service: " + err);
			}
		}
		
		
		if (typeof weather_service!=="undefined") {
			trigger();
		} else {
			$(document).ready(function(){
				if (typeof weather_service!=="undefined") {
					trigger();
				} else {
					showToConsole("[header.weather] Cannot find weather service: weather_service");
				}
			});
		}
	},
	submitForm: function() {
		showToConsole("[header.weather] Submitting form");
	},
	toggle: function(hide,show) {
		hide.css("display","none");
		show.css({ opacity:"0", display:"block" });
		animateFn.show(show,{ speed:300,type:"swing" });
	}
};

/***** Fox Header On Air *****//* 
fn.onair
- On Air items
******************************/
FoxHeader.prototype.onAir = {
	set: function() {
		var FH = this;
		var root = this.root;
		// expose layer function to window scope
		
		if (root.headObj.onAir.size()===0) { return false; }
		
		window[CONST.feed.onAir.feedFunction] = function(data) { 
			FH.renderOnAirNow(data);
		};

		// get the feed
		root.feeds.onAir();

		// static on-air
		FH.carousel();
		FH.setCarouselListeners();
	},
	renderOnAirNow: function(data) {
		var FH = this;
		var root = this.root;
		var schedules = data.schedules.today;
		var nextDay = data.schedules.tomorrow;
		var elm = $("#show-data");
		var list = elm.children("ul:first");
		
		if (elm.size()===0) { return false; }
		
		function setTime(info) {
			info = $.trim(info);
			return (info.split(":"))[0] + info.charAt(info.length-2) + '<span>et</span>';
		}
		
		function setDescription(desc) {
			var limit = CONST.feed.onAir.descCharLimit;
			return (desc.length>limit) ? desc.substring(0,limit) + "..." : desc;
		}
		
		function renderHtml(index,over) {
			over = over || false;
			var htmlArr = [];
			var end = (over) ? index : index + 1;
			
			function pushToArr(info) {
				htmlArr.push('<li'+((x===0)?' class="first"':'')+'>');
				htmlArr.push('<p class="media"><a href="'+info.link+'"><img src="'+CONST.feed.onAir.imageDomainLoc+info.img+'" alt="" /></a></p>');
				htmlArr.push('<div class="info">');
				htmlArr.push('<p class="relation">'+info.title+'</p>');
				htmlArr.push('<p class="summary"><a href="'+info.link+'" title="'+info.dek+'">'+setDescription(info.dek)+'</a></p></div>');
				htmlArr.push('<p class="time">'+setTime(info.time)+'</p></li>');
			}
			
			
			for (var x = index; x <= end; x++) {
				var info = schedules[x];
				pushToArr(info);
			}
			
			if (over && nextDay[0]) {
				pushToArr(nextDay[0]);
			}
			
			//list.css({ opacity:"0" }).html(htmlArr.join("")).animate({ opacity:"1" },300,"swing");
			list.html(htmlArr.join(""));
		}
		
		var current = new Date(data.now.date + " " + data.now.time);
		var currentMil = parseInt(current.getHours() + "" + ((current.getMinutes()<10) ? "0" + current.getMinutes() : current.getMinutes()),10);
		
		for (var x = 0; x < schedules.length; x++) {
			var info = schedules[x];
			var nextSched = (schedules[x+1]) ? schedules[x+1] : false;
			try {
				
				var tm = $.trim(info.time).toLowerCase();
				tm =  (tm.charAt(tm.indexOf("m")-2)!==" ") ? tm.replace("pm"," pm").replace("am"," am") : tm;
				var schedDate = new Date(data.now.date + " " + tm);
				var schedDateMil = parseInt(schedDate.getHours() + "" + ((schedDate.getMinutes()<10) ? "0" + schedDate.getMinutes() : schedDate.getMinutes()),10);
				
				if (nextSched) {
					var tm2 = $.trim(nextSched.time).toLowerCase();
					tm2 =  (tm2.charAt(tm2.indexOf("m")-2)!==" ") ? tm2.replace("pm"," pm").replace("am"," am") : tm2;
					var nextSchedDate = new Date(data.now.date + " " + tm2);
					var nextSchedDateMil = parseInt(nextSchedDate.getHours() + "" + ((nextSchedDate.getMinutes()<10) ? "0" + nextSchedDate.getMinutes() : nextSchedDate.getMinutes()),10);
					
					if (currentMil >= schedDateMil && currentMil < nextSchedDateMil) {
						renderHtml(x); break;
					}
					
				} else {
					renderHtml(x,true); break;					
				}
				
			} catch(err) {
				showToConsole("[head.onAir] Error: " + err);
			}
			
		}
		
	},
	// DEPRECATED: not used for now
	renderOnAirCarousel: function(data) {
		var FH = this;
		var root = this.root;
		var schedules = data.schedules;
		return false;
		/*
		<li class="section section-first">	
			<p class="media"><a href="#"><img src="img/show-americas-newsroom-slide.png" alt="" /></a></p>

			<h3><a href="#">America's Newsroom</a></h3>						
			<p>Bill Hemmer<br />Martha MacCallum</p>
			<p class="time">8p<span>et</span><br /> <span class="day">Mon - Fri</span></p>							
		</li>
		*/
		
		function buildAnchors(anchors) {
			var arr = anchors.split("|");
			return arr.join("<br/>");
		}
		
		function buildTimeInfo(time) {
			var arr = time.splid("|");
			var str = [];
			
			str.push(arr[0]);
			if (str.length>0 && arr[1].length>0) {
				str.push('<span>'+arr[1]+'</span>');
			}
		}
		
		var htmlArr = [];
		for (var x = 0; x < schedules.length; x++) {
			var info = schedules[x];
			
			htmlArr.push('<li class="section'+((x===0)?" section-first":"")+'">');
			htmlArr.push('<p class="media"><a href="#"><img src="'+info.img+'" alt="" /></a></p>');
			htmlArr.push('<h3><a href="'+info.link+'">'+info.name+'</a></h3>');
			htmlArr.push('<p>'+buildAnchors(info.anchor)+'</p>');
			htmlArr.push('<p>'+buildTimeInfo(info.time)+'</p>');
		}
		
	},
	carousel: function() {
		var root = this.root;
		var onAirCarousel = root.headObj.onAir.children(".content");
		
		// carousel config
		var cfg = { 
			elm: onAirCarousel,
			slide: 'horizontal', // horizontal or vertical
			scroll: 1, // number of items to scroll per event
			speed: 600, // scroll speed
			show: 3, // items shown
			rotate: false // rotate
		};
		
		
		function createCarouselButtons(obj) {	
			var list = [];
			for (var x=1;x<=obj.batch.max;x++) {
				list.push('<li><a href="#">&nbsp;</a></li>');
			}
			return list.join('');
		}

		cfg.elm.each(function(i){
			var maxNum = 0;
			var carousel = $(this);
			
			if (carousel.data("carousel_initialized")) { return false; } // don't re-init if already initialized
			
			var buttonElm = carousel.find("ol");
			var nav = {
				prev: carousel.find(".prev"),
				next: carousel.find(".next"),
				first: carousel.find(".first-page"),
				last: carousel.find(".last-page")
			};

			var config = {
				auto: { set:false,speed:3000 }, // auto scroll
				slide: cfg.slide,
				scroll: cfg.scroll,
				show: cfg.show,
				speed: cfg.speed,
				rotate: cfg.rotate, 
				eventCallback: function(obj) { // callback function for all carousel events

					if (obj.event === 'init') { 
						maxNum = obj.batch.max;
						buttonElm.html(createCarouselButtons(obj)); // create the buttons 
						buttonElm.children(":first").addClass("active"); // set the first one as always active

						carousel.data("carousel_initialized",true); // set this carousel as initialized

					} else {
						buttonElm.children().each(function(i){ // button highlight listener
							if (i+1 == obj.batch.current) {
								$(this).addClass("active");
							} else {
								$(this).removeClass("active");
							}
						});
					}

					// setting up the navigation classes
					if (obj.batch.current === 1) { 
						nav.prev.addClass("inactive-prev");
						nav.first.addClass("inactive-first");
					} else {
						nav.prev.removeClass("inactive-prev");
						nav.first.removeClass("inactive-first");
					}

					if (obj.batch.current === obj.batch.max) {
						nav.next.addClass("inactive-next");
						nav.last.addClass("inactive-last");
					} else {
						nav.next.removeClass("inactive-next");
						nav.last.removeClass("inactive-last");
					}

				},
				controlsCallback: function(control) { // callback to set up controls
					buttonElm.children().find("a").each(function(i){ // buttons links
						$(this).click(function(){
							control.stopAutoScroll();
							control.scrollToBatch(i+1);
							return false;
						});
					});

					nav.first.click(function(){ // first-page link
						control.stopAutoScroll();
						control.scrollToItem(1);
						return false;
					});

					nav.prev.click(function(){ // previous link
						control.stopAutoScroll();
						control.slide('prev');
						return false;
					});

					nav.next.click(function(){ // next link
						control.stopAutoScroll();
						control.slide('next');
						return false;
					});

					nav.last.click(function(){ // last-page link
						control.stopAutoScroll();
						control.scrollToItem(maxNum);
						return false;
					});

				}
			};
			
			if (typeof $.fn.jfoxCarousel!=="undefined") {
				carousel.jfoxCarousel(config);
			} else {
				$(document).ready(function(){
					carousel.jfoxCarousel(config);
				});
			}
			
		});
		
	},
	setCarouselListeners: function() {
		var root = this.root;
		var navElm = root.headObj.primaryNav;
		var linkElm = navElm.children("ul:first").children("li.on-air");
		var onAirElm = root.headObj.onAir;
		var height = onAirElm.css("height").replace("px","");
		
		var delay = CONST.onAir.hideDelay;
		
		var icon = {
			close: linkElm.find("img").filter(function(){ return ($(this).attr("src").indexOf("close.png")>-1) ? true : false; }),
			open: linkElm.find("img").filter(function(){ return ($(this).attr("src").indexOf("plus.png")>-1) ? true : false; })
		};
		
		function hide() {
			onAirElm.data("isRendering",true);
			onAirElm.data("hidden",true);
			onAirElm.animate({ height:"0px",opacity:"0" },delay,"swing",function(){ onAirElm.data("isRendering",false); });
			/*
			animateFn.hide(onAirElm,{ 
				speed:300,
				type:"swing",
				css: { height:"0px" },
				callback:function() { onAirElm.data("isRendering",false); } 
			});
			*/
			icon.close.hide();
			icon.open.show();
			linkElm.removeClass("active");
		}
		
		function show() {
			if (!onAirElm.data("initToFirst")) {
				onAirElm.data("initToFirst",true);
				onAirElm.find("p.controls > a.prev").click();
			}
			
			onAirElm.data("isRendering",true);
			onAirElm.data("hidden",false);
			onAirElm.animate({ height:height,opacity:"1" },delay,"swing",function(){ onAirElm.data("isRendering",false); });
			/*
			animateFn.show(onAirElm,{ 
				speed:300,
				type:"swing",
				css: { height:height },
				callback:function() { onAirElm.data("isRendering",false); } 
			});
			*/
			icon.close.show();
			icon.open.hide();
			linkElm.addClass("active");
		}
		
		// set elements
		icon.close.hide(); // hide close by default
		onAirElm.css({ height:"0px",opacity:"0" }).data("hidden",true); // hide by default
		
		// set click events
		linkElm.find("a").click(function(){
			if (onAirElm.data("isRendering")) { return false; }
			if (onAirElm.data("hidden")) { show(); } else { hide(); }
			return false;
		});
	}
};

/***** What's Hot / Live *****//* 
fn.whatshot
What's Hot & What's Live feed
******************************/
FoxHeader.prototype.whatsHot = {
	set: function() {
		var FH = this;
		var root = this.root;
		this.render.root = root;
		
		if (root.headObj.headlines.size()===0) { return false; }
		//root.headObj.headlines.append("&nbsp;");
		
		// expose layer function to window scope
		window[CONST.feed.whatsHotLive.feedFunction] = function(data) { 
			FH.render.set(data);
		};
		
		// get the feed
		root.feeds.whatsHotLive();
	},
	
	render: {
		set: function(data) {
			var root = this.root;
			var headlines = root.headObj.headlines;
			
			var hotHtml = this.hot({
				title: "Full Coverage",
				feed: data.whatshot,
				len: CONST.feed.whatsHotLive.show.hot
			});
			
			var liveHtml = this.live({
				title: "Watch Live",
				feed: data.watchlive,
				len: CONST.feed.whatsHotLive.show.live
			});
			
			if (typeof data.whatshot!=="undefined") {
				if (data.whatshot.length===0) { return false; } // if no feed, don't render
				
				var whatsHotElm = $('<dl class="first"></dl>');
				whatsHotElm.append(hotHtml).children("dd:first").addClass("first");
				headlines.html(whatsHotElm);
			}
			
			if (typeof data.watchlive!=="undefined") {
				if (data.watchlive.length===0) { return false; } // if no feed, don't render
				
				var watchLiveElm = $('<dl></dl>');
				watchLiveElm.append(liveHtml).children("dd:first").addClass("first");
				headlines.append(watchLiveElm);
	
				// rotate watch live if greater than 2
				var watchLiveHtml = headlines.children("dl:nth-child(2)");
				
				if (data.watchlive.length>CONST.feed.whatsHotLive.show.live) {
					this.rotate(watchLiveHtml);
				} else {
					watchLiveHtml.children("dd").css("display","inline");
				}
			}
		},
		hot: function(cfg) {
			if (typeof cfg.feed==="undefined") { return false; } 
			cfg.len = cfg.len || cfg.feed.length;
			var htmlArr = [];
			
			if (cfg.len>0) {
				htmlArr.push('<dt>'+cfg.title+'</dt>');
				for (var x = 0; x < cfg.feed.length; x++) {
					var hot = cfg.feed[x];
					var style = (x >= cfg.len) ? ' style="display:none;"' : '';
					htmlArr.push('<dd'+style+'><a href="'+hot.link+'">'+hot.title+'</a></dd>');
				}
			}
			return $(htmlArr.join(""));
		},
		live: function(cfg) {
			if (typeof cfg.feed==="undefined") { return false; } 
			cfg.len = cfg.len || cfg.feed.length;
			var htmlArr = [];
			
			if (cfg.len>0) {
				htmlArr.push('<dt>'+cfg.title+'</dt>');
				for (var x = 0; x < cfg.feed.length; x++) {
					var hot = cfg.feed[x];
					var style = (x >= cfg.len) ? ' style="display:none;"' : '';
					htmlArr.push('<dd'+style+'><a href="'+hot.link+'">'+hot.title+'</a></dd>');
				}
			}
			return $(htmlArr.join(""));
		},
		rotate: function(elm) {
			var items = elm.children("dd");
			var titleElm = elm.children("dt:first");
			var length = items.size();
			var fader = CONST.feed.whatsHotLive.fade;
			
			if (length===0) { return false; }
			
			var timer = CONST.feed.whatsHotLive.rotate * 1000; // in seconds
			if (fader.use) {
				timer = timer + fader.delay;
			}
			
			var interval = false;
			
			items.each(function(i){
				var elm = $(this);
				elm.addClass("first");
				elm.css("display",(i===0) ? "inline" : "none");
				if (i===0) { elm.data("rotateNumber",i+1); }
			});
			
			if (!elm.data("rotateNumber")) { elm.data("rotateNumber",1); }
			
			function trigger(type) {
				type = type || false;
				
				var rotate = elm.data("rotateNumber") + 1;
				rotate = (rotate > length) ? 1 : rotate;
				elm.data("rotateNumber",rotate);
				
				items.each(function(i){
					var elm = $(this);
					var pos = i+1;
					
					if (!fader.use || type==="click") {
						elm.css("display",(pos===rotate) ? "inline" : "none");
					} else {
						if (pos===rotate) { elm.fadeIn(fader.delay); } else { elm.hide(); }
					}
				});
			}
			
			function set() {
				clearInterval(interval);
				interval = setInterval(function(){ // continue rotate
					trigger();
				},timer);
			}
			
			set();
			
			titleElm.css("cursor","pointer").click(function(){
				trigger("click");
			});
			
			elm.hover(function(){ // hover in
				clearInterval(interval); 
			},function(){ // hover out
				set();
			});
			
			
			
		}
	}
};

/***** Fox Header Feeds *****//* 
fn.feeds
All feed related information should pass through here
Handled feeds:
- What's Hot / Live
******************************/
FoxHeader.prototype.feeds = {
	set: function() {
		
	},
	
	// Whats Hot / Watch Live feed
	whatsHotLive: function() {
		// insert script
		this.get.append({ url:CONST.feed.whatsHotLive.url }); 
	},
	
	// On-Air feed
	onAir: function() {
		this.get.append({ url:CONST.feed.onAir.url }); 
	},
	
	// consuming feeds through different types
	get: {
		// append to head asynchronously
		append: function(cfg) {
			(function(){
				var elm = document.createElement("script");
				//elm.src = document.location.protocol + "//www.foxnews.com/static/all/js/authentication.js";
				elm.src = cfg.url;
				elm.async = true;
				$("head").get(0).appendChild(elm);
			}());
		},
		// json / jsonp
		json: function(cfg) {
			
			$.ajax({
				type: "GET",
				url: cfg.url,
				data: cfg.query,
				success: function(data){
					if (cfg.callback) { cfg.callback(data); }
				}
			});
			
		}
	}
};

//helper functions
FoxHeader.prototype.helper = {
	getQueryStr: function(query) {
		var search = (window.location.search).substr(1);
		if (search.length>0) {
			var pairs = search.split('&');
			var valuePair = {};
			for (var x=0;x<pairs.length;x++) {
				var split = pairs[x].split('=');
				valuePair[split[0]] = split[1];
			}
			
			return (valuePair[query]) ? valuePair[query] : false;
		}
		return false;
	}
};

function showToConsole(str) {
	if (typeof window.console==='object') { console.log(str); }
}

var animateFn = {
	isIE: function() {
		return $.browser.msie;
	},
	animate: function(elm,cfg) {
		if (!this.isIE()) {
			if (!cfg.speed) { cfg.speed = 300; }
			if (!cfg.type) { cfg.type = "swing"; }

			elm.animate(cfg.css,cfg.speed,cfg.type,function(){
				if (cfg.callback) { cfg.callback(); }
			});
		} else {
			elm.css(cfg.css);
			elm.css("opacity","");
			if (cfg.callback) { cfg.callback(); }
		}
	},
	hide: function(elm,cfg) {
		if (!elm) { return false; }
		var cssObj = { opacity:"0" };
		if (cfg.css) {
			for (i in cfg.css) { cssObj[i] = cfg.css[i]; }
		}
		cfg.css = cssObj;
		this.animate(elm,cfg);
	},
	show: function(elm,cfg) {
		if (!elm) { return false; }
		var cssObj = { opacity:"1" };
		if (cfg.css) {
			for (i in cfg.css) { cssObj[i] = cfg.css[i]; }
		}
		cfg.css = cssObj;
		this.animate(elm,cfg);
	}
};

var foxHeader = window.foxHeader = new FoxHeader();

/***** Global Functions *****//* 
- window scope functions/objects
******************************/

// streaming video - legacy
var siteStreamFlashPlayer = window.siteStreamFlashPlayer = function(n, site) {
	var s = typeof(n) == 'undefined' ? 1 : n;
	var d = typeof(site) == 'undefined' ? 'foxnews' : site;
	var w = 850;
	var h = 506;
	var left = (screen.width-w)/2;
	var top = (screen.height-h)/2;
	var settings = "height="+h+",width="+w+",top="+top+",left="+left+",toolbar=no,location=no,scrollbars=no,status=no,resizable=no,fullscreen=no";
	var win = window.open('http://interactive.' + d + '.com/livestream/live.html?chanId='+s+'&openAIR=true','livePlayer',settings);
	win.focus();
};

})(jQuery);

(function($){ // footer
	$(document).ready(function(){
		$("#section-footer").find('a[rel="external"]').attr("target","_blank");
	});
})(jQuery);
