// Global Scripts
//toggle function for editor
function toggle(id){ 
    tarID = document.getElementById(id);
    tarID.style.display = (tarID.style.display == 'block') ? 'none' : 'block';
}

/* fixes ClearType issues */
jQuery.fn.fadeIn = function(speed, callback) {
	return this.animate({opacity: 'show'}, speed, function() {
		if (jQuery.browser.msie)
    	this.style.removeAttribute('filter');
			if (jQuery.isFunction(callback))
				callback();
	});
};
jQuery.fn.fadeOut = function(speed, callback) {
	return this.animate({opacity: 'hide'}, speed, function() {
		if (jQuery.browser.msie)
			this.style.removeAttribute('filter');
		if (jQuery.isFunction(callback))
			callback();
	});
};
jQuery.fn.fadeTo = function(speed,to,callback) {
	return this.animate({opacity: to}, speed, function() {
		if (to == 1 && jQuery.browser.msie)
			this.style.removeAttribute('filter');
		if (jQuery.isFunction(callback))
			callback();
	});
};

//easing
	jQuery.easing['swingOld'] = jQuery.easing['swing']; //rename the swing function
	jQuery.extend(jQuery.easing,{
		easingFunc: function (x, t, b, c, d) {
		return -c *(t/=d)*(t-2) + b;
	}
	});
	//Preload Images
	jQuery.preloadImages = function(){
		for(var i = 0; i<arguments.length; i++){
			jQuery("<img>").attr("src", arguments[i]);
		}
	};
	
	
	function trackLink(anchor, idx)
	{
		var reportsuite = "ihcpublichealingforlife";
		//if(curURL.indexOf('author.intermountainhealthcare.org')!=-1 || curURL.indexOf('ihc-dev')!=-1 || curURL.indexOf('test.intermountainhealthcare.org')!=-1 || curURL.indexOf('dev.intermountainhealthcare.org')!=-1)	
		//	reportsuite ="ihcorgdev";

		var s=s_gi(reportsuite);
		//alert($('.activeNav').children('a').text());
		var tabTitle = (idx + 1) + " - " + $(anchor).find('img:eq(0)').attr('title');
		//alert(tabTitle);
		s.linkTrackVars = 'prop1,prop2';
		s.linkTrackEvents='event4';
		s.events='event4';
		s.prop1 = $('.activeNav').children('a').text();
		s.prop2 = tabTitle;
		//alert(s.prop1);
		//alert(s.prop2);
		s.tl(true, 'o', tabTitle);
	}


// Actions
$(document).ready(function(){
	var predefinedIndex = 0;
	var predefinedTrackingIndex = s.campaign.substring(s.campaign.length - 1,s.campaign.length) - 1;
	var predefinedNonTrackingIndex = s.getQueryParam('Tab') - 1;
	if (predefinedNonTrackingIndex != -1) predefinedIndex=predefinedNonTrackingIndex;
	if (predefinedTrackingIndex != -1) predefinedIndex=predefinedTrackingIndex;
	// Turn off current link
	var noClick = 'dt a, .expandLnk > a';
	$(noClick).click(function() {
			return false;														 
	});		
	// slidedown and fade in												 
	jQuery.fn.slideDownFade = function(speed, easing) {
			return this.animate({height: 'show'}, speed, easing);	
		};
		
//Delay (thanks Evan)
		jQuery.fn.delay = function(time,func){
			return this.each(function(){
				setTimeout(func,time);
			});
		};

	// CYCLE CONTENT
	// Pause the slide transitions when hovering over content
	$('.cContent').mouseover(function(){
		$('#cSplashWrap').cycle('pause');
	});

	function funcBefore(){
		var tabIndex = $('#cSplashWrap div').index(this);
		var contentIndex = '#cContentWrap div.cContent:eq('+tabIndex+')';																						
		var NotContentIndex = '#cContentWrap div.cContent:not(:eq('+tabIndex+'))';
		//$('#cSplashWrap div dl').fadeOut();
		$('#cContentWrap div.cContent').hide(); //Old Content
		$(contentIndex).show(); // New Content
		tabOn(tabIndex);
		tabOff(tabIndex);

	};
	function funcAfter(){
		var tabIndex = $('#cSplashWrap div').index(this);
		var titleThis = '#cSplashWrap div:eq('+tabIndex+') h2';
		var titleNotThis = '#cSplashWrap div:not(:eq('+tabIndex+')) h2';
		//$(titleThis).fadeIn();
		//var detailsThis = '#cSplashWrap div:eq('+tabIndex+') dl dd';
		//$('#cSplashWrap div dl dt').left(100);		
		//$(detailsThis).delay(2000,function(){
		//	$(detailsThis).fadeIn(); //FadeIn sub																	 
		//});		

	};
	//Turn on the Active Nav Item
	function tabOn(navNum) {
		var tabOnID = '#cNav ul li:eq('+navNum+') img';
		$(tabOnID).animate({opacity:100},200);
	};
	//Turn off non-active Nav
	function tabOff(navNum) {
		var tabOffID = '#cNav ul li:not(:eq('+navNum+') img)';
		$(tabOffID).animate({opacity:80},200);
	};
	// Cycle Banners 
	
	$('#cSplashWrap').cycle({
		timeout: 8000, // Time between transitions
		speed: 2000,
		next: '#cSplashWrap', // Advance when clicked
		pause: 1, //Pause on hover
		before: funcBefore,
		after: funcAfter,
		startingSlide: predefinedIndex,
		pager: '#cNav ul',
		pagerAnchorBuilder: function(idx, slide) {
        var src = $('img.thumb',slide).attr('src');
				var slideText = $(slide).children('h2').text();
        return '<li><a href="?Tab=' + idx + '" onclick="trackLink(this, ' + idx + ');"><span><img src="' +src+ '" title="' + $('img.thumb',slide).attr('title') + '"/>'+slideText+'</span></a></li>';
    } 
	});
	
	//On Page Load, log a click to the first tab.
	trackLink($('#cNav a:eq(' + predefinedIndex + ')'), predefinedIndex );
	//trackLink($('#cNav a:eq(0)'), 0);
	/*
	$('#cNav img').each(function(index) {
		var TabTitle = $(this).attr('title');
		$(this).parents('a:eq(0)').click(function(){
			s.linkTrackVars = 'prop1,prop2';
			s.linkTrackEvents = 'None';
			s.prop1 = $('.activeNav').children('a').text();
			s.prop2 = TabTitle;
			s.tl(this, 'o');
		});
	});
*/	
	//Stop slideshow
	$('#cNav ul li a').click(function(){
		$('#cSplashWrap').cycle('pause');																	
	});
	//Expanding Link in Left Nav
	$('.expandLnk a').click(function(){
		$(this).parent().toggleClass('showList');
		$(this).next('ul').slideToggle(1000);
	});
	
	//PopOver
	$('.adBanner').click(function(){
		$('#popOver').fadeIn(500);
		$('#cSplashWrap').cycle('pause');
	});
	$('#popOver a.closePop').click(function(){
		$('#popOver').fadeOut(500);
		$('#cSplashWrap').cycle('resume');
	});
	//PopOver Menu
	$('.vidMenuWrap dd:first').show();
	$('.vidMenuWrap dt:first').css('background','white');
	$('.vidMenuWrap dt').hoverIntent(function(){
		var menuIndex = $('.vidMenuWrap dt').index(this);
		var DDThis = '.vidMenuWrap dd:eq('+menuIndex+')';
		var DDNotThis = '.vidMenuWrap dd:not(:eq('+menuIndex+'))';
		var menuPosition = $(this).position();
		$(DDThis).css('margin-top',menuPosition.top);
		$(DDNotThis).fadeOut(100,fadeInMenu);
		function fadeInMenu (){
			$(DDThis).fadeIn(250);
		}
	},function(){
		$('.vidMenuWrap dd').hide();
	});
});

