var ord = Math.floor(Math.random()*999999999999999999);
function setCookie(){
	if (document.cookie.length>0) {
		tc_start=document.cookie.indexOf("totalvisits=");
		if (tc_start == -1) {
			var texdate=new Date();
			var texpiredays=10000;
			var tcurrentdate=new Date();
			texdate.setDate(texdate.getDate()+texpiredays);
			tcurrentdate.setDate(tcurrentdate.getDate());
			document.cookie="totalvisits=1;expires="+texdate.toGMTString();
		} else {
			var texdate=new Date();
			var texpiredays=10000;
			var tcurrentdate=new Date();
			texdate.setDate(texdate.getDate()+texpiredays);
			tcurrentdate.setDate(tcurrentdate.getDate());

			tc_start = tc_start + 12;
			tc_end = document.cookie.indexOf(";",tc_start);
			if (tc_end==-1) tc_end=document.cookie.length;
			
    		num_visits = Number(unescape(document.cookie.substring(tc_start,tc_end)));
    		num_visits = num_visits + 1;
    		document.cookie="totalvisits=" + num_visits + ";expires="+texdate.toGMTString();
    	}
		
		c_start=document.cookie.indexOf("firstvisit=");
		if (c_start == -1) {
			var exdate=new Date();
			var expiredays=10000;
			var currentdate=new Date();
			exdate.setDate(exdate.getDate()+expiredays);
			currentdate.setDate(currentdate.getDate());
			document.cookie="firstvisit="+currentdate.toGMTString() + ";expires="+exdate.toGMTString();
			document.cookie="totalvisits=1;expires="+exdate.toGMTString();
		}
	}
	var exdate=new Date();
	var expiredays=10000;
	var currentdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	currentdate.setDate(currentdate.getDate());
	document.cookie="lastvisit="+currentdate.toGMTString() + ";expires="+exdate.toGMTString();
}
setCookie();
$(document).ready(function(){

	$("#news_menu").mouseover(function() {
		$("#news_submenu").show();
	});
	
	$("#news_menu").mouseout(function() {
		$("#news_submenu").hide();
	});
	
	$("#ae_menu").mouseover(function() {
		$("#ae_submenu").show();
	});
	
	$("#ae_menu").mouseout(function() {
		$("#ae_submenu").hide();
	});
	
	$("#most_read_link a").css("color","#9fa1a4");
	$("#most_read_link").click(function(){
		$("#shared").hide();
		$("#printed").hide();
		$("#emailed").hide();
		$("#most_read").show();
		$("#most_read_link a").css("color","#9fa1a4");
		$("#shared_link a").css("color","#3582a6");
		$("#printed_link a").css("color","#3582a6");
		$("#emailed_link a").css("color","#3582a6");
	});
	$("#shared_link").click(function(){
		$("#printed").hide();
		$("#emailed").hide();
		$("#most_read").hide();
		$("#shared").show();
		$("#shared_link a").css("color","#9fa1a4");
		$("#most_read_link a").css("color","#3582a6");
		$("#printed_link a").css("color","#3582a6");
		$("#emailed_link a").css("color","#3582a6");
	});
	$("#printed_link").click(function(){
		$("#shared").hide();
		$("#emailed").hide();
		$("#most_read").hide();
		$("#printed").show();
		$("#printed_link a").css("color","#9fa1a4");
		$("#shared_link a").css("color","#3582a6");
		$("#most_read_link a").css("color","#3582a6");
		$("#emailed_link a").css("color","#3582a6");
	});
	$("#emailed_link").click(function(){
		$("#shared").hide();
		$("#printed").hide();
		$("#most_read").hide();
		$("#emailed").show();
		$("#emailed_link a").css("color","#9fa1a4");
		$("#shared_link a").css("color","#3582a6");
		$("#printed_link a").css("color","#3582a6");
		$("#most_read_link a").css("color","#3582a6");
	});
	
	$(function() {
		$(".carousel").jCarouselLite({
			btnNext: ".next",
			btnPrev: ".prev",
			visible: 5
		});
		
	});
	/* Hide Comments function */
	var displayCommentsValue = 5;
	if($(".commentlist .comment").length > displayCommentsValue){
		$("#comment-toggle").show();
		var sliceEnd = $(".commentlist .comment").length - displayCommentsValue;
		$(".commentlist .comment").slice(0,sliceEnd).addClass('hide-comment');
		$(".commentlist .hide-comment").hide();
		$("#comment-toggle").text("Showing "+displayCommentsValue+" recent comments. View all comments.");
		$("#comment-toggle").toggle(
			function(){
				$(this).text("Hide the comments.");
				$(".commentlist .hide-comment").fadeIn("slow");						  
			},
			function(){
				$(this).text("Showing "+displayCommentsValue+" recent comments. View all comments.");
				$(".commentlist .hide-comment").fadeOut("slow");						  
			}
		);
	};
	/* TABS RADIO SWITCH START */
	// Hide all tab content, then show the first one
	$('.tab-radio .tab-content').hide();
	var TabSetCounter = $('.tab-radio').length;
	for(z=0;z<TabSetCounter;z++){
		$('.tab-radio').eq(z).find('.tab:first').addClass('active');
		$('.tab-radio').eq(z).find('.tab-content:first').fadeIn('slow');
		//console.log(z+' '+$('.tab-radio').eq(z).find('.tab:first').html());
	};
	// Function when tab toggle is clicked
	$('.tab-radio .tab').click(function(){$(this).tabAction()});
	// Hover Extension Class added
	$('.tab-radio .tab, .tab-accordian .tab').hoverAction()
	/* TABS RADIO SWITCH END */
	/* Add Hover Class to CTA Objects */
	$('input.cta').hover(
		function(){
		//alert('hover');
		},
		function(){
		//alert('hover off');
		}
	);
	/* Drop Down Nav function */
	$(".drop-down").hover(
		function(){
		//console.log('active roll');
		var box1 = $(this).outerWidth();
		var box2 = $(this).children("ul,ol").width();
		//console.log(box1+" "+box2);
		if(box1 > box2){
			$(this).children("ul,ol").width(box1);
		}
		$(this).addClass('drop-down-active');
		$(this).children("ul,ol").show();
		},
		function(){
		//console.log('active roll out');
		$(this).removeClass('drop-down-active');
		$(this).children("ul,ol").hide();
		}
	);
	/* CHANGE TEXT SIZE START */
	$('.text-size').toggle(
		function(){
			// Resize to 15px
			$('.text-resize').css('font-size','1.25em');
			$('.text-noresize').css('font-size','0.8em');
			//console.log($('.text-resize-jsa').css('font-size'))  
		},
		function(){
			// Resize to 17px
			$('.text-resize').css('font-size','1.416em');
			$('.text-noresize').css('font-size','0.705em');
			//console.log($('.text-resize-jsa').css('font-size'))  
		},
		function(){
			// Original 12px
			$('.text-resize').css('font-size','1em');
			$('.text-noresize').css('font-size','1em');
			//console.log($('.text-resize-jsa').css('font-size'))  
		}
	);
	/* CHANGE TEXT SIZE END */
	/* ANCHOR ANIMATE */
	$("a.anchorSlide").anchorAnimate()
});
function addBookmark(title, url) {
	if (window.sidebar) { // Firefox
		window.sidebar.addPanel(title, url, '');
	} else if (window.opera) { // Opera
		var a = document.createElement('A');
		a.rel = 'sidebar';
		a.target = '_search';
		a.title = title;
		a.href = url;
		a.click();
	} else if (document.all) { // IE
		window.external.AddFavorite(url, title);
	}
}
/**
Anchor Slider by Cedric Dugas 
Http://www.position-absolute.com 
**/
$.fn.anchorAnimate = function(settings) {
 	settings = jQuery.extend({
		speed : 1100
	}, settings);	
	return this.each(function(){
		var caller = this
		$(caller).click(function (event) {	
			event.preventDefault()
			var locationHref = window.location.href
			var elementClick = $(caller).attr("href")
			var destination = $(elementClick).offset().top;
			$("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination}, settings.speed, function() {
				window.location.hash = elementClick
			});
		  	return false;
		})
	})
}
$.fn.hoverAction = function(option) {
	if(option == null){
	return this.hover(
		function(){
			if($(this).hasClass('active')){
				return;
			}else{
				$(this).addClass('hover');
			};
		},
		function(){
			$(this).removeClass('hover');
		}
	);
	//used for objects added post DOM load
	}else{
		if(option == 'rollOver'){
			if($(this).hasClass('active')){
				return;
			}else{
				return $(this).addClass('hover');
			};
		} else if(option == 'rollOut'){
			 return $(this).removeClass('hover');	
		};
	};
}
$.fn.tabAction = function(){
	if($(this).hasClass('active')){
		return;
	}else{
		var ThisTabSet = $(this).parents('.mod');
		ThisTabSet.find('.tab').removeClass('active');
		$(this).removeClass('hover');
		$(this).addClass('active');
		ThisTabSet.find('.tab-content').hide();
		ThisTabSet.find('.tab-content').eq(ThisTabSet.find('.tab').index(this)).fadeIn("slow");	
	};
}
$.fn.duplicate = function(count, cloneEvents) {
	var tmp = [];
	for ( var i = 0; i < count; i++ ) {
		$.merge( tmp, this.clone( cloneEvents ).get() );
	}
	return this.pushStack( tmp );
};
/**
hoverIntent r5 // 2007.03.27 // jQuery 1.1.2
<http://cherne.net/brian/resources/jquery.hoverIntent.html>
@author    Brian Cherne <brian@cherne.net>
**/
$.fn.hoverIntent = function(f,g) {
	// default configuration options
	var cfg = {
		sensitivity: 7,
		interval: 100,
		timeout: 0
	};
	// override configuration options with user supplied object
	cfg = $.extend(cfg, g ? { over: f, out: g } : f );

	// instantiate variables
	// cX, cY = current X and Y position of mouse, updated by mousemove event
	// pX, pY = previous X and Y position of mouse, set by mouseover and polling interval
	var cX, cY, pX, pY;

	// A private function for getting mouse position
	var track = function(ev) {
		cX = ev.pageX;
		cY = ev.pageY;
	};

	// A private function for comparing current and previous mouse position
	var compare = function(ev,ob) {
		ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t);
		// compare mouse positions to see if they've crossed the threshold
		if ( ( Math.abs(pX-cX) + Math.abs(pY-cY) ) < cfg.sensitivity ) {
			$(ob).unbind("mousemove",track);
			// set hoverIntent state to true (so mouseOut can be called)
			ob.hoverIntent_s = 1;
			return cfg.over.apply(ob,[ev]);
		} else {
		// set previous coordinates for next time
			pX = cX; pY = cY;
			// use self-calling timeout, guarantees intervals are spaced out properly (avoids JavaScript timer bugs)
			ob.hoverIntent_t = setTimeout( function(){compare(ev, ob);} , cfg.interval );
		}
	};

	// A private function for delaying the mouseOut function
	var delay = function(ev,ob) {
		ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t);
		ob.hoverIntent_s = 0;
		return cfg.out.apply(ob,[ev]);
	};

	// A private function for handling mouse 'hovering'
	var handleHover = function(e) {
		// next three lines copied from jQuery.hover, ignore children onMouseOver/onMouseOut
		var p = (e.type == "mouseover" ? e.fromElement : e.toElement) || e.relatedTarget;
		while ( p && p != this ) { try { p = p.parentNode; } catch(e) { p = this; } }
		if ( p == this ) { return false; }

	// copy objects to be passed into t (required for event object to be passed in IE)
		var ev = jQuery.extend({},e);
		var ob = this;

	// cancel hoverIntent timer if it exists
		if (ob.hoverIntent_t) { ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t); }

		// else e.type == "onmouseover"
		if (e.type == "mouseover") {
			// set "previous" X and Y position based on initial entry point
			pX = ev.pageX; pY = ev.pageY;
			// update "current" X and Y position based on mousemove
			$(ob).bind("mousemove",track);
			// start polling interval (self-calling timeout) to compare mouse coordinates over time
			if (ob.hoverIntent_s != 1) { ob.hoverIntent_t = setTimeout( function(){compare(ev,ob);} , cfg.interval );}

		// else e.type == "onmouseout"
		} else {
			// unbind expensive mousemove event
			$(ob).unbind("mousemove",track);
			// if hoverIntent state is true, then call the mouseOut function after the specified delay
			if (ob.hoverIntent_s == 1) { ob.hoverIntent_t = setTimeout( function(){delay(ev,ob);} , cfg.timeout );}
		}
	};

	// bind the function to the two event listeners
	return this.mouseover(handleHover).mouseout(handleHover);
};